|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.setup.auth; |
|
6 |
|
7 import java.util.LinkedList; |
|
8 import java.util.Queue; |
|
9 |
|
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; |
|
14 |
|
15 public class AccountAuthenticator { |
|
16 private final String LOG_TAG = "AccountAuthenticator"; |
|
17 |
|
18 private AccountActivity activityCallback; |
|
19 private Queue<AuthenticatorStage> stages; |
|
20 |
|
21 // Values for authentication. |
|
22 public String password; |
|
23 public String username; |
|
24 |
|
25 public String authServer; |
|
26 public String nodeServer; |
|
27 |
|
28 public boolean isSuccess = false; |
|
29 public boolean isCanceled = false; |
|
30 |
|
31 public AccountAuthenticator(AccountActivity activity) { |
|
32 activityCallback = activity; |
|
33 prepareStages(); |
|
34 } |
|
35 |
|
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 } |
|
42 |
|
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; |
|
50 |
|
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 } |
|
63 |
|
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 } |
|
84 |
|
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 } |
|
100 |
|
101 /* Helper functions */ |
|
102 public static void runOnThread(Runnable run) { |
|
103 ThreadPool.run(run); |
|
104 } |
|
105 } |