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.net.URI;
michael@0: import java.util.concurrent.Executor;
michael@0:
michael@0: import org.json.simple.JSONObject;
michael@0: import org.mozilla.gecko.background.common.log.Logger;
michael@0: import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
michael@0: import org.mozilla.gecko.fxa.FxAccountConstants;
michael@0: import org.mozilla.gecko.sync.ExtendedJSONObject;
michael@0: import org.mozilla.gecko.sync.Utils;
michael@0: import org.mozilla.gecko.sync.net.BaseResource;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.HttpResponse;
michael@0:
michael@0: public class FxAccountClient20 extends FxAccountClient10 implements FxAccountClient {
michael@0: protected static final String[] LOGIN_RESPONSE_REQUIRED_STRING_FIELDS = new String[] { JSON_KEY_UID, JSON_KEY_SESSIONTOKEN };
michael@0: protected static final String[] LOGIN_RESPONSE_REQUIRED_STRING_FIELDS_KEYS = new String[] { JSON_KEY_UID, JSON_KEY_SESSIONTOKEN, JSON_KEY_KEYFETCHTOKEN, };
michael@0: protected static final String[] LOGIN_RESPONSE_REQUIRED_BOOLEAN_FIELDS = new String[] { JSON_KEY_VERIFIED };
michael@0:
michael@0: public FxAccountClient20(String serverURI, Executor executor) {
michael@0: super(serverURI, executor);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Thin container for login response.
michael@0: *
michael@0: * The remoteEmail
field is the email address as normalized by the
michael@0: * server, and is not necessarily the email address delivered to the
michael@0: * login
or create
call.
michael@0: */
michael@0: public static class LoginResponse {
michael@0: public final String remoteEmail;
michael@0: public final String uid;
michael@0: public final byte[] sessionToken;
michael@0: public final boolean verified;
michael@0: public final byte[] keyFetchToken;
michael@0:
michael@0: public LoginResponse(String remoteEmail, String uid, boolean verified, byte[] sessionToken, byte[] keyFetchToken) {
michael@0: this.remoteEmail = remoteEmail;
michael@0: this.uid = uid;
michael@0: this.verified = verified;
michael@0: this.sessionToken = sessionToken;
michael@0: this.keyFetchToken = keyFetchToken;
michael@0: }
michael@0: }
michael@0:
michael@0: // Public for testing only; prefer login and loginAndGetKeys (without boolean parameter).
michael@0: public void login(final byte[] emailUTF8, final byte[] quickStretchedPW, final boolean getKeys,
michael@0: final RequestDelegate delegate) {
michael@0: BaseResource resource;
michael@0: JSONObject body;
michael@0: final String path = getKeys ? "account/login?keys=true" : "account/login";
michael@0: try {
michael@0: resource = new BaseResource(new URI(serverURI + path));
michael@0: body = new FxAccount20LoginDelegate(emailUTF8, quickStretchedPW).getCreateBody();
michael@0: } catch (Exception e) {
michael@0: invokeHandleError(delegate, e);
michael@0: return;
michael@0: }
michael@0:
michael@0: resource.delegate = new ResourceDelegate(resource, delegate) {
michael@0: @Override
michael@0: public void handleSuccess(int status, HttpResponse response, ExtendedJSONObject body) {
michael@0: try {
michael@0: final String[] requiredStringFields = getKeys ? LOGIN_RESPONSE_REQUIRED_STRING_FIELDS_KEYS : LOGIN_RESPONSE_REQUIRED_STRING_FIELDS;
michael@0: body.throwIfFieldsMissingOrMisTyped(requiredStringFields, String.class);
michael@0:
michael@0: final String[] requiredBooleanFields = LOGIN_RESPONSE_REQUIRED_BOOLEAN_FIELDS;
michael@0: body.throwIfFieldsMissingOrMisTyped(requiredBooleanFields, Boolean.class);
michael@0:
michael@0: String uid = body.getString(JSON_KEY_UID);
michael@0: boolean verified = body.getBoolean(JSON_KEY_VERIFIED);
michael@0: byte[] sessionToken = Utils.hex2Byte(body.getString(JSON_KEY_SESSIONTOKEN));
michael@0: byte[] keyFetchToken = null;
michael@0: if (getKeys) {
michael@0: keyFetchToken = Utils.hex2Byte(body.getString(JSON_KEY_KEYFETCHTOKEN));
michael@0: }
michael@0: LoginResponse loginResponse = new LoginResponse(new String(emailUTF8, "UTF-8"), uid, verified, sessionToken, keyFetchToken);
michael@0:
michael@0: delegate.handleSuccess(loginResponse);
michael@0: return;
michael@0: } catch (Exception e) {
michael@0: delegate.handleError(e);
michael@0: return;
michael@0: }
michael@0: }
michael@0: };
michael@0:
michael@0: post(resource, body, delegate);
michael@0: }
michael@0:
michael@0: public void createAccount(final byte[] emailUTF8, final byte[] quickStretchedPW, final boolean getKeys, final boolean preVerified,
michael@0: final RequestDelegate delegate) {
michael@0: BaseResource resource;
michael@0: JSONObject body;
michael@0: final String path = getKeys ? "account/create?keys=true" : "account/create";
michael@0: try {
michael@0: resource = new BaseResource(new URI(serverURI + path));
michael@0: body = new FxAccount20CreateDelegate(emailUTF8, quickStretchedPW, preVerified).getCreateBody();
michael@0: } catch (Exception e) {
michael@0: invokeHandleError(delegate, e);
michael@0: return;
michael@0: }
michael@0:
michael@0: // This is very similar to login, except verified is not required.
michael@0: resource.delegate = new ResourceDelegate(resource, delegate) {
michael@0: @Override
michael@0: public void handleSuccess(int status, HttpResponse response, ExtendedJSONObject body) {
michael@0: try {
michael@0: final String[] requiredStringFields = getKeys ? LOGIN_RESPONSE_REQUIRED_STRING_FIELDS_KEYS : LOGIN_RESPONSE_REQUIRED_STRING_FIELDS;
michael@0: body.throwIfFieldsMissingOrMisTyped(requiredStringFields, String.class);
michael@0:
michael@0: String uid = body.getString(JSON_KEY_UID);
michael@0: boolean verified = false; // In production, we're definitely not verified immediately upon creation.
michael@0: Boolean tempVerified = body.getBoolean(JSON_KEY_VERIFIED);
michael@0: if (tempVerified != null) {
michael@0: verified = tempVerified.booleanValue();
michael@0: }
michael@0: byte[] sessionToken = Utils.hex2Byte(body.getString(JSON_KEY_SESSIONTOKEN));
michael@0: byte[] keyFetchToken = null;
michael@0: if (getKeys) {
michael@0: keyFetchToken = Utils.hex2Byte(body.getString(JSON_KEY_KEYFETCHTOKEN));
michael@0: }
michael@0: LoginResponse loginResponse = new LoginResponse(new String(emailUTF8, "UTF-8"), uid, verified, sessionToken, keyFetchToken);
michael@0:
michael@0: delegate.handleSuccess(loginResponse);
michael@0: return;
michael@0: } catch (Exception e) {
michael@0: delegate.handleError(e);
michael@0: return;
michael@0: }
michael@0: }
michael@0: };
michael@0:
michael@0: post(resource, body, delegate);
michael@0: }
michael@0:
michael@0: @Override
michael@0: public void createAccountAndGetKeys(byte[] emailUTF8, PasswordStretcher passwordStretcher, RequestDelegate delegate) {
michael@0: try {
michael@0: byte[] quickStretchedPW = passwordStretcher.getQuickStretchedPW(emailUTF8);
michael@0: createAccount(emailUTF8, quickStretchedPW, true, false, delegate);
michael@0: } catch (Exception e) {
michael@0: invokeHandleError(delegate, e);
michael@0: return;
michael@0: }
michael@0: }
michael@0:
michael@0: @Override
michael@0: public void loginAndGetKeys(byte[] emailUTF8, PasswordStretcher passwordStretcher, RequestDelegate delegate) {
michael@0: login(emailUTF8, passwordStretcher, true, delegate);
michael@0: }
michael@0:
michael@0: /**
michael@0: * We want users to be able to enter their email address case-insensitively.
michael@0: * We stretch the password locally using the email address as a salt, to make
michael@0: * dictionary attacks more expensive. This means that a client with a
michael@0: * case-differing email address is unable to produce the correct
michael@0: * authorization, even though it knows the password. In this case, the server
michael@0: * returns the email that the account was created with, so that the client can
michael@0: * re-stretch the password locally with the correct email salt. This version
michael@0: * of login
retries at most one time with a server provided email
michael@0: * address.
michael@0: *
michael@0: * Be aware that consumers will not see the initial error response from the
michael@0: * server providing an alternate email (if there is one).
michael@0: *
michael@0: * @param emailUTF8
michael@0: * user entered email address.
michael@0: * @param stretcher
michael@0: * delegate to stretch and re-stretch password.
michael@0: * @param getKeys
michael@0: * true if a keyFetchToken
should be returned (in
michael@0: * addition to the standard sessionToken
).
michael@0: * @param delegate
michael@0: * to invoke callbacks.
michael@0: */
michael@0: public void login(final byte[] emailUTF8, final PasswordStretcher stretcher, final boolean getKeys,
michael@0: final RequestDelegate delegate) {
michael@0: byte[] quickStretchedPW;
michael@0: try {
michael@0: FxAccountConstants.pii(LOG_TAG, "Trying user provided email: '" + new String(emailUTF8, "UTF-8") + "'" );
michael@0: quickStretchedPW = stretcher.getQuickStretchedPW(emailUTF8);
michael@0: } catch (Exception e) {
michael@0: delegate.handleError(e);
michael@0: return;
michael@0: }
michael@0:
michael@0: this.login(emailUTF8, quickStretchedPW, getKeys, new RequestDelegate() {
michael@0: @Override
michael@0: public void handleSuccess(LoginResponse result) {
michael@0: delegate.handleSuccess(result);
michael@0: }
michael@0:
michael@0: @Override
michael@0: public void handleError(Exception e) {
michael@0: delegate.handleError(e);
michael@0: }
michael@0:
michael@0: @Override
michael@0: public void handleFailure(FxAccountClientRemoteException e) {
michael@0: String alternateEmail = e.body.getString(JSON_KEY_EMAIL);
michael@0: if (!e.isBadEmailCase() || alternateEmail == null) {
michael@0: delegate.handleFailure(e);
michael@0: return;
michael@0: };
michael@0:
michael@0: Logger.info(LOG_TAG, "Server returned alternate email; retrying login with provided email.");
michael@0: FxAccountConstants.pii(LOG_TAG, "Trying server provided email: '" + alternateEmail + "'" );
michael@0:
michael@0: try {
michael@0: // Nota bene: this is not recursive, since we call the fixed password
michael@0: // signature here, which invokes a non-retrying version.
michael@0: byte[] alternateEmailUTF8 = alternateEmail.getBytes("UTF-8");
michael@0: byte[] alternateQuickStretchedPW = stretcher.getQuickStretchedPW(alternateEmailUTF8);
michael@0: login(alternateEmailUTF8, alternateQuickStretchedPW, getKeys, delegate);
michael@0: } catch (Exception innerException) {
michael@0: delegate.handleError(innerException);
michael@0: return;
michael@0: }
michael@0: }
michael@0: });
michael@0: }
michael@0: }