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