-
Notifications
You must be signed in to change notification settings - Fork 214
/
JwtValidatorTest.java
239 lines (186 loc) · 8.87 KB
/
JwtValidatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.security.authentication.jwt;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import java.nio.charset.StandardCharsets;
import java.security.PublicKey;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.eclipse.ditto.base.model.common.BinaryValidationResult;
import org.eclipse.ditto.gateway.service.util.config.security.DefaultOAuthConfig;
import org.eclipse.ditto.gateway.service.util.config.security.OAuthConfig;
import org.eclipse.ditto.internal.utils.jwt.JjwtDeserializer;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.jwt.model.Audience;
import org.eclipse.ditto.jwt.model.ImmutableJsonWebToken;
import org.eclipse.ditto.jwt.model.JsonWebToken;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.typesafe.config.ConfigFactory;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.UnsupportedJwtException;
/**
* Unit test for {@link DefaultJwtValidator}.
*/
@RunWith(MockitoJUnitRunner.class)
public final class JwtValidatorTest {
private static final JsonWebToken VALID_JSON_WEB_TOKEN =
ImmutableJsonWebToken.fromToken(JwtTestConstants.VALID_JWT_TOKEN);
private static final JsonWebToken VALID_JSON_WEB_TOKEN_WITHOUT_SIGNATURE =
// Can't use ImmutableJsonWebToken as it already verifies that the token contains a signature
new JsonWebTokenWithoutSignature(JwtTestConstants.UNSIGNED_JWT_TOKEN);
private static final JsonWebToken INVALID_JSON_WEB_TOKEN =
ImmutableJsonWebToken.fromToken(JwtTestConstants.EXPIRED_JWT_TOKEN);
private static final JsonWebToken VALID_JSON_WEB_TOKEN_WITH_NBF_AHEAD_OF_TIME =
ImmutableJsonWebToken.fromToken(JwtTestConstants.VALID_NBF_AHEAD_OF_TIME_JWT_TOKEN);
private static final JsonWebToken INVALID_JSON_WEB_TOKEN_WITH_NBF_AHEAD_OF_TIME =
ImmutableJsonWebToken.fromToken(JwtTestConstants.INVALID_NBF_AHEAD_OF_TIME_JWT_TOKEN);
private static OAuthConfig oAuthConfig;
@Mock
private PublicKeyProvider publicKeyProvider;
@BeforeClass
public static void initTestFixture() {
oAuthConfig = DefaultOAuthConfig.of(ConfigFactory.parseMap(
Map.of("allowedClockSkew", Duration.ofSeconds(10))
));
}
@Test
public void validate() throws ExecutionException, InterruptedException {
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID)).thenReturn(
CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY, getJwtParser(JwtTestConstants.PUBLIC_KEY)))));
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);
final BinaryValidationResult jwtValidationResult = underTest.validate(VALID_JSON_WEB_TOKEN).get();
assertThat(jwtValidationResult.isValid()).isTrue();
}
private JwtParser getJwtParser(final PublicKey publicKey) {
final var jwtParserBuilder = Jwts.parser();
return jwtParserBuilder.json(JjwtDeserializer.getInstance())
.verifyWith(publicKey)
.clockSkewSeconds(oAuthConfig.getAllowedClockSkew().getSeconds())
.build();
}
@Test
public void validateTokenWithNbfAheadOfTime() throws ExecutionException, InterruptedException {
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID)).thenReturn(
CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY, getJwtParser(JwtTestConstants.PUBLIC_KEY)))));
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);
final BinaryValidationResult jwtValidationResult =
underTest.validate(VALID_JSON_WEB_TOKEN_WITH_NBF_AHEAD_OF_TIME).get();
assertThat(jwtValidationResult.isValid()).isTrue();
}
@Test
public void validateFailsIfNbfIsTooFarInTheFuture() throws ExecutionException, InterruptedException {
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID)).thenReturn(
CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY, getJwtParser(JwtTestConstants.PUBLIC_KEY)))));
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);
final BinaryValidationResult jwtValidationResult =
underTest.validate(INVALID_JSON_WEB_TOKEN_WITH_NBF_AHEAD_OF_TIME).get();
assertThat(jwtValidationResult.isValid()).isFalse();
}
@Test
public void validateFailsIfSignatureIsMissing() throws ExecutionException, InterruptedException {
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID)).thenReturn(
CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY, getJwtParser(JwtTestConstants.PUBLIC_KEY)))));
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);
final BinaryValidationResult jwtValidationResult =
underTest.validate(VALID_JSON_WEB_TOKEN_WITHOUT_SIGNATURE).get();
assertThat(jwtValidationResult.isValid()).isFalse();
assertThat(jwtValidationResult.getReasonForInvalidity()).isInstanceOf(UnsupportedJwtException.class);
}
@Test
public void validateFails() throws ExecutionException, InterruptedException {
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID))
.thenReturn(CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY,
getJwtParser(JwtTestConstants.PUBLIC_KEY)))));
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);
final BinaryValidationResult jwtValidationResult = underTest.validate(INVALID_JSON_WEB_TOKEN).get();
assertThat(jwtValidationResult.isValid()).isFalse();
assertThat(jwtValidationResult.getReasonForInvalidity()).isInstanceOf(ExpiredJwtException.class);
}
private static final class JsonWebTokenWithoutSignature implements JsonWebToken {
private final String token;
private final JsonObject header;
private final JsonObject body;
private JsonWebTokenWithoutSignature(final String token) {
this.token = token;
final String[] tokenParts = this.token.split("\\.");
header = decodeJwtPart(tokenParts[0]);
body = decodeJwtPart(tokenParts[1]);
}
private static JsonObject decodeJwtPart(final String jwtPart) {
final Base64.Decoder decoder = Base64.getUrlDecoder();
return JsonFactory.newObject(new String(decoder.decode(jwtPart), StandardCharsets.UTF_8));
}
@Override
public String getToken() {
return token;
}
@Override
public JsonObject getHeader() {
return header;
}
@Override
public JsonObject getBody() {
return body;
}
@Override
public String getKeyId() {
return header.getValueOrThrow(JsonFields.KID);
}
@Override
public String getIssuer() {
return body.getValueOrThrow(JsonFields.ISS);
}
@Override
public String getSignature() {
return "";
}
@Override
public List<String> getSubjects() {
return List.of();
}
@Override
public Audience getAudience() {
return Audience.empty();
}
@Override
public String getAuthorizedParty() {
return "";
}
@Override
public List<String> getScopes() {
return List.of();
}
@Override
public Instant getExpirationTime() {
return Instant.ofEpochSecond(body.getValueOrThrow(JsonFields.EXP));
}
@Override
public boolean isExpired() {
return Instant.now().isAfter(getExpirationTime());
}
}
}