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.background.fxa; michael@0: michael@0: import java.io.UnsupportedEncodingException; michael@0: import java.security.GeneralSecurityException; michael@0: michael@0: import org.json.simple.JSONObject; michael@0: import org.mozilla.gecko.background.fxa.FxAccountClient10.CreateDelegate; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: michael@0: public class FxAccount20CreateDelegate implements CreateDelegate { michael@0: protected final byte[] emailUTF8; michael@0: protected final byte[] authPW; michael@0: protected final boolean preVerified; michael@0: michael@0: /** michael@0: * Make a new "create account" delegate. michael@0: * michael@0: * @param emailUTF8 michael@0: * email as UTF-8 bytes. michael@0: * @param quickStretchedPW michael@0: * quick stretched password as bytes. michael@0: * @param preVerified michael@0: * true if account should be marked already verified; only effective michael@0: * for non-production auth servers. michael@0: * @throws UnsupportedEncodingException michael@0: * @throws GeneralSecurityException michael@0: */ michael@0: public FxAccount20CreateDelegate(byte[] emailUTF8, byte[] quickStretchedPW, boolean preVerified) throws UnsupportedEncodingException, GeneralSecurityException { michael@0: this.emailUTF8 = emailUTF8; michael@0: this.authPW = FxAccountUtils.generateAuthPW(quickStretchedPW); michael@0: this.preVerified = preVerified; michael@0: } michael@0: michael@0: @SuppressWarnings("unchecked") michael@0: @Override michael@0: public JSONObject getCreateBody() throws FxAccountClientException { michael@0: final JSONObject body = new JSONObject(); michael@0: try { michael@0: body.put("email", new String(emailUTF8, "UTF-8")); michael@0: body.put("authPW", Utils.byte2Hex(authPW)); michael@0: if (preVerified) { michael@0: // Production endpoints do not allow preVerified; this assumes we only michael@0: // set it when it's okay to send it. michael@0: body.put("preVerified", preVerified); michael@0: } michael@0: return body; michael@0: } catch (UnsupportedEncodingException e) { michael@0: throw new FxAccountClientException(e); michael@0: } michael@0: } michael@0: }