Example usage for android.accounts AccountManager KEY_PASSWORD

List of usage examples for android.accounts AccountManager KEY_PASSWORD

Introduction

In this page you can find the example usage for android.accounts AccountManager KEY_PASSWORD.

Prototype

String KEY_PASSWORD

To view the source code for android.accounts AccountManager KEY_PASSWORD.

Click Source Link

Document

Bundle key used to supply the password directly in options to #confirmCredentials , rather than prompting the user with the standard password prompt.

Usage

From source file:edu.mit.mobile.android.locast.accounts.Authenticator.java

/**
 * {@inheritDoc}/*from w w  w.ja va 2  s.  c  om*/
 */
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) {
    if (options != null && options.containsKey(AccountManager.KEY_PASSWORD)) {
        final String password = options.getString(AccountManager.KEY_PASSWORD);
        final Bundle verified = onlineConfirmPassword(account, password);
        final Bundle result = new Bundle();
        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, verified != null);
        return result;
    }
    // Launch AuthenticatorActivity to confirm credentials
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.EXTRA_USERNAME, account.name);
    intent.putExtra(AuthenticatorActivity.EXTRA_CONFIRMCREDENTIALS, true);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticator.java

/**
 * {@inheritDoc}/*  w  w  w  .jav  a  2s.c o  m*/
 */
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) {
    if (options != null && options.containsKey(AccountManager.KEY_PASSWORD)) {
        final String password = options.getString(AccountManager.KEY_PASSWORD);
        final Bundle verified = onlineConfirmPassword(account, password);
        final Bundle result = new Bundle();
        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, verified != null);
        return result;
    }
    // Launch AuthenticatorActivity to confirm credentials
    final Intent intent = getAuthenticator(mContext);
    intent.putExtra(AbsLocastAuthenticatorActivity.EXTRA_USERNAME, account.name);
    intent.putExtra(AbsLocastAuthenticatorActivity.EXTRA_CONFIRMCREDENTIALS, true);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:dev.drsoran.moloko.auth.AuthenticatorActivity.java

/**
 * Response is received from the server for authentication request. Sets the AccountAuthenticatorResult which is sent
 * back to the caller. Also sets the authToken in AccountManager for this account.
 *///from   www . ja  v a 2s.  c o  m
@Override
public void onAuthenticationFinished(RtmAuth rtmAuth) {
    final Account account = new Account(rtmAuth.getUser().getUsername(), Constants.ACCOUNT_TYPE);
    try {
        boolean ok = true;

        if (isNewAccount) {
            ok = accountManager.addAccountExplicitly(account, rtmAuth.getToken(), null);
            if (ok) {
                ContentResolver.setSyncAutomatically(account, Rtm.AUTHORITY, true);
            }
        }

        if (ok) {
            accountManager.setUserData(account, Constants.FEAT_API_KEY, MolokoApp.getRtmApiKey(this));
            accountManager.setUserData(account, Constants.FEAT_SHARED_SECRET,
                    MolokoApp.getRtmSharedSecret(this));
            accountManager.setUserData(account, Constants.FEAT_PERMISSION, rtmAuth.getPerms().toString());
            accountManager.setUserData(account, Constants.ACCOUNT_USER_ID, rtmAuth.getUser().getId());
            accountManager.setUserData(account, Constants.ACCOUNT_FULLNAME, rtmAuth.getUser().getFullname());

            final Intent intent = new Intent();

            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, rtmAuth.getUser().getUsername());
            // We store the authToken as password
            intent.putExtra(AccountManager.KEY_PASSWORD, rtmAuth.getToken());
            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            intent.putExtra(AccountManager.KEY_AUTHTOKEN, rtmAuth.getToken());

            intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, true);

            setAccountAuthenticatorResult(intent.getExtras());
            setResult(RESULT_OK, intent);
        }
    } catch (SecurityException e) {
        MolokoApp.Log.e(getClass(), e.getLocalizedMessage());
        onAuthenticationFailed(getString(R.string.auth_err_cause_scurity));
    } finally {
        finish();
    }
}

From source file:org.xwiki.android.authenticator.auth.AuthenticatorActivity.java

