-
Notifications
You must be signed in to change notification settings - Fork 214
/
JwtExtractor.java
56 lines (49 loc) · 2 KB
/
JwtExtractor.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
/*
* 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 java.util.Optional;
import java.util.function.BiFunction;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.gateway.api.GatewayAuthenticationFailedException;
import org.eclipse.ditto.jwt.model.JsonWebToken;
import org.apache.pekko.http.javadsl.server.RequestContext;
/**
* Extract a JsonWebToken from the given RequestContext or returns an empty Optional if no token is present.
*/
public interface JwtExtractor extends BiFunction<RequestContext, DittoHeaders, Optional<JsonWebToken>> {
/**
* Indicates whether the given {@link RequestContext request context} contains the required information to
* extract a JWT.
*
* @param requestContext the current request context
* @return {@code true} if the request context contains the required information
*/
boolean isApplicable(RequestContext requestContext);
/**
* Builds an exception with detailed description where the token information was expected.
*
* @param dittoHeaders ditto headers of the current request
* @return the built exception
*/
default Exception buildMissingJwtException(final DittoHeaders dittoHeaders) {
return GatewayAuthenticationFailedException
.newBuilder("The JWT was missing.")
.description(getErrorDescription())
.dittoHeaders(dittoHeaders)
.build();
}
/**
* @return detail information about the error and where the token information was expected.
*/
String getErrorDescription();
}