|
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.fxa.login; |
|
6 |
|
7 import java.security.NoSuchAlgorithmException; |
|
8 |
|
9 import org.mozilla.gecko.background.fxa.FxAccountClient10.TwoKeys; |
|
10 import org.mozilla.gecko.background.fxa.FxAccountUtils; |
|
11 import org.mozilla.gecko.browserid.BrowserIDKeyPair; |
|
12 import org.mozilla.gecko.fxa.FxAccountConstants; |
|
13 import org.mozilla.gecko.fxa.login.FxAccountLoginStateMachine.ExecuteDelegate; |
|
14 import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.AccountVerified; |
|
15 import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.LocalError; |
|
16 import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.LogMessage; |
|
17 import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.RemoteError; |
|
18 import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.Transition; |
|
19 import org.mozilla.gecko.sync.ExtendedJSONObject; |
|
20 import org.mozilla.gecko.sync.Utils; |
|
21 |
|
22 public class Engaged extends State { |
|
23 private static final String LOG_TAG = Engaged.class.getSimpleName(); |
|
24 |
|
25 protected final byte[] sessionToken; |
|
26 protected final byte[] keyFetchToken; |
|
27 protected final byte[] unwrapkB; |
|
28 |
|
29 public Engaged(String email, String uid, boolean verified, byte[] unwrapkB, byte[] sessionToken, byte[] keyFetchToken) { |
|
30 super(StateLabel.Engaged, email, uid, verified); |
|
31 Utils.throwIfNull(unwrapkB, sessionToken, keyFetchToken); |
|
32 this.unwrapkB = unwrapkB; |
|
33 this.sessionToken = sessionToken; |
|
34 this.keyFetchToken = keyFetchToken; |
|
35 } |
|
36 |
|
37 @Override |
|
38 public ExtendedJSONObject toJSONObject() { |
|
39 ExtendedJSONObject o = super.toJSONObject(); |
|
40 // Fields are non-null by constructor. |
|
41 o.put("unwrapkB", Utils.byte2Hex(unwrapkB)); |
|
42 o.put("sessionToken", Utils.byte2Hex(sessionToken)); |
|
43 o.put("keyFetchToken", Utils.byte2Hex(keyFetchToken)); |
|
44 return o; |
|
45 } |
|
46 |
|
47 @Override |
|
48 public void execute(final ExecuteDelegate delegate) { |
|
49 BrowserIDKeyPair theKeyPair; |
|
50 try { |
|
51 theKeyPair = delegate.generateKeyPair(); |
|
52 } catch (NoSuchAlgorithmException e) { |
|
53 delegate.handleTransition(new LocalError(e), new Doghouse(email, uid, verified)); |
|
54 return; |
|
55 } |
|
56 final BrowserIDKeyPair keyPair = theKeyPair; |
|
57 |
|
58 delegate.getClient().keys(keyFetchToken, new BaseRequestDelegate<TwoKeys>(this, delegate) { |
|
59 @Override |
|
60 public void handleSuccess(TwoKeys result) { |
|
61 byte[] kB; |
|
62 try { |
|
63 kB = FxAccountUtils.unwrapkB(unwrapkB, result.wrapkB); |
|
64 if (FxAccountConstants.LOG_PERSONAL_INFORMATION) { |
|
65 FxAccountConstants.pii(LOG_TAG, "Fetched kA: " + Utils.byte2Hex(result.kA)); |
|
66 FxAccountConstants.pii(LOG_TAG, "And wrapkB: " + Utils.byte2Hex(result.wrapkB)); |
|
67 FxAccountConstants.pii(LOG_TAG, "Giving kB : " + Utils.byte2Hex(kB)); |
|
68 } |
|
69 } catch (Exception e) { |
|
70 delegate.handleTransition(new RemoteError(e), new Separated(email, uid, verified)); |
|
71 return; |
|
72 } |
|
73 Transition transition = verified |
|
74 ? new LogMessage("keys succeeded") |
|
75 : new AccountVerified(); |
|
76 delegate.handleTransition(transition, new Cohabiting(email, uid, sessionToken, result.kA, kB, keyPair)); |
|
77 } |
|
78 }); |
|
79 } |
|
80 |
|
81 @Override |
|
82 public Action getNeededAction() { |
|
83 if (!verified) { |
|
84 return Action.NeedsVerification; |
|
85 } |
|
86 return Action.None; |
|
87 } |
|
88 |
|
89 public byte[] getSessionToken() { |
|
90 return sessionToken; |
|
91 } |
|
92 } |