Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit 5fb87c7

Browse files
authored
Merge pull request #1148 from stormpath/issue-1141-generic-oauth2-provider-support
Issue 1141 generic oauth2 provider support
2 parents f14593c + 995f5e6 commit 5fb87c7

File tree

32 files changed

+941
-8
lines changed

32 files changed

+941
-8
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.stormpath.sdk.provider;
2+
3+
4+
import com.stormpath.sdk.lang.Assert;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
* A value specifying how to present the accessToken credential when interacting with the authorizationEndpoint and/or
11+
* the tokenEndpoint.
12+
* Possible values are bearer (token as part of authorization header), access_token and oauth_token (both of which are
13+
* query parameters).
14+
*
15+
* Different OAuth2 providers support different ways to present the accessToken. Please read the provider's "OAuth2"
16+
* implementation documentation in detail to identify and pick the type that they support. This allows us to support
17+
* any current OAuth2 provider.
18+
* Additionally, we can support new types as they emerge by updating this enum.
19+
*
20+
* @since 1.3.0
21+
*/
22+
public enum AccessTokenType {
23+
24+
BEARER("bearer"),
25+
ACCESS_TOKEN_PARAMETER("access_token"),
26+
OAUTH_TOKEN_PARAMETER("oauth_token");
27+
28+
private static final Map<String, AccessTokenType> TOKEN_TYPE_MAP;
29+
30+
static {
31+
TOKEN_TYPE_MAP = new HashMap<>();
32+
33+
for (AccessTokenType accessTokenType : AccessTokenType.values()) {
34+
TOKEN_TYPE_MAP.put(accessTokenType.nameKey, accessTokenType);
35+
}
36+
}
37+
38+
private String nameKey;
39+
40+
AccessTokenType(String nameKey) {
41+
this.nameKey = nameKey;
42+
}
43+
44+
public static AccessTokenType fromNameKey(String nameKey) {
45+
46+
Assert.notNull(nameKey, "accessTokenType is required.");
47+
48+
AccessTokenType accessTokenType = TOKEN_TYPE_MAP.get(nameKey.toLowerCase());
49+
50+
Assert.notNull(accessTokenType, "Invalid accessTokenType : " + nameKey);
51+
52+
return accessTokenType;
53+
}
54+
55+
public String getNameKey() {
56+
return this.nameKey;
57+
}
58+
59+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link CreateProviderRequestBuilder} interface for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2CreateProviderRequestBuilder extends CreateProviderRequestBuilder<GenericOAuth2CreateProviderRequestBuilder> {
9+
10+
/**
11+
* Setter for the authorizationEndpoint for the OAuth2 provider.
12+
*
13+
* @param authorizationEndpoint the authorizationEndpoint for the OAuth2 provider.
14+
* @return the builder instance for method chaining.
15+
*/
16+
GenericOAuth2CreateProviderRequestBuilder setAuthorizationEndpoint(String authorizationEndpoint);
17+
18+
/**
19+
* Setter for the tokenEndpoint for the OAuth2 provider.
20+
*
21+
* @param tokenEndpoint the tokenEndpoint for the OAuth2 provider.
22+
* @return the builder instance for method chaining.
23+
*/
24+
GenericOAuth2CreateProviderRequestBuilder setTokenEndpoint(String tokenEndpoint);
25+
26+
/**
27+
* Setter for the resourceEndpoint for the OAuth2 provider.
28+
*
29+
* @param resourceEndpoint the resourceEndpoint for the OAuth2 provider.
30+
* @return the builder instance for method chaining.
31+
*/
32+
GenericOAuth2CreateProviderRequestBuilder setResourceEndpoint(String resourceEndpoint);
33+
34+
/**
35+
* Setter for the accessTokenType for the OAuth2 provider.
36+
*
37+
* @param accessTokenType the accessTokenType for the OAuth2 provider.
38+
* @return the builder instance for method chaining.
39+
*/
40+
GenericOAuth2CreateProviderRequestBuilder setAccessTokenType(AccessTokenType accessTokenType);
41+
42+
/**
43+
* Setter for the provider id of the OAuth2 provider(e.g. "amazon").
44+
*
45+
* @param providerId the provider id of the OAuth2 provider.
46+
* @return the builder instance for method chaining.
47+
*/
48+
GenericOAuth2CreateProviderRequestBuilder setClientId(String providerId);
49+
50+
GenericOAuth2CreateProviderRequestBuilder setProviderId(String providerId);
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link Provider} Resource for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2Provider extends OAuthProvider {
9+
10+
GenericOAuth2Provider setProviderId(String providerId);
11+
12+
String getAuthorizationEndpoint();
13+
14+
String getTokenEndpoint();
15+
16+
String getResourceEndpoint();
17+
18+
AccessTokenType getAccessType();
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link ProviderAccountRequestBuilder} interface for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2ProviderAccountRequestBuilder extends ProviderAccountRequestBuilder<GenericOAuth2ProviderAccountRequestBuilder> {
9+
10+
/**
11+
* Setter for the provider id of the OAuth2 Provider (as provided when creating the provider directory).
12+
*
13+
* @param providerId the provider id of the OAuth2 Provider
14+
* @return the builder instance for method chaining.
15+
*/
16+
GenericOAuth2ProviderAccountRequestBuilder setProviderId(String providerId);
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link ProviderData} Resource for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2ProviderData extends ProviderData {
9+
10+
/**
11+
* Getter for the OAuth2 provider's access token.
12+
*
13+
* @return the OAuth2 provider's access token.
14+
*/
15+
String getAccessToken();
16+
17+
GenericOAuth2ProviderData setProviderId(String providerId);
18+
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.stormpath.sdk.provider;
2+
3+
/**
4+
* {@link ProviderRequestFactory} interface for any Generic OAuth2 provider.
5+
*
6+
* @since 1.3.0
7+
*/
8+
public interface GenericOAuth2ProviderRequestFactory extends ProviderRequestFactory<GenericOAuth2ProviderAccountRequestBuilder, GenericOAuth2CreateProviderRequestBuilder> {
9+
}

api/src/main/java/com/stormpath/sdk/provider/OAuthProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ public interface OAuthProvider extends Provider {
5151
* @since 1.3.0
5252
*/
5353
UserInfoMappingRules getUserInfoMappingRules();
54+
55+
/**
56+
* Returns the Type of the Provider (e.g. "facebook" or "google", "oauth2").
57+
* @return the Type of the Provider.
58+
* @since 1.3.0
59+
*/
60+
String getProviderType();
5461
}

api/src/main/java/com/stormpath/sdk/provider/Providers.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,13 @@ private Providers() {
9393
*/
9494
public static final TwitterRequestFactory TWITTER = (TwitterRequestFactory) Classes.newInstance("com.stormpath.sdk.impl.provider.DefaultTwitterRequestFactory");
9595

96+
/**
97+
* Returns a new {@link GenericOAuth2ProviderRequestFactory} instance, used to construct Twitter requests, like Twitter Account creation and retrieval.
98+
*
99+
* @return a new {@link GenericOAuth2ProviderRequestFactory} instance, used to construct Twitter requests, like Twitter Account creation and retrieval.
100+
* @since 1.3.0
101+
*/
102+
public static final GenericOAuth2ProviderRequestFactory OAUTH2 = (GenericOAuth2ProviderRequestFactory) Classes.newInstance("com.stormpath.sdk.impl.provider.DefaultGenericOAuth2ProviderRequestFactory");
103+
96104
}
97105

extensions/httpclient/src/test/groovy/com/stormpath/sdk/client/DirectoryIT.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,30 @@ class DirectoryIT extends ClientIT {
444444

445445
}
446446

447+
/**
448+
* @since 1.3.0
449+
*/
450+
@Test(enabled = false) //TODO : enable this when the generic OAuth2 changes are there in prod (api.stormpath.com)
451+
void testCreateGenericOAuth2DirectoryWithUserInfoMappingRules() {
452+
Directory dir = client.instantiate(Directory)
453+
dir.name = uniquify("Java SDK: DirectoryIT.testCreateGenericOAuth2DirectoryRequest")
454+
455+
def request = Directories.newCreateRequestFor(dir)
456+
.forProvider(Providers.OAUTH2.builder().setProviderId("imgur")
457+
.setClientId("73i1dq2fko01s2")
458+
.setClientSecret("wJhXc81l63qEOc43")
459+
.setAccessTokenType(AccessTokenType.BEARER)
460+
.setAuthorizationEndpoint("https://api.imgur.com/oauth2/authorize")
461+
.setTokenEndpoint("https://api.imgur.com/oauth2/token")
462+
.setResourceEndpoint("https://api.imgur.com/oauth2/token")
463+
.setUserInfoMappingRules(buildSampleUserInfoMappingRules()).build()).build()
464+
dir = client.createDirectory(request);
465+
deleteOnTeardown(dir)
466+
assertNotNull dir.href
467+
assertUserInfoMappingRuleWasCreatedAndUpdate((AbstractOAuthProvider<TwitterProvider>) dir.provider)
468+
469+
}
470+
447471
void assertUserInfoMappingRuleWasCreatedAndUpdate(AbstractOAuthProvider provider) {
448472

449473
UserInfoMappingRules rules = provider.getUserInfoMappingRules()

extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/application/ApplicationIT.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,8 @@ class ApplicationIT extends ClientIT {
679679
} catch (com.stormpath.sdk.resource.ResourceException e) {
680680
assertEquals(e.getStatus(), 400)
681681
assertEquals(e.getCode(), 7200)
682-
assertTrue(e.getDeveloperMessage().contains("Stormpath was not able to complete the request to Google: this can be caused by either a bad Google Directory configuration, or the provided Account credentials are not valid."))
682+
assertTrue(e.getDeveloperMessage().contains("Stormpath was not able to complete the request to"))
683+
assertTrue(e.getDeveloperMessage().contains("Google") || e.getDeveloperMessage().contains("google"))
683684
}
684685
}
685686

0 commit comments

Comments
 (0)