|
| 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