|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
25 | 25 | import org.mockito.junit.jupiter.MockitoExtension;
|
26 | 26 |
|
27 | 27 | import org.springframework.core.convert.converter.Converter;
|
| 28 | +import org.springframework.security.authentication.AbstractAuthenticationToken; |
28 | 29 | import org.springframework.security.core.AuthenticationException;
|
29 | 30 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
30 | 31 | import org.springframework.security.oauth2.jwt.BadJwtException;
|
|
43 | 44 | * Tests for {@link JwtAuthenticationProvider}
|
44 | 45 | *
|
45 | 46 | * @author Josh Cummings
|
| 47 | + * @author Jerome Wacongne ch4mp@c4-soft.com |
46 | 48 | */
|
47 | 49 | @ExtendWith(MockitoExtension.class)
|
48 | 50 | public class JwtAuthenticationProviderTests {
|
49 | 51 |
|
50 | 52 | @Mock
|
51 |
| - Converter<Jwt, JwtAuthenticationToken> jwtAuthenticationConverter; |
| 53 | + Converter<Jwt, AbstractAuthenticationToken> jwtAuthenticationConverter; |
52 | 54 |
|
53 | 55 | @Mock
|
54 | 56 | JwtDecoder jwtDecoder;
|
@@ -107,17 +109,46 @@ public void authenticateWhenDecoderFailsGenericallyThenThrowsGenericException()
|
107 | 109 |
|
108 | 110 | @Test
|
109 | 111 | 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() { |
110 | 139 | BearerTokenAuthenticationToken token = this.authentication();
|
111 | 140 | Object details = mock(Object.class);
|
112 | 141 | token.setDetails(details);
|
113 | 142 | Jwt jwt = TestJwts.jwt().build();
|
114 | 143 | JwtAuthenticationToken authentication = new JwtAuthenticationToken(jwt);
|
| 144 | + Object expectedDetails = "To be kept as is"; |
| 145 | + authentication.setDetails(expectedDetails); |
115 | 146 | given(this.jwtDecoder.decode(token.getToken())).willReturn(jwt);
|
116 | 147 | given(this.jwtAuthenticationConverter.convert(jwt)).willReturn(authentication);
|
117 | 148 | // @formatter:off
|
118 | 149 | assertThat(this.provider.authenticate(token))
|
119 | 150 | .isEqualTo(authentication).hasFieldOrPropertyWithValue("details",
|
120 |
| - details); |
| 151 | + expectedDetails); |
121 | 152 | // @formatter:on
|
122 | 153 | }
|
123 | 154 |
|
|
0 commit comments