mobile/android/base/sync/setup/auth/AccountAuthenticator.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.sync.setup.auth;
     7 import java.util.LinkedList;
     8 import java.util.Queue;
    10 import org.mozilla.gecko.background.common.log.Logger;
    11 import org.mozilla.gecko.sync.ThreadPool;
    12 import org.mozilla.gecko.sync.Utils;
    13 import org.mozilla.gecko.sync.setup.activities.AccountActivity;
    15 public class AccountAuthenticator {
    16   private final String LOG_TAG = "AccountAuthenticator";
    18   private AccountActivity activityCallback;
    19   private Queue<AuthenticatorStage> stages;
    21   // Values for authentication.
    22   public String password;
    23   public String username;
    25   public String authServer;
    26   public String nodeServer;
    28   public boolean isSuccess = false;
    29   public boolean isCanceled = false;
    31   public AccountAuthenticator(AccountActivity activity) {
    32     activityCallback = activity;
    33     prepareStages();
    34   }
    36   private void prepareStages() {
    37     stages = new LinkedList<AuthenticatorStage>();
    38     stages.add(new EnsureUserExistenceStage());
    39     stages.add(new FetchUserNodeStage());
    40     stages.add(new AuthenticateAccountStage());
    41   }
    43   public void authenticate(String server, String account, String password) {
    44     // Set authentication values.
    45     if (!server.endsWith("/")) {
    46       server += "/";
    47     }
    48     nodeServer = server;
    49     this.password = password;
    51     // Calculate and save username hash.
    52     try {
    53       username = Utils.usernameFromAccount(account);
    54     } catch (Exception e) {
    55       abort(AuthenticationResult.FAILURE_OTHER, e);
    56       return;
    57     }
    58     Logger.pii(LOG_TAG, "Username:" + username);
    59     Logger.debug(LOG_TAG, "Running first stage.");
    60     // Start first stage of authentication.
    61     runNextStage();
    62   }
    64   /**
    65    * Run next stage of authentication.
    66    */
    67   public void runNextStage() {
    68     if (isCanceled) {
    69       return;
    70     }
    71     if (stages.size() == 0) {
    72       Logger.debug(LOG_TAG, "Authentication completed.");
    73       activityCallback.authCallback(isSuccess ? AuthenticationResult.SUCCESS : AuthenticationResult.FAILURE_PASSWORD);
    74       return;
    75     }
    76     AuthenticatorStage nextStage = stages.remove();
    77     try {
    78       nextStage.execute(this);
    79     } catch (Exception e) {
    80       Logger.warn(LOG_TAG, "Unhandled exception in stage " + nextStage);
    81       abort(AuthenticationResult.FAILURE_OTHER, e);
    82     }
    83   }
    85   /**
    86    * Abort authentication.
    87    *
    88    * @param result
    89    *    returned to callback.
    90    * @param e
    91    *    Exception causing abort.
    92    */
    93   public void abort(AuthenticationResult result, Exception e) {
    94     if (isCanceled) {
    95       return;
    96     }
    97     Logger.warn(LOG_TAG, "Authentication failed.", e);
    98     activityCallback.authCallback(result);
    99   }
   101   /* Helper functions */
   102   public static void runOnThread(Runnable run) {
   103     ThreadPool.run(run);
   104   }
   105 }

mercurial