Search in sources :

Example 1 with AuthInvalidGrantException

use of com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException in project aws-sdk-android by aws-amplify.

the class AuthClient method refreshSession.

/**
 * Internal method to refresh tokens.
 * <p>
 *     Makes an http call to Amazon Cognito token end-point to refresh token. On successful
 *     token refresh, the refresh tokens is retained.
 * </p>
 * @param session Required: The android application {@link Context}.
 * @param redirectUri Required: The redirect Uri, which will be launched after authentication.
 * @param tokenScopes Required: A {@link Set<String>} specifying all scopes for the tokens.
 * @param callback Required: {@link AuthHandler}.
 * @param showSignInIfExpired true if the web UI should launch when the refresh token is expired
 * @param browserPackage String specifying the browser package to launch the specified url.
 * @param activity The activity to launch the sign in experience from.
 *                 This must not be null if showSignInIfExpired is true.
 */
private void refreshSession(final AuthUserSession session, final String redirectUri, final Set<String> tokenScopes, final AuthHandler callback, final boolean showSignInIfExpired, final String browserPackage, final Activity activity) {
    new Thread(new Runnable() {

        final Handler handler = new Handler(context.getMainLooper());

        Runnable returnCallback;

        @Override
        public void run() {
            final Uri fqdn = new Uri.Builder().scheme(ClientConstants.DOMAIN_SCHEME).authority(pool.getAppWebDomain()).appendPath(ClientConstants.DOMAIN_PATH_OAUTH2).appendPath(ClientConstants.DOMAIN_PATH_TOKEN_ENDPOINT).build();
            // Make http POST call
            final AuthHttpClient httpClient = new AuthHttpClient();
            Map<String, String> httpHeaderParams = getHttpHeader();
            Map<String, String> httpBodyParams = generateTokenRefreshRequest(redirectUri, session);
            try {
                String response = httpClient.httpPost(new URL(fqdn.toString()), httpHeaderParams, httpBodyParams);
                AuthUserSession parsedSession = AuthHttpResponseParser.parseHttpResponse(response);
                final AuthUserSession refreshedSession = new AuthUserSession(parsedSession.getIdToken(), parsedSession.getAccessToken(), session.getRefreshToken());
                final String username = refreshedSession.getUsername();
                // Cache session
                LocalDataManager.cacheSession(pool.awsKeyValueStore, context, pool.getAppId(), username, refreshedSession, pool.getScopes());
                // Return tokens
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onSuccess(refreshedSession);
                    }
                };
            } catch (final AuthInvalidGrantException invg) {
                if (showSignInIfExpired) {
                    returnCallback = new Runnable() {

                        @Override
                        public void run() {
                            launchCognitoAuth(redirectUri, tokenScopes, activity, browserPackage);
                        }
                    };
                } else {
                    returnCallback = new Runnable() {

                        @Override
                        public void run() {
                            userHandler.onFailure(invg);
                        }
                    };
                }
            } catch (final Exception e) {
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onFailure(e);
                    }
                };
            }
            handler.post(returnCallback);
        }
    }).start();
}
Also used : AuthHttpClient(com.amazonaws.mobileconnectors.cognitoauth.util.AuthHttpClient) Handler(android.os.Handler) AuthHandler(com.amazonaws.mobileconnectors.cognitoauth.handlers.AuthHandler) AuthInvalidGrantException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException) Uri(android.net.Uri) URL(java.net.URL) AuthServiceException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException) AuthClientException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException) BrowserNotInstalledException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.BrowserNotInstalledException) InvalidParameterException(java.security.InvalidParameterException) AuthInvalidGrantException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException) AuthNavigationException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)

Example 2 with AuthInvalidGrantException

use of com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException in project aws-sdk-android by aws-amplify.

the class AuthHttpResponseParser method parseHttpResponse.

/**
 * Parses the http response from Cognito service and extracts tokens.
 * <p>
 *     Throws {@link AuthInvalidGrantException when }
 * </p>
 * @param responseStr Required: Response from Cognito Service Token-Endpoint.
 * @return {@link AuthUserSession}.
 */
public static final AuthUserSession parseHttpResponse(String responseStr) {
    if (responseStr == null || responseStr.isEmpty()) {
        throw new AuthInvalidParameterException("Invalid (null) response from Amazon Cognito Auth endpoint");
    }
    AccessToken accessToken = new AccessToken(null);
    IdToken idToken = new IdToken(null);
    RefreshToken refreshToken = new RefreshToken(null);
    JSONObject responseJson;
    try {
        responseJson = new JSONObject(responseStr);
        if (responseJson.has(ClientConstants.DOMAIN_QUERY_PARAM_ERROR)) {
            String errorText = responseJson.getString(ClientConstants.DOMAIN_QUERY_PARAM_ERROR);
            if (ClientConstants.HTTP_RESPONSE_INVALID_GRANT.equals(errorText)) {
                throw new AuthInvalidGrantException(errorText);
            } else {
                throw new AuthServiceException(errorText);
            }
        }
        if (responseJson.has(ClientConstants.HTTP_RESPONSE_ACCESS_TOKEN)) {
            accessToken = new AccessToken(responseJson.getString(ClientConstants.HTTP_RESPONSE_ACCESS_TOKEN));
        }
        if (responseJson.has(ClientConstants.HTTP_RESPONSE_ID_TOKEN)) {
            idToken = new IdToken(responseJson.getString(ClientConstants.HTTP_RESPONSE_ID_TOKEN));
        }
        if (responseJson.has(ClientConstants.HTTP_RESPONSE_REFRESH_TOKEN)) {
            refreshToken = new RefreshToken(responseJson.getString(ClientConstants.HTTP_RESPONSE_REFRESH_TOKEN));
        }
    } catch (AuthInvalidGrantException invg) {
        throw invg;
    } catch (AuthServiceException seve) {
        throw seve;
    } catch (Exception e) {
        throw new AuthClientException(e.getMessage(), e);
    }
    return new AuthUserSession(idToken, accessToken, refreshToken);
}
Also used : IdToken(com.amazonaws.mobileconnectors.cognitoauth.tokens.IdToken) AuthServiceException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException) RefreshToken(com.amazonaws.mobileconnectors.cognitoauth.tokens.RefreshToken) JSONObject(org.json.JSONObject) AuthClientException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) AccessToken(com.amazonaws.mobileconnectors.cognitoauth.tokens.AccessToken) AuthInvalidGrantException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException) AuthUserSession(com.amazonaws.mobileconnectors.cognitoauth.AuthUserSession) AuthServiceException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) AuthInvalidGrantException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException) AuthClientException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException)

Aggregations

AuthClientException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException)2 AuthInvalidGrantException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException)2 AuthServiceException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException)2 Uri (android.net.Uri)1 Handler (android.os.Handler)1 AuthUserSession (com.amazonaws.mobileconnectors.cognitoauth.AuthUserSession)1 AuthInvalidParameterException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException)1 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)1 BrowserNotInstalledException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.BrowserNotInstalledException)1 AuthHandler (com.amazonaws.mobileconnectors.cognitoauth.handlers.AuthHandler)1 AccessToken (com.amazonaws.mobileconnectors.cognitoauth.tokens.AccessToken)1 IdToken (com.amazonaws.mobileconnectors.cognitoauth.tokens.IdToken)1 RefreshToken (com.amazonaws.mobileconnectors.cognitoauth.tokens.RefreshToken)1 AuthHttpClient (com.amazonaws.mobileconnectors.cognitoauth.util.AuthHttpClient)1 URL (java.net.URL)1 InvalidParameterException (java.security.InvalidParameterException)1 JSONObject (org.json.JSONObject)1