1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 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.
24
24
import com .nimbusds .jwt .proc .DefaultJWTProcessor ;
25
25
import okhttp3 .mockwebserver .MockResponse ;
26
26
import okhttp3 .mockwebserver .MockWebServer ;
27
+ import org .assertj .core .api .Assertions ;
27
28
import org .junit .Test ;
28
29
import org .junit .runner .RunWith ;
29
30
import org .powermock .core .classloader .annotations .PowerMockIgnore ;
30
31
import org .powermock .core .classloader .annotations .PrepareForTest ;
31
32
import org .powermock .modules .junit4 .PowerMockRunner ;
32
-
33
+ import org . springframework . http . RequestEntity ;
33
34
import org .springframework .security .oauth2 .jose .jws .JwsAlgorithms ;
35
+ import org .springframework .web .client .RestTemplate ;
34
36
35
37
import static org .assertj .core .api .AssertionsForClassTypes .assertThatCode ;
36
38
import static org .assertj .core .api .AssertionsForClassTypes .assertThatThrownBy ;
37
- import static org .mockito .ArgumentMatchers .any ;
38
- import static org .mockito .ArgumentMatchers .anyString ;
39
- import static org .mockito .ArgumentMatchers .eq ;
39
+ import static org .mockito .ArgumentMatchers .*;
40
40
import static org .mockito .Mockito .mock ;
41
- import static org .powermock .api .mockito .PowerMockito .mockStatic ;
42
- import static org .powermock .api .mockito .PowerMockito .when ;
43
- import static org .powermock .api .mockito .PowerMockito .whenNew ;
41
+ import static org .mockito .Mockito .verify ;
42
+ import static org .powermock .api .mockito .PowerMockito .*;
44
43
45
44
/**
46
45
* Tests for {@link NimbusJwtDecoderJwkSupport}.
@@ -62,6 +61,8 @@ public class NimbusJwtDecoderJwkSupportTests {
62
61
private static final String MALFORMED_JWT = "eyJhbGciOiJSUzI1NiJ9.eyJuYmYiOnt9LCJleHAiOjQ2ODQyMjUwODd9.guoQvujdWvd3xw7FYQEn4D6-gzM_WqFvXdmvAUNSLbxG7fv2_LLCNujPdrBHJoYPbOwS1BGNxIKQWS1tylvqzmr1RohQ-RZ2iAM1HYQzboUlkoMkcd8ENM__ELqho8aNYBfqwkNdUOyBFoy7Syu_w2SoJADw2RTjnesKO6CVVa05bW118pDS4xWxqC4s7fnBjmZoTn4uQ-Kt9YSQZQk8YQxkJSiyanozzgyfgXULA6mPu1pTNU3FVFaK1i1av_xtH_zAPgb647ZeaNe4nahgqC5h8nhOlm8W2dndXbwAt29nd2ZWBsru_QwZz83XSKLhTPFz-mPBByZZDsyBbIHf9A" ;
63
62
private static final String UNSIGNED_JWT = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJleHAiOi0yMDMzMjI0OTcsImp0aSI6IjEyMyIsInR5cCI6IkpXVCJ9." ;
64
63
64
+ private NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL , JWS_ALGORITHM );
65
+
65
66
@ Test
66
67
public void constructorWhenJwkSetUrlIsNullThenThrowIllegalArgumentException () {
67
68
assertThatThrownBy (() -> new NimbusJwtDecoderJwkSupport (null ))
@@ -80,10 +81,15 @@ public void constructorWhenJwsAlgorithmIsNullThenThrowIllegalArgumentException()
80
81
.isInstanceOf (IllegalArgumentException .class );
81
82
}
82
83
84
+ @ Test
85
+ public void setRestOperationsWhenNullThenThrowIllegalArgumentException () {
86
+ Assertions .assertThatThrownBy (() -> this .jwtDecoder .setRestOperations (null ))
87
+ .isInstanceOf (IllegalArgumentException .class );
88
+ }
89
+
83
90
@ Test
84
91
public void decodeWhenJwtInvalidThenThrowJwtException () {
85
- NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL , JWS_ALGORITHM );
86
- assertThatThrownBy (() -> jwtDecoder .decode ("invalid" ))
92
+ assertThatThrownBy (() -> this .jwtDecoder .decode ("invalid" ))
87
93
.isInstanceOf (JwtException .class );
88
94
}
89
95
@@ -103,16 +109,14 @@ public void decodeWhenExpClaimNullThenDoesNotThrowException() throws Exception {
103
109
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().audience ("resource1" ).build ();
104
110
when (jwtProcessor .process (any (JWT .class ), eq (null ))).thenReturn (jwtClaimsSet );
105
111
106
- NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL , JWS_ALGORITHM );
112
+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL );
107
113
assertThatCode (() -> jwtDecoder .decode ("encoded-jwt" )).doesNotThrowAnyException ();
108
114
}
109
115
110
116
// gh-5457
111
117
@ Test
112
- public void decodeWhenPlainJwtThenExceptionDoesNotMentionClass () throws Exception {
113
- NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (JWK_SET_URL , JWS_ALGORITHM );
114
-
115
- assertThatCode (() -> jwtDecoder .decode (UNSIGNED_JWT ))
118
+ public void decodeWhenPlainJwtThenExceptionDoesNotMentionClass () {
119
+ assertThatCode (() -> this .jwtDecoder .decode (UNSIGNED_JWT ))
116
120
.isInstanceOf (JwtException .class )
117
121
.hasMessageContaining ("Unsupported algorithm of none" );
118
122
}
@@ -122,12 +126,11 @@ public void decodeWhenJwtIsMalformedThenReturnsStockException() throws Exception
122
126
try ( MockWebServer server = new MockWebServer () ) {
123
127
server .enqueue (new MockResponse ().setBody (JWK_SET ));
124
128
String jwkSetUrl = server .url ("/.well-known/jwks.json" ).toString ();
125
-
126
- NimbusJwtDecoderJwkSupport decoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
127
-
128
- assertThatCode (() -> decoder .decode (MALFORMED_JWT ))
129
+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
130
+ assertThatCode (() -> jwtDecoder .decode (MALFORMED_JWT ))
129
131
.isInstanceOf (JwtException .class )
130
132
.hasMessage ("An error occurred while attempting to decode the Jwt: Malformed payload" );
133
+ server .shutdown ();
131
134
}
132
135
}
133
136
@@ -136,28 +139,39 @@ public void decodeWhenJwkResponseIsMalformedThenReturnsStockException() throws E
136
139
try ( MockWebServer server = new MockWebServer () ) {
137
140
server .enqueue (new MockResponse ().setBody (MALFORMED_JWK_SET ));
138
141
String jwkSetUrl = server .url ("/.well-known/jwks.json" ).toString ();
139
-
140
- NimbusJwtDecoderJwkSupport decoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
141
-
142
- assertThatCode (() -> decoder .decode (SIGNED_JWT ))
142
+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
143
+ assertThatCode (() -> jwtDecoder .decode (SIGNED_JWT ))
143
144
.isInstanceOf (JwtException .class )
144
145
.hasMessage ("An error occurred while attempting to decode the Jwt: Malformed Jwk set" );
146
+ server .shutdown ();
145
147
}
146
148
}
147
149
148
150
@ Test
149
- public void decodeWhenJwkEndpointIsUnresponsiveThenRetrunsJwtException () throws Exception {
151
+ public void decodeWhenJwkEndpointIsUnresponsiveThenReturnsJwtException () throws Exception {
150
152
try ( MockWebServer server = new MockWebServer () ) {
151
153
server .enqueue (new MockResponse ().setBody (MALFORMED_JWK_SET ));
152
154
String jwkSetUrl = server .url ("/.well-known/jwks.json" ).toString ();
153
-
154
- NimbusJwtDecoderJwkSupport decoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
155
-
156
- server .shutdown ();
157
-
158
- assertThatCode (() -> decoder .decode (SIGNED_JWT ))
155
+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
156
+ assertThatCode (() -> jwtDecoder .decode (SIGNED_JWT ))
159
157
.isInstanceOf (JwtException .class )
160
158
.hasMessageContaining ("An error occurred while attempting to decode the Jwt" );
159
+ server .shutdown ();
160
+ }
161
+ }
162
+
163
+ // gh-5603
164
+ @ Test
165
+ public void decodeWhenCustomRestOperationsSetThenUsed () throws Exception {
166
+ try ( MockWebServer server = new MockWebServer () ) {
167
+ server .enqueue (new MockResponse ().setBody (JWK_SET ));
168
+ String jwkSetUrl = server .url ("/.well-known/jwks.json" ).toString ();
169
+ NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport (jwkSetUrl );
170
+ RestTemplate restTemplate = spy (new RestTemplate ());
171
+ jwtDecoder .setRestOperations (restTemplate );
172
+ assertThatCode (() -> jwtDecoder .decode (SIGNED_JWT )).doesNotThrowAnyException ();
173
+ verify (restTemplate ).exchange (any (RequestEntity .class ), eq (String .class ));
174
+ server .shutdown ();
161
175
}
162
176
}
163
177
}
0 commit comments