public void finishLogin(Intent intent) {
    Log.d(TAG, "> finishLogin");

    //before add new account, clear old account data.
    clearOldAccount();/*from  ww w. ja  v a 2  s.c  om*/

    //get values
    String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    String accountPassword = intent.getStringExtra(PARAM_USER_PASS);
    String accountServer = intent.getStringExtra(PARAM_USER_SERVER);

    // Creating the account on the device and setting the auth token we got
    // (Not setting the auth token will cause another call to the server to authenticate the user)
    Log.d(TAG, "finishLogin > addAccountExplicitly" + " "
            + intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));
    final Account account = new Account(accountName, Constants.ACCOUNT_TYPE);
    mAccountManager.addAccountExplicitly(account, accountPassword, null);
    mAccountManager.setUserData(account, AccountManager.KEY_USERDATA, accountName);
    mAccountManager.setUserData(account, AccountManager.KEY_PASSWORD, accountPassword);
    mAccountManager.setUserData(account, AuthenticatorActivity.PARAM_USER_SERVER, accountServer);

    //grant permission if adding user from the third-party app (UID,PackageName);
    String packaName = getIntent().getStringExtra(PARAM_APP_PACKAGENAME);
    int uid = getIntent().getIntExtra(PARAM_APP_UID, 0);
    Log.d(TAG, packaName + ", " + getPackageName());
    //only if adding account from the third-party apps exclude android.uid.system, this will execute to grant permission and set token
    if (!packaName.contains("android.uid.system")) {
        AppContext.addAuthorizedApp(uid, packaName);
        String authToken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
        if (!TextUtils.isEmpty(authToken)) {
            String authTokenType = getIntent().getStringExtra(KEY_AUTH_TOKEN_TYPE);
            mAccountManager.setAuthToken(account, authTokenType, authToken);
        }
    }

    //return value to AccountManager
    Intent intentReturn = new Intent();
    intentReturn.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
    intentReturn.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName);
    setAccountAuthenticatorResult(intentReturn.getExtras());
    setResult(RESULT_OK, intentReturn);
    Log.d(TAG, ">" + "finish return");
    // in SettingSyncViewFlipper this activity finish;
}

From source file:org.ohmage.auth.AuthenticatorActivity.java

private void finishAccountAdd(String accountName, String authToken, String password) {
    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AuthUtil.ACCOUNT_TYPE);
    if (authToken != null)
        intent.putExtra(AccountManager.KEY_AUTHTOKEN, authToken);
    intent.putExtra(AccountManager.KEY_PASSWORD, password);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);/* w  w  w  .  j  av  a  2s. c o m*/
    finish();

    if (!calledByAuthenticator())
        startActivity(new Intent(getBaseContext(), MainActivity.class));
}

From source file:mobile.tiis.appv2.LoginActivity.java

/**
 * This method will take the url built to use the webservice
 * and will try to parse JSON from the webservice stream to get
 * the user and password if they are correct or not. In case correct, fills
 * the Android Account Manager.//from   w w w.  j  a va 2 s  .  co m
 *
 * <p>This method will throw a Toast message when user and password
 * are not valid
 *
 */

