Skip to content

Commit

Permalink
Move login client out of the data-client (#5476)
Browse files Browse the repository at this point in the history
  • Loading branch information
psh authored Jan 26, 2024
1 parent 8789879 commit 0541aac
Show file tree
Hide file tree
Showing 23 changed files with 195 additions and 234 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
}
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
implementation "com.squareup.retrofit2:converter-gson:2.8.1"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.8.1"
implementation 'com.squareup.okio:okio:2.2.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.3'
Expand Down
14 changes: 3 additions & 11 deletions app/src/main/java/fr/free/nrw/commons/CommonsAppAdapter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package fr.free.nrw.commons;

import androidx.annotation.NonNull;

import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import okhttp3.OkHttpClient;
import org.wikipedia.AppAdapter;
import org.wikipedia.dataclient.SharedPreferenceCookieManager;
import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.json.GsonMarshaller;
import org.wikipedia.json.GsonUnmarshaller;
import org.wikipedia.login.LoginResult;

import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import okhttp3.OkHttpClient;

public class CommonsAppAdapter extends AppAdapter {
private final int DEFAULT_THUMB_SIZE = 640;
Expand Down Expand Up @@ -60,11 +57,6 @@ public String getPassword() {
return sessionManager.getPassword();
}

@Override
public void updateAccount(@NonNull LoginResult result) {
sessionManager.updateAccount(result);
}

@Override
public SharedPreferenceCookieManager getCookies() {
if (!preferences.contains(COOKIE_STORE_NAME)) {
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@
import androidx.core.app.NavUtils;
import androidx.core.content.ContextCompat;

import fr.free.nrw.commons.auth.login.LoginClient;
import fr.free.nrw.commons.auth.login.LoginInterface;
import fr.free.nrw.commons.auth.login.LoginResult;
import fr.free.nrw.commons.databinding.ActivityLoginBinding;
import fr.free.nrw.commons.utils.ActivityUtils;
import java.util.Locale;
import org.wikipedia.AppAdapter;
import org.wikipedia.dataclient.ServiceFactory;
import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.dataclient.mwapi.MwQueryResponse;
import org.wikipedia.login.LoginClient;
import org.wikipedia.login.LoginClient.LoginCallback;
import org.wikipedia.login.LoginResult;
import fr.free.nrw.commons.auth.login.LoginClient.LoginCallback;

import javax.inject.Inject;
import javax.inject.Named;
Expand Down Expand Up @@ -231,7 +232,7 @@ public void performLogin() {

private void doLogin(String username, String password, String twoFactorCode) {
progressDialog.show();
loginToken = ServiceFactory.get(commonsWikiSite).getLoginToken();
loginToken = ServiceFactory.get(commonsWikiSite, LoginInterface.class).getLoginToken();
loginToken.enqueue(
new Callback<MwQueryResponse>() {
@Override
Expand Down Expand Up @@ -313,7 +314,7 @@ private void onLoginSuccess(LoginResult loginResult) {
}
compositeDisposable.clear();
sessionManager.setUserLoggedIn(true);
AppAdapter.get().updateAccount(loginResult);
sessionManager.updateAccount(loginResult);
progressDialog.dismiss();
showSuccessAndDismissDialog();
startMainActivity();
Expand Down
36 changes: 0 additions & 36 deletions app/src/main/java/fr/free/nrw/commons/auth/LogoutClient.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.wikipedia.login.LoginResult;

import fr.free.nrw.commons.auth.login.LoginResult;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
Expand Down
22 changes: 14 additions & 8 deletions app/src/main/java/fr/free/nrw/commons/auth/csrf/CsrfTokenClient.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package fr.free.nrw.commons.auth.csrf

import androidx.annotation.VisibleForTesting
import fr.free.nrw.commons.auth.SessionManager
import org.wikipedia.AppAdapter
import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.dataclient.SharedPreferenceCookieManager
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.dataclient.mwapi.MwQueryResponse
import org.wikipedia.login.LoginClient
import org.wikipedia.login.LoginClient.LoginCallback
import org.wikipedia.login.LoginClient.LoginFailedException
import org.wikipedia.login.LoginResult
import fr.free.nrw.commons.auth.login.LoginClient
import fr.free.nrw.commons.auth.login.LoginClient.LoginCallback
import fr.free.nrw.commons.auth.login.LoginClient.LoginFailedException
import fr.free.nrw.commons.auth.login.LoginResult
import retrofit2.Call
import retrofit2.Response
import timber.log.Timber
import java.io.IOException
import java.util.concurrent.Callable
import java.util.concurrent.Executors.newSingleThreadExecutor

class CsrfTokenClient(private val csrfWikiSite: WikiSite) {
class CsrfTokenClient(
private val csrfWikiSite: WikiSite,
private val sessionManager: SessionManager
) {
private var retries = 0
private var csrfTokenCall: Call<MwQueryResponse?>? = null
private val loginClient = LoginClient()
Expand All @@ -33,7 +37,8 @@ class CsrfTokenClient(private val csrfWikiSite: WikiSite) {
try {
if (retry > 0) {
// Log in explicitly
LoginClient().loginBlocking(csrfWikiSite, userName, password, "")
LoginClient()
.loginBlocking(csrfWikiSite, userName, password, "")
}

// Get CSRFToken response off the main thread.
Expand Down Expand Up @@ -121,10 +126,11 @@ class CsrfTokenClient(private val csrfWikiSite: WikiSite) {
password: String,
callback: Callback,
retryCallback: () -> Unit
) = LoginClient().request(csrfWikiSite, username, password, object : LoginCallback {
) = LoginClient()
.request(csrfWikiSite, username, password, object : LoginCallback {
override fun success(loginResult: LoginResult) {
if (loginResult.pass()) {
AppAdapter.get().updateAccount(loginResult)
sessionManager.updateAccount(loginResult)
retryCallback()
} else {
callback.failure(LoginFailedException(loginResult.message))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.wikipedia.login;
package fr.free.nrw.commons.auth.login;

import android.annotation.SuppressLint;
import android.text.TextUtils;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void request(@NonNull final WikiSite wiki, @NonNull final String userName
@NonNull final String password, @NonNull final LoginCallback cb) {
cancel();

tokenCall = ServiceFactory.get(wiki).getLoginToken();
tokenCall = ServiceFactory.get(wiki, LoginInterface.class).getLoginToken();
tokenCall.enqueue(new Callback<MwQueryResponse>() {
@Override public void onResponse(@NonNull Call<MwQueryResponse> call,
@NonNull Response<MwQueryResponse> response) {
Expand All @@ -75,8 +75,8 @@ public void login(@NonNull final WikiSite wiki, @NonNull final String userName,
@Nullable final String loginToken, @NonNull final String userLanguage, @NonNull final LoginCallback cb) {
this.userLanguage = userLanguage;
loginCall = TextUtils.isEmpty(twoFactorCode) && TextUtils.isEmpty(retypedPassword)
? ServiceFactory.get(wiki).postLogIn(userName, password, loginToken, userLanguage, Service.WIKIPEDIA_URL)
: ServiceFactory.get(wiki).postLogIn(userName, password, retypedPassword, twoFactorCode, loginToken,
? ServiceFactory.get(wiki, LoginInterface.class).postLogIn(userName, password, loginToken, userLanguage, Service.WIKIPEDIA_URL)
: ServiceFactory.get(wiki, LoginInterface.class).postLogIn(userName, password, retypedPassword, twoFactorCode, loginToken,
userLanguage, true);
loginCall.enqueue(new Callback<LoginResponse>() {
@Override
Expand Down Expand Up @@ -117,15 +117,15 @@ public void onFailure(@NonNull Call<LoginResponse> call, @NonNull Throwable t) {

public void loginBlocking(@NonNull final WikiSite wiki, @NonNull final String userName,
@NonNull final String password, @Nullable final String twoFactorCode) throws Throwable {
Response<MwQueryResponse> tokenResponse = ServiceFactory.get(wiki).getLoginToken().execute();
Response<MwQueryResponse> tokenResponse = ServiceFactory.get(wiki, LoginInterface.class).getLoginToken().execute();
if (tokenResponse.body() == null || TextUtils.isEmpty(tokenResponse.body().query().loginToken())) {
throw new IOException("Unexpected response when getting login token.");
}
String loginToken = tokenResponse.body().query().loginToken();

Call<LoginResponse> tempLoginCall = StringUtils.defaultIfEmpty(twoFactorCode, "").isEmpty()
? ServiceFactory.get(wiki).postLogIn(userName, password, loginToken, userLanguage, Service.WIKIPEDIA_URL)
: ServiceFactory.get(wiki).postLogIn(userName, password, null, twoFactorCode, loginToken,
? ServiceFactory.get(wiki, LoginInterface.class).postLogIn(userName, password, loginToken, userLanguage, Service.WIKIPEDIA_URL)
: ServiceFactory.get(wiki, LoginInterface.class).postLogIn(userName, password, null, twoFactorCode, loginToken,
userLanguage, true);
Response<LoginResponse> response = tempLoginCall.execute();
LoginResponse loginResponse = response.body();
Expand Down Expand Up @@ -153,7 +153,7 @@ public void loginBlocking(@NonNull final WikiSite wiki, @NonNull final String us
@SuppressLint("CheckResult")
private void getExtendedInfo(@NonNull final WikiSite wiki, @NonNull String userName,
@NonNull final LoginResult loginResult, @NonNull final LoginCallback cb) {
ServiceFactory.get(wiki).getUserInfo(userName)
ServiceFactory.get(wiki, LoginInterface.class).getUserInfo(userName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/auth/login/LoginInterface.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fr.free.nrw.commons.auth.login

import io.reactivex.Observable
import org.wikipedia.dataclient.Service
import org.wikipedia.dataclient.mwapi.MwQueryResponse
import retrofit2.Call
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Query

interface LoginInterface {
@Headers("Cache-Control: no-cache")
@GET(Service.MW_API_PREFIX + "action=query&meta=tokens&type=login")
fun getLoginToken(): Call<MwQueryResponse?>

@Headers("Cache-Control: no-cache")
@FormUrlEncoded
@POST(Service.MW_API_PREFIX + "action=clientlogin&rememberMe=")
fun postLogIn(
@Field("username") user: String?,
@Field("password") pass: String?,
@Field("logintoken") token: String?,
@Field("uselang") userLanguage: String?,
@Field("loginreturnurl") url: String?
): Call<LoginClient.LoginResponse?>

@Headers("Cache-Control: no-cache")
@FormUrlEncoded
@POST(Service.MW_API_PREFIX + "action=clientlogin&rememberMe=")
fun postLogIn(
@Field("username") user: String?,
@Field("password") pass: String?,
@Field("retype") retypedPass: String?,
@Field("OATHToken") twoFactorCode: String?,
@Field("logintoken") token: String?,
@Field("uselang") userLanguage: String?,
@Field("logincontinue") loginContinue: Boolean
): Call<LoginClient.LoginResponse?>

@GET(Service.MW_API_PREFIX + "action=query&meta=userinfo&list=users&usprop=groups|cancreate")
fun getUserInfo(@Query("ususers") userName: String): Observable<MwQueryResponse?>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.wikipedia.login;
package fr.free.nrw.commons.auth.login;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.wikipedia.login;
package fr.free.nrw.commons.auth.login;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.wikipedia.login;
package fr.free.nrw.commons.auth.login;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import fr.free.nrw.commons.actions.PageEditClient;
import fr.free.nrw.commons.actions.PageEditInterface;
import fr.free.nrw.commons.actions.ThanksInterface;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.category.CategoryInterface;
import fr.free.nrw.commons.explore.depictions.DepictsClient;
import fr.free.nrw.commons.kvstore.JsonKvStore;
Expand Down Expand Up @@ -40,7 +41,7 @@
import org.wikipedia.dataclient.ServiceFactory;
import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.json.GsonUtil;
import org.wikipedia.login.LoginClient;
import fr.free.nrw.commons.auth.login.LoginClient;
import timber.log.Timber;

@Module
Expand Down Expand Up @@ -105,8 +106,9 @@ public OkHttpJsonApiClient provideOkHttpJsonApiClient(OkHttpClient okHttpClient,
@Named(NAMED_COMMONS_CSRF)
@Provides
@Singleton
public CsrfTokenClient provideCommonsCsrfTokenClient(@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) {
return new CsrfTokenClient(commonsWikiSite);
public CsrfTokenClient provideCommonsCsrfTokenClient(
@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite, SessionManager sessionManager) {
return new CsrfTokenClient(commonsWikiSite, sessionManager);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.wikipedia.dataclient.SharedPreferenceCookieManager
import org.wikipedia.json.GsonMarshaller
import org.wikipedia.login.LoginResult
import fr.free.nrw.commons.auth.login.LoginResult

class CommonsAppAdapterUnitTest {

Expand Down Expand Up @@ -80,13 +80,6 @@ class CommonsAppAdapterUnitTest {
Assert.assertEquals(adapter.password, "test")
}

@Test
@Throws(Exception::class)
fun testUpdateAccount() {
adapter.updateAccount(result)
verify(sessionManager).updateAccount(result)
}

@Test
@Throws(Exception::class)
fun testSetCookies() {
Expand Down
5 changes: 0 additions & 5 deletions app/src/test/kotlin/fr/free/nrw/commons/TestAppAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.dataclient.okhttp.TestStubInterceptor;
import org.wikipedia.dataclient.okhttp.UnsuccessfulResponseInterceptor;
import org.wikipedia.login.LoginResult;

public class TestAppAdapter extends AppAdapter {

Expand Down Expand Up @@ -50,10 +49,6 @@ public String getPassword() {
return null;
}

@Override
public void updateAccount(@NonNull LoginResult result) {
}

@Override
public SharedPreferenceCookieManager getCookies() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import android.view.KeyEvent
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.Button
import android.widget.TextView
import androidx.test.core.app.ApplicationProvider
import fr.free.nrw.commons.R
import fr.free.nrw.commons.TestAppAdapter
import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.auth.login.LoginResult
import fr.free.nrw.commons.kvstore.JsonKvStore
import org.junit.Assert
import org.junit.Before
Expand All @@ -29,7 +28,6 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.fakes.RoboMenuItem
import org.wikipedia.AppAdapter
import org.wikipedia.login.LoginResult
import java.lang.reflect.Method


Expand Down
Loading

0 comments on commit 0541aac

Please sign in to comment.