From ed4555286aad9519673eacb8d75e4737564c5313 Mon Sep 17 00:00:00 2001 From: majimene Date: Thu, 24 Nov 2016 10:48:29 +0100 Subject: [PATCH] Making possible to setup the response type in AuthorizationFlow Builder --- .../wuman/android/auth/AuthorizationFlow.java | 90 +++++++++++++------ .../java/com/wuman/oauth/samples/OAuth.java | 21 ++++- 2 files changed, 82 insertions(+), 29 deletions(-) diff --git a/library/src/main/java/com/wuman/android/auth/AuthorizationFlow.java b/library/src/main/java/com/wuman/android/auth/AuthorizationFlow.java index c3da704..efcabb4 100644 --- a/library/src/main/java/com/wuman/android/auth/AuthorizationFlow.java +++ b/library/src/main/java/com/wuman/android/auth/AuthorizationFlow.java @@ -27,6 +27,8 @@ import com.google.api.client.json.JsonFactory; import com.google.api.client.util.Beta; import com.google.api.client.util.Clock; +import com.google.api.client.util.Lists; +import com.google.api.client.util.Preconditions; import com.wuman.android.auth.oauth.OAuthHmacCredential; import com.wuman.android.auth.oauth2.explicit.LenientAuthorizationCodeTokenRequest; import com.wuman.android.auth.oauth2.implicit.ImplicitResponseUrl; @@ -61,7 +63,7 @@ * {@link #createAndStoreCredential(ImplicitResponseUrl, String)} to store and * obtain a credential for accessing protected resources. *

- * + * * @author David Wu */ public class AuthorizationFlow extends AuthorizationCodeFlow { @@ -74,6 +76,9 @@ public class AuthorizationFlow extends AuthorizationCodeFlow { /** Temporary token request URL */ private String temporaryTokenRequestUrl; + /** Collection of response types. */ + private Collection responseTypes = Lists.newArrayList(); + /** * Listener for a created credential after a successful token response in * {@link AuthorizationFlow#createAndStoreCredential(OAuthCredentialsResponse, String)} @@ -94,7 +99,7 @@ public interface CredentialCreatedListener extends * Typical use is to parse additional fields from the credential * created, such as an ID token. *

- * + * * @param credential created credential * @param implicitResponse successful implicit response URL */ @@ -104,7 +109,7 @@ void onCredentialCreated(Credential credential, ImplicitResponseUrl implicitResp /** * Notifies of a created credential after a successful token response in * {@link AuthorizationFlow#createAndStoreCredential(OAuthCredentialsResponse, String)} - * + * * @param credential * @param oauth10aResponse * @throws IOException @@ -117,11 +122,12 @@ void onCredentialCreated(Credential credential, OAuthCredentialsResponse oauth10 super(builder); credentialCreatedListener = builder.getGeneralCredentialCreatedListener(); temporaryTokenRequestUrl = builder.getTemporaryTokenRequestUrl(); + responseTypes = builder.getResponseTypes(); } /** * Returns the Request Token URL in OAuth 1.0a. - * + * * @return */ public final String getTemporaryTokenRequestUrl() { @@ -131,7 +137,7 @@ public final String getTemporaryTokenRequestUrl() { /** * Loads the OAuth 1.0a credential of the given user ID from the credential * store. - * + * * @param userId user ID or {@code null} if not using a persisted credential * store * @return OAuth 1.0a credential found in the credential store of the given @@ -152,7 +158,7 @@ public OAuthHmacCredential load10aCredential(String userId) throws IOException { * Returns the response of a Request Token request as defined in Obtaining an Unauthorized * Request Token. - * + * * @param redirectUri the {@code oauth_callback} as defined in Consumer * Obtains a Request Token @@ -178,7 +184,7 @@ public OAuthCredentialsResponse new10aTemporaryTokenRequest(String redirectUri) * defined in Consumer Directs the * User to the Service Provider. - * + * * @param temporaryToken * @return */ @@ -194,13 +200,13 @@ public OAuthAuthorizeTemporaryTokenUrl new10aAuthorizationUrl(String temporaryTo * code. This step is defined in Obtaining an Access * Token. - * + * * @param temporaryCredentials * @param verifierCode * @return */ public OAuthGetAccessToken new10aTokenRequest(OAuthCredentialsResponse temporaryCredentials, - String verifierCode) { + String verifierCode) { OAuthGetAccessToken request = new OAuthGetAccessToken(getTokenServerEncodedUrl()); request.temporaryToken = temporaryCredentials.token; request.transport = getTransport(); @@ -246,10 +252,10 @@ public void initialize(HttpRequest request) throws IOException { * {@link #getAuthorizationServerEncodedUrl()}, {@link #getClientId()}, and * {@link #getScopes()}. Sample usage: *

- * + * *
      * private AuthorizationFlow flow;
-     * 
+     *
      * public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
      *     String url = flow.newExplicitAuthorizationUrl().setState("xyz")
      *             .setRedirectUri("https://client.example.com/rd").build();
@@ -270,10 +276,10 @@ public AuthorizationCodeRequestUrl newExplicitAuthorizationUrl() {
      * {@link #getAuthorizationServerEncodedUrl()}, {@link #getClientId()}, and
      * {@link #getScopes()}. Sample usage:
      * 

- * + * *
      * private AuthorizationFlow flow;
-     * 
+     *
      * public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
      *     String url = flow.newImplicitAuthorizationUrl().setState("xyz")
      *             .setRedirectUri("https://client.example.com/rd").build();
@@ -282,14 +288,19 @@ public AuthorizationCodeRequestUrl newExplicitAuthorizationUrl() {
      * 
*/ public BrowserClientRequestUrl newImplicitAuthorizationUrl() { - return new BrowserClientRequestUrl(getAuthorizationServerEncodedUrl(), getClientId()) + BrowserClientRequestUrl browserClientRequestUrl = new BrowserClientRequestUrl(getAuthorizationServerEncodedUrl(), getClientId()) .setScopes(getScopes()); + Collection responseTypes = getResponseTypes(); + if (responseTypes != null && !responseTypes.isEmpty()) { + browserClientRequestUrl.setResponseTypes(getResponseTypes()); + } + return browserClientRequestUrl; } /** * Creates a new credential for the given user ID based on the given token * response and store in the credential store. - * + * * @param response OAuth 1.0a authorization token response * @param userId user ID or {@code null} if not using a persisted credential * store @@ -297,7 +308,7 @@ public BrowserClientRequestUrl newImplicitAuthorizationUrl() { * @throws IOException */ public OAuthHmacCredential createAndStoreCredential(OAuthCredentialsResponse response, - String userId) throws IOException { + String userId) throws IOException { OAuthHmacCredential credential = new10aCredential(userId) .setAccessToken(response.token) .setTokenSharedSecret(response.tokenSecret); @@ -314,7 +325,7 @@ public OAuthHmacCredential createAndStoreCredential(OAuthCredentialsResponse res /** * Creates a new credential for the given user ID based on the given token * response and store in the credential store. - * + * * @param response implicit authorization token response * @param userId user ID or {@code null} if not using a persisted credential * store @@ -338,7 +349,7 @@ public Credential createAndStoreCredential(ImplicitResponseUrl implicitResponse, /** * Returns a new OAuth 1.0a credential instance based on the given user ID. - * + * * @param userId user ID or {@code null} if not using a persisted credential * store */ @@ -365,7 +376,7 @@ private OAuthHmacCredential new10aCredential(String userId) { /** * Returns a new OAuth 2.0 credential instance based on the given user ID. - * + * * @param userId user ID or {@code null} if not using a persisted credential * store */ @@ -387,6 +398,14 @@ private Credential newCredential(String userId) { return builder.build(); } + + /** + * Returns the a collection of response types. + */ + public final Collection getResponseTypes() { + return this.responseTypes; + } + /** * Authorization flow builder. *

@@ -402,6 +421,9 @@ public static class Builder extends /** Temporary token request URL */ String temporaryTokenRequestUrl; + /** Collection of response types. */ + Collection responseTypes = Lists.newArrayList(); + /** * @param method method of presenting the access token to the resource * server (for example @@ -417,12 +439,12 @@ public static class Builder extends * @param authorizationServerEncodedUrl authorization server encoded URL */ public Builder(AccessMethod method, - HttpTransport transport, - JsonFactory jsonFactory, - GenericUrl tokenServerUrl, - HttpExecuteInterceptor clientAuthentication, - String clientId, - String authorizationServerEncodedUrl) { + HttpTransport transport, + JsonFactory jsonFactory, + GenericUrl tokenServerUrl, + HttpExecuteInterceptor clientAuthentication, + String clientId, + String authorizationServerEncodedUrl) { super(method, transport, jsonFactory, @@ -442,7 +464,7 @@ public AuthorizationFlow build() { /** * Sets the temporary token request URL. - * + * * @param temporaryTokenRequestUrl * @return */ @@ -453,13 +475,22 @@ public Builder setTemporaryTokenRequestUrl(String temporaryTokenRequestUrl) { /** * Returns the temporary token request URL. - * + * * @return */ public String getTemporaryTokenRequestUrl() { return temporaryTokenRequestUrl; } + /** + * Returns the response types. + * + * @return + */ + public Collection getResponseTypes() { + return responseTypes; + } + @Override public Builder setMethod(AccessMethod method) { return (Builder) super.setMethod(method); @@ -530,6 +561,11 @@ public Builder setScopes(Collection scopes) { return (Builder) super.setScopes(scopes); } + public Builder setResponseTypes(Collection responseTypes) { + this.responseTypes = Preconditions.checkNotNull(responseTypes); + return this; + } + /** * Sets the credential created listener or {@code null} for none. * *

diff --git a/samples/src/main/java/com/wuman/oauth/samples/OAuth.java b/samples/src/main/java/com/wuman/oauth/samples/OAuth.java index fb4f152..91600dd 100644 --- a/samples/src/main/java/com/wuman/oauth/samples/OAuth.java +++ b/samples/src/main/java/com/wuman/oauth/samples/OAuth.java @@ -41,7 +41,19 @@ public static OAuth newInstance(Context context, final String redirectUri, List scopes) { return newInstance(context, fragmentManager, client, - authorizationRequestUrl, tokenServerUrl, redirectUri, scopes, null); + authorizationRequestUrl, tokenServerUrl, redirectUri, scopes, null, null); + } + + public static OAuth newInstance(Context context, + FragmentManager fragmentManager, + ClientParametersAuthentication client, + String authorizationRequestUrl, + String tokenServerUrl, + final String redirectUri, + List scopes, + String temporaryTokenRequestUrl) { + return newInstance(context, fragmentManager, client, + authorizationRequestUrl, tokenServerUrl, redirectUri, scopes, temporaryTokenRequestUrl, null); } public static OAuth newInstance(Context context, @@ -51,7 +63,8 @@ public static OAuth newInstance(Context context, String tokenServerUrl, final String redirectUri, List scopes, - String temporaryTokenRequestUrl) { + String temporaryTokenRequestUrl, + List responseTypes) { Preconditions.checkNotNull(client.getClientId()); boolean fullScreen = context.getSharedPreferences("Preference", 0) .getBoolean(SamplesActivity.KEY_AUTH_MODE, false); @@ -70,6 +83,10 @@ public static OAuth newInstance(Context context, authorizationRequestUrl) .setScopes(scopes) .setCredentialStore(credentialStore); + // set response types + if (responseTypes != null) { + flowBuilder.setResponseTypes(responseTypes); + } // set temporary token request url for 1.0a flow if applicable if (!TextUtils.isEmpty(temporaryTokenRequestUrl)) { flowBuilder.setTemporaryTokenRequestUrl(temporaryTokenRequestUrl);