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