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();
}
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);
}
Aggregations