-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractJwtAuthenticationProviderTest.java
282 lines (237 loc) · 13.6 KB
/
AbstractJwtAuthenticationProviderTest.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright (c) 2021 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.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import org.apache.pekko.http.javadsl.model.HttpHeader;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.server.RequestContext;
import org.assertj.core.api.AssertionsForClassTypes;
import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.common.BinaryValidationResult;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.gateway.api.GatewayAuthenticationFailedException;
import org.eclipse.ditto.gateway.service.security.authentication.AuthenticationProvider;
import org.eclipse.ditto.gateway.service.security.authentication.AuthenticationResult;
import org.eclipse.ditto.jwt.model.ImmutableJsonWebToken;
import org.eclipse.ditto.jwt.model.JsonWebToken;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
/**
* Provides tests for {@link JwtAuthenticationProvider}.
*/
@RunWith(MockitoJUnitRunner.class)
abstract class AbstractJwtAuthenticationProviderTest {
private static final HttpHeader VALID_AUTHORIZATION_HEADER =
HttpHeader.parse("authorization", "Bearer " + JwtTestConstants.VALID_JWT_TOKEN);
private static final HttpHeader INVALID_AUTHORIZATION_HEADER =
HttpHeader.parse("authorization", "Basic " + JwtTestConstants.VALID_JWT_TOKEN);
protected static final String
URI_WITH_ACCESS_TOKEN_PARAMETER =
"https://localhost/ws/2?x=5&access_token=" + JwtTestConstants.VALID_JWT_TOKEN;
@Rule public final TestName testName = new TestName();
@Rule public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
@Mock protected JwtAuthenticationResultProvider authenticationContextProvider;
@Mock protected JwtValidator jwtValidator;
protected DittoHeaders knownDittoHeaders;
/**
* @return the JwtAuthenticationProvider instance under test
*/
protected abstract JwtAuthenticationProvider getUnderTest();
/**
* @return whether the instance under test supports the access token parameter
*/
protected abstract boolean supportsAccessTokenParameter();
/**
* @return the expected error description
*/
protected abstract String getExpectedMissingJwtDescription();
@Test
public void isApplicableWithHeader() {
final RequestContext requestContext = mockRequestContext(VALID_AUTHORIZATION_HEADER);
softly.assertThat(getUnderTest().isApplicable(requestContext)).isTrue();
}
@Test
public void isApplicableWithAccessTokenParameter() {
final RequestContext requestContext = mockRequestContext(URI_WITH_ACCESS_TOKEN_PARAMETER);
softly.assertThat(getUnderTest().isApplicable(requestContext)).isEqualTo(supportsAccessTokenParameter());
}
@Test
public void isApplicableWithAccessTokenParameterAndHeader() {
final RequestContext requestContext = mockRequestContext(URI_WITH_ACCESS_TOKEN_PARAMETER,
VALID_AUTHORIZATION_HEADER);
softly.assertThat(getUnderTest().isApplicable(requestContext)).isTrue();
}
@Test
public void isApplicableWithoutAuthorizationHeaderOrAccessTokenParameter() {
final RequestContext requestContext = mockRequestContext();
softly.assertThat(getUnderTest().isApplicable(requestContext)).isFalse();
}
@Test
public void isApplicableWithInvalidAuthorizationHeader() {
final RequestContext requestContext = mockRequestContext(INVALID_AUTHORIZATION_HEADER);
softly.assertThat(getUnderTest().isApplicable(requestContext)).isFalse();
}
@Test
public void doExtractAuthenticationFromRequestHeaders() {
when(jwtValidator.validate(any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(BinaryValidationResult.valid()));
when(authenticationContextProvider.getAuthenticationResult(any(JsonWebToken.class), any(DittoHeaders.class)))
.thenReturn(CompletableFuture.completedFuture(JwtAuthenticationResult.successful(knownDittoHeaders,
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
AuthorizationSubject.newInstance("myAuthSubj")),
ImmutableJsonWebToken.fromToken(JwtTestConstants.VALID_JWT_TOKEN))));
final RequestContext requestContext = mockRequestContext(VALID_AUTHORIZATION_HEADER);
authenticate(getUnderTest(), requestContext, true);
}
@Test
public void doExtractAuthenticationFromAccessTokenParameter() {
lenient().when(jwtValidator.validate(any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(BinaryValidationResult.valid()));
lenient().when(
authenticationContextProvider.getAuthenticationResult(any(JsonWebToken.class), any(DittoHeaders.class)))
.thenReturn(CompletableFuture.completedFuture(JwtAuthenticationResult.successful(knownDittoHeaders,
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
AuthorizationSubject.newInstance("myAuthSubj")),
ImmutableJsonWebToken.fromToken(JwtTestConstants.VALID_JWT_TOKEN))));
final RequestContext requestContext =
mockRequestContext(URI_WITH_ACCESS_TOKEN_PARAMETER);
authenticate(getUnderTest(), requestContext, supportsAccessTokenParameter());
}
@Test
public void doExtractAuthenticationWhenAuthorizationContextProviderErrors() {
when(jwtValidator.validate(any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(BinaryValidationResult.valid()));
when(authenticationContextProvider.getAuthenticationResult(any(JsonWebToken.class), any(DittoHeaders.class)))
.thenReturn(CompletableFuture.failedFuture(new RuntimeException("Something happened")));
final RequestContext requestContext = mockRequestContext(VALID_AUTHORIZATION_HEADER);
final AuthenticationResult authenticationResult =
getUnderTest().authenticate(requestContext, knownDittoHeaders).join();
verify(authenticationContextProvider).getAuthenticationResult(any(JsonWebToken.class), any(DittoHeaders.class));
softly.assertThat(authenticationResult.isSuccess()).isFalse();
softly.assertThat(authenticationResult.getReasonOfFailure())
.isExactlyInstanceOf(GatewayAuthenticationFailedException.class);
final GatewayAuthenticationFailedException reasonOfFailure =
(GatewayAuthenticationFailedException) authenticationResult.getReasonOfFailure();
softly.assertThat(reasonOfFailure).hasMessage("The JWT could not be verified.");
softly.assertThat(reasonOfFailure.getDescription()).contains("Something happened");
softly.assertThat(reasonOfFailure.getDittoHeaders().getCorrelationId())
.isEqualTo(knownDittoHeaders.getCorrelationId());
}
@Test
public void doExtractAuthenticationWithMissingJwt() {
final RequestContext requestContext = mockRequestContext();
final AuthenticationResult authenticationResult =
getUnderTest().authenticate(requestContext, knownDittoHeaders).join();
softly.assertThat(authenticationResult.isSuccess()).isFalse();
softly.assertThat(authenticationResult.getReasonOfFailure())
.isInstanceOf(GatewayAuthenticationFailedException.class);
final DittoRuntimeException reasonOfFailureDre =
(DittoRuntimeException) authenticationResult.getReasonOfFailure();
softly.assertThat(reasonOfFailureDre).hasMessage("The JWT was missing.");
softly.assertThat(reasonOfFailureDre.getErrorCode()).isEqualTo("gateway:authentication.failed");
softly.assertThat(reasonOfFailureDre.getHttpStatus()).isEqualTo(HttpStatus.UNAUTHORIZED);
softly.assertThat(reasonOfFailureDre.getDittoHeaders().getCorrelationId())
.isEqualTo(knownDittoHeaders.getCorrelationId());
softly.assertThat(reasonOfFailureDre.getDescription())
.contains(getExpectedMissingJwtDescription());
}
@Test
public void doExtractAuthenticationWithInvalidJwtFromHeader() {
final RequestContext requestContext = mockRequestContext(VALID_AUTHORIZATION_HEADER);
doExtractAuthenticationWithInvalidJwt(getUnderTest(), requestContext);
}
protected void doExtractAuthenticationWithInvalidJwt(final AuthenticationProvider<AuthenticationResult> provider,
final RequestContext requestContext) {
when(jwtValidator.validate(any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(
BinaryValidationResult.invalid(new IllegalStateException("foo"))));
final AuthenticationResult authenticationResult =
provider.authenticate(requestContext, knownDittoHeaders).join();
softly.assertThat(authenticationResult.isSuccess()).isFalse();
softly.assertThat(authenticationResult.getReasonOfFailure())
.isInstanceOf(GatewayAuthenticationFailedException.class);
final DittoRuntimeException reasonOfFailureDre =
(DittoRuntimeException) authenticationResult.getReasonOfFailure();
softly.assertThat(reasonOfFailureDre).hasMessage("The JWT could not be verified.");
softly.assertThat(reasonOfFailureDre.getErrorCode()).isEqualTo("gateway:authentication.failed");
softly.assertThat(reasonOfFailureDre.getHttpStatus()).isEqualTo(HttpStatus.UNAUTHORIZED);
softly.assertThat(reasonOfFailureDre.getDittoHeaders().getCorrelationId())
.isEqualTo(knownDittoHeaders.getCorrelationId());
}
@Test
public void toFailedAuthenticationResultExtractsDittoRuntimeExceptionFromCause() {
final DittoRuntimeException dre = PublicKeyProviderUnavailableException.newBuilder().build();
final IllegalStateException illegalStateException = new IllegalStateException("notExpected", dre);
final Throwable reasonOfFailure =
getUnderTest().toFailedAuthenticationResult(illegalStateException, knownDittoHeaders)
.getReasonOfFailure();
softly.assertThat(reasonOfFailure).isEqualTo(dre);
}
@Test
public void toFailedAuthenticationResult() {
final DittoRuntimeException dre = PublicKeyProviderUnavailableException.newBuilder().build();
final AuthenticationResult authenticationResult =
getUnderTest().toFailedAuthenticationResult(dre, knownDittoHeaders);
softly.assertThat(authenticationResult.getReasonOfFailure()).isEqualTo(dre);
}
@Test
public void getTypeWithAuthorizationHeader() {
AssertionsForClassTypes.assertThat(getUnderTest().getType(mockRequestContext(VALID_AUTHORIZATION_HEADER)))
.isEqualTo(DittoAuthorizationContextType.JWT);
}
@Test
public void getTypeWithAccessTokenParameter() {
AssertionsForClassTypes.assertThat(getUnderTest().getType(mockRequestContext(URI_WITH_ACCESS_TOKEN_PARAMETER)))
.isEqualTo(DittoAuthorizationContextType.JWT);
}
private static RequestContext mockRequestContext(final HttpHeader... httpHeaders) {
final HttpRequest httpRequest = HttpRequest.create().addHeaders(Arrays.asList(httpHeaders));
final RequestContext requestContext = mock(RequestContext.class);
when(requestContext.getRequest()).thenReturn(httpRequest);
return requestContext;
}
protected static RequestContext mockRequestContext(final String uri) {
final HttpRequest httpRequest = HttpRequest.create(uri);
final RequestContext requestContext = mock(RequestContext.class);
when(requestContext.getRequest()).thenReturn(httpRequest);
return requestContext;
}
private static RequestContext mockRequestContext(final String uri, final HttpHeader... httpHeaders) {
final HttpRequest httpRequest = HttpRequest.create(uri).addHeaders(Arrays.asList(httpHeaders));
final RequestContext requestContext = mock(RequestContext.class);
when(requestContext.getRequest()).thenReturn(httpRequest);
return requestContext;
}
private void authenticate(final AuthenticationProvider<AuthenticationResult> provider,
final RequestContext requestContext,
final boolean expectedResult) {
final AuthenticationResult authenticationResult =
provider.authenticate(requestContext, knownDittoHeaders).join();
softly.assertThat(authenticationResult.isSuccess()).isEqualTo(expectedResult);
}
}