michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.fxa.login; michael@0: michael@0: import java.security.NoSuchAlgorithmException; michael@0: michael@0: import org.mozilla.gecko.background.fxa.FxAccountClient10.TwoKeys; michael@0: import org.mozilla.gecko.background.fxa.FxAccountUtils; michael@0: import org.mozilla.gecko.browserid.BrowserIDKeyPair; michael@0: import org.mozilla.gecko.fxa.FxAccountConstants; michael@0: import org.mozilla.gecko.fxa.login.FxAccountLoginStateMachine.ExecuteDelegate; michael@0: import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.AccountVerified; michael@0: import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.LocalError; michael@0: import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.LogMessage; michael@0: import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.RemoteError; michael@0: import org.mozilla.gecko.fxa.login.FxAccountLoginTransition.Transition; michael@0: import org.mozilla.gecko.sync.ExtendedJSONObject; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: michael@0: public class Engaged extends State { michael@0: private static final String LOG_TAG = Engaged.class.getSimpleName(); michael@0: michael@0: protected final byte[] sessionToken; michael@0: protected final byte[] keyFetchToken; michael@0: protected final byte[] unwrapkB; michael@0: michael@0: public Engaged(String email, String uid, boolean verified, byte[] unwrapkB, byte[] sessionToken, byte[] keyFetchToken) { michael@0: super(StateLabel.Engaged, email, uid, verified); michael@0: Utils.throwIfNull(unwrapkB, sessionToken, keyFetchToken); michael@0: this.unwrapkB = unwrapkB; michael@0: this.sessionToken = sessionToken; michael@0: this.keyFetchToken = keyFetchToken; michael@0: } michael@0: michael@0: @Override michael@0: public ExtendedJSONObject toJSONObject() { michael@0: ExtendedJSONObject o = super.toJSONObject(); michael@0: // Fields are non-null by constructor. michael@0: o.put("unwrapkB", Utils.byte2Hex(unwrapkB)); michael@0: o.put("sessionToken", Utils.byte2Hex(sessionToken)); michael@0: o.put("keyFetchToken", Utils.byte2Hex(keyFetchToken)); michael@0: return o; michael@0: } michael@0: michael@0: @Override michael@0: public void execute(final ExecuteDelegate delegate) { michael@0: BrowserIDKeyPair theKeyPair; michael@0: try { michael@0: theKeyPair = delegate.generateKeyPair(); michael@0: } catch (NoSuchAlgorithmException e) { michael@0: delegate.handleTransition(new LocalError(e), new Doghouse(email, uid, verified)); michael@0: return; michael@0: } michael@0: final BrowserIDKeyPair keyPair = theKeyPair; michael@0: michael@0: delegate.getClient().keys(keyFetchToken, new BaseRequestDelegate(this, delegate) { michael@0: @Override michael@0: public void handleSuccess(TwoKeys result) { michael@0: byte[] kB; michael@0: try { michael@0: kB = FxAccountUtils.unwrapkB(unwrapkB, result.wrapkB); michael@0: if (FxAccountConstants.LOG_PERSONAL_INFORMATION) { michael@0: FxAccountConstants.pii(LOG_TAG, "Fetched kA: " + Utils.byte2Hex(result.kA)); michael@0: FxAccountConstants.pii(LOG_TAG, "And wrapkB: " + Utils.byte2Hex(result.wrapkB)); michael@0: FxAccountConstants.pii(LOG_TAG, "Giving kB : " + Utils.byte2Hex(kB)); michael@0: } michael@0: } catch (Exception e) { michael@0: delegate.handleTransition(new RemoteError(e), new Separated(email, uid, verified)); michael@0: return; michael@0: } michael@0: Transition transition = verified michael@0: ? new LogMessage("keys succeeded") michael@0: : new AccountVerified(); michael@0: delegate.handleTransition(transition, new Cohabiting(email, uid, sessionToken, result.kA, kB, keyPair)); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: @Override michael@0: public Action getNeededAction() { michael@0: if (!verified) { michael@0: return Action.NeedsVerification; michael@0: } michael@0: return Action.None; michael@0: } michael@0: michael@0: public byte[] getSessionToken() { michael@0: return sessionToken; michael@0: } michael@0: }