mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 05:43:55 +01:00
Issue-5662-kotlinstyle (#5833)
* *.kt: bulk correction of formatting using ktlint --format * *.kt: replace wildcard imports and second stage auto format ktlint --format * QuizQuestionTest.kt: modified property names to camel case to meet ktlint standard * LevelControllerTest.kt: modified property names to camel case to meet ktlint standard * QuizActivityUnitTest.kt: modified property names to camel case to meet ktlint standard * MediaDetailFragmentUnitTests.kt: modified property names to camel case to meet ktlint standard * UploadWorker.kt: modified property names to camel case to meet ktlint standard * UploadClient.kt: modified property names to camel case to meet ktlint standard * BasePagingPresenter.kt: modified property names to camel case to meet ktlint standard * DescriptionEditActivity.kt: modified property names to camel case to meet ktlint standard * OnSwipeTouchListener.kt: modified property names to camel case to meet ktlint standard * MediaDetailFragmentUnitTests.kt: corrected excessive line length to meet ktlint standard * DepictedItem.kt: corrected property name format and catch format to for ktlint standard * UploadCategoryAdapter.kt: corrected class definition format to meet ktlint standard * CustomSelectorActivity.kt: reformatted function names to first letter lowercase to meet ktlint standard * MediaDetailFragmentUnitTests.kt: fix string literal indentation to meet ktlint standard * NotForUploadDao.kt: file renamed to match class name, new file NotForUploadStatusDao.kt * UploadedDao.kt: file renamed to match class name, new file UploadedStatusDao.kt * Urls.kt: fixed excessive line length for ktLint standard * Snak_partial.kt & Statement_partial.kt: refactored to remove underscores in class names to meet ktLint standard * *.kt: fixed consecutive KDOC error for ktLint * PageableBaseDataSourceTest.kt & UploadPresenterTest.kt: fixed excessive line lengths to meet ktLint standard * CheckboxTriStatesTest.kt: renamed file to match class name to meet ktLint standard * .kt: resolved backing-property-naming error in ktLint, made matching properties public, matched names and refactored * TestConnectionFactory.kt: fixed property naming to adhere to ktLint standard
This commit is contained in:
parent
950539c55c
commit
2d82a430c4
405 changed files with 11032 additions and 9137 deletions
|
|
@ -2,11 +2,11 @@ package fr.free.nrw.commons.auth.csrf
|
|||
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
|
||||
import fr.free.nrw.commons.auth.login.LoginClient
|
||||
import fr.free.nrw.commons.auth.login.LoginCallback
|
||||
import fr.free.nrw.commons.auth.login.LoginClient
|
||||
import fr.free.nrw.commons.auth.login.LoginFailedException
|
||||
import fr.free.nrw.commons.auth.login.LoginResult
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
|
||||
import retrofit2.Call
|
||||
import retrofit2.Response
|
||||
import timber.log.Timber
|
||||
|
|
@ -17,12 +17,11 @@ class CsrfTokenClient(
|
|||
private val sessionManager: SessionManager,
|
||||
private val csrfTokenInterface: CsrfTokenInterface,
|
||||
private val loginClient: LoginClient,
|
||||
private val logoutClient: LogoutClient
|
||||
private val logoutClient: LogoutClient,
|
||||
) {
|
||||
private var retries = 0
|
||||
private var csrfTokenCall: Call<MwQueryResponse?>? = null
|
||||
|
||||
|
||||
@Throws(Throwable::class)
|
||||
fun getTokenBlocking(): String {
|
||||
var token = ""
|
||||
|
|
@ -37,11 +36,20 @@ class CsrfTokenClient(
|
|||
}
|
||||
|
||||
// Get CSRFToken response off the main thread.
|
||||
val response = newSingleThreadExecutor().submit(Callable {
|
||||
csrfTokenInterface.getCsrfTokenCall().execute()
|
||||
}).get()
|
||||
val response =
|
||||
newSingleThreadExecutor()
|
||||
.submit(
|
||||
Callable {
|
||||
csrfTokenInterface.getCsrfTokenCall().execute()
|
||||
},
|
||||
).get()
|
||||
|
||||
if (response.body()?.query()?.csrfToken().isNullOrEmpty()) {
|
||||
if (response
|
||||
.body()
|
||||
?.query()
|
||||
?.csrfToken()
|
||||
.isNullOrEmpty()
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -51,9 +59,8 @@ class CsrfTokenClient(
|
|||
}
|
||||
break
|
||||
} catch (e: LoginFailedException) {
|
||||
throw InvalidLoginTokenException(ANONYMOUS_TOKEN_MESSAGE)
|
||||
}
|
||||
catch (t: Throwable) {
|
||||
throw InvalidLoginTokenException(ANONYMOUS_TOKEN_MESSAGE)
|
||||
} catch (t: Throwable) {
|
||||
Timber.w(t)
|
||||
}
|
||||
}
|
||||
|
|
@ -65,45 +72,65 @@ class CsrfTokenClient(
|
|||
}
|
||||
|
||||
@VisibleForTesting
|
||||
fun request(service: CsrfTokenInterface, cb: Callback): Call<MwQueryResponse?> =
|
||||
requestToken(service, object : Callback {
|
||||
override fun success(token: String?) {
|
||||
if (sessionManager.isUserLoggedIn && token == ANON_TOKEN) {
|
||||
retryWithLogin(cb) {
|
||||
InvalidLoginTokenException(ANONYMOUS_TOKEN_MESSAGE)
|
||||
fun request(
|
||||
service: CsrfTokenInterface,
|
||||
cb: Callback,
|
||||
): Call<MwQueryResponse?> =
|
||||
requestToken(
|
||||
service,
|
||||
object : Callback {
|
||||
override fun success(token: String?) {
|
||||
if (sessionManager.isUserLoggedIn && token == ANON_TOKEN) {
|
||||
retryWithLogin(cb) {
|
||||
InvalidLoginTokenException(ANONYMOUS_TOKEN_MESSAGE)
|
||||
}
|
||||
} else {
|
||||
cb.success(token)
|
||||
}
|
||||
} else {
|
||||
cb.success(token)
|
||||
}
|
||||
}
|
||||
|
||||
override fun failure(caught: Throwable?) = retryWithLogin(cb) { caught }
|
||||
override fun failure(caught: Throwable?) = retryWithLogin(cb) { caught }
|
||||
|
||||
override fun twoFactorPrompt() = cb.twoFactorPrompt()
|
||||
})
|
||||
override fun twoFactorPrompt() = cb.twoFactorPrompt()
|
||||
},
|
||||
)
|
||||
|
||||
@VisibleForTesting
|
||||
fun requestToken(service: CsrfTokenInterface, cb: Callback): Call<MwQueryResponse?> {
|
||||
fun requestToken(
|
||||
service: CsrfTokenInterface,
|
||||
cb: Callback,
|
||||
): Call<MwQueryResponse?> {
|
||||
val call = service.getCsrfTokenCall()
|
||||
call.enqueue(object : retrofit2.Callback<MwQueryResponse?> {
|
||||
override fun onResponse(call: Call<MwQueryResponse?>, response: Response<MwQueryResponse?>) {
|
||||
if (call.isCanceled) {
|
||||
return
|
||||
call.enqueue(
|
||||
object : retrofit2.Callback<MwQueryResponse?> {
|
||||
override fun onResponse(
|
||||
call: Call<MwQueryResponse?>,
|
||||
response: Response<MwQueryResponse?>,
|
||||
) {
|
||||
if (call.isCanceled) {
|
||||
return
|
||||
}
|
||||
cb.success(response.body()!!.query()!!.csrfToken())
|
||||
}
|
||||
cb.success(response.body()!!.query()!!.csrfToken())
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<MwQueryResponse?>, t: Throwable) {
|
||||
if (call.isCanceled) {
|
||||
return
|
||||
override fun onFailure(
|
||||
call: Call<MwQueryResponse?>,
|
||||
t: Throwable,
|
||||
) {
|
||||
if (call.isCanceled) {
|
||||
return
|
||||
}
|
||||
cb.failure(t)
|
||||
}
|
||||
cb.failure(t)
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
return call
|
||||
}
|
||||
|
||||
private fun retryWithLogin(callback: Callback, caught: () -> Throwable?) {
|
||||
private fun retryWithLogin(
|
||||
callback: Callback,
|
||||
caught: () -> Throwable?,
|
||||
) {
|
||||
val userName = sessionManager.userName
|
||||
val password = sessionManager.password
|
||||
if (retries < MAX_RETRIES && !userName.isNullOrEmpty() && !password.isNullOrEmpty()) {
|
||||
|
|
@ -123,26 +150,31 @@ class CsrfTokenClient(
|
|||
username: String,
|
||||
password: String,
|
||||
callback: Callback,
|
||||
retryCallback: () -> Unit
|
||||
) = loginClient.request(username, password, object : LoginCallback {
|
||||
override fun success(loginResult: LoginResult) {
|
||||
if (loginResult.pass) {
|
||||
sessionManager.updateAccount(loginResult)
|
||||
retryCallback()
|
||||
} else {
|
||||
callback.failure(LoginFailedException(loginResult.message))
|
||||
retryCallback: () -> Unit,
|
||||
) = loginClient.request(
|
||||
username,
|
||||
password,
|
||||
object : LoginCallback {
|
||||
override fun success(loginResult: LoginResult) {
|
||||
if (loginResult.pass) {
|
||||
sessionManager.updateAccount(loginResult)
|
||||
retryCallback()
|
||||
} else {
|
||||
callback.failure(LoginFailedException(loginResult.message))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun twoFactorPrompt(caught: Throwable, token: String?) =
|
||||
callback.twoFactorPrompt()
|
||||
override fun twoFactorPrompt(
|
||||
caught: Throwable,
|
||||
token: String?,
|
||||
) = callback.twoFactorPrompt()
|
||||
|
||||
// Should not happen here, but call the callback just in case.
|
||||
override fun passwordResetPrompt(token: String?) =
|
||||
callback.failure(LoginFailedException("Logged in with temporary password."))
|
||||
// Should not happen here, but call the callback just in case.
|
||||
override fun passwordResetPrompt(token: String?) = callback.failure(LoginFailedException("Logged in with temporary password."))
|
||||
|
||||
override fun error(caught: Throwable) = callback.failure(caught)
|
||||
})
|
||||
override fun error(caught: Throwable) = callback.failure(caught)
|
||||
},
|
||||
)
|
||||
|
||||
private fun cancel() {
|
||||
loginClient.cancel()
|
||||
|
|
@ -154,7 +186,9 @@ class CsrfTokenClient(
|
|||
|
||||
interface Callback {
|
||||
fun success(token: String?)
|
||||
|
||||
fun failure(caught: Throwable?)
|
||||
|
||||
fun twoFactorPrompt()
|
||||
}
|
||||
|
||||
|
|
@ -166,5 +200,7 @@ class CsrfTokenClient(
|
|||
const val ANONYMOUS_TOKEN_MESSAGE = "App believes we're logged in, but got anonymous token."
|
||||
}
|
||||
}
|
||||
class InvalidLoginTokenException(message: String) : Exception(message)
|
||||
|
||||
class InvalidLoginTokenException(
|
||||
message: String,
|
||||
) : Exception(message)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,10 @@ package fr.free.nrw.commons.auth.csrf
|
|||
import fr.free.nrw.commons.wikidata.cookies.CommonsCookieStorage
|
||||
import javax.inject.Inject
|
||||
|
||||
class LogoutClient @Inject constructor(private val store: CommonsCookieStorage) {
|
||||
fun logout() = store.clear()
|
||||
}
|
||||
class LogoutClient
|
||||
@Inject
|
||||
constructor(
|
||||
private val store: CommonsCookieStorage,
|
||||
) {
|
||||
fun logout() = store.clear()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,13 @@ package fr.free.nrw.commons.auth.login
|
|||
|
||||
interface LoginCallback {
|
||||
fun success(loginResult: LoginResult)
|
||||
fun twoFactorPrompt(caught: Throwable, token: String?)
|
||||
|
||||
fun twoFactorPrompt(
|
||||
caught: Throwable,
|
||||
token: String?,
|
||||
)
|
||||
|
||||
fun passwordResetPrompt(token: String?)
|
||||
|
||||
fun error(caught: Throwable)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ import android.text.TextUtils
|
|||
import fr.free.nrw.commons.auth.login.LoginResult.OAuthResult
|
||||
import fr.free.nrw.commons.auth.login.LoginResult.ResetPasswordResult
|
||||
import fr.free.nrw.commons.wikidata.WikidataConstants.WIKIPEDIA_URL
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
|
|
@ -16,7 +16,9 @@ import java.io.IOException
|
|||
/**
|
||||
* Responsible for making login related requests to the server.
|
||||
*/
|
||||
class LoginClient(private val loginInterface: LoginInterface) {
|
||||
class LoginClient(
|
||||
private val loginInterface: LoginInterface,
|
||||
) {
|
||||
private var tokenCall: Call<MwQueryResponse?>? = null
|
||||
private var loginCall: Call<LoginResponse?>? = null
|
||||
|
||||
|
|
@ -30,80 +32,116 @@ class LoginClient(private val loginInterface: LoginInterface) {
|
|||
|
||||
private fun getLoginToken() = loginInterface.getLoginToken()
|
||||
|
||||
fun request(userName: String, password: String, cb: LoginCallback) {
|
||||
fun request(
|
||||
userName: String,
|
||||
password: String,
|
||||
cb: LoginCallback,
|
||||
) {
|
||||
cancel()
|
||||
|
||||
tokenCall = getLoginToken()
|
||||
tokenCall!!.enqueue(object : Callback<MwQueryResponse?> {
|
||||
override fun onResponse(call: Call<MwQueryResponse?>, response: Response<MwQueryResponse?>) {
|
||||
login(
|
||||
userName, password, null, null, response.body()!!.query()!!.loginToken(),
|
||||
userLanguage, cb
|
||||
)
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<MwQueryResponse?>, caught: Throwable) {
|
||||
if (call.isCanceled) {
|
||||
return
|
||||
tokenCall!!.enqueue(
|
||||
object : Callback<MwQueryResponse?> {
|
||||
override fun onResponse(
|
||||
call: Call<MwQueryResponse?>,
|
||||
response: Response<MwQueryResponse?>,
|
||||
) {
|
||||
login(
|
||||
userName,
|
||||
password,
|
||||
null,
|
||||
null,
|
||||
response.body()!!.query()!!.loginToken(),
|
||||
userLanguage,
|
||||
cb,
|
||||
)
|
||||
}
|
||||
cb.error(caught)
|
||||
}
|
||||
})
|
||||
|
||||
override fun onFailure(
|
||||
call: Call<MwQueryResponse?>,
|
||||
caught: Throwable,
|
||||
) {
|
||||
if (call.isCanceled) {
|
||||
return
|
||||
}
|
||||
cb.error(caught)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fun login(
|
||||
userName: String, password: String, retypedPassword: String?, twoFactorCode: String?,
|
||||
loginToken: String?, userLanguage: String, cb: LoginCallback
|
||||
userName: String,
|
||||
password: String,
|
||||
retypedPassword: String?,
|
||||
twoFactorCode: String?,
|
||||
loginToken: String?,
|
||||
userLanguage: String,
|
||||
cb: LoginCallback,
|
||||
) {
|
||||
this.userLanguage = userLanguage
|
||||
|
||||
loginCall = if (twoFactorCode.isNullOrEmpty() && retypedPassword.isNullOrEmpty()) {
|
||||
loginInterface.postLogIn(userName, password, loginToken, userLanguage, WIKIPEDIA_URL)
|
||||
} else {
|
||||
loginInterface.postLogIn(
|
||||
userName, password, retypedPassword, twoFactorCode, loginToken, userLanguage, true
|
||||
)
|
||||
}
|
||||
loginCall =
|
||||
if (twoFactorCode.isNullOrEmpty() && retypedPassword.isNullOrEmpty()) {
|
||||
loginInterface.postLogIn(userName, password, loginToken, userLanguage, WIKIPEDIA_URL)
|
||||
} else {
|
||||
loginInterface.postLogIn(
|
||||
userName,
|
||||
password,
|
||||
retypedPassword,
|
||||
twoFactorCode,
|
||||
loginToken,
|
||||
userLanguage,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
loginCall!!.enqueue(object : Callback<LoginResponse?> {
|
||||
override fun onResponse(
|
||||
call: Call<LoginResponse?>,
|
||||
response: Response<LoginResponse?>
|
||||
) {
|
||||
val loginResult = response.body()?.toLoginResult(password)
|
||||
if (loginResult != null) {
|
||||
if (loginResult.pass && !loginResult.userName.isNullOrEmpty()) {
|
||||
// The server could do some transformations on user names, e.g. on some
|
||||
// wikis is uppercases the first letter.
|
||||
getExtendedInfo(loginResult.userName, loginResult, cb)
|
||||
} else if ("UI" == loginResult.status) {
|
||||
when (loginResult) {
|
||||
is OAuthResult -> cb.twoFactorPrompt(
|
||||
LoginFailedException(loginResult.message),
|
||||
loginToken
|
||||
)
|
||||
loginCall!!.enqueue(
|
||||
object : Callback<LoginResponse?> {
|
||||
override fun onResponse(
|
||||
call: Call<LoginResponse?>,
|
||||
response: Response<LoginResponse?>,
|
||||
) {
|
||||
val loginResult = response.body()?.toLoginResult(password)
|
||||
if (loginResult != null) {
|
||||
if (loginResult.pass && !loginResult.userName.isNullOrEmpty()) {
|
||||
// The server could do some transformations on user names, e.g. on some
|
||||
// wikis is uppercases the first letter.
|
||||
getExtendedInfo(loginResult.userName, loginResult, cb)
|
||||
} else if ("UI" == loginResult.status) {
|
||||
when (loginResult) {
|
||||
is OAuthResult ->
|
||||
cb.twoFactorPrompt(
|
||||
LoginFailedException(loginResult.message),
|
||||
loginToken,
|
||||
)
|
||||
|
||||
is ResetPasswordResult -> cb.passwordResetPrompt(loginToken)
|
||||
is ResetPasswordResult -> cb.passwordResetPrompt(loginToken)
|
||||
|
||||
is LoginResult.Result -> cb.error(
|
||||
LoginFailedException(loginResult.message)
|
||||
)
|
||||
is LoginResult.Result ->
|
||||
cb.error(
|
||||
LoginFailedException(loginResult.message),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
cb.error(LoginFailedException(loginResult.message))
|
||||
}
|
||||
} else {
|
||||
cb.error(LoginFailedException(loginResult.message))
|
||||
cb.error(IOException("Login failed. Unexpected response."))
|
||||
}
|
||||
} else {
|
||||
cb.error(IOException("Login failed. Unexpected response."))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<LoginResponse?>, t: Throwable) {
|
||||
if (call.isCanceled) {
|
||||
return
|
||||
override fun onFailure(
|
||||
call: Call<LoginResponse?>,
|
||||
t: Throwable,
|
||||
) {
|
||||
if (call.isCanceled) {
|
||||
return
|
||||
}
|
||||
cb.error(t)
|
||||
}
|
||||
cb.error(t)
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fun doLogin(
|
||||
|
|
@ -111,43 +149,65 @@ class LoginClient(private val loginInterface: LoginInterface) {
|
|||
password: String,
|
||||
twoFactorCode: String,
|
||||
userLanguage: String,
|
||||
loginCallback: LoginCallback
|
||||
loginCallback: LoginCallback,
|
||||
) {
|
||||
getLoginToken().enqueue(object :Callback<MwQueryResponse?>{
|
||||
override fun onResponse(
|
||||
call: Call<MwQueryResponse?>,
|
||||
response: Response<MwQueryResponse?>
|
||||
) = if (response.isSuccessful){
|
||||
val loginToken = response.body()?.query()?.loginToken()
|
||||
loginToken?.let {
|
||||
login(username, password, null, twoFactorCode, it, userLanguage, loginCallback)
|
||||
} ?: run {
|
||||
getLoginToken().enqueue(
|
||||
object : Callback<MwQueryResponse?> {
|
||||
override fun onResponse(
|
||||
call: Call<MwQueryResponse?>,
|
||||
response: Response<MwQueryResponse?>,
|
||||
) = if (response.isSuccessful) {
|
||||
val loginToken = response.body()?.query()?.loginToken()
|
||||
loginToken?.let {
|
||||
login(username, password, null, twoFactorCode, it, userLanguage, loginCallback)
|
||||
} ?: run {
|
||||
loginCallback.error(IOException("Failed to retrieve login token"))
|
||||
}
|
||||
} else {
|
||||
loginCallback.error(IOException("Failed to retrieve login token"))
|
||||
}
|
||||
} else {
|
||||
loginCallback.error(IOException("Failed to retrieve login token"))
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<MwQueryResponse?>, t: Throwable) {
|
||||
loginCallback.error(t)
|
||||
}
|
||||
})
|
||||
override fun onFailure(
|
||||
call: Call<MwQueryResponse?>,
|
||||
t: Throwable,
|
||||
) {
|
||||
loginCallback.error(t)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@Throws(Throwable::class)
|
||||
fun loginBlocking(userName: String, password: String, twoFactorCode: String?) {
|
||||
fun loginBlocking(
|
||||
userName: String,
|
||||
password: String,
|
||||
twoFactorCode: String?,
|
||||
) {
|
||||
val tokenResponse = getLoginToken().execute()
|
||||
if (tokenResponse.body()?.query()?.loginToken().isNullOrEmpty()) {
|
||||
if (tokenResponse
|
||||
.body()
|
||||
?.query()
|
||||
?.loginToken()
|
||||
.isNullOrEmpty()
|
||||
) {
|
||||
throw IOException("Unexpected response when getting login token.")
|
||||
}
|
||||
|
||||
val loginToken = tokenResponse.body()?.query()?.loginToken()
|
||||
val tempLoginCall = if (twoFactorCode.isNullOrEmpty()) {
|
||||
loginInterface.postLogIn(userName, password, loginToken, userLanguage, WIKIPEDIA_URL)
|
||||
} else {
|
||||
loginInterface.postLogIn(
|
||||
userName, password, null, twoFactorCode, loginToken, userLanguage, true
|
||||
)
|
||||
}
|
||||
val tempLoginCall =
|
||||
if (twoFactorCode.isNullOrEmpty()) {
|
||||
loginInterface.postLogIn(userName, password, loginToken, userLanguage, WIKIPEDIA_URL)
|
||||
} else {
|
||||
loginInterface.postLogIn(
|
||||
userName,
|
||||
password,
|
||||
null,
|
||||
twoFactorCode,
|
||||
loginToken,
|
||||
userLanguage,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
val response = tempLoginCall.execute()
|
||||
val loginResponse = response.body() ?: throw IOException("Unexpected response when logging in.")
|
||||
|
|
@ -166,18 +226,23 @@ class LoginClient(private val loginInterface: LoginInterface) {
|
|||
}
|
||||
}
|
||||
|
||||
private fun getExtendedInfo(userName: String, loginResult: LoginResult, cb: LoginCallback) =
|
||||
loginInterface.getUserInfo(userName)
|
||||
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe({ response: MwQueryResponse? ->
|
||||
loginResult.userId = response?.query()?.userInfo()?.id() ?: 0
|
||||
loginResult.groups =
|
||||
response?.query()?.getUserResponse(userName)?.groups ?: emptySet()
|
||||
cb.success(loginResult)
|
||||
}, { caught: Throwable ->
|
||||
Timber.e(caught, "Login succeeded but getting group information failed. ")
|
||||
cb.error(caught)
|
||||
})
|
||||
private fun getExtendedInfo(
|
||||
userName: String,
|
||||
loginResult: LoginResult,
|
||||
cb: LoginCallback,
|
||||
) = loginInterface
|
||||
.getUserInfo(userName)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe({ response: MwQueryResponse? ->
|
||||
loginResult.userId = response?.query()?.userInfo()?.id() ?: 0
|
||||
loginResult.groups =
|
||||
response?.query()?.getUserResponse(userName)?.groups ?: emptySet()
|
||||
cb.success(loginResult)
|
||||
}, { caught: Throwable ->
|
||||
Timber.e(caught, "Login succeeded but getting group information failed. ")
|
||||
cb.error(caught)
|
||||
})
|
||||
|
||||
fun cancel() {
|
||||
tokenCall?.let {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
package fr.free.nrw.commons.auth.login
|
||||
|
||||
class LoginFailedException(message: String?) : Throwable(message)
|
||||
class LoginFailedException(
|
||||
message: String?,
|
||||
) : Throwable(message)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package fr.free.nrw.commons.auth.login
|
||||
|
||||
import fr.free.nrw.commons.wikidata.WikidataConstants.MW_API_PREFIX
|
||||
import io.reactivex.Observable
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
|
||||
import io.reactivex.Observable
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.Field
|
||||
import retrofit2.http.FormUrlEncoded
|
||||
|
|
@ -24,7 +24,7 @@ interface LoginInterface {
|
|||
@Field("password") pass: String?,
|
||||
@Field("logintoken") token: String?,
|
||||
@Field("uselang") userLanguage: String?,
|
||||
@Field("loginreturnurl") url: String?
|
||||
@Field("loginreturnurl") url: String?,
|
||||
): Call<LoginResponse?>
|
||||
|
||||
@Headers("Cache-Control: no-cache")
|
||||
|
|
@ -37,9 +37,11 @@ interface LoginInterface {
|
|||
@Field("OATHToken") twoFactorCode: String?,
|
||||
@Field("logintoken") token: String?,
|
||||
@Field("uselang") userLanguage: String?,
|
||||
@Field("logincontinue") loginContinue: Boolean
|
||||
@Field("logincontinue") loginContinue: Boolean,
|
||||
): Call<LoginResponse?>
|
||||
|
||||
@GET(MW_API_PREFIX + "action=query&meta=userinfo&list=users&usprop=groups|cancreate")
|
||||
fun getUserInfo(@Query("ususers") userName: String): Observable<MwQueryResponse?>
|
||||
}
|
||||
fun getUserInfo(
|
||||
@Query("ususers") userName: String,
|
||||
): Observable<MwQueryResponse?>
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,9 +13,7 @@ class LoginResponse {
|
|||
@SerializedName("clientlogin")
|
||||
private val clientLogin: ClientLogin? = null
|
||||
|
||||
fun toLoginResult(password: String): LoginResult? {
|
||||
return clientLogin?.toLoginResult(password)
|
||||
}
|
||||
fun toLoginResult(password: String): LoginResult? = clientLogin?.toLoginResult(password)
|
||||
}
|
||||
|
||||
internal class ClientLogin {
|
||||
|
|
@ -39,7 +37,7 @@ internal class ClientLogin {
|
|||
}
|
||||
}
|
||||
} else if ("PASS" != status && "FAIL" != status) {
|
||||
//TODO: String resource -- Looks like needed for others in this class too
|
||||
// TODO: String resource -- Looks like needed for others in this class too
|
||||
userMessage = "An unknown error occurred."
|
||||
}
|
||||
return Result(status ?: "", userName, password, userMessage)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ sealed class LoginResult(
|
|||
val status: String,
|
||||
val userName: String?,
|
||||
val password: String?,
|
||||
val message: String?
|
||||
val message: String?,
|
||||
) {
|
||||
var userId = 0
|
||||
var groups = emptySet<String>()
|
||||
|
|
@ -14,20 +14,20 @@ sealed class LoginResult(
|
|||
status: String,
|
||||
userName: String?,
|
||||
password: String?,
|
||||
message: String?
|
||||
): LoginResult(status, userName, password, message)
|
||||
message: String?,
|
||||
) : LoginResult(status, userName, password, message)
|
||||
|
||||
class OAuthResult(
|
||||
status: String,
|
||||
userName: String?,
|
||||
password: String?,
|
||||
message: String?
|
||||
message: String?,
|
||||
) : LoginResult(status, userName, password, message)
|
||||
|
||||
class ResetPasswordResult(
|
||||
status: String,
|
||||
userName: String?,
|
||||
password: String?,
|
||||
message: String?
|
||||
message: String?,
|
||||
) : LoginResult(status, userName, password, message)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue