Skip to content

Commit 7ad4ebd

Browse files
ch4mpySteve Riesenberg
authored andcommitted
Allow authentication details to be set by converter
Prevent JwtAuthenticationProvider from setting authentication details when jwtAuthenticationConverter returned an authentication instance with non null details. Closes gh-11822
1 parent c2d0ea3 commit 7ad4ebd

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationProvider.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -56,6 +56,7 @@
5656
*
5757
* @author Josh Cummings
5858
* @author Joe Grandja
59+
* @author Jerome Wacongne ch4mp@c4-soft.com
5960
* @since 5.1
6061
* @see AuthenticationProvider
6162
* @see JwtDecoder
@@ -86,7 +87,9 @@ public Authentication authenticate(Authentication authentication) throws Authent
8687
BearerTokenAuthenticationToken bearer = (BearerTokenAuthenticationToken) authentication;
8788
Jwt jwt = getJwt(bearer);
8889
AbstractAuthenticationToken token = this.jwtAuthenticationConverter.convert(jwt);
89-
token.setDetails(bearer.getDetails());
90+
if (token.getDetails() == null) {
91+
token.setDetails(bearer.getDetails());
92+
}
9093
this.logger.debug("Authenticated token");
9194
return token;
9295
}

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationProviderTests.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -25,6 +25,7 @@
2525
import org.mockito.junit.jupiter.MockitoExtension;
2626

2727
import org.springframework.core.convert.converter.Converter;
28+
import org.springframework.security.authentication.AbstractAuthenticationToken;
2829
import org.springframework.security.core.AuthenticationException;
2930
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
3031
import org.springframework.security.oauth2.jwt.BadJwtException;
@@ -43,12 +44,13 @@
4344
* Tests for {@link JwtAuthenticationProvider}
4445
*
4546
* @author Josh Cummings
47+
* @author Jerome Wacongne ch4mp@c4-soft.com
4648
*/
4749
@ExtendWith(MockitoExtension.class)
4850
public class JwtAuthenticationProviderTests {
4951

5052
@Mock
51-
Converter<Jwt, JwtAuthenticationToken> jwtAuthenticationConverter;
53+
Converter<Jwt, AbstractAuthenticationToken> jwtAuthenticationConverter;
5254

5355
@Mock
5456
JwtDecoder jwtDecoder;
@@ -107,17 +109,46 @@ public void authenticateWhenDecoderFailsGenericallyThenThrowsGenericException()
107109

108110
@Test
109111
public void authenticateWhenConverterReturnsAuthenticationThenProviderPropagatesIt() {
112+
BearerTokenAuthenticationToken token = this.authentication();
113+
Jwt jwt = TestJwts.jwt().build();
114+
JwtAuthenticationToken authentication = new JwtAuthenticationToken(jwt);
115+
given(this.jwtDecoder.decode(token.getToken())).willReturn(jwt);
116+
given(this.jwtAuthenticationConverter.convert(jwt)).willReturn(authentication);
117+
118+
assertThat(this.provider.authenticate(token)).isEqualTo(authentication);
119+
}
120+
121+
@Test
122+
public void authenticateWhenConverterDoesNotSetAuthenticationDetailsThenProviderSetsItWithTokenDetails() {
123+
BearerTokenAuthenticationToken token = this.authentication();
124+
Object details = mock(Object.class);
125+
token.setDetails(details);
126+
Jwt jwt = TestJwts.jwt().build();
127+
JwtAuthenticationToken authentication = new JwtAuthenticationToken(jwt);
128+
given(this.jwtDecoder.decode(token.getToken())).willReturn(jwt);
129+
given(this.jwtAuthenticationConverter.convert(jwt)).willReturn(authentication);
130+
// @formatter:off
131+
assertThat(this.provider.authenticate(token))
132+
.isEqualTo(authentication).hasFieldOrPropertyWithValue("details",
133+
details);
134+
// @formatter:on
135+
}
136+
137+
@Test
138+
public void authenticateWhenConverterSetsAuthenticationDetailsThenProviderDoesNotOverwriteIt() {
110139
BearerTokenAuthenticationToken token = this.authentication();
111140
Object details = mock(Object.class);
112141
token.setDetails(details);
113142
Jwt jwt = TestJwts.jwt().build();
114143
JwtAuthenticationToken authentication = new JwtAuthenticationToken(jwt);
144+
Object expectedDetails = "To be kept as is";
145+
authentication.setDetails(expectedDetails);
115146
given(this.jwtDecoder.decode(token.getToken())).willReturn(jwt);
116147
given(this.jwtAuthenticationConverter.convert(jwt)).willReturn(authentication);
117148
// @formatter:off
118149
assertThat(this.provider.authenticate(token))
119150
.isEqualTo(authentication).hasFieldOrPropertyWithValue("details",
120-
details);
151+
expectedDetails);
121152
// @formatter:on
122153
}
123154

0 commit comments

Comments
 (0)