Example usage for com.amazonaws.services.cognitoidp.model AuthFlowType REFRESH_TOKEN

List of usage examples for com.amazonaws.services.cognitoidp.model AuthFlowType REFRESH_TOKEN

Introduction

In this page you can find the example usage for com.amazonaws.services.cognitoidp.model AuthFlowType REFRESH_TOKEN.

Prototype

AuthFlowType REFRESH_TOKEN

To view the source code for com.amazonaws.services.cognitoidp.model AuthFlowType REFRESH_TOKEN.

Click Source Link

Usage

From source file:com.kdgregory.example.cognito.servlets.ValidatedAction.java

License:Apache License

/**
 *  Attempts to create a new access token based on the provided refresh token.
 *///from ww w  . j  a v  a2s . c  o m
private void attemptRefresh(String refreshToken, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        Map<String, String> authParams = new HashMap<String, String>();
        authParams.put("REFRESH_TOKEN", refreshToken);

        AdminInitiateAuthRequest refreshRequest = new AdminInitiateAuthRequest()
                .withAuthFlow(AuthFlowType.REFRESH_TOKEN).withAuthParameters(authParams)
                .withClientId(cognitoClientId()).withUserPoolId(cognitoPoolId());

        AdminInitiateAuthResult refreshResponse = cognitoClient.adminInitiateAuth(refreshRequest);
        if (StringUtil.isBlank(refreshResponse.getChallengeName())) {
            logger.debug("successfully refreshed token");
            updateCredentialCookies(response, refreshResponse.getAuthenticationResult());
            reportResult(response, Constants.ResponseMessages.LOGGED_IN);
        } else {
            logger.warn("unexpected challenge when refreshing token: {}", refreshResponse.getChallengeName());
            reportResult(response, Constants.ResponseMessages.NOT_LOGGED_IN);
        }
    } catch (TooManyRequestsException ex) {
        logger.warn("caught TooManyRequestsException, delaying then retrying");
        ThreadUtil.sleepQuietly(250);
        attemptRefresh(refreshToken, response);
    } catch (AWSCognitoIdentityProviderException ex) {
        logger.debug("exception during token refresh: {}", ex.getMessage());
        reportResult(response, Constants.ResponseMessages.NOT_LOGGED_IN);
    }
}