Search in sources :

Example 1 with AuthClientException

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

the class AuthHttpClient method httpPost.

public String httpPost(final URL uri, final Map<String, String> headerParams, final Map<String, String> bodyParams) throws Exception {
    if (uri == null || bodyParams == null || bodyParams.size() < 1) {
        throw new AuthClientException("Invalid http request parameters");
    }
    final HttpsURLConnection httpsURLConnection = (HttpsURLConnection) uri.openConnection();
    DataOutputStream httpOutputStream = null;
    BufferedReader br = null;
    try {
        // Request header
        httpsURLConnection.setRequestMethod(ClientConstants.HTTP_REQUEST_TYPE_POST);
        httpsURLConnection.setDoOutput(true);
        for (Map.Entry<String, String> param : headerParams.entrySet()) {
            httpsURLConnection.addRequestProperty(param.getKey(), param.getValue());
        }
        // Request body
        StringBuilder reqBuilder = new StringBuilder();
        int index = bodyParams.size();
        for (Map.Entry<String, String> param : bodyParams.entrySet()) {
            reqBuilder.append(URLEncoder.encode(param.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(param.getValue(), "UTF-8"));
            if (index-- > 1) {
                reqBuilder.append('&');
            }
        }
        String requestBody = reqBuilder.toString();
        httpOutputStream = new DataOutputStream(httpsURLConnection.getOutputStream());
        httpOutputStream.writeBytes(requestBody);
        httpOutputStream.flush();
        // Parse response
        Map<String, List<String>> respHeaders = httpsURLConnection.getHeaderFields();
        int responseCode = httpsURLConnection.getResponseCode();
        if (responseCode >= HttpURLConnection.HTTP_OK && responseCode < HttpURLConnection.HTTP_INTERNAL_ERROR) {
            // Return response from the http call
            InputStream httpDataStream;
            if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
                httpDataStream = httpsURLConnection.getInputStream();
            } else {
                httpDataStream = httpsURLConnection.getErrorStream();
            }
            br = new BufferedReader(new InputStreamReader(httpDataStream));
            String line = "";
            StringBuilder responseOutput = new StringBuilder();
            while ((line = br.readLine()) != null) {
                responseOutput.append(line);
            }
            return responseOutput.toString();
        } else {
            // Throw a Cognito Client Exception
            throw new AuthServiceException(httpsURLConnection.getResponseMessage());
        }
    } catch (final Exception e) {
        throw e;
    } finally {
        if (httpOutputStream != null) {
            httpOutputStream.close();
        }
        if (br != null) {
            br.close();
        }
    }
}
Also used : AuthServiceException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException) InputStreamReader(java.io.InputStreamReader) DataOutputStream(java.io.DataOutputStream) InputStream(java.io.InputStream) AuthServiceException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException) AuthClientException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException) AuthClientException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException) BufferedReader(java.io.BufferedReader) List(java.util.List) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 2 with AuthClientException

use of com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException 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 3 with AuthClientException

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

the class OAuth2Constants method httpPost.

public static String httpPost(final URL uri, final Map<String, String> headerParams, final Map<String, String> bodyParams, final String userAgentOverride) throws Exception {
    if (uri == null || bodyParams == null || bodyParams.size() < 1) {
        throw new AuthClientException("Invalid http request parameters");
    }
    final HttpsURLConnection httpsURLConnection = (HttpsURLConnection) uri.openConnection();
    DataOutputStream httpOutputStream = null;
    BufferedReader br = null;
    try {
        // Request header
        httpsURLConnection.setRequestMethod(ClientConstants.HTTP_REQUEST_TYPE_POST);
        httpsURLConnection.setDoOutput(true);
        for (Map.Entry<String, String> param : headerParams.entrySet()) {
            httpsURLConnection.addRequestProperty(param.getKey(), param.getValue());
        }
        httpsURLConnection.addRequestProperty("x-amz-user-agent", userAgentOverride != null ? userAgentOverride : AWSMobileClient.DEFAULT_USER_AGENT);
        httpsURLConnection.setRequestProperty("User-Agent", userAgentOverride != null ? userAgentOverride : httpsURLConnection.getRequestProperty("User-Agent") + " " + AWSMobileClient.DEFAULT_USER_AGENT);
        // Request body
        StringBuilder reqBuilder = new StringBuilder();
        int index = bodyParams.size();
        for (Map.Entry<String, String> param : bodyParams.entrySet()) {
            reqBuilder.append(URLEncoder.encode(param.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(param.getValue(), "UTF-8"));
            if (index-- > 1) {
                reqBuilder.append('&');
            }
        }
        String requestBody = reqBuilder.toString();
        httpOutputStream = new DataOutputStream(httpsURLConnection.getOutputStream());
        httpOutputStream.writeBytes(requestBody);
        httpOutputStream.flush();
        // Parse response
        Map<String, List<String>> respHeaders = httpsURLConnection.getHeaderFields();
        int responseCode = httpsURLConnection.getResponseCode();
        if (responseCode >= HttpURLConnection.HTTP_OK && responseCode < HttpURLConnection.HTTP_INTERNAL_ERROR) {
            // Return response from the http call
            InputStream httpDataStream;
            if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
                httpDataStream = httpsURLConnection.getInputStream();
            } else {
                httpDataStream = httpsURLConnection.getErrorStream();
            }
            br = new BufferedReader(new InputStreamReader(httpDataStream));
            String line;
            StringBuilder responseOutput = new StringBuilder();
            while ((line = br.readLine()) != null) {
                responseOutput.append(line);
            }
            return responseOutput.toString();
        } else {
            // Throw a Cognito Client Exception
            throw new AuthServiceException(httpsURLConnection.getResponseMessage());
        }
    } catch (final Exception e) {
        throw e;
    } finally {
        if (httpOutputStream != null) {
            httpOutputStream.close();
        }
        if (br != null) {
            br.close();
        }
    }
}
Also used : AuthServiceException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException) InputStreamReader(java.io.InputStreamReader) DataOutputStream(java.io.DataOutputStream) InputStream(java.io.InputStream) AuthServiceException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException) AuthClientException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException) JSONException(org.json.JSONException) AuthClientException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException) BufferedReader(java.io.BufferedReader) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

AuthClientException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthClientException)3 AuthServiceException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException)3 BufferedReader (java.io.BufferedReader)2 DataOutputStream (java.io.DataOutputStream)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 List (java.util.List)2 Map (java.util.Map)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 AuthUserSession (com.amazonaws.mobileconnectors.cognitoauth.AuthUserSession)1 AuthInvalidGrantException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidGrantException)1 AuthInvalidParameterException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthInvalidParameterException)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 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1