1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/fxa/login/Engaged.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,92 @@ 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.fxa.login; 1.9 + 1.10 +import java.security.NoSuchAlgorithmException; 1.11 + 1.12 +import org.mozilla.gecko.background.fxa.FxAccountClient10.TwoKeys; 1.13 +import org.mozilla.gecko.background.fxa.FxAccountUtils; 1.14 +import org.mozilla.gecko.browserid.BrowserIDKeyPair; 1.15 +import org.mozilla.gecko.fxa.FxAccountConstants; 1.16 +import org.mozilla.gecko.fxa.login.FxAccountLoginStateMachine.ExecuteDelegate; 1.17 +import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.AccountVerified; 1.18 +import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.LocalError; 1.19 +import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.LogMessage; 1.20 +import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.RemoteError; 1.21 +import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.Transition; 1.22 +import org.mozilla.gecko.sync.ExtendedJSONObject; 1.23 +import org.mozilla.gecko.sync.Utils; 1.24 + 1.25 +public class Engaged extends State { 1.26 + private static final String LOG_TAG = Engaged.class.getSimpleName(); 1.27 + 1.28 + protected final byte[] sessionToken; 1.29 + protected final byte[] keyFetchToken; 1.30 + protected final byte[] unwrapkB; 1.31 + 1.32 + public Engaged(String email, String uid, boolean verified, byte[] unwrapkB, byte[] sessionToken, byte[] keyFetchToken) { 1.33 + super(StateLabel.Engaged, email, uid, verified); 1.34 + Utils.throwIfNull(unwrapkB, sessionToken, keyFetchToken); 1.35 + this.unwrapkB = unwrapkB; 1.36 + this.sessionToken = sessionToken; 1.37 + this.keyFetchToken = keyFetchToken; 1.38 + } 1.39 + 1.40 + @Override 1.41 + public ExtendedJSONObject toJSONObject() { 1.42 + ExtendedJSONObject o = super.toJSONObject(); 1.43 + // Fields are non-null by constructor. 1.44 + o.put("unwrapkB", Utils.byte2Hex(unwrapkB)); 1.45 + o.put("sessionToken", Utils.byte2Hex(sessionToken)); 1.46 + o.put("keyFetchToken", Utils.byte2Hex(keyFetchToken)); 1.47 + return o; 1.48 + } 1.49 + 1.50 + @Override 1.51 + public void execute(final ExecuteDelegate delegate) { 1.52 + BrowserIDKeyPair theKeyPair; 1.53 + try { 1.54 + theKeyPair = delegate.generateKeyPair(); 1.55 + } catch (NoSuchAlgorithmException e) { 1.56 + delegate.handleTransition(new LocalError(e), new Doghouse(email, uid, verified)); 1.57 + return; 1.58 + } 1.59 + final BrowserIDKeyPair keyPair = theKeyPair; 1.60 + 1.61 + delegate.getClient().keys(keyFetchToken, new BaseRequestDelegate<TwoKeys>(this, delegate) { 1.62 + @Override 1.63 + public void handleSuccess(TwoKeys result) { 1.64 + byte[] kB; 1.65 + try { 1.66 + kB = FxAccountUtils.unwrapkB(unwrapkB, result.wrapkB); 1.67 + if (FxAccountConstants.LOG_PERSONAL_INFORMATION) { 1.68 + FxAccountConstants.pii(LOG_TAG, "Fetched kA: " + Utils.byte2Hex(result.kA)); 1.69 + FxAccountConstants.pii(LOG_TAG, "And wrapkB: " + Utils.byte2Hex(result.wrapkB)); 1.70 + FxAccountConstants.pii(LOG_TAG, "Giving kB : " + Utils.byte2Hex(kB)); 1.71 + } 1.72 + } catch (Exception e) { 1.73 + delegate.handleTransition(new RemoteError(e), new Separated(email, uid, verified)); 1.74 + return; 1.75 + } 1.76 + Transition transition = verified 1.77 + ? new LogMessage("keys succeeded") 1.78 + : new AccountVerified(); 1.79 + delegate.handleTransition(transition, new Cohabiting(email, uid, sessionToken, result.kA, kB, keyPair)); 1.80 + } 1.81 + }); 1.82 + } 1.83 + 1.84 + @Override 1.85 + public Action getNeededAction() { 1.86 + if (!verified) { 1.87 + return Action.NeedsVerification; 1.88 + } 1.89 + return Action.None; 1.90 + } 1.91 + 1.92 + public byte[] getSessionToken() { 1.93 + return sessionToken; 1.94 + } 1.95 +}