1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/setup/auth/AccountAuthenticator.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.sync.setup.auth; 1.9 + 1.10 +import java.util.LinkedList; 1.11 +import java.util.Queue; 1.12 + 1.13 +import org.mozilla.gecko.background.common.log.Logger; 1.14 +import org.mozilla.gecko.sync.ThreadPool; 1.15 +import org.mozilla.gecko.sync.Utils; 1.16 +import org.mozilla.gecko.sync.setup.activities.AccountActivity; 1.17 + 1.18 +public class AccountAuthenticator { 1.19 + private final String LOG_TAG = "AccountAuthenticator"; 1.20 + 1.21 + private AccountActivity activityCallback; 1.22 + private Queue<AuthenticatorStage> stages; 1.23 + 1.24 + // Values for authentication. 1.25 + public String password; 1.26 + public String username; 1.27 + 1.28 + public String authServer; 1.29 + public String nodeServer; 1.30 + 1.31 + public boolean isSuccess = false; 1.32 + public boolean isCanceled = false; 1.33 + 1.34 + public AccountAuthenticator(AccountActivity activity) { 1.35 + activityCallback = activity; 1.36 + prepareStages(); 1.37 + } 1.38 + 1.39 + private void prepareStages() { 1.40 + stages = new LinkedList<AuthenticatorStage>(); 1.41 + stages.add(new EnsureUserExistenceStage()); 1.42 + stages.add(new FetchUserNodeStage()); 1.43 + stages.add(new AuthenticateAccountStage()); 1.44 + } 1.45 + 1.46 + public void authenticate(String server, String account, String password) { 1.47 + // Set authentication values. 1.48 + if (!server.endsWith("/")) { 1.49 + server += "/"; 1.50 + } 1.51 + nodeServer = server; 1.52 + this.password = password; 1.53 + 1.54 + // Calculate and save username hash. 1.55 + try { 1.56 + username = Utils.usernameFromAccount(account); 1.57 + } catch (Exception e) { 1.58 + abort(AuthenticationResult.FAILURE_OTHER, e); 1.59 + return; 1.60 + } 1.61 + Logger.pii(LOG_TAG, "Username:" + username); 1.62 + Logger.debug(LOG_TAG, "Running first stage."); 1.63 + // Start first stage of authentication. 1.64 + runNextStage(); 1.65 + } 1.66 + 1.67 + /** 1.68 + * Run next stage of authentication. 1.69 + */ 1.70 + public void runNextStage() { 1.71 + if (isCanceled) { 1.72 + return; 1.73 + } 1.74 + if (stages.size() == 0) { 1.75 + Logger.debug(LOG_TAG, "Authentication completed."); 1.76 + activityCallback.authCallback(isSuccess ? AuthenticationResult.SUCCESS : AuthenticationResult.FAILURE_PASSWORD); 1.77 + return; 1.78 + } 1.79 + AuthenticatorStage nextStage = stages.remove(); 1.80 + try { 1.81 + nextStage.execute(this); 1.82 + } catch (Exception e) { 1.83 + Logger.warn(LOG_TAG, "Unhandled exception in stage " + nextStage); 1.84 + abort(AuthenticationResult.FAILURE_OTHER, e); 1.85 + } 1.86 + } 1.87 + 1.88 + /** 1.89 + * Abort authentication. 1.90 + * 1.91 + * @param result 1.92 + * returned to callback. 1.93 + * @param e 1.94 + * Exception causing abort. 1.95 + */ 1.96 + public void abort(AuthenticationResult result, Exception e) { 1.97 + if (isCanceled) { 1.98 + return; 1.99 + } 1.100 + Logger.warn(LOG_TAG, "Authentication failed.", e); 1.101 + activityCallback.authCallback(result); 1.102 + } 1.103 + 1.104 + /* Helper functions */ 1.105 + public static void runOnThread(Runnable run) { 1.106 + ThreadPool.run(run); 1.107 + } 1.108 +}