Skip to content

Commit 5286aff

Browse files
committed
Fix tests for OAuth2 Authorization Server Metadata Endpoint
Closes spring-projectsgh-1419
1 parent b086233 commit 5286aff

File tree

4 files changed

+95
-95
lines changed

4 files changed

+95
-95
lines changed

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/OAuth2AuthorizationServerMetadataTests.java

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,43 +38,43 @@ public class OAuth2AuthorizationServerMetadataTests {
3838
// @formatter:off
3939
private final Builder minimalBuilder =
4040
OAuth2AuthorizationServerMetadata.builder()
41-
.issuer("https://example.com/issuer1")
42-
.authorizationEndpoint("https://example.com/issuer1/oauth2/authorize")
43-
.tokenEndpoint("https://example.com/issuer1/oauth2/token")
41+
.issuer("https://example.com")
42+
.authorizationEndpoint("https://example.com/oauth2/authorize")
43+
.tokenEndpoint("https://example.com/oauth2/token")
4444
.responseType("code");
4545
// @formatter:on
4646

4747
@Test
4848
public void buildWhenAllClaimsProvidedThenCreated() {
4949
OAuth2AuthorizationServerMetadata authorizationServerMetadata = OAuth2AuthorizationServerMetadata.builder()
50-
.issuer("https://example.com/issuer1")
51-
.authorizationEndpoint("https://example.com/issuer1/oauth2/authorize")
52-
.tokenEndpoint("https://example.com/issuer1/oauth2/token")
50+
.issuer("https://example.com")
51+
.authorizationEndpoint("https://example.com/oauth2/authorize")
52+
.tokenEndpoint("https://example.com/oauth2/token")
5353
.tokenEndpointAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC.getValue())
54-
.jwkSetUrl("https://example.com/issuer1/oauth2/jwks")
54+
.jwkSetUrl("https://example.com/oauth2/jwks")
5555
.scope("openid")
5656
.responseType("code")
5757
.grantType("authorization_code")
5858
.grantType("client_credentials")
59-
.tokenRevocationEndpoint("https://example.com/issuer1/oauth2/revoke")
59+
.tokenRevocationEndpoint("https://example.com/oauth2/revoke")
6060
.tokenRevocationEndpointAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC.getValue())
61-
.tokenIntrospectionEndpoint("https://example.com/issuer1/oauth2/introspect")
61+
.tokenIntrospectionEndpoint("https://example.com/oauth2/introspect")
6262
.tokenIntrospectionEndpointAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC.getValue())
6363
.codeChallengeMethod("S256")
6464
.claim("a-claim", "a-value")
6565
.build();
6666

