Search in sources :

Example 1 with AuthInvalidParameterException

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

the class JWTParser method getPayload.

/**
 * Returns payload of a JWT as a JSON object.
 *
 * @param JWT       REQUIRED: valid JSON Web Token as String.
 * @return payload as a JSONObject.
 */
public static JSONObject getPayload(String JWT) {
    try {
        validateJWT(JWT);
        String payload = JWT.split("\\.")[PAYLOAD];
        byte[] sectionDecoded = Base64.decode(payload, Base64.URL_SAFE);
        String jwtSection = new String(sectionDecoded, "UTF-8");
        return new JSONObject(jwtSection);
    } catch (Exception e) {
        throw new AuthInvalidParameterException("error while parsing JSON", e);
    }
}
Also used : JSONObject(org.json.JSONObject) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) JSONException(org.json.JSONException) InvalidParameterException(java.security.InvalidParameterException) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with AuthInvalidParameterException

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

the class JWTParser method getSignature.

/**
 * Returns signature of a JWT as a String.
 *
 * @param JWT       REQUIRED: valid JSON Web Token as String.
 * @return signature as a String.
 */
public static String getSignature(String JWT) {
    try {
        validateJWT(JWT);
        byte[] sectionDecoded = Base64.decode(JWT.split("\\.")[SIGNATURE], Base64.URL_SAFE);
        return new String(sectionDecoded, "UTF-8");
    } catch (Exception e) {
        throw new AuthInvalidParameterException("error while parsing JSON", e);
    }
}
Also used : AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) JSONException(org.json.JSONException) InvalidParameterException(java.security.InvalidParameterException) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with AuthInvalidParameterException

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

the class JWTParser method getHeader.

/**
 * Returns header for a JWT as a JSON object.
 *
 * @param JWT       REQUIRED: valid JSON Web Token as String.
 * @return header as a JSONObject.
 */
public static JSONObject getHeader(String JWT) {
    try {
        validateJWT(JWT);
        byte[] sectionDecoded = Base64.decode(JWT.split("\\.")[HEADER], Base64.URL_SAFE);
        String jwtSection = new String(sectionDecoded, "UTF-8");
        return new JSONObject(jwtSection);
    } catch (Exception e) {
        throw new AuthInvalidParameterException("error while parsing JSON", e);
    }
}
Also used : JSONObject(org.json.JSONObject) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) JSONException(org.json.JSONException) InvalidParameterException(java.security.InvalidParameterException) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with AuthInvalidParameterException

use of com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException 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)

Example 5 with AuthInvalidParameterException

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

the class JWTParser method getClaim.

/**
 * Returns a claim, from the {@code JWT}s' payload, as a String.
 *
 * @param JWT       REQUIRED: valid JSON Web Token as String.
 * @param claim     REQUIRED: claim name as String.
 * @return  claim from the JWT as a String.
 */
public static String getClaim(String JWT, String claim) {
    try {
        JSONObject payload = getPayload(JWT);
        Object claimValue = payload.get(claim);
        if (claimValue != null) {
            return claimValue.toString();
        }
    } catch (Exception e) {
        throw new AuthInvalidParameterException("error while parsing JSON", e);
    }
    return null;
}
Also used : JSONObject(org.json.JSONObject) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) InvalidParameterException(java.security.InvalidParameterException) AuthInvalidParameterException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

AuthInvalidParameterException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 InvalidParameterException (java.security.InvalidParameterException)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 AuthUserSession (com.amazonaws.mobileconnectors.cognitoauth.AuthUserSession)1 AuthClientException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException)1 AuthInvalidGrantException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException)1 AuthServiceException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException)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