forked from eclipse-ditto/ditto
-
Notifications
You must be signed in to change notification settings - Fork 7
/
JwtAuthenticationResult.java
69 lines (60 loc) · 2.68 KB
/
JwtAuthenticationResult.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
/*
* Copyright (c) 2020 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.services.gateway.security.authentication.jwt;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.util.Optional;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.jwt.JsonWebToken;
import org.eclipse.ditto.services.gateway.security.authentication.AuthenticationResult;
/**
* The result of a JWT authentication.
*/
public interface JwtAuthenticationResult extends AuthenticationResult {
/**
* Retrieve the parsed json web token, or an empty optional if parsing failed.
* Guaranteed to be nonempty if the authentication result is successful.
*
* @return the JWT.
*/
Optional<JsonWebToken> getJwt();
/**
* Initializes a successful authentication result with a JWT.
*
* @param dittoHeaders the DittoHeaders of the succeeded authentication result.
* @param authorizationContext the authorization context found by authentication.
* @param jwt the parsed jwt.
* @return a successfully completed authentication result containing the {@code given authorizationContext}.
* @throws NullPointerException if any argument is {@code null}.
*/
static JwtAuthenticationResult successful(final DittoHeaders dittoHeaders,
final AuthorizationContext authorizationContext,
final JsonWebToken jwt) {
return new DefaultJwtAuthenticationResult(dittoHeaders,
checkNotNull(authorizationContext, "authorizationContext"),
null,
checkNotNull(jwt, "jwt"));
}
/**
* Initializes a result of a failed JWT authentication.
*
* @param dittoHeaders the DittoHeaders of the failed authentication result.
* @param reasonOfFailure the reason of the authentication failure.
* @return a failed authentication result containing the {@code given reasonOfFailure}.
* @throws NullPointerException if any argument is {@code null}.
*/
static JwtAuthenticationResult failed(final DittoHeaders dittoHeaders, final Throwable reasonOfFailure) {
return new DefaultJwtAuthenticationResult(dittoHeaders, null, checkNotNull(reasonOfFailure, "reasonOfFailure"),
null);
}
}