Skip to content

Making possible to setup the response type in AuthorizationFlow Builder #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 63 additions & 27 deletions library/src/main/java/com/wuman/android/auth/AuthorizationFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +63,7 @@
* {@link #createAndStoreCredential(ImplicitResponseUrl, String)} to store and
* obtain a credential for accessing protected resources.
* </p>
*
*
* @author David Wu
*/
public class AuthorizationFlow extends AuthorizationCodeFlow {
Expand All @@ -74,6 +76,9 @@ public class AuthorizationFlow extends AuthorizationCodeFlow {
/** Temporary token request URL */
private String temporaryTokenRequestUrl;

/** Collection of response types. */
private Collection<String> responseTypes = Lists.newArrayList();

/**
* Listener for a created credential after a successful token response in
* {@link AuthorizationFlow#createAndStoreCredential(OAuthCredentialsResponse, String)}
Expand All @@ -94,7 +99,7 @@ public interface CredentialCreatedListener extends
* Typical use is to parse additional fields from the credential
* created, such as an ID token.
* </p>
*
*
* @param credential created credential
* @param implicitResponse successful implicit response URL
*/
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -152,7 +158,7 @@ public OAuthHmacCredential load10aCredential(String userId) throws IOException {
* Returns the response of a Request Token request as defined in <a
* href="http://oauth.net/core/1.0a/#auth_step1">Obtaining an Unauthorized
* Request Token</a>.
*
*
* @param redirectUri the {@code oauth_callback} as defined in <a
* href="http://oauth.net/core/1.0a/#rfc.section.6.1.1">Consumer
* Obtains a Request Token</a>
Expand All @@ -178,7 +184,7 @@ public OAuthCredentialsResponse new10aTemporaryTokenRequest(String redirectUri)
* defined in <a
* href="http://oauth.net/core/1.0a/#rfc.section.6.2.1">Consumer Directs the
* User to the Service Provider</a>.
*
*
* @param temporaryToken
* @return
*/
Expand All @@ -194,13 +200,13 @@ public OAuthAuthorizeTemporaryTokenUrl new10aAuthorizationUrl(String temporaryTo
* code. This step is defined in <a
* href="http://oauth.net/core/1.0a/#auth_step3">Obtaining an Access
* Token</a>.
*
*
* @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();
Expand Down Expand Up @@ -246,10 +252,10 @@ public void initialize(HttpRequest request) throws IOException {
* {@link #getAuthorizationServerEncodedUrl()}, {@link #getClientId()}, and
* {@link #getScopes()}. Sample usage:
* </p>
*
*
* <pre>
* private AuthorizationFlow flow;
*
*
* public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
* String url = flow.newExplicitAuthorizationUrl().setState(&quot;xyz&quot;)
* .setRedirectUri(&quot;https://client.example.com/rd&quot;).build();
Expand All @@ -270,10 +276,10 @@ public AuthorizationCodeRequestUrl newExplicitAuthorizationUrl() {
* {@link #getAuthorizationServerEncodedUrl()}, {@link #getClientId()}, and
* {@link #getScopes()}. Sample usage:
* </p>
*
*
* <pre>
* private AuthorizationFlow flow;
*
*
* public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
* String url = flow.newImplicitAuthorizationUrl().setState(&quot;xyz&quot;)
* .setRedirectUri(&quot;https://client.example.com/rd&quot;).build();
Expand All @@ -282,22 +288,27 @@ public AuthorizationCodeRequestUrl newExplicitAuthorizationUrl() {
* </pre>
*/
public BrowserClientRequestUrl newImplicitAuthorizationUrl() {
return new BrowserClientRequestUrl(getAuthorizationServerEncodedUrl(), getClientId())
BrowserClientRequestUrl browserClientRequestUrl = new BrowserClientRequestUrl(getAuthorizationServerEncodedUrl(), getClientId())
.setScopes(getScopes());
Collection<String> 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
* @return newly created credential
* @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);
Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -387,6 +398,14 @@ private Credential newCredential(String userId) {
return builder.build();
}


/**
* Returns the a collection of response types.
*/
public final Collection<String> getResponseTypes() {
return this.responseTypes;
}

/**
* Authorization flow builder.
* <p>
Expand All @@ -402,6 +421,9 @@ public static class Builder extends
/** Temporary token request URL */
String temporaryTokenRequestUrl;

/** Collection of response types. */
Collection<String> responseTypes = Lists.newArrayList();

/**
* @param method method of presenting the access token to the resource
* server (for example
Expand All @@ -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,
Expand All @@ -442,7 +464,7 @@ public AuthorizationFlow build() {

/**
* Sets the temporary token request URL.
*
*
* @param temporaryTokenRequestUrl
* @return
*/
Expand All @@ -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<String> getResponseTypes() {
return responseTypes;
}

@Override
public Builder setMethod(AccessMethod method) {
return (Builder) super.setMethod(method);
Expand Down Expand Up @@ -530,6 +561,11 @@ public Builder setScopes(Collection<String> scopes) {
return (Builder) super.setScopes(scopes);
}

public Builder setResponseTypes(Collection<String> responseTypes) {
this.responseTypes = Preconditions.checkNotNull(responseTypes);
return this;
}

/**
* Sets the credential created listener or {@code null} for none. *
* <p>
Expand Down
21 changes: 19 additions & 2 deletions samples/src/main/java/com/wuman/oauth/samples/OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,19 @@ public static OAuth newInstance(Context context,
final String redirectUri,
List<String> 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<String> scopes,
String temporaryTokenRequestUrl) {
return newInstance(context, fragmentManager, client,
authorizationRequestUrl, tokenServerUrl, redirectUri, scopes, temporaryTokenRequestUrl, null);
}

public static OAuth newInstance(Context context,
Expand All @@ -51,7 +63,8 @@ public static OAuth newInstance(Context context,
String tokenServerUrl,
final String redirectUri,
List<String> scopes,
String temporaryTokenRequestUrl) {
String temporaryTokenRequestUrl,
List<String> responseTypes) {
Preconditions.checkNotNull(client.getClientId());
boolean fullScreen = context.getSharedPreferences("Preference", 0)
.getBoolean(SamplesActivity.KEY_AUTH_MODE, false);
Expand All @@ -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);
Expand Down