Fix auth issues in the app (#3064)

This commit is contained in:
Vivek Maskara 2019-07-12 01:49:28 +05:30 committed by GitHub
parent 5d64cacb30
commit cd57552d76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 26 additions and 109 deletions

View file

@ -12,48 +12,19 @@ import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import static fr.free.nrw.commons.auth.AccountUtil.AUTH_COOKIE;
public abstract class AuthenticatedActivity extends NavigationBaseActivity { public abstract class AuthenticatedActivity extends NavigationBaseActivity {
@Inject @Inject
protected SessionManager sessionManager; protected SessionManager sessionManager;
@Inject @Inject
MediaWikiApi mediaWikiApi; MediaWikiApi mediaWikiApi;
private String authCookie;
protected void requestAuthToken() {
if (authCookie != null) {
onAuthCookieAcquired(authCookie);
return;
}
authCookie = sessionManager.getAuthCookie();
if (authCookie != null) {
onAuthCookieAcquired(authCookie);
}
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
authCookie = savedInstanceState.getString(AUTH_COOKIE);
}
showBlockStatus(); showBlockStatus();
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(AUTH_COOKIE, authCookie);
}
protected abstract void onAuthCookieAcquired(String authCookie);
protected abstract void onAuthFailure();
/** /**
* Makes API call to check if user is blocked from Commons. If the user is blocked, a snackbar * Makes API call to check if user is blocked from Commons. If the user is blocked, a snackbar
* is created to notify the user * is created to notify the user

View file

@ -212,8 +212,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
} }
if (sessionManager.getCurrentAccount() != null if (sessionManager.getCurrentAccount() != null
&& sessionManager.isUserLoggedIn() && sessionManager.isUserLoggedIn()) {
&& sessionManager.getCachedAuthCookie() != null) {
applicationKvStore.putBoolean("login_skipped", false); applicationKvStore.putBoolean("login_skipped", false);
startMainActivity(); startMainActivity();
} }
@ -308,6 +307,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
// no longer attached to activity! // no longer attached to activity!
return; return;
} }
sessionManager.setUserLoggedIn(true);
LoginResult loginResult = new LoginResult(commonsWikiSite, "PASS", username, password, ""); LoginResult loginResult = new LoginResult(commonsWikiSite, "PASS", username, password, "");
AppAdapter.get().updateAccount(loginResult); AppAdapter.get().updateAccount(loginResult);
progressDialog.dismiss(); progressDialog.dismiss();

View file

@ -20,7 +20,6 @@ import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.mwapi.MediaWikiApi;
import io.reactivex.Completable; import io.reactivex.Completable;
import io.reactivex.Observable; import io.reactivex.Observable;
import timber.log.Timber;
/** /**
* Manage the current logged in user session. * Manage the current logged in user session.
@ -53,7 +52,7 @@ public class SessionManager {
return true; return true;
} }
public void removeAccount() { private void removeAccount() {
Account account = getCurrentAccount(); Account account = getCurrentAccount();
if (account != null) { if (account != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
@ -101,7 +100,7 @@ public class SessionManager {
} }
@Nullable @Nullable
public String getRawUserName() { private String getRawUserName() {
Account account = getCurrentAccount(); Account account = getCurrentAccount();
return account == null ? null : accountManager().getUserData(account, KEY_RAWUSERNAME); return account == null ? null : accountManager().getUserData(account, KEY_RAWUSERNAME);
} }
@ -121,27 +120,14 @@ public class SessionManager {
return AccountManager.get(context); return AccountManager.get(context);
} }
public String getAuthCookie() {
if (!isUserLoggedIn()) {
Timber.e("User is not logged in");
return null;
} else {
String authCookie = getCachedAuthCookie();
if (authCookie == null) {
Timber.e("Auth cookie is null even after login");
}
return authCookie;
}
}
public String getCachedAuthCookie() {
return defaultKvStore.getString("getAuthCookie", null);
}
public boolean isUserLoggedIn() { public boolean isUserLoggedIn() {
return defaultKvStore.getBoolean("isUserLoggedIn", false); return defaultKvStore.getBoolean("isUserLoggedIn", false);
} }
void setUserLoggedIn(boolean isLoggedIn) {
defaultKvStore.putBoolean("isUserLoggedIn", isLoggedIn);
}
public void forceLogin(Context context) { public void forceLogin(Context context) {
if (context != null) { if (context != null) {
LoginActivity.startYourself(context); LoginActivity.startYourself(context);

View file

@ -2,20 +2,19 @@ package fr.free.nrw.commons.category;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle; import android.os.Bundle;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import fr.free.nrw.commons.Media; import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.AuthenticatedActivity;
import fr.free.nrw.commons.explore.SearchActivity; import fr.free.nrw.commons.explore.SearchActivity;
import fr.free.nrw.commons.media.MediaDetailPagerFragment; import fr.free.nrw.commons.media.MediaDetailPagerFragment;
import fr.free.nrw.commons.theme.NavigationBaseActivity; import fr.free.nrw.commons.theme.NavigationBaseActivity;
@ -28,7 +27,7 @@ import fr.free.nrw.commons.theme.NavigationBaseActivity;
*/ */
public class CategoryImagesActivity public class CategoryImagesActivity
extends AuthenticatedActivity extends NavigationBaseActivity
implements FragmentManager.OnBackStackChangedListener, implements FragmentManager.OnBackStackChangedListener,
MediaDetailPagerFragment.MediaDetailProvider, MediaDetailPagerFragment.MediaDetailProvider,
AdapterView.OnItemClickListener{ AdapterView.OnItemClickListener{
@ -38,16 +37,6 @@ public class CategoryImagesActivity
private CategoryImagesListFragment categoryImagesListFragment; private CategoryImagesListFragment categoryImagesListFragment;
private MediaDetailPagerFragment mediaDetails; private MediaDetailPagerFragment mediaDetails;
@Override
protected void onAuthCookieAcquired(String authCookie) {
}
@Override
protected void onAuthFailure() {
}
/** /**
* This method is called on backPressed of anyFragment in the activity. * This method is called on backPressed of anyFragment in the activity.
* We are changing the icon here from back to hamburger icon. * We are changing the icon here from back to hamburger icon.
@ -69,7 +58,6 @@ public class CategoryImagesActivity
supportFragmentManager = getSupportFragmentManager(); supportFragmentManager = getSupportFragmentManager();
setCategoryImagesFragment(); setCategoryImagesFragment();
supportFragmentManager.addOnBackStackChangedListener(this); supportFragmentManager.addOnBackStackChangedListener(this);
requestAuthToken();
initDrawer(); initDrawer();
setPageTitle(); setPageTitle();
} }

View file

@ -85,8 +85,8 @@ public class ContributionsFragment
private ContributionsListFragment contributionsListFragment; private ContributionsListFragment contributionsListFragment;
private MediaDetailPagerFragment mediaDetailPagerFragment; private MediaDetailPagerFragment mediaDetailPagerFragment;
public static final String CONTRIBUTION_LIST_FRAGMENT_TAG = "ContributionListFragmentTag"; private static final String CONTRIBUTION_LIST_FRAGMENT_TAG = "ContributionListFragmentTag";
public static final String MEDIA_DETAIL_PAGER_FRAGMENT_TAG = "MediaDetailFragmentTag"; static final String MEDIA_DETAIL_PAGER_FRAGMENT_TAG = "MediaDetailFragmentTag";
@BindView(R.id.card_view_nearby) public NearbyNotificationCardView nearbyNotificationCardView; @BindView(R.id.card_view_nearby) public NearbyNotificationCardView nearbyNotificationCardView;
@BindView(R.id.campaigns_view) CampaignView campaignView; @BindView(R.id.campaigns_view) CampaignView campaignView;
@ -257,7 +257,7 @@ public class ContributionsFragment
operations on first time fragment attached to an activity. Then they will be retained operations on first time fragment attached to an activity. Then they will be retained
until fragment life time ends. until fragment life time ends.
*/ */
if (((MainActivity)getActivity()).isAuthCookieAcquired && !isFragmentAttachedBefore) { if (!isFragmentAttachedBefore) {
onAuthCookieAcquired(((MainActivity)getActivity()).uploadServiceIntent); onAuthCookieAcquired(((MainActivity)getActivity()).uploadServiceIntent);
isFragmentAttachedBefore = true; isFragmentAttachedBefore = true;
@ -268,7 +268,7 @@ public class ContributionsFragment
* Replace FrameLayout with ContributionsListFragment, user will see contributions list. Creates * Replace FrameLayout with ContributionsListFragment, user will see contributions list. Creates
* new one if null. * new one if null.
*/ */
public void showContributionsListFragment() { private void showContributionsListFragment() {
// show tabs on contribution list is visible // show tabs on contribution list is visible
((MainActivity) getActivity()).showTabs(); ((MainActivity) getActivity()).showTabs();
// show nearby card view on contributions list is visible // show nearby card view on contributions list is visible
@ -289,7 +289,7 @@ public class ContributionsFragment
* Replace FrameLayout with MediaDetailPagerFragment, user will see details of selected media. * Replace FrameLayout with MediaDetailPagerFragment, user will see details of selected media.
* Creates new one if null. * Creates new one if null.
*/ */
public void showMediaDetailPagerFragment() { private void showMediaDetailPagerFragment() {
// hide tabs on media detail view is visible // hide tabs on media detail view is visible
((MainActivity)getActivity()).hideTabs(); ((MainActivity)getActivity()).hideTabs();
// hide nearby card view on media detail is visible // hide nearby card view on media detail is visible
@ -308,7 +308,7 @@ public class ContributionsFragment
* Called when onAuthCookieAcquired is called on authenticated parent activity * Called when onAuthCookieAcquired is called on authenticated parent activity
* @param uploadServiceIntent * @param uploadServiceIntent
*/ */
public void onAuthCookieAcquired(Intent uploadServiceIntent) { void onAuthCookieAcquired(Intent uploadServiceIntent) {
// Since we call onAuthCookieAcquired method from onAttach, isAdded is still false. So don't use it // Since we call onAuthCookieAcquired method from onAttach, isAdded is still false. So don't use it
if (getActivity() != null) { // If fragment is attached to parent activity if (getActivity() != null) { // If fragment is attached to parent activity
@ -324,7 +324,7 @@ public class ContributionsFragment
* mediaDetailPagerFragment, and preserve previous state in back stack. * mediaDetailPagerFragment, and preserve previous state in back stack.
* Called when user selects a contribution. * Called when user selects a contribution.
*/ */
public void showDetail(int i) { private void showDetail(int i) {
if (mediaDetailPagerFragment == null || !mediaDetailPagerFragment.isVisible()) { if (mediaDetailPagerFragment == null || !mediaDetailPagerFragment.isVisible()) {
mediaDetailPagerFragment = new MediaDetailPagerFragment(); mediaDetailPagerFragment = new MediaDetailPagerFragment();
showMediaDetailPagerFragment(); showMediaDetailPagerFragment();

View file

@ -29,7 +29,6 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import fr.free.nrw.commons.BuildConfig; import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.AuthenticatedActivity;
import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.location.LocationServiceManager; import fr.free.nrw.commons.location.LocationServiceManager;
import fr.free.nrw.commons.nearby.NearbyFragment; import fr.free.nrw.commons.nearby.NearbyFragment;
@ -38,6 +37,7 @@ import fr.free.nrw.commons.notification.Notification;
import fr.free.nrw.commons.notification.NotificationActivity; import fr.free.nrw.commons.notification.NotificationActivity;
import fr.free.nrw.commons.notification.NotificationController; import fr.free.nrw.commons.notification.NotificationController;
import fr.free.nrw.commons.quiz.QuizChecker; import fr.free.nrw.commons.quiz.QuizChecker;
import fr.free.nrw.commons.theme.NavigationBaseActivity;
import fr.free.nrw.commons.upload.UploadService; import fr.free.nrw.commons.upload.UploadService;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
@ -45,7 +45,7 @@ import timber.log.Timber;
import static android.content.ContentResolver.requestSync; import static android.content.ContentResolver.requestSync;
public class MainActivity extends AuthenticatedActivity implements FragmentManager.OnBackStackChangedListener { public class MainActivity extends NavigationBaseActivity implements FragmentManager.OnBackStackChangedListener {
@Inject @Inject
SessionManager sessionManager; SessionManager sessionManager;
@ -63,7 +63,6 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag
public Intent uploadServiceIntent; public Intent uploadServiceIntent;
public boolean isAuthCookieAcquired = false;
public ContributionsActivityPagerAdapter contributionsActivityPagerAdapter; public ContributionsActivityPagerAdapter contributionsActivityPagerAdapter;
public static final int CONTRIBUTIONS_TAB_POSITION = 0; public static final int CONTRIBUTIONS_TAB_POSITION = 0;
@ -82,10 +81,10 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag
setContentView(R.layout.activity_contributions); setContentView(R.layout.activity_contributions);
ButterKnife.bind(this); ButterKnife.bind(this);
requestAuthToken();
initDrawer(); initDrawer();
setTitle(getString(R.string.navigation_item_home)); // Should I create a new string variable with another name instead? setTitle(getString(R.string.navigation_item_home)); // Should I create a new string variable with another name instead?
initMain();
if (savedInstanceState != null ) { if (savedInstanceState != null ) {
onOrientationChanged = true; // Will be used in nearby fragment to determine significant update of map onOrientationChanged = true; // Will be used in nearby fragment to determine significant update of map
@ -103,16 +102,13 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag
outState.putInt("viewPagerCurrentItem", viewPager.getCurrentItem()); outState.putInt("viewPagerCurrentItem", viewPager.getCurrentItem());
} }
@Override private void initMain() {
protected void onAuthCookieAcquired(String authCookie) {
// Do a sync everytime we get here!
requestSync(sessionManager.getCurrentAccount(), BuildConfig.CONTRIBUTION_AUTHORITY, new Bundle()); requestSync(sessionManager.getCurrentAccount(), BuildConfig.CONTRIBUTION_AUTHORITY, new Bundle());
uploadServiceIntent = new Intent(this, UploadService.class); uploadServiceIntent = new Intent(this, UploadService.class);
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE); uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
startService(uploadServiceIntent); startService(uploadServiceIntent);
addTabsAndFragments(); addTabsAndFragments();
isAuthCookieAcquired = true;
if (contributionsActivityPagerAdapter.getItem(0) != null) { if (contributionsActivityPagerAdapter.getItem(0) != null) {
((ContributionsFragment)contributionsActivityPagerAdapter.getItem(0)).onAuthCookieAcquired(uploadServiceIntent); ((ContributionsFragment)contributionsActivityPagerAdapter.getItem(0)).onAuthCookieAcquired(uploadServiceIntent);
} }
@ -232,14 +228,9 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag
} }
} }
@Override
protected void onAuthFailure() {
}
@Override @Override
public void onBackPressed() { public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); DrawerLayout drawer = findViewById(R.id.drawer_layout);
String contributionsFragmentTag = ((ContributionsActivityPagerAdapter) viewPager.getAdapter()).makeFragmentName(R.id.pager, 0); String contributionsFragmentTag = ((ContributionsActivityPagerAdapter) viewPager.getAdapter()).makeFragmentName(R.id.pager, 0);
String nearbyFragmentTag = ((ContributionsActivityPagerAdapter) viewPager.getAdapter()).makeFragmentName(R.id.pager, 1); String nearbyFragmentTag = ((ContributionsActivityPagerAdapter) viewPager.getAdapter()).makeFragmentName(R.id.pager, 1);
if (drawer.isDrawerOpen(GravityCompat.START)) { if (drawer.isDrawerOpen(GravityCompat.START)) {

View file

@ -62,13 +62,6 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
return; return;
} }
String authCookie = sessionManager.getAuthCookie();
if (isNullOrWhiteSpace(authCookie)) {
Timber.d("Could not authenticate :(");
return;
}
allModifications.moveToFirst(); allModifications.moveToFirst();
Timber.d("Found %d modifications to execute", allModifications.getCount()); Timber.d("Found %d modifications to execute", allModifications.getCount());
@ -130,7 +123,4 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
} }
} }
private boolean isNullOrWhiteSpace(String value) {
return value == null || value.trim().isEmpty();
}
} }

View file

@ -26,16 +26,16 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import fr.free.nrw.commons.Media; import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.AuthenticatedActivity;
import fr.free.nrw.commons.delete.DeleteHelper; import fr.free.nrw.commons.delete.DeleteHelper;
import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.theme.NavigationBaseActivity;
import fr.free.nrw.commons.utils.DialogUtil; import fr.free.nrw.commons.utils.DialogUtil;
import fr.free.nrw.commons.utils.ViewUtil; import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
public class ReviewActivity extends AuthenticatedActivity { public class ReviewActivity extends NavigationBaseActivity {
@BindView(R.id.pager_indicator_review) @BindView(R.id.pager_indicator_review)
public CirclePageIndicator pagerIndicator; public CirclePageIndicator pagerIndicator;
@ -94,15 +94,6 @@ public class ReviewActivity extends AuthenticatedActivity {
return media; return media;
} }
@Override
protected void onAuthCookieAcquired(String authCookie) {
}
@Override
protected void onAuthFailure() {
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);