summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/background/fxa/FxAccount20CreateDelegate.java
blob: 98809137fb2d270660e8b50bff5093add6d520fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.background.fxa;

import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.Utils;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

public class FxAccount20CreateDelegate {
  protected final byte[] emailUTF8;
  protected final byte[] authPW;
  protected final boolean preVerified;

  /**
   * Make a new "create account" delegate.
   *
   * @param emailUTF8
   *          email as UTF-8 bytes.
   * @param quickStretchedPW
   *          quick stretched password as bytes.
   * @param preVerified
   *          true if account should be marked already verified; only effective
   *          for non-production auth servers.
   * @throws UnsupportedEncodingException
   * @throws GeneralSecurityException
   */
  public FxAccount20CreateDelegate(byte[] emailUTF8, byte[] quickStretchedPW, boolean preVerified) throws UnsupportedEncodingException, GeneralSecurityException {
    this.emailUTF8 = emailUTF8;
    this.authPW = FxAccountUtils.generateAuthPW(quickStretchedPW);
    this.preVerified = preVerified;
  }

  public ExtendedJSONObject getCreateBody() throws FxAccountClientException {
    final ExtendedJSONObject body = new ExtendedJSONObject();
    try {
      body.put("email", new String(emailUTF8, "UTF-8"));
      body.put("authPW", Utils.byte2Hex(authPW));
      if (preVerified) {
        // Production endpoints do not allow preVerified; this assumes we only
        // set it when it's okay to send it.
        body.put("preVerified", preVerified);
      }
      return body;
    } catch (UnsupportedEncodingException e) {
      throw new FxAccountClientException(e);
    }
  }
}