Skip to content

Commit 926ad45

Browse files
committed
Add default config for common OAuth2 Providers
Fixes gh-4597
1 parent eca2b67 commit 926ad45

File tree

2 files changed

+258
-0
lines changed

2 files changed

+258
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.config.oauth2.client;
17+
18+
import org.springframework.security.oauth2.client.registration.ClientRegistration;
19+
import org.springframework.security.oauth2.client.registration.ClientRegistration.Builder;
20+
import org.springframework.security.oauth2.core.AuthorizationGrantType;
21+
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
22+
import org.springframework.security.oauth2.oidc.core.IdTokenClaim;
23+
24+
/**
25+
* Common OAuth2 Providers that can be used to create
26+
* {@link org.springframework.security.oauth2.client.registration.ClientRegistration.Builder
27+
* builders} pre-configured with sensible defaults.
28+
*
29+
* @author Phillip Webb
30+
* @since 5.0
31+
*/
32+
public enum CommonOAuth2Provider {
33+
34+
GOOGLE {
35+
36+
@Override
37+
public Builder getBuilder(String registrationId) {
38+
ClientRegistration.Builder builder = getBuilder(registrationId,
39+
ClientAuthenticationMethod.BASIC, DEFAULT_REDIRECT_URL);
40+
builder.scope("openid", "profile", "email", "address", "phone");
41+
builder.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth");
42+
builder.tokenUri("https://www.googleapis.com/oauth2/v4/token");
43+
builder.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs");
44+
builder.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo");
45+
builder.userNameAttributeName(IdTokenClaim.SUB);
46+
builder.clientName("Google");
47+
return builder;
48+
}
49+
},
50+
51+
GITHUB {
52+
53+
@Override
54+
public Builder getBuilder(String registrationId) {
55+
ClientRegistration.Builder builder = getBuilder(registrationId,
56+
ClientAuthenticationMethod.BASIC, DEFAULT_REDIRECT_URL);
57+
builder.scope("user");
58+
builder.authorizationUri("https://github.com/login/oauth/authorize");
59+
builder.tokenUri("https://github.com/login/oauth/access_token");
60+
builder.userInfoUri("https://api.github.com/user");
61+
builder.userNameAttributeName("name");
62+
builder.clientName("GitHub");
63+
return builder;
64+
}
65+
},
66+
67+
FACEBOOK {
68+
69+
@Override
70+
public Builder getBuilder(String registrationId) {
71+
ClientRegistration.Builder builder = getBuilder(registrationId,
72+
ClientAuthenticationMethod.POST, DEFAULT_REDIRECT_URL);
73+
builder.scope("public_profile", "email");
74+
builder.authorizationUri("https://www.facebook.com/v2.8/dialog/oauth");
75+
builder.tokenUri("https://graph.facebook.com/v2.8/oauth/access_token");
76+
builder.userInfoUri("https://graph.facebook.com/me");
77+
builder.userNameAttributeName("name");
78+
builder.clientName("Facebook");
79+
return builder;
80+
}
81+
},
82+
83+
OKTA {
84+
85+
@Override
86+
public Builder getBuilder(String registrationId) {
87+
ClientRegistration.Builder builder = getBuilder(registrationId,
88+
ClientAuthenticationMethod.BASIC, DEFAULT_REDIRECT_URL);
89+
builder.scope("openid", "profile", "email", "address", "phone");
90+
builder.userNameAttributeName(IdTokenClaim.SUB);
91+
builder.clientName("Okta");
92+
return builder;
93+
}
94+
};
95+
96+
private static final String DEFAULT_REDIRECT_URL = "{scheme}://{serverName}:{serverPort}{contextPath}/oauth2/authorize/code/{registrationId}";
97+
98+
protected final ClientRegistration.Builder getBuilder(String registrationId,
99+
ClientAuthenticationMethod method, String redirectUri) {
100+
ClientRegistration.Builder builder = new ClientRegistration.Builder(registrationId);
101+
builder.clientAuthenticationMethod(method);
102+
builder.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE);
103+
builder.redirectUri(redirectUri);
104+
return builder;
105+
}
106+
107+
/**
108+
* Create a new
109+
* {@link org.springframework.security.oauth2.client.registration.ClientRegistration.Builder
110+
* ClientRegistration.Builder} pre-configured with provider defaults.
111+
* @param registrationId the registration-id used with the new builder
112+
* @return a builder instance
113+
*/
114+
public abstract ClientRegistration.Builder getBuilder(String registrationId);
115+
116+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.config.oauth2.client;
17+
18+
import org.junit.Test;
19+
import org.springframework.security.oauth2.client.registration.ClientRegistration;
20+
import org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails;
21+
import org.springframework.security.oauth2.core.AuthorizationGrantType;
22+
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
23+
import org.springframework.security.oauth2.oidc.core.IdTokenClaim;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Tests for {@link CommonOAuth2Provider}.
29+
*
30+
* @author Phillip Webb
31+
*/
32+
public class CommonOAuth2ProviderTests {
33+
34+
private static final String DEFAULT_REDIRECT_URL = "{scheme}://{serverName}:{serverPort}{contextPath}/oauth2/authorize/code/{registrationId}";
35+
36+
@Test
37+
public void getBuilderWhenGoogleShouldHaveGoogleSettings() throws Exception {
38+
ClientRegistration registration = build(CommonOAuth2Provider.GOOGLE);
39+
ProviderDetails providerDetails = registration.getProviderDetails();
40+
assertThat(providerDetails.getAuthorizationUri())
41+
.isEqualTo("https://accounts.google.com/o/oauth2/v2/auth");
42+
assertThat(providerDetails.getTokenUri())
43+
.isEqualTo("https://www.googleapis.com/oauth2/v4/token");
44+
assertThat(providerDetails.getUserInfoEndpoint().getUri())
45+
.isEqualTo("https://www.googleapis.com/oauth2/v3/userinfo");
46+
assertThat(providerDetails.getUserInfoEndpoint().getUserNameAttributeName())
47+
.isEqualTo(IdTokenClaim.SUB);
48+
assertThat(providerDetails.getJwkSetUri())
49+
.isEqualTo("https://www.googleapis.com/oauth2/v3/certs");
50+
assertThat(registration.getClientAuthenticationMethod())
51+
.isEqualTo(ClientAuthenticationMethod.BASIC);
52+
assertThat(registration.getAuthorizationGrantType())
53+
.isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
54+
assertThat(registration.getRedirectUri()).isEqualTo(DEFAULT_REDIRECT_URL);
55+
assertThat(registration.getScope()).containsOnly("openid", "profile", "email",
56+
"address", "phone");
57+
assertThat(registration.getClientName()).isEqualTo("Google");
58+
assertThat(registration.getRegistrationId()).isEqualTo("123");
59+
}
60+
61+
@Test
62+
public void getBuilderWhenGitHubShouldHaveGitHubSettings() throws Exception {
63+
ClientRegistration registration = build(CommonOAuth2Provider.GITHUB);
64+
ProviderDetails providerDetails = registration.getProviderDetails();
65+
assertThat(providerDetails.getAuthorizationUri())
66+
.isEqualTo("https://github.com/login/oauth/authorize");
67+
assertThat(providerDetails.getTokenUri())
68+
.isEqualTo("https://github.com/login/oauth/access_token");
69+
assertThat(providerDetails.getUserInfoEndpoint().getUri())
70+
.isEqualTo("https://api.github.com/user");
71+
assertThat(providerDetails.getUserInfoEndpoint().getUserNameAttributeName())
72+
.isEqualTo("name");
73+
assertThat(providerDetails.getJwkSetUri()).isNull();
74+
assertThat(registration.getClientAuthenticationMethod())
75+
.isEqualTo(ClientAuthenticationMethod.BASIC);
76+
assertThat(registration.getAuthorizationGrantType())
77+
.isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
78+
assertThat(registration.getRedirectUri()).isEqualTo(DEFAULT_REDIRECT_URL);
79+
assertThat(registration.getScope()).containsOnly("user");
80+
assertThat(registration.getClientName()).isEqualTo("GitHub");
81+
assertThat(registration.getRegistrationId()).isEqualTo("123");
82+
}
83+
84+
@Test
85+
public void getBuilderWhenFacebookShouldHaveFacebookSettings() throws Exception {
86+
ClientRegistration registration = build(CommonOAuth2Provider.FACEBOOK);
87+
ProviderDetails providerDetails = registration.getProviderDetails();
88+
assertThat(providerDetails.getAuthorizationUri())
89+
.isEqualTo("https://www.facebook.com/v2.8/dialog/oauth");
90+
assertThat(providerDetails.getTokenUri())
91+
.isEqualTo("https://graph.facebook.com/v2.8/oauth/access_token");
92+
assertThat(providerDetails.getUserInfoEndpoint().getUri())
93+
.isEqualTo("https://graph.facebook.com/me");
94+
assertThat(providerDetails.getUserInfoEndpoint().getUserNameAttributeName())
95+
.isEqualTo("name");
96+
assertThat(providerDetails.getJwkSetUri()).isNull();
97+
assertThat(registration.getClientAuthenticationMethod())
98+
.isEqualTo(ClientAuthenticationMethod.POST);
99+
assertThat(registration.getAuthorizationGrantType())
100+
.isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
101+
assertThat(registration.getRedirectUri()).isEqualTo(DEFAULT_REDIRECT_URL);
102+
assertThat(registration.getScope()).containsOnly("public_profile", "email");
103+
assertThat(registration.getClientName()).isEqualTo("Facebook");
104+
assertThat(registration.getRegistrationId()).isEqualTo("123");
105+
}
106+
107+
@Test
108+
public void getBuilderWhenOktaShouldHaveOktaSettings() throws Exception {
109+
ClientRegistration registration = builder(CommonOAuth2Provider.OKTA)
110+
.authorizationUri("http://example.com/auth")
111+
.tokenUri("http://example.com/token")
112+
.userInfoUri("http://example.com/info").build();
113+
ProviderDetails providerDetails = registration.getProviderDetails();
114+
assertThat(providerDetails.getAuthorizationUri())
115+
.isEqualTo("http://example.com/auth");
116+
assertThat(providerDetails.getTokenUri()).isEqualTo("http://example.com/token");
117+
assertThat(providerDetails.getUserInfoEndpoint().getUri()).isEqualTo("http://example.com/info");
118+
assertThat(providerDetails.getUserInfoEndpoint().getUserNameAttributeName())
119+
.isEqualTo(IdTokenClaim.SUB);
120+
assertThat(providerDetails.getJwkSetUri()).isNull();
121+
assertThat(registration.getClientAuthenticationMethod())
122+
.isEqualTo(ClientAuthenticationMethod.BASIC);
123+
assertThat(registration.getAuthorizationGrantType())
124+
.isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
125+
assertThat(registration.getRedirectUri()).isEqualTo(DEFAULT_REDIRECT_URL);
126+
assertThat(registration.getScope()).containsOnly("openid", "profile", "email",
127+
"address", "phone");
128+
assertThat(registration.getClientName()).isEqualTo("Okta");
129+
assertThat(registration.getRegistrationId()).isEqualTo("123");
130+
}
131+
132+
private ClientRegistration build(CommonOAuth2Provider provider) {
133+
return builder(provider).build();
134+
}
135+
136+
private ClientRegistration.Builder builder(CommonOAuth2Provider provider) {
137+
return provider.getBuilder("123")
138+
.clientId("abcd")
139+
.clientSecret("secret");
140+
}
141+
142+
}

0 commit comments

Comments
 (0)