mobile/android/base/background/fxa/FxAccountClient20.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.background.fxa;
michael@0 6
michael@0 7 import java.net.URI;
michael@0 8 import java.util.concurrent.Executor;
michael@0 9
michael@0 10 import org.json.simple.JSONObject;
michael@0 11 import org.mozilla.gecko.background.common.log.Logger;
michael@0 12 import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
michael@0 13 import org.mozilla.gecko.fxa.FxAccountConstants;
michael@0 14 import org.mozilla.gecko.sync.ExtendedJSONObject;
michael@0 15 import org.mozilla.gecko.sync.Utils;
michael@0 16 import org.mozilla.gecko.sync.net.BaseResource;
michael@0 17
michael@0 18 import ch.boye.httpclientandroidlib.HttpResponse;
michael@0 19
michael@0 20 public class FxAccountClient20 extends FxAccountClient10 implements FxAccountClient {
michael@0 21 protected static final String[] LOGIN_RESPONSE_REQUIRED_STRING_FIELDS = new String[] { JSON_KEY_UID, JSON_KEY_SESSIONTOKEN };
michael@0 22 protected static final String[] LOGIN_RESPONSE_REQUIRED_STRING_FIELDS_KEYS = new String[] { JSON_KEY_UID, JSON_KEY_SESSIONTOKEN, JSON_KEY_KEYFETCHTOKEN, };
michael@0 23 protected static final String[] LOGIN_RESPONSE_REQUIRED_BOOLEAN_FIELDS = new String[] { JSON_KEY_VERIFIED };
michael@0 24
michael@0 25 public FxAccountClient20(String serverURI, Executor executor) {
michael@0 26 super(serverURI, executor);
michael@0 27 }
michael@0 28
michael@0 29 /**
michael@0 30 * Thin container for login response.
michael@0 31 * <p>
michael@0 32 * The <code>remoteEmail</code> field is the email address as normalized by the
michael@0 33 * server, and is <b>not necessarily</b> the email address delivered to the
michael@0 34 * <code>login</code> or <code>create</code> call.
michael@0 35 */
michael@0 36 public static class LoginResponse {
michael@0 37 public final String remoteEmail;
michael@0 38 public final String uid;
michael@0 39 public final byte[] sessionToken;
michael@0 40 public final boolean verified;
michael@0 41 public final byte[] keyFetchToken;
michael@0 42
michael@0 43 public LoginResponse(String remoteEmail, String uid, boolean verified, byte[] sessionToken, byte[] keyFetchToken) {
michael@0 44 this.remoteEmail = remoteEmail;
michael@0 45 this.uid = uid;
michael@0 46 this.verified = verified;
michael@0 47 this.sessionToken = sessionToken;
michael@0 48 this.keyFetchToken = keyFetchToken;
michael@0 49 }
michael@0 50 }
michael@0 51
michael@0 52 // Public for testing only; prefer login and loginAndGetKeys (without boolean parameter).
michael@0 53 public void login(final byte[] emailUTF8, final byte[] quickStretchedPW, final boolean getKeys,
michael@0 54 final RequestDelegate<LoginResponse> delegate) {
michael@0 55 BaseResource resource;
michael@0 56 JSONObject body;
michael@0 57 final String path = getKeys ? "account/login?keys=true" : "account/login";
michael@0 58 try {
michael@0 59 resource = new BaseResource(new URI(serverURI + path));
michael@0 60 body = new FxAccount20LoginDelegate(emailUTF8, quickStretchedPW).getCreateBody();
michael@0 61 } catch (Exception e) {
michael@0 62 invokeHandleError(delegate, e);
michael@0 63 return;
michael@0 64 }
michael@0 65
michael@0 66 resource.delegate = new ResourceDelegate<LoginResponse>(resource, delegate) {
michael@0 67 @Override
michael@0 68 public void handleSuccess(int status, HttpResponse response, ExtendedJSONObject body) {
michael@0 69 try {
michael@0 70 final String[] requiredStringFields = getKeys ? LOGIN_RESPONSE_REQUIRED_STRING_FIELDS_KEYS : LOGIN_RESPONSE_REQUIRED_STRING_FIELDS;
michael@0 71 body.throwIfFieldsMissingOrMisTyped(requiredStringFields, String.class);
michael@0 72
michael@0 73 final String[] requiredBooleanFields = LOGIN_RESPONSE_REQUIRED_BOOLEAN_FIELDS;
michael@0 74 body.throwIfFieldsMissingOrMisTyped(requiredBooleanFields, Boolean.class);
michael@0 75
michael@0 76 String uid = body.getString(JSON_KEY_UID);
michael@0 77 boolean verified = body.getBoolean(JSON_KEY_VERIFIED);
michael@0 78 byte[] sessionToken = Utils.hex2Byte(body.getString(JSON_KEY_SESSIONTOKEN));
michael@0 79 byte[] keyFetchToken = null;
michael@0 80 if (getKeys) {
michael@0 81 keyFetchToken = Utils.hex2Byte(body.getString(JSON_KEY_KEYFETCHTOKEN));
michael@0 82 }
michael@0 83 LoginResponse loginResponse = new LoginResponse(new String(emailUTF8, "UTF-8"), uid, verified, sessionToken, keyFetchToken);
michael@0 84
michael@0 85 delegate.handleSuccess(loginResponse);
michael@0 86 return;
michael@0 87 } catch (Exception e) {
michael@0 88 delegate.handleError(e);
michael@0 89 return;
michael@0 90 }
michael@0 91 }
michael@0 92 };
michael@0 93
michael@0 94 post(resource, body, delegate);
michael@0 95 }
michael@0 96
michael@0 97 public void createAccount(final byte[] emailUTF8, final byte[] quickStretchedPW, final boolean getKeys, final boolean preVerified,
michael@0 98 final RequestDelegate<LoginResponse> delegate) {
michael@0 99 BaseResource resource;
michael@0 100 JSONObject body;
michael@0 101 final String path = getKeys ? "account/create?keys=true" : "account/create";
michael@0 102 try {
michael@0 103 resource = new BaseResource(new URI(serverURI + path));
michael@0 104 body = new FxAccount20CreateDelegate(emailUTF8, quickStretchedPW, preVerified).getCreateBody();
michael@0 105 } catch (Exception e) {
michael@0 106 invokeHandleError(delegate, e);
michael@0 107 return;
michael@0 108 }
michael@0 109
michael@0 110 // This is very similar to login, except verified is not required.
michael@0 111 resource.delegate = new ResourceDelegate<LoginResponse>(resource, delegate) {
michael@0 112 @Override
michael@0 113 public void handleSuccess(int status, HttpResponse response, ExtendedJSONObject body) {
michael@0 114 try {
michael@0 115 final String[] requiredStringFields = getKeys ? LOGIN_RESPONSE_REQUIRED_STRING_FIELDS_KEYS : LOGIN_RESPONSE_REQUIRED_STRING_FIELDS;
michael@0 116 body.throwIfFieldsMissingOrMisTyped(requiredStringFields, String.class);
michael@0 117
michael@0 118 String uid = body.getString(JSON_KEY_UID);
michael@0 119 boolean verified = false; // In production, we're definitely not verified immediately upon creation.
michael@0 120 Boolean tempVerified = body.getBoolean(JSON_KEY_VERIFIED);
michael@0 121 if (tempVerified != null) {
michael@0 122 verified = tempVerified.booleanValue();
michael@0 123 }
michael@0 124 byte[] sessionToken = Utils.hex2Byte(body.getString(JSON_KEY_SESSIONTOKEN));
michael@0 125 byte[] keyFetchToken = null;
michael@0 126 if (getKeys) {
michael@0 127 keyFetchToken = Utils.hex2Byte(body.getString(JSON_KEY_KEYFETCHTOKEN));
michael@0 128 }
michael@0 129 LoginResponse loginResponse = new LoginResponse(new String(emailUTF8, "UTF-8"), uid, verified, sessionToken, keyFetchToken);
michael@0 130
michael@0 131 delegate.handleSuccess(loginResponse);
michael@0 132 return;
michael@0 133 } catch (Exception e) {
michael@0 134 delegate.handleError(e);
michael@0 135 return;
michael@0 136 }
michael@0 137 }
michael@0 138 };
michael@0 139
michael@0 140 post(resource, body, delegate);
michael@0 141 }
michael@0 142
michael@0 143 @Override
michael@0 144 public void createAccountAndGetKeys(byte[] emailUTF8, PasswordStretcher passwordStretcher, RequestDelegate<LoginResponse> delegate) {
michael@0 145 try {
michael@0 146 byte[] quickStretchedPW = passwordStretcher.getQuickStretchedPW(emailUTF8);
michael@0 147 createAccount(emailUTF8, quickStretchedPW, true, false, delegate);
michael@0 148 } catch (Exception e) {
michael@0 149 invokeHandleError(delegate, e);
michael@0 150 return;
michael@0 151 }
michael@0 152 }
michael@0 153
michael@0 154 @Override
michael@0 155 public void loginAndGetKeys(byte[] emailUTF8, PasswordStretcher passwordStretcher, RequestDelegate<LoginResponse> delegate) {
michael@0 156 login(emailUTF8, passwordStretcher, true, delegate);
michael@0 157 }
michael@0 158
michael@0 159 /**
michael@0 160 * We want users to be able to enter their email address case-insensitively.
michael@0 161 * We stretch the password locally using the email address as a salt, to make
michael@0 162 * dictionary attacks more expensive. This means that a client with a
michael@0 163 * case-differing email address is unable to produce the correct
michael@0 164 * authorization, even though it knows the password. In this case, the server
michael@0 165 * returns the email that the account was created with, so that the client can
michael@0 166 * re-stretch the password locally with the correct email salt. This version
michael@0 167 * of <code>login</code> retries at most one time with a server provided email
michael@0 168 * address.
michael@0 169 * <p>
michael@0 170 * Be aware that consumers will not see the initial error response from the
michael@0 171 * server providing an alternate email (if there is one).
michael@0 172 *
michael@0 173 * @param emailUTF8
michael@0 174 * user entered email address.
michael@0 175 * @param stretcher
michael@0 176 * delegate to stretch and re-stretch password.
michael@0 177 * @param getKeys
michael@0 178 * true if a <code>keyFetchToken</code> should be returned (in
michael@0 179 * addition to the standard <code>sessionToken</code>).
michael@0 180 * @param delegate
michael@0 181 * to invoke callbacks.
michael@0 182 */
michael@0 183 public void login(final byte[] emailUTF8, final PasswordStretcher stretcher, final boolean getKeys,
michael@0 184 final RequestDelegate<LoginResponse> delegate) {
michael@0 185 byte[] quickStretchedPW;
michael@0 186 try {
michael@0 187 FxAccountConstants.pii(LOG_TAG, "Trying user provided email: '" + new String(emailUTF8, "UTF-8") + "'" );
michael@0 188 quickStretchedPW = stretcher.getQuickStretchedPW(emailUTF8);
michael@0 189 } catch (Exception e) {
michael@0 190 delegate.handleError(e);
michael@0 191 return;
michael@0 192 }
michael@0 193
michael@0 194 this.login(emailUTF8, quickStretchedPW, getKeys, new RequestDelegate<LoginResponse>() {
michael@0 195 @Override
michael@0 196 public void handleSuccess(LoginResponse result) {
michael@0 197 delegate.handleSuccess(result);
michael@0 198 }
michael@0 199
michael@0 200 @Override
michael@0 201 public void handleError(Exception e) {
michael@0 202 delegate.handleError(e);
michael@0 203 }
michael@0 204
michael@0 205 @Override
michael@0 206 public void handleFailure(FxAccountClientRemoteException e) {
michael@0 207 String alternateEmail = e.body.getString(JSON_KEY_EMAIL);
michael@0 208 if (!e.isBadEmailCase() || alternateEmail == null) {
michael@0 209 delegate.handleFailure(e);
michael@0 210 return;
michael@0 211 };
michael@0 212
michael@0 213 Logger.info(LOG_TAG, "Server returned alternate email; retrying login with provided email.");
michael@0 214 FxAccountConstants.pii(LOG_TAG, "Trying server provided email: '" + alternateEmail + "'" );
michael@0 215
michael@0 216 try {
michael@0 217 // Nota bene: this is not recursive, since we call the fixed password
michael@0 218 // signature here, which invokes a non-retrying version.
michael@0 219 byte[] alternateEmailUTF8 = alternateEmail.getBytes("UTF-8");
michael@0 220 byte[] alternateQuickStretchedPW = stretcher.getQuickStretchedPW(alternateEmailUTF8);
michael@0 221 login(alternateEmailUTF8, alternateQuickStretchedPW, getKeys, delegate);
michael@0 222 } catch (Exception innerException) {
michael@0 223 delegate.handleError(innerException);
michael@0 224 return;
michael@0 225 }
michael@0 226 }
michael@0 227 });
michael@0 228 }
michael@0 229 }

mercurial