protected void startWebService(final CharSequence loginURL, final String username, final String password) {
    client.setBasicAuth(username, password, true);

    //new handler in case of login error in the thread
    handler = new Handler();

    Thread thread = new Thread(new Runnable() {
        public void run() {
            try {
                int balanceCounter = 0;
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(loginURL.toString());
                Utils.writeNetworkLogFileOnSD(
                        Utils.returnDeviceIdAndTimestamp(getApplicationContext()) + loginURL.toString());
                httpGet.setHeader("Authorization", "Basic "
                        + Base64.encodeToString((username + ":" + password).getBytes(), Base64.NO_WRAP));
                HttpResponse httpResponse = httpClient.execute(httpGet);
                InputStream inputStream = httpResponse.getEntity().getContent();
                Log.d("", loginURL.toString());

                ByteArrayInputStream bais = Utils.getMultiReadInputStream(inputStream);
                Utils.writeNetworkLogFileOnSD(Utils.returnDeviceIdAndTimestamp(getApplicationContext())
                        + Utils.getStringFromInputStreamAndLeaveStreamOpen(bais));
                bais.reset();
                JsonFactory factory = new JsonFactory();
                JsonParser jsonParser = factory.createJsonParser(bais);
                JsonToken token = jsonParser.nextToken();

                if (token == JsonToken.START_OBJECT) {
                    balanceCounter++;
                    boolean idNextToHfId = false;
                    while (!(balanceCounter == 0)) {
                        token = jsonParser.nextToken();

                        if (token == JsonToken.START_OBJECT) {
                            balanceCounter++;
                        } else if (token == JsonToken.END_OBJECT) {
                            balanceCounter--;
                        } else if (token == JsonToken.FIELD_NAME) {
                            String object = jsonParser.getCurrentName();
                            switch (object) {
                            case "HealthFacilityId":
                                token = jsonParser.nextToken();
                                app.setLoggedInUserHealthFacilityId(jsonParser.getText());
                                Log.d("", "healthFacilityId is: " + jsonParser.getText());
                                idNextToHfId = true;
                                break;
                            case "Firstname":
                                token = jsonParser.nextToken();
                                app.setLoggedInFirstname(jsonParser.getText());
                                Log.d("", "firstname is: " + jsonParser.getText());
                                break;
                            case "Lastname":
                                token = jsonParser.nextToken();
                                app.setLoggedInLastname(jsonParser.getText());
                                Log.d("", "lastname is: " + jsonParser.getText());
                                break;
                            case "Username":
                                token = jsonParser.nextToken();
                                app.setLoggedInUsername(jsonParser.getText());
                                Log.d("", "username is: " + jsonParser.getText());
                                break;
                            case "Lastlogin":
                                token = jsonParser.nextToken();
                                Log.d("", "lastlogin is: " + jsonParser.getText());
                                break;
                            case "Id":
                                if (idNextToHfId) {
                                    token = jsonParser.nextToken();
                                    app.setLoggedInUserId(jsonParser.getText());
                                    Log.d("", "Id is: " + jsonParser.getText());
                                }
                                break;
                            default:
                                break;
                            }
                        }
                    }

                    Account account = new Account(username, ACCOUNT_TYPE);
                    AccountManager accountManager = AccountManager.get(LoginActivity.this);
                    //                        boolean accountCreated = accountManager.addAccountExplicitly(account, LoginActivity.this.password, null);
                    boolean accountCreated = accountManager.addAccountExplicitly(account, password, null);

                    Bundle extras = LoginActivity.this.getIntent().getExtras();
                    if (extras != null) {
                        if (accountCreated) { //Pass the new account back to the account manager
                            AccountAuthenticatorResponse response = extras
                                    .getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
                            Bundle res = new Bundle();
                            res.putString(AccountManager.KEY_ACCOUNT_NAME, username);
                            res.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
                            res.putString(AccountManager.KEY_PASSWORD, password);
                            response.onResult(res);
                        }
                    }

                    SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("secondSyncNeeded", true);
                    editor.commit();

                    ContentValues values = new ContentValues();
                    values.put(SQLHandler.SyncColumns.UPDATED, 1);
                    values.put(SQLHandler.UserColumns.FIRSTNAME, app.getLOGGED_IN_FIRSTNAME());
                    values.put(SQLHandler.UserColumns.LASTNAME, app.getLOGGED_IN_LASTNAME());
                    values.put(SQLHandler.UserColumns.HEALTH_FACILITY_ID, app.getLOGGED_IN_USER_HF_ID());
                    values.put(SQLHandler.UserColumns.ID, app.getLOGGED_IN_USER_ID());
                    values.put(SQLHandler.UserColumns.USERNAME, app.getLOGGED_IN_USERNAME());
                    values.put(SQLHandler.UserColumns.PASSWORD, password);
                    databaseHandler.addUser(values);

                    Log.d(TAG, "initiating offline for " + username + " password = " + password);
                    app.initializeOffline(username, password);

                    Intent intent;
                    if (prefs.getBoolean("synchronization_needed", true)) {
                        Log.d("supportLog", "call the loggin second time before the account was found");
                        intent = new Intent(LoginActivity.this, LotSettingsActivity.class);
                    } else {
                        Log.d("supportLog", "call the loggin second time before the account was found");
                        intent = new Intent(LoginActivity.this, LotSettingsActivity.class);
                        evaluateIfFirstLogin(app.getLOGGED_IN_USER_ID());
                    }
                    app.setUsername(username);

                    startActivity(intent);
                }
                //if login failed show error
                else {
                    handler.post(new Runnable() {
                        public void run() {
                            progressDialog.show();
                            progressDialog.dismiss();
                            toastMessage("Login failed.\nPlease check your details!");
                            loginButton.setEnabled(true);
                        }
                    });
                }
            } catch (Exception e) {
                handler.post(new Runnable() {
                    public void run() {
                        progressDialog.show();
                        progressDialog.dismiss();
                        toastMessage("Login failed Login failed.\n"
                                + "Please check your details or your web connectivity");
                        loginButton.setEnabled(true);

                    }
                });
                e.printStackTrace();
            }
        }
    });
    thread.start();

}