-
Notifications
You must be signed in to change notification settings - Fork 214
/
AsyncJwtLoader.java
191 lines (170 loc) · 8.63 KB
/
AsyncJwtLoader.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
/*
* Copyright (c) 2022 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.connectivity.service.messaging.httppush;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.base.service.UriEncoding;
import org.eclipse.ditto.connectivity.model.OAuthClientCredentials;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonRuntimeException;
import org.eclipse.ditto.jwt.model.ImmutableJsonWebToken;
import org.eclipse.ditto.jwt.model.JsonWebToken;
import org.eclipse.ditto.jwt.model.JwtInvalidException;
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.http.javadsl.Http;
import org.apache.pekko.http.javadsl.model.ContentTypes;
import org.apache.pekko.http.javadsl.model.HttpEntities;
import org.apache.pekko.http.javadsl.model.HttpHeader;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.HttpResponse;
import org.apache.pekko.http.javadsl.model.MediaRanges;
import org.apache.pekko.http.javadsl.model.MediaTypes;
import org.apache.pekko.http.javadsl.model.headers.Accept;
import org.apache.pekko.stream.Materializer;
import org.apache.pekko.stream.javadsl.Flow;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.util.ByteString;
import scala.util.Failure;
import scala.util.Success;
import scala.util.Try;
/**
* Implementation of AsyncCacheLoader for loading {@code JsonWebToken}s.
*/
final class AsyncJwtLoader implements AsyncCacheLoader<String, JsonWebToken> {
private static final JsonFieldDefinition<String> ACCESS_TOKEN =
JsonFactory.newStringFieldDefinition("access_token");
private static final HttpHeader ACCEPT_JSON = Accept.create(MediaRanges.create(MediaTypes.APPLICATION_JSON));
private final Flow<HttpRequest, Try<HttpResponse>, NotUsed> httpFlow;
private final Materializer materializer;
private final HttpRequest tokenRequest;
AsyncJwtLoader(final ActorSystem actorSystem, final OAuthClientCredentials credentials) {
this(actorSystem, buildHttpFlow(Http.get(actorSystem)), credentials);
}
AsyncJwtLoader(final ActorSystem actorSystem, final Flow<HttpRequest, Try<HttpResponse>, NotUsed> httpFlow,
final OAuthClientCredentials credentials) {
tokenRequest = toTokenRequest(credentials.getTokenEndpoint(), credentials.getClientId(),
credentials.getClientSecret(), credentials.getRequestedScopes(), credentials.getAudience().orElse(null));
materializer = Materializer.createMaterializer(actorSystem);
this.httpFlow = httpFlow;
}
@Override
public CompletableFuture<? extends JsonWebToken> asyncLoad(final String key, final Executor executor) {
return Source.single(tokenRequest)
.via(httpFlow)
.flatMapConcat(this::asJsonWebToken)
.runWith(Sink.head(), materializer).toCompletableFuture();
}
private Source<JsonWebToken, NotUsed> asJsonWebToken(final Try<HttpResponse> tryResponse) {
if (tryResponse.isFailure()) {
return Source.failed(convertException(tryResponse.failed().get()));
} else {
return parseJwt(tryResponse.get());
}
}
private Source<JsonWebToken, NotUsed> parseJwt(final HttpResponse response) {
final boolean areStatusAndContentTypeExpected = response.status().isSuccess() &&
response.entity().getContentType().equals(ContentTypes.APPLICATION_JSON);
if (areStatusAndContentTypeExpected) {
return response.entity()
.getDataBytes()
.fold(ByteString.emptyByteString(), ByteString::concat)
.map(ByteString::utf8String)
.flatMapConcat(this::extractJwt)
.mapMaterializedValue(any -> NotUsed.getInstance());
} else {
final String description = String.format("Response status is <%d> and content type is <%s>.",
response.status().intValue(), response.entity().getContentType());
return Source.failed(getJwtInvalidExceptionForResponse().description(description).build());
}
}
private Source<JsonWebToken, NotUsed> extractJwt(final String body) {
try {
final var json = JsonObject.of(body);
return Source.single(ImmutableJsonWebToken.fromToken(json.getValueOrThrow(ACCESS_TOKEN)));
} catch (final NullPointerException | IllegalArgumentException | JsonRuntimeException |
DittoRuntimeException e) {
final JwtInvalidException jwtInvalid;
if (e instanceof JwtInvalidException jwtInvalidException) {
jwtInvalid = jwtInvalidException;
} else {
final var bodySummary = body.length() > 100 ? body.substring(0, 100) + "..." : body;
jwtInvalid = getJwtInvalidExceptionForResponse()
.description(String.format("Response body: <%s>", bodySummary))
.build();
}
return Source.failed(jwtInvalid);
}
}
private JwtInvalidException convertException(final Throwable error) {
if (error instanceof JwtInvalidException jwtInvalidException) {
return jwtInvalidException;
} else {
return JwtInvalidException.newBuilder()
.message(String.format("Request to token endpoint <%s> failed.", tokenRequest.getUri()))
.description(
String.format("Cause: %s: %s", error.getClass().getCanonicalName(), error.getMessage()))
.build();
}
}
private DittoRuntimeExceptionBuilder<JwtInvalidException> getJwtInvalidExceptionForResponse() {
return JwtInvalidException.newBuilder()
.message(String.format("Received invalid JSON web token response from <%s>.", tokenRequest.getUri()))
.description("Please verify that the token endpoint and client credentials are correct.");
}
private static HttpRequest toTokenRequest(final String tokenEndpoint,
final String clientId,
final String clientSecret,
final String scope,
@Nullable final String audience) {
final var body = asFormEncoded(clientId, clientSecret, scope, audience);
final var entity = HttpEntities.create(ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED, body);
return HttpRequest.POST(tokenEndpoint).withEntity(entity).addHeader(ACCEPT_JSON);
}
private static String asFormEncoded(final String clientId, final String clientSecret, final String scope, @Nullable final String audience) {
if (audience == null) {
return String.format("grant_type=client_credentials" +
"&client_id=%s" +
"&client_secret=%s" +
"&scope=%s",
UriEncoding.encodeAllButUnreserved(clientId),
UriEncoding.encodeAllButUnreserved(clientSecret),
UriEncoding.encodeAllButUnreserved(scope)
);
}
return String.format("grant_type=client_credentials" +
"&client_id=%s" +
"&client_secret=%s" +
"&scope=%s" +
"&audience=%s",
UriEncoding.encodeAllButUnreserved(clientId),
UriEncoding.encodeAllButUnreserved(clientSecret),
UriEncoding.encodeAllButUnreserved(scope),
UriEncoding.encodeAllButUnreserved(audience)
);
}
private static Flow<HttpRequest, Try<HttpResponse>, NotUsed> buildHttpFlow(final Http http) {
return Flow.<HttpRequest>create()
.mapAsync(1, request -> http.singleRequest(request)
.<Try<HttpResponse>>thenApply(Success::new)
.exceptionally(Failure::new)
);
}
}