67-
assertThat(authorizationServerMetadata.getIssuer()).isEqualTo(url("https://example.com/issuer1"));
68-
assertThat(authorizationServerMetadata.getAuthorizationEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/authorize"));
69-
assertThat(authorizationServerMetadata.getTokenEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/token"));
67+
assertThat(authorizationServerMetadata.getIssuer()).isEqualTo(url("https://example.com"));
68+
assertThat(authorizationServerMetadata.getAuthorizationEndpoint()).isEqualTo(url("https://example.com/oauth2/authorize"));
69+
assertThat(authorizationServerMetadata.getTokenEndpoint()).isEqualTo(url("https://example.com/oauth2/token"));
7070
assertThat(authorizationServerMetadata.getTokenEndpointAuthenticationMethods()).containsExactly(ClientAuthenticationMethod.CLIENT_SECRET_BASIC.getValue());
71-
assertThat(authorizationServerMetadata.getJwkSetUrl()).isEqualTo(url("https://example.com/issuer1/oauth2/jwks"));
71+
assertThat(authorizationServerMetadata.getJwkSetUrl()).isEqualTo(url("https://example.com/oauth2/jwks"));
7272
assertThat(authorizationServerMetadata.getScopes()).containsExactly("openid");
7373
assertThat(authorizationServerMetadata.getResponseTypes()).containsExactly("code");
7474
assertThat(authorizationServerMetadata.getGrantTypes()).containsExactlyInAnyOrder("authorization_code", "client_credentials");
75-
assertThat(authorizationServerMetadata.getTokenRevocationEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/revoke"));
75+
assertThat(authorizationServerMetadata.getTokenRevocationEndpoint()).isEqualTo(url("https://example.com/oauth2/revoke"));
7676
assertThat(authorizationServerMetadata.getTokenRevocationEndpointAuthenticationMethods()).containsExactly(ClientAuthenticationMethod.CLIENT_SECRET_BASIC.getValue());
77-
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/introspect"));
77+
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpoint()).isEqualTo(url("https://example.com/oauth2/introspect"));
7878
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpointAuthenticationMethods()).containsExactly(ClientAuthenticationMethod.CLIENT_SECRET_BASIC.getValue());
7979
assertThat(authorizationServerMetadata.getCodeChallengeMethods()).containsExactly("S256");
8080
assertThat(authorizationServerMetadata.getClaimAsString("a-claim")).isEqualTo("a-value");
@@ -83,15 +83,15 @@ public void buildWhenAllClaimsProvidedThenCreated() {
8383
@Test
8484
public void buildWhenOnlyRequiredClaimsProvidedThenCreated() {
8585
OAuth2AuthorizationServerMetadata authorizationServerMetadata = OAuth2AuthorizationServerMetadata.builder()
86-
.issuer("https://example.com/issuer1")
87-
.authorizationEndpoint("https://example.com/issuer1/oauth2/authorize")
88-
.tokenEndpoint("https://example.com/issuer1/oauth2/token")
86+
.issuer("https://example.com")
87+
.authorizationEndpoint("https://example.com/oauth2/authorize")
88+
.tokenEndpoint("https://example.com/oauth2/token")
8989
.responseType("code")
9090
.build();
9191

92-
assertThat(authorizationServerMetadata.getIssuer()).isEqualTo(url("https://example.com/issuer1"));
93-
assertThat(authorizationServerMetadata.getAuthorizationEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/authorize"));
94-
assertThat(authorizationServerMetadata.getTokenEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/token"));
92+
assertThat(authorizationServerMetadata.getIssuer()).isEqualTo(url("https://example.com"));
93+
assertThat(authorizationServerMetadata.getAuthorizationEndpoint()).isEqualTo(url("https://example.com/oauth2/authorize"));
94+
assertThat(authorizationServerMetadata.getTokenEndpoint()).isEqualTo(url("https://example.com/oauth2/token"));
9595
assertThat(authorizationServerMetadata.getTokenEndpointAuthenticationMethods()).isNull();
9696
assertThat(authorizationServerMetadata.getJwkSetUrl()).isNull();
9797
assertThat(authorizationServerMetadata.getScopes()).isNull();
@@ -107,29 +107,29 @@ public void buildWhenOnlyRequiredClaimsProvidedThenCreated() {
107107
@Test
108108
public void withClaimsWhenClaimsProvidedThenCreated() {
109109
HashMap<String, Object> claims = new HashMap<>();
110-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.ISSUER, "https://example.com/issuer1");
111-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.AUTHORIZATION_ENDPOINT, "https://example.com/issuer1/oauth2/authorize");
112-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.TOKEN_ENDPOINT, "https://example.com/issuer1/oauth2/token");
113-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.JWKS_URI, "https://example.com/issuer1/oauth2/jwks");
110+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.ISSUER, "https://example.com");
111+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.AUTHORIZATION_ENDPOINT, "https://example.com/oauth2/authorize");
112+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.TOKEN_ENDPOINT, "https://example.com/oauth2/token");
113+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.JWKS_URI, "https://example.com/oauth2/jwks");
114114
claims.put(OAuth2AuthorizationServerMetadataClaimNames.SCOPES_SUPPORTED, Collections.singletonList("openid"));
115115
claims.put(OAuth2AuthorizationServerMetadataClaimNames.RESPONSE_TYPES_SUPPORTED, Collections.singletonList("code"));
116-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.REVOCATION_ENDPOINT, "https://example.com/issuer1/oauth2/revoke");
117-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.INTROSPECTION_ENDPOINT, "https://example.com/issuer1/oauth2/introspect");
116+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.REVOCATION_ENDPOINT, "https://example.com/oauth2/revoke");
117+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.INTROSPECTION_ENDPOINT, "https://example.com/oauth2/introspect");
118118
claims.put("some-claim", "some-value");
119119

120120
OAuth2AuthorizationServerMetadata authorizationServerMetadata = OAuth2AuthorizationServerMetadata.withClaims(claims).build();
121121

