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