mobile/android/base/fxa/activities/FxAccountUpdateCredentialsActivity.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.fxa.activities;
michael@0 6
michael@0 7 import java.util.concurrent.Executor;
michael@0 8 import java.util.concurrent.Executors;
michael@0 9
michael@0 10 import org.mozilla.gecko.R;
michael@0 11 import org.mozilla.gecko.background.common.log.Logger;
michael@0 12 import org.mozilla.gecko.background.fxa.FxAccountClient;
michael@0 13 import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate;
michael@0 14 import org.mozilla.gecko.background.fxa.FxAccountClient20;
michael@0 15 import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse;
michael@0 16 import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
michael@0 17 import org.mozilla.gecko.background.fxa.FxAccountUtils;
michael@0 18 import org.mozilla.gecko.background.fxa.PasswordStretcher;
michael@0 19 import org.mozilla.gecko.fxa.FirefoxAccounts;
michael@0 20 import org.mozilla.gecko.fxa.FxAccountConstants;
michael@0 21 import org.mozilla.gecko.fxa.activities.FxAccountSetupTask.FxAccountSignInTask;
michael@0 22 import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
michael@0 23 import org.mozilla.gecko.fxa.login.Engaged;
michael@0 24 import org.mozilla.gecko.fxa.login.State;
michael@0 25 import org.mozilla.gecko.fxa.login.State.StateLabel;
michael@0 26 import org.mozilla.gecko.sync.setup.activities.ActivityUtils;
michael@0 27
michael@0 28 import android.os.Bundle;
michael@0 29 import android.view.View;
michael@0 30 import android.view.View.OnClickListener;
michael@0 31 import android.widget.AutoCompleteTextView;
michael@0 32 import android.widget.Button;
michael@0 33 import android.widget.EditText;
michael@0 34 import android.widget.ProgressBar;
michael@0 35 import android.widget.TextView;
michael@0 36
michael@0 37 /**
michael@0 38 * Activity which displays a screen for updating the local password.
michael@0 39 */
michael@0 40 public class FxAccountUpdateCredentialsActivity extends FxAccountAbstractSetupActivity {
michael@0 41 protected static final String LOG_TAG = FxAccountUpdateCredentialsActivity.class.getSimpleName();
michael@0 42
michael@0 43 protected AndroidFxAccount fxAccount;
michael@0 44
michael@0 45 public FxAccountUpdateCredentialsActivity() {
michael@0 46 // We want to share code with the other setup activities, but this activity
michael@0 47 // doesn't create a new Android Account, it modifies an existing one. If you
michael@0 48 // manage to get an account, and somehow be locked out too, we'll let you
michael@0 49 // update it.
michael@0 50 super(CANNOT_RESUME_WHEN_NO_ACCOUNTS_EXIST);
michael@0 51 }
michael@0 52
michael@0 53 /**
michael@0 54 * {@inheritDoc}
michael@0 55 */
michael@0 56 @Override
michael@0 57 public void onCreate(Bundle icicle) {
michael@0 58 Logger.debug(LOG_TAG, "onCreate(" + icicle + ")");
michael@0 59
michael@0 60 super.onCreate(icicle);
michael@0 61 setContentView(R.layout.fxaccount_update_credentials);
michael@0 62
michael@0 63 emailEdit = (AutoCompleteTextView) ensureFindViewById(null, R.id.email, "email edit");
michael@0 64 passwordEdit = (EditText) ensureFindViewById(null, R.id.password, "password edit");
michael@0 65 showPasswordButton = (Button) ensureFindViewById(null, R.id.show_password, "show password button");
michael@0 66 remoteErrorTextView = (TextView) ensureFindViewById(null, R.id.remote_error, "remote error text view");
michael@0 67 button = (Button) ensureFindViewById(null, R.id.button, "update credentials");
michael@0 68 progressBar = (ProgressBar) ensureFindViewById(null, R.id.progress, "progress bar");
michael@0 69
michael@0 70 minimumPasswordLength = 1; // Minimal restriction on passwords entered to sign in.
michael@0 71 createButton();
michael@0 72 addListeners();
michael@0 73 updateButtonState();
michael@0 74 createShowPasswordButton();
michael@0 75
michael@0 76 emailEdit.setEnabled(false);
michael@0 77
michael@0 78 TextView view = (TextView) findViewById(R.id.forgot_password_link);
michael@0 79 ActivityUtils.linkTextView(view, R.string.fxaccount_sign_in_forgot_password, R.string.fxaccount_link_forgot_password);
michael@0 80 }
michael@0 81
michael@0 82 @Override
michael@0 83 public void onResume() {
michael@0 84 super.onResume();
michael@0 85 this.fxAccount = getAndroidFxAccount();
michael@0 86 if (fxAccount == null) {
michael@0 87 Logger.warn(LOG_TAG, "Could not get Firefox Account.");
michael@0 88 setResult(RESULT_CANCELED);
michael@0 89 finish();
michael@0 90 return;
michael@0 91 }
michael@0 92 State state = fxAccount.getState();
michael@0 93 if (state.getStateLabel() != StateLabel.Separated) {
michael@0 94 Logger.warn(LOG_TAG, "Cannot update credentials from Firefox Account in state: " + state.getStateLabel());
michael@0 95 setResult(RESULT_CANCELED);
michael@0 96 finish();
michael@0 97 return;
michael@0 98 }
michael@0 99 emailEdit.setText(fxAccount.getEmail());
michael@0 100 }
michael@0 101
michael@0 102 protected class UpdateCredentialsDelegate implements RequestDelegate<LoginResponse> {
michael@0 103 public final String email;
michael@0 104 public final String serverURI;
michael@0 105 public final PasswordStretcher passwordStretcher;
michael@0 106
michael@0 107 public UpdateCredentialsDelegate(String email, PasswordStretcher passwordStretcher, String serverURI) {
michael@0 108 this.email = email;
michael@0 109 this.serverURI = serverURI;
michael@0 110 this.passwordStretcher = passwordStretcher;
michael@0 111 }
michael@0 112
michael@0 113 @Override
michael@0 114 public void handleError(Exception e) {
michael@0 115 showRemoteError(e, R.string.fxaccount_update_credentials_unknown_error);
michael@0 116 }
michael@0 117
michael@0 118 @Override
michael@0 119 public void handleFailure(FxAccountClientRemoteException e) {
michael@0 120 if (e.isUpgradeRequired()) {
michael@0 121 Logger.error(LOG_TAG, "Got upgrade required from remote server; transitioning Firefox Account to Doghouse state.");
michael@0 122 final State state = fxAccount.getState();
michael@0 123 fxAccount.setState(state.makeDoghouseState());
michael@0 124 // The status activity will say that the user needs to upgrade.
michael@0 125 redirectToActivity(FxAccountStatusActivity.class);
michael@0 126 return;
michael@0 127 }
michael@0 128 showRemoteError(e, R.string.fxaccount_update_credentials_unknown_error);
michael@0 129 }
michael@0 130
michael@0 131 @Override
michael@0 132 public void handleSuccess(LoginResponse result) {
michael@0 133 Logger.info(LOG_TAG, "Got success signing in.");
michael@0 134
michael@0 135 if (fxAccount == null) {
michael@0 136 this.handleError(new IllegalStateException("fxAccount must not be null"));
michael@0 137 return;
michael@0 138 }
michael@0 139
michael@0 140 byte[] unwrapkB;
michael@0 141 try {
michael@0 142 // It is crucial that we use the email address provided by the server
michael@0 143 // (rather than whatever the user entered), because the user's keys are
michael@0 144 // wrapped and salted with the initial email they provided to
michael@0 145 // /create/account. Of course, we want to pass through what the user
michael@0 146 // entered locally as much as possible.
michael@0 147 byte[] quickStretchedPW = passwordStretcher.getQuickStretchedPW(result.remoteEmail.getBytes("UTF-8"));
michael@0 148 unwrapkB = FxAccountUtils.generateUnwrapBKey(quickStretchedPW);
michael@0 149 } catch (Exception e) {
michael@0 150 this.handleError(e);
michael@0 151 return;
michael@0 152 }
michael@0 153 fxAccount.setState(new Engaged(email, result.uid, result.verified, unwrapkB, result.sessionToken, result.keyFetchToken));
michael@0 154 fxAccount.requestSync(FirefoxAccounts.FORCE);
michael@0 155
michael@0 156 // For great debugging.
michael@0 157 if (FxAccountConstants.LOG_PERSONAL_INFORMATION) {
michael@0 158 fxAccount.dump();
michael@0 159 }
michael@0 160
michael@0 161 redirectToActivity(FxAccountStatusActivity.class);
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 public void updateCredentials(String email, String password) {
michael@0 166 String serverURI = fxAccount.getAccountServerURI();
michael@0 167 Executor executor = Executors.newSingleThreadExecutor();
michael@0 168 FxAccountClient client = new FxAccountClient20(serverURI, executor);
michael@0 169 PasswordStretcher passwordStretcher = makePasswordStretcher(password);
michael@0 170 try {
michael@0 171 hideRemoteError();
michael@0 172 RequestDelegate<LoginResponse> delegate = new UpdateCredentialsDelegate(email, passwordStretcher, serverURI);
michael@0 173 new FxAccountSignInTask(this, this, email, passwordStretcher, client, delegate).execute();
michael@0 174 } catch (Exception e) {
michael@0 175 Logger.warn(LOG_TAG, "Got exception updating credentials for account.", e);
michael@0 176 showRemoteError(e, R.string.fxaccount_update_credentials_unknown_error);
michael@0 177 }
michael@0 178 }
michael@0 179
michael@0 180 protected void createButton() {
michael@0 181 button.setOnClickListener(new OnClickListener() {
michael@0 182 @Override
michael@0 183 public void onClick(View v) {
michael@0 184 final String email = emailEdit.getText().toString();
michael@0 185 final String password = passwordEdit.getText().toString();
michael@0 186 updateCredentials(email, password);
michael@0 187 }
michael@0 188 });
michael@0 189 }
michael@0 190 }

mercurial