122-
assertThat(authorizationServerMetadata.getIssuer()).isEqualTo(url("https://example.com/issuer1"));
123-
assertThat(authorizationServerMetadata.getAuthorizationEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/authorize"));
124-
assertThat(authorizationServerMetadata.getTokenEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/token"));
122+
assertThat(authorizationServerMetadata.getIssuer()).isEqualTo(url("https://example.com"));
123+
assertThat(authorizationServerMetadata.getAuthorizationEndpoint()).isEqualTo(url("https://example.com/oauth2/authorize"));
124+
assertThat(authorizationServerMetadata.getTokenEndpoint()).isEqualTo(url("https://example.com/oauth2/token"));
125125
assertThat(authorizationServerMetadata.getTokenEndpointAuthenticationMethods()).isNull();
126-
assertThat(authorizationServerMetadata.getJwkSetUrl()).isEqualTo(url("https://example.com/issuer1/oauth2/jwks"));
126+
assertThat(authorizationServerMetadata.getJwkSetUrl()).isEqualTo(url("https://example.com/oauth2/jwks"));
127127
assertThat(authorizationServerMetadata.getScopes()).containsExactly("openid");
128128
assertThat(authorizationServerMetadata.getResponseTypes()).containsExactly("code");
129129
assertThat(authorizationServerMetadata.getGrantTypes()).isNull();
130-
assertThat(authorizationServerMetadata.getTokenRevocationEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/revoke"));
130+
assertThat(authorizationServerMetadata.getTokenRevocationEndpoint()).isEqualTo(url("https://example.com/oauth2/revoke"));
131131
assertThat(authorizationServerMetadata.getTokenRevocationEndpointAuthenticationMethods()).isNull();
132-
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/introspect"));
132+
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpoint()).isEqualTo(url("https://example.com/oauth2/introspect"));
133133
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpointAuthenticationMethods()).isNull();
134134
assertThat(authorizationServerMetadata.getCodeChallengeMethods()).isNull();
135135
assertThat(authorizationServerMetadata.getClaimAsString("some-claim")).isEqualTo("some-value");
@@ -138,28 +138,28 @@ public void withClaimsWhenClaimsProvidedThenCreated() {
138138
@Test
139139
public void withClaimsWhenClaimsWithUrlsProvidedThenCreated() {
140140
HashMap<String, Object> claims = new HashMap<>();
141-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.ISSUER, url("https://example.com/issuer1"));
142-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.AUTHORIZATION_ENDPOINT, url("https://example.com/issuer1/oauth2/authorize"));
143-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.TOKEN_ENDPOINT, url("https://example.com/issuer1/oauth2/token"));
144-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.JWKS_URI, url("https://example.com/issuer1/oauth2/jwks"));
141+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.ISSUER, url("https://example.com"));
142+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.AUTHORIZATION_ENDPOINT, url("https://example.com/oauth2/authorize"));
143+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.TOKEN_ENDPOINT, url("https://example.com/oauth2/token"));
144+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.JWKS_URI, url("https://example.com/oauth2/jwks"));
145145
claims.put(OAuth2AuthorizationServerMetadataClaimNames.RESPONSE_TYPES_SUPPORTED, Collections.singletonList("code"));
146-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.REVOCATION_ENDPOINT, url("https://example.com/issuer1/oauth2/revoke"));
147-
claims.put(OAuth2AuthorizationServerMetadataClaimNames.INTROSPECTION_ENDPOINT, url("https://example.com/issuer1/oauth2/introspect"));
146+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.REVOCATION_ENDPOINT, url("https://example.com/oauth2/revoke"));
147+
claims.put(OAuth2AuthorizationServerMetadataClaimNames.INTROSPECTION_ENDPOINT, url("https://example.com/oauth2/introspect"));
148148
claims.put("some-claim", "some-value");
149149

150150
OAuth2AuthorizationServerMetadata authorizationServerMetadata = OAuth2AuthorizationServerMetadata.withClaims(claims).build();
151151

152-
assertThat(authorizationServerMetadata.getIssuer()).isEqualTo(url("https://example.com/issuer1"));
153-
assertThat(authorizationServerMetadata.getAuthorizationEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/authorize"));
154-
assertThat(authorizationServerMetadata.getTokenEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/token"));
152+
assertThat(authorizationServerMetadata.getIssuer()).isEqualTo(url("https://example.com"));
153+
assertThat(authorizationServerMetadata.getAuthorizationEndpoint()).isEqualTo(url("https://example.com/oauth2/authorize"));
154+
assertThat(authorizationServerMetadata.getTokenEndpoint()).isEqualTo(url("https://example.com/oauth2/token"));
155155
assertThat(authorizationServerMetadata.getTokenEndpointAuthenticationMethods()).isNull();
156-
assertThat(authorizationServerMetadata.getJwkSetUrl()).isEqualTo(url("https://example.com/issuer1/oauth2/jwks"));
156+
assertThat(authorizationServerMetadata.getJwkSetUrl()).isEqualTo(url("https://example.com/oauth2/jwks"));
157157
assertThat(authorizationServerMetadata.getScopes()).isNull();
158158
assertThat(authorizationServerMetadata.getResponseTypes()).containsExactly("code");
159159
assertThat(authorizationServerMetadata.getGrantTypes()).isNull();
160-
assertThat(authorizationServerMetadata.getTokenRevocationEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/revoke"));
160+
assertThat(authorizationServerMetadata.getTokenRevocationEndpoint()).isEqualTo(url("https://example.com/oauth2/revoke"));
161161
assertThat(authorizationServerMetadata.getTokenRevocationEndpointAuthenticationMethods()).isNull();
162-
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpoint()).isEqualTo(url("https://example.com/issuer1/oauth2/introspect"));
162+
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpoint()).isEqualTo(url("https://example.com/oauth2/introspect"));
163163
assertThat(authorizationServerMetadata.getTokenIntrospectionEndpointAuthenticationMethods()).isNull();
164164
assertThat(authorizationServerMetadata.getCodeChallengeMethods()).isNull();
165165
assertThat(authorizationServerMetadata.getClaimAsString("some-claim")).isEqualTo("some-value");

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2AuthorizationServerMetadataTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@
6565
@ExtendWith(SpringTestContextExtension.class)
6666
public class OAuth2AuthorizationServerMetadataTests {
6767
private static final String DEFAULT_OAUTH2_AUTHORIZATION_SERVER_METADATA_ENDPOINT_URI = "/.well-known/oauth-authorization-server";
68-
private static final String issuerUrl = "https://example.com/issuer1";
68+
private static final String ISSUER_URL = "https://example.com";
6969
private static EmbeddedDatabase db;
7070
private static JWKSource<SecurityContext> jwkSource;
7171

@@ -105,17 +105,17 @@ public static void destroy() {
105105
public void requestWhenAuthorizationServerMetadataRequestAndIssuerSetThenUsed() throws Exception {
106106
this.spring.register(AuthorizationServerConfiguration.class).autowire();
107107

108-
this.mvc.perform(get(DEFAULT_OAUTH2_AUTHORIZATION_SERVER_METADATA_ENDPOINT_URI))
108+
this.mvc.perform(get(ISSUER_URL.concat(DEFAULT_OAUTH2_AUTHORIZATION_SERVER_METADATA_ENDPOINT_URI)))
109109
.andExpect(status().is2xxSuccessful())
110-
.andExpect(jsonPath("issuer").value(issuerUrl))
110+
.andExpect(jsonPath("issuer").value(ISSUER_URL))
111111
.andReturn();
112112
}
113113

114114
@Test
115115
public void requestWhenAuthorizationServerMetadataRequestAndIssuerNotSetThenResolveFromRequest() throws Exception {
116116
this.spring.register(AuthorizationServerConfigurationWithIssuerNotSet.class).autowire();
117117

118-
this.mvc.perform(get(DEFAULT_OAUTH2_AUTHORIZATION_SERVER_METADATA_ENDPOINT_URI))
118+
this.mvc.perform(get("http://localhost".concat(DEFAULT_OAUTH2_AUTHORIZATION_SERVER_METADATA_ENDPOINT_URI)))
119119
.andExpect(status().is2xxSuccessful())
120120
.andExpect(jsonPath("issuer").value("http://localhost"))
121121
.andReturn();
@@ -126,7 +126,7 @@ public void requestWhenAuthorizationServerMetadataRequestAndIssuerNotSetThenReso
126126
public void requestWhenAuthorizationServerMetadataRequestAndMetadataCustomizerSetThenReturnCustomMetadataResponse() throws Exception {
127127
this.spring.register(AuthorizationServerConfigurationWithMetadataCustomizer.class).autowire();
128128

129-
this.mvc.perform(get(DEFAULT_OAUTH2_AUTHORIZATION_SERVER_METADATA_ENDPOINT_URI))
129+
this.mvc.perform(get(ISSUER_URL.concat(DEFAULT_OAUTH2_AUTHORIZATION_SERVER_METADATA_ENDPOINT_URI)))
130130
.andExpect(status().is2xxSuccessful())
131131
.andExpect(jsonPath(OAuth2AuthorizationServerMetadataClaimNames.SCOPES_SUPPORTED,
132132
hasItems("scope1", "scope2")));
@@ -156,7 +156,7 @@ JWKSource<SecurityContext> jwkSource() {
156156

157157
@Bean
158158
AuthorizationServerSettings authorizationServerSettings() {
159-
return AuthorizationServerSettings.builder().issuer(issuerUrl).build();
159+
return AuthorizationServerSettings.builder().issuer(ISSUER_URL).build();
160160
}
161161
}
162162

0 commit comments

Comments
 (0)