-
Notifications
You must be signed in to change notification settings - Fork 214
/
GatewayAuthenticationDirective.java
executable file
·123 lines (103 loc) · 5.51 KB
/
GatewayAuthenticationDirective.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
/*
* Copyright (c) 2017 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.endpoints.directives.auth;
import static akka.http.javadsl.server.Directives.extractRequestContext;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
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.AuthenticationChain;
import org.eclipse.ditto.gateway.service.security.authentication.AuthenticationResult;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLogger;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import akka.http.javadsl.model.Uri;
import akka.http.javadsl.server.Directives;
import akka.http.javadsl.server.Route;
import scala.util.Try;
/**
* Akka Http directive which performs authentication for the gateway.
*/
public final class GatewayAuthenticationDirective {
private static final DittoLogger LOGGER = DittoLoggerFactory.getLogger(GatewayAuthenticationDirective.class);
private final AuthenticationChain authenticationChain;
private final Function<DittoHeaders, DittoRuntimeException> defaultUnauthorizedExceptionFactory;
/**
* Constructor.
*
* @param authenticationChain the authentication chain that should be applied.
* @throws NullPointerException if authenticationChain is {@code null}.
*/
public GatewayAuthenticationDirective(final AuthenticationChain authenticationChain) {
this(authenticationChain, dittoHeaders -> GatewayAuthenticationFailedException.newBuilder("Unauthorized.")
.dittoHeaders(dittoHeaders)
.build());
}
/**
* Constructor.
*
* @param authenticationChain the authentication chain that should be applied.
* @param defaultUnauthorizedExceptionFactory a function that creates a DittoRuntimeException that should be
* returned if {@code authenticationChain} did not contain an applicable AuthenticationProvider.
* @throws NullPointerException if any argument is {@code null}.
*/
public GatewayAuthenticationDirective(final AuthenticationChain authenticationChain,
final Function<DittoHeaders, DittoRuntimeException> defaultUnauthorizedExceptionFactory) {
this.authenticationChain = checkNotNull(authenticationChain, "authenticationChain");
this.defaultUnauthorizedExceptionFactory =
checkNotNull(defaultUnauthorizedExceptionFactory, "defaultUnauthorizedExceptionFactory");
}
/**
* Depending on the request headers, one of the supported authentication mechanisms is applied.
*
* @param dittoHeaders the DittoHeaders containing already gathered context information.
* @param inner the inner route which will be wrapped with the {@link DittoHeaders}.
* @return the inner route.
*/
public Route authenticate(final DittoHeaders dittoHeaders, final Function<AuthenticationResult, Route> inner) {
return extractRequestContext(requestContext -> {
final Uri requestUri = requestContext.getRequest().getUri();
final CompletableFuture<AuthenticationResult> authenticationResult =
authenticationChain.authenticate(requestContext, dittoHeaders);
final Function<Try<AuthenticationResult>, Route> handleAuthenticationTry =
authenticationResultTry -> handleAuthenticationTry(authenticationResultTry, requestUri,
dittoHeaders, inner);
return Directives.onComplete(authenticationResult, handleAuthenticationTry);
});
}
private Route handleAuthenticationTry(final Try<AuthenticationResult> authenticationResultTry,
final Uri requestUri,
final DittoHeaders dittoHeaders,
final Function<AuthenticationResult, Route> inner) {
if (authenticationResultTry.isSuccess()) {
final AuthenticationResult authenticationResult = authenticationResultTry.get();
if (authenticationResult.isSuccess()) {
return inner.apply(authenticationResult);
}
return handleFailedAuthentication(authenticationResult.getReasonOfFailure(), requestUri, dittoHeaders);
}
return handleFailedAuthentication(authenticationResultTry.failed().get(), requestUri, dittoHeaders);
}
private Route handleFailedAuthentication(final Throwable reasonOfFailure, final Uri requestUri,
final DittoHeaders dittoHeaders) {
if (reasonOfFailure instanceof DittoRuntimeException) {
LOGGER.debug("Authentication for URI <{}> failed. Rethrow DittoRuntimeException.", requestUri,
reasonOfFailure);
throw (DittoRuntimeException) reasonOfFailure;
}
LOGGER.debug("Unexpected error during authentication for URI <{}>! Applying unauthorizedDirective",
requestUri, reasonOfFailure);
throw defaultUnauthorizedExceptionFactory.apply(dittoHeaders);
}
}