Search in sources :

Example 1 with OAuth2Tokens

use of com.amazonaws.mobile.client.internal.oauth2.OAuth2Tokens in project aws-sdk-android by aws-amplify.

the class OAuth2Utils method _showSignInOAuth2UI.

private Runnable _showSignInOAuth2UI(final Activity callingActivity, final SignInUIOptions signInUIOptions, final Callback<UserStateDetails> callback) {
    return new Runnable() {

        @Override
        public void run() {
            final HostedUIOptions hostedUIOptions = signInUIOptions.getHostedUIOptions();
            // Reset settings to JSON
            JSONObject hostedUIJSON = getHostedUIJSONFromJSON();
            if (hostedUIJSON == null) {
                callback.onError(new Exception("Could not create OAuth configuration object"));
            }
            if (hostedUIOptions.getFederationEnabled() != null) {
                mStore.set(FEDERATION_ENABLED_KEY, hostedUIOptions.getFederationEnabled() ? "true" : "false");
            } else {
                mStore.set(FEDERATION_ENABLED_KEY, "true");
            }
            mStore.set(SIGN_IN_MODE, SignInMode.OAUTH2.toString());
            if (isFederationEnabled() && hostedUIOptions.getFederationProviderName() == null) {
                throw new IllegalArgumentException("OAuth flow requires a federation provider name if federation is enabled.");
            }
            if (hostedUIOptions.getSignOutQueryParameters() != null) {
                try {
                    JSONObject signOutParams = new JSONObject();
                    for (Map.Entry<String, String> e : hostedUIOptions.getSignOutQueryParameters().entrySet()) {
                        signOutParams.put(e.getKey(), e.getValue());
                    }
                    hostedUIJSON.put("SignOutQueryParameters", signOutParams);
                } catch (JSONException e1) {
                    callback.onError(new Exception("Failed to construct sign-out query parameters", e1));
                    return;
                }
            }
            if (hostedUIOptions.getTokenQueryParameters() != null) {
                try {
                    JSONObject tokenParams = new JSONObject();
                    for (Map.Entry<String, String> e : hostedUIOptions.getTokenQueryParameters().entrySet()) {
                        tokenParams.put(e.getKey(), e.getValue());
                    }
                    hostedUIJSON.put("TokenQueryParameters", tokenParams);
                } catch (JSONException e1) {
                    callback.onError(new Exception("Failed to construct token query parameters", e1));
                    return;
                }
            }
            mStore.set(HOSTED_UI_KEY, hostedUIJSON.toString());
            Uri.Builder authorizeUriBuilder;
            try {
                authorizeUriBuilder = Uri.parse(hostedUIJSON.getString("SignInURI")).buildUpon();
                if (hostedUIOptions.getSignInQueryParameters() != null) {
                    for (Map.Entry<String, String> e : hostedUIOptions.getSignInQueryParameters().entrySet()) {
                        authorizeUriBuilder.appendQueryParameter(e.getKey(), e.getValue());
                    }
                }
                authorizeUriBuilder.appendQueryParameter("redirect_uri", hostedUIJSON.getString("SignInRedirectURI"));
                authorizeUriBuilder.appendQueryParameter("scopes", hostedUIJSON.getJSONArray("Scopes").join(" "));
                authorizeUriBuilder.appendQueryParameter("client_id", hostedUIJSON.getString("AppClientId"));
            } catch (Exception e) {
                throw new RuntimeException("Failed to construct authorization url for OAuth", e);
            }
            Uri.Builder tokensUriBuilder;
            final Map<String, String> tokensBody = new HashMap<String, String>();
            try {
                tokensUriBuilder = Uri.parse(hostedUIJSON.getString("TokenURI")).buildUpon();
                if (hostedUIOptions.getTokenQueryParameters() != null) {
                    for (Map.Entry<String, String> e : hostedUIOptions.getTokenQueryParameters().entrySet()) {
                        tokensUriBuilder.appendQueryParameter(e.getKey(), e.getValue());
                    }
                }
                tokensBody.put("client_id", hostedUIJSON.getString("AppClientId"));
                tokensBody.put("redirect_uri", hostedUIJSON.getString("SignInRedirectURI"));
            } catch (Exception e) {
                throw new RuntimeException("Failed to construct tokens url for OAuth", e);
            }
            final Uri tokensUri = tokensUriBuilder.build();
            mOAuth2Client.authorize(authorizeUriBuilder.build(), new Callback<AuthorizeResponse>() {

                @Override
                public void onResult(AuthorizeResponse result) {
                    Log.i(TAG, "onResult: OAuth2 callback occurred, exchanging code for token");
                    mOAuth2Client.requestTokens(tokensUri, new HashMap<String, String>(), tokensBody, result.getCode(), new Callback<OAuth2Tokens>() {

                        @Override
                        public void onResult(OAuth2Tokens result) {
                            if (isFederationEnabled()) {
                                federatedSignInWithoutAssigningState(hostedUIOptions.getFederationProviderName(), // TODO verify id token is correct, this would mean OAuth support requires scope openid
                                result.getIdToken(), new Callback<UserStateDetails>() {

                                    @Override
                                    public void onResult(UserStateDetails result) {
                                        final UserStateDetails userStateDetails = getUserStateDetails(false);
                                        callback.onResult(userStateDetails);
                                        setUserState(userStateDetails);
                                    }

                                    @Override
                                    public void onError(Exception e) {
                                        final UserStateDetails userStateDetails = getUserStateDetails(false);
                                        callback.onResult(userStateDetails);
                                        setUserState(userStateDetails);
                                    }
                                });
                            } else {
                                final UserStateDetails userStateDetails = getUserStateDetails(false);
                                callback.onResult(userStateDetails);
                                setUserState(userStateDetails);
                            }
                        }

                        @Override
                        public void onError(Exception e) {
                            callback.onError(e);
                        }
                    });
                }

                @Override
                public void onError(Exception e) {
                    callback.onError(e);
                }
            });
        }
    };
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Uri(android.net.Uri) JSONException(org.json.JSONException) InvalidUserPoolConfigurationException(com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException) AmazonClientException(com.amazonaws.AmazonClientException) NotAuthorizedException(com.amazonaws.services.cognitoidentity.model.NotAuthorizedException) AuthorizeResponse(com.amazonaws.mobile.client.internal.oauth2.AuthorizeResponse) CustomTabsCallback(androidx.browser.customtabs.CustomTabsCallback) InternalCallback(com.amazonaws.mobile.client.internal.InternalCallback) JSONObject(org.json.JSONObject) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) OAuth2Tokens(com.amazonaws.mobile.client.internal.oauth2.OAuth2Tokens) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Aggregations

Uri (android.net.Uri)1 CustomTabsCallback (androidx.browser.customtabs.CustomTabsCallback)1 AmazonClientException (com.amazonaws.AmazonClientException)1 InternalCallback (com.amazonaws.mobile.client.internal.InternalCallback)1 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)1 AuthorizeResponse (com.amazonaws.mobile.client.internal.oauth2.AuthorizeResponse)1 OAuth2Tokens (com.amazonaws.mobile.client.internal.oauth2.OAuth2Tokens)1 NotAuthorizedException (com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)1 InvalidUserPoolConfigurationException (com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1