Example usage for android.accounts AuthenticatorException AuthenticatorException

List of usage examples for android.accounts AuthenticatorException AuthenticatorException

Introduction

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

Prototype

public AuthenticatorException(Throwable cause) 

Source Link

Usage

From source file:com.android.volley.toolbox.AndroidAuthenticatorTest.java

@Test(expected = AuthFailureError.class)
public void failedGetAuthToken() throws Exception {
    when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
    when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!"));
    mAuthenticator.getAuthToken();//from w  w w.  ja  v a2 s  .c o m
}

From source file:com.owncloud.android.network.OwnCloudClientUtils.java

public static WebdavClient createOwnCloudClient(Account account, Context appContext, Activity currentActivity)
        throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
    Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
    AccountManager am = AccountManager.get(appContext);
    boolean isOauth2 = am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
    boolean isSamlSso = am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null;
    WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso);

    if (isOauth2) { // TODO avoid a call to getUserData here
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, null, currentActivity, null, null);
        Bundle result = future.getResult();
        String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
        if (accessToken == null)
            throw new AuthenticatorException("WTF!");
        client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token

    } else if (isSamlSso) { // TODO avoid a call to getUserData here
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE, null, currentActivity, null,
                null);/*  w w  w  .ja  v a2  s.com*/
        Bundle result = future.getResult();
        String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
        if (accessToken == null)
            throw new AuthenticatorException("WTF!");
        client.setSsoSessionCookie(accessToken);

    } else {
        String username = account.name.substring(0, account.name.lastIndexOf('@'));
        //String password = am.getPassword(account);
        //String password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, false);
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, null, currentActivity, null, null);
        Bundle result = future.getResult();
        String password = result.getString(AccountManager.KEY_AUTHTOKEN);
        client.setBasicCredentials(username, password);
    }

    return client;
}

From source file:net.vleu.par.android.rpc.Transceiver.java

/**
 * This method may block while a network request completes, and must never
 * be made from the main thread. It requests a new GoogleAuthToken.
 * /*from w  w  w . j a  va  2  s  .c o  m*/
 * @param account
 *            The account to fetch an auth token for
 * @return A token associated with this account
 * @throws OperationCanceledException
 *             if the request was canceled for any reason, including the
 *             user canceling a credential request
 * @throws AuthenticatorException
 *             if the authenticator failed to respond
 * @throws IOException
 *             if the authenticator experienced an I/O problem creating a
 *             new auth token, usually because of network trouble
 */
private GoogleAuthToken blockingGetNewAuthToken()
        throws OperationCanceledException, AuthenticatorException, IOException {
    final AccountManager am = AccountManager.get(this.context);
    final String authTokenStr = am.blockingGetAuthToken(this.account, APPENGINE_TOKEN_TYPE, true);
    if (Log.isLoggable(TAG, Log.INFO))
        Log.i(TAG, "Got a new GoogleAuthToken for account: " + this.account.name + ": " + authTokenStr);
    if (authTokenStr == null)
        throw new AuthenticatorException("Could not get an auth token");
    else
        return new GoogleAuthToken(authTokenStr);
}

From source file:net.vleu.par.android.rpc.Transceiver.java

/**
 * Performs authentication if necessary, then exchange the data with the
 * server and returns the response//w w w  .  j a  v  a2 s  .c o  m
 * 
 * @param request
 * @return The response to the request, null if none
 * @throws IOException
 * @throws OperationCanceledException
 * @throws AuthenticatorException
 */
public GatewayResponseData exchangeWithServer(final GatewayRequestData request)
        throws IOException, OperationCanceledException, AuthenticatorException {
    final int maxRetries = 10;
    for (int retry = 0; retry < maxRetries; retry++) {
        if (!hasSacsidToken()) {
            /* Gets a Google Auth Token and promotes it to a SACSID Token */
            final GoogleAuthToken googleAuthToken = blockingGetNewAuthToken();
            try {
                promoteToken(googleAuthToken);
            } catch (final InvalidGoogleAuthTokenException e) {
                if (Log.isLoggable(TAG, Log.WARN))
                    Log.w(TAG, "The google auth token is invalid. Refreshing all cookies. ", e);
                invalidatesGoogleAuthToken(googleAuthToken);
                clearSacsidToken();
                continue;
            }
        }
        /* Executes the query */
        try {
            return postData(request);
        } catch (final AuthenticationTokenExpired e) {
            clearSacsidToken();
            if (Log.isLoggable(TAG, Log.WARN))
                Log.w(TAG, "Google and/or SACSID tokens expired. Retried " + retry + " times.", e);
            continue;
        }
    }
    final String failureMessage = "Failed to get valid Google and SACSID tokens !";
    if (Log.isLoggable(TAG, Log.ERROR))
        Log.e(TAG, failureMessage);
    throw new AuthenticatorException(failureMessage);
}