11package fr .free .nrw .commons .auth ;
22
3+ import static android .view .KeyEvent .KEYCODE_ENTER ;
4+ import static android .view .View .VISIBLE ;
5+ import static android .view .inputmethod .EditorInfo .IME_ACTION_DONE ;
6+ import static fr .free .nrw .commons .di .NetworkingModule .NAMED_COMMONS_WIKI_SITE ;
7+
38import android .accounts .AccountAuthenticatorActivity ;
49import android .app .ProgressDialog ;
510import android .content .Context ;
1621import android .widget .Button ;
1722import android .widget .EditText ;
1823import android .widget .TextView ;
19-
2024import androidx .annotation .ColorRes ;
2125import androidx .annotation .NonNull ;
26+ import androidx .annotation .Nullable ;
2227import androidx .annotation .StringRes ;
2328import androidx .appcompat .app .AlertDialog ;
2429import androidx .appcompat .app .AppCompatDelegate ;
2530import androidx .core .app .NavUtils ;
2631import androidx .core .content .ContextCompat ;
27-
28- import com .google .android .material .textfield .TextInputLayout ;
29-
30- import org .wikipedia .AppAdapter ;
31- import org .wikipedia .dataclient .WikiSite ;
32- import org .wikipedia .login .LoginClient ;
33- import org .wikipedia .login .LoginResult ;
34-
35- import javax .inject .Inject ;
36- import javax .inject .Named ;
37-
3832import butterknife .BindView ;
3933import butterknife .ButterKnife ;
4034import butterknife .OnClick ;
4135import butterknife .OnEditorAction ;
4236import butterknife .OnFocusChange ;
37+ import com .google .android .material .textfield .TextInputLayout ;
4338import fr .free .nrw .commons .BuildConfig ;
4439import fr .free .nrw .commons .R ;
4540import fr .free .nrw .commons .Utils ;
5247import fr .free .nrw .commons .theme .NavigationBaseActivity ;
5348import fr .free .nrw .commons .utils .ConfigUtils ;
5449import fr .free .nrw .commons .utils .ViewUtil ;
55- import io .reactivex .Completable ;
56- import io .reactivex .android .schedulers .AndroidSchedulers ;
5750import io .reactivex .disposables .CompositeDisposable ;
58- import io .reactivex .functions .Action ;
59- import io .reactivex .schedulers .Schedulers ;
51+ import javax .inject .Inject ;
52+ import javax .inject .Named ;
53+ import org .wikipedia .AppAdapter ;
54+ import org .wikipedia .dataclient .ServiceFactory ;
55+ import org .wikipedia .dataclient .WikiSite ;
56+ import org .wikipedia .dataclient .mwapi .MwQueryResponse ;
57+ import org .wikipedia .login .LoginClient ;
58+ import org .wikipedia .login .LoginClient .LoginCallback ;
59+ import org .wikipedia .login .LoginResult ;
60+ import retrofit2 .Call ;
61+ import retrofit2 .Callback ;
62+ import retrofit2 .Response ;
6063import timber .log .Timber ;
6164
62- import static android .view .KeyEvent .KEYCODE_ENTER ;
63- import static android .view .View .VISIBLE ;
64- import static android .view .inputmethod .EditorInfo .IME_ACTION_DONE ;
65- import static fr .free .nrw .commons .di .NetworkingModule .NAMED_COMMONS_WIKI_SITE ;
66-
6765public class LoginActivity extends AccountAuthenticatorActivity {
6866
6967 @ Inject
@@ -111,6 +109,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
111109 private AppCompatDelegate delegate ;
112110 private LoginTextWatcher textWatcher = new LoginTextWatcher ();
113111 private CompositeDisposable compositeDisposable = new CompositeDisposable ();
112+ private Call <MwQueryResponse > loginToken ;
114113
115114 @ Override
116115 public void onCreate (Bundle savedInstanceState ) {
@@ -238,6 +237,9 @@ protected void onDestroy() {
238237 passwordEdit .removeTextChangedListener (textWatcher );
239238 twoFactorEdit .removeTextChangedListener (textWatcher );
240239 delegate .onDestroy ();
240+ if (null !=loginClient ) {
241+ loginClient .cancel ();
242+ }
241243 super .onDestroy ();
242244 }
243245
@@ -255,34 +257,62 @@ public void performLogin() {
255257
256258 private void doLogin (String username , String password , String twoFactorCode ) {
257259 progressDialog .show ();
260+ loginToken = ServiceFactory .get (commonsWikiSite ).getLoginToken ();
261+ loginToken .enqueue (
262+ new Callback <MwQueryResponse >() {
263+ @ Override
264+ public void onResponse (Call <MwQueryResponse > call ,
265+ Response <MwQueryResponse > response ) {
266+ loginClient .login (commonsWikiSite , username , password , null , twoFactorCode ,
267+ response .body ().query ().loginToken (), new LoginCallback () {
268+ @ Override
269+ public void success (@ NonNull LoginResult result ) {
270+ Timber .d ("Login Success" );
271+ onLoginSuccess (result );
272+ }
273+
274+ @ Override
275+ public void twoFactorPrompt (@ NonNull Throwable caught ,
276+ @ Nullable String token ) {
277+ Timber .d ("Requesting 2FA prompt" );
278+ hideProgress ();
279+ askUserForTwoFactorAuth ();
280+ }
281+
282+ @ Override
283+ public void passwordResetPrompt (@ Nullable String token ) {
284+ Timber .d ("Showing password reset prompt" );
285+ hideProgress ();
286+ showPasswordResetPrompt ();
287+ }
288+
289+ @ Override
290+ public void error (@ NonNull Throwable caught ) {
291+ Timber .e (caught );
292+ hideProgress ();
293+ showMessageAndCancelDialog (caught .getLocalizedMessage ());
294+ }
295+ });
296+ }
297+
298+ @ Override
299+ public void onFailure (Call <MwQueryResponse > call , Throwable t ) {
300+ Timber .e (t );
301+ showMessageAndCancelDialog (t .getLocalizedMessage ());
302+ }
303+ });
304+
305+ }
306+
307+ private void hideProgress () {
308+ progressDialog .dismiss ();
309+ }
258310
259- Action action = () -> {
260- try {
261- loginClient .loginBlocking (commonsWikiSite , username , password , twoFactorCode );
262- } catch (Throwable throwable ) {
263- throwable .printStackTrace ();
264- }
265- };
266-
267- compositeDisposable .add (Completable .fromAction (action )
268- .subscribeOn (Schedulers .io ())
269- .observeOn (AndroidSchedulers .mainThread ())
270- .subscribe (() -> onLoginSuccess (username , password ),
271- error -> {
272- if (error instanceof LoginClient .LoginFailedException ) {
273- LoginClient .LoginFailedException exception = (LoginClient .LoginFailedException ) error ;
274- if (exception .getMessage ().equals ("2FA" )) {
275- askUserForTwoFactorAuth ();
276- }
277- }
278- if (!progressDialog .isShowing ()) {
279- return ;
280- }
281- progressDialog .dismiss ();
282- showMessageAndCancelDialog (R .string .error_occurred );
283- }));
311+ private void showPasswordResetPrompt () {
312+ showMessageAndCancelDialog (getString (R .string .you_must_reset_your_passsword ));
284313 }
285314
315+
286316 /**
287317 * This function is called when user skips the login.
288318 * It redirects the user to Explore Activity.
@@ -302,13 +332,12 @@ private void showLoggingProgressBar() {
302332 progressDialog .show ();
303333 }
304334
305- private void onLoginSuccess (String username , String password ) {
335+ private void onLoginSuccess (LoginResult loginResult ) {
306336 if (!progressDialog .isShowing ()) {
307337 // no longer attached to activity!
308338 return ;
309339 }
310340 sessionManager .setUserLoggedIn (true );
311- LoginResult loginResult = new LoginResult (commonsWikiSite , "PASS" , username , password , "" );
312341 AppAdapter .get ().updateAccount (loginResult );
313342 progressDialog .dismiss ();
314343 showSuccessAndDismissDialog ();
0 commit comments