|
| 1 | +package io.avaje.aws.client.cognito; |
| 2 | + |
| 3 | +import io.avaje.http.client.AuthToken; |
| 4 | +import io.avaje.http.client.AuthTokenProvider; |
| 5 | +import io.avaje.http.client.BasicAuthIntercept; |
| 6 | +import io.avaje.http.client.HttpClientRequest; |
| 7 | +import io.avaje.json.simple.SimpleMapper; |
| 8 | + |
| 9 | +import java.net.http.HttpResponse; |
| 10 | +import java.time.Instant; |
| 11 | + |
| 12 | +final class AmzCognitoAuthTokenProvider implements CognitoAuthTokenProvider.Builder { |
| 13 | + |
| 14 | + private String url; |
| 15 | + private String clientId; |
| 16 | + private String clientSecret; |
| 17 | + private String scope; |
| 18 | + |
| 19 | + @Override |
| 20 | + public CognitoAuthTokenProvider.Builder url(String url) { |
| 21 | + this.url = url; |
| 22 | + return this; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public CognitoAuthTokenProvider.Builder clientId(String clientId) { |
| 27 | + this.clientId = clientId; |
| 28 | + return this; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public CognitoAuthTokenProvider.Builder clientSecret(String clientSecret) { |
| 33 | + this.clientSecret = clientSecret; |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public CognitoAuthTokenProvider.Builder scope(String scope) { |
| 39 | + this.scope = scope; |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public AuthTokenProvider build() { |
| 45 | + return new Provider(url, clientId, clientSecret, scope); |
| 46 | + } |
| 47 | + |
| 48 | + private static final class Provider implements AuthTokenProvider { |
| 49 | + |
| 50 | + private static final SimpleMapper MAPPER = SimpleMapper.builder().build(); |
| 51 | + |
| 52 | + private final String url; |
| 53 | + private final String clientId; |
| 54 | + private final String scope; |
| 55 | + private final String authHeader; |
| 56 | + |
| 57 | + public Provider(String url, String clientId, String clientSecret, String scope) { |
| 58 | + this.url = url; |
| 59 | + this.clientId = clientId; |
| 60 | + this.scope = scope; |
| 61 | + this.authHeader = "Basic " + BasicAuthIntercept.encode(clientId, clientSecret); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public AuthToken obtainToken(HttpClientRequest request) { |
| 66 | + HttpResponse<String> res = request |
| 67 | + .url(url) |
| 68 | + .header("Authorization", authHeader) |
| 69 | + .formParam("grant_type", "client_credentials") |
| 70 | + .formParam("client_id", clientId) |
| 71 | + .formParam("scope", scope) |
| 72 | + .POST() |
| 73 | + .asString(); |
| 74 | + |
| 75 | + if (res.statusCode() != 200) { |
| 76 | + throw new IllegalStateException("Error response getting access token statusCode:" + res.statusCode() + " res:" + res); |
| 77 | + } |
| 78 | + return decodeAuthToken(res.body()); |
| 79 | + } |
| 80 | + |
| 81 | + private AuthToken decodeAuthToken(String responseBody) { |
| 82 | + final var responseMap = MAPPER.fromJsonObject(responseBody); |
| 83 | + final var accessToken = (String) responseMap.get("access_token"); |
| 84 | + final var expiresIn = (Long) responseMap.get("expires_in"); |
| 85 | + |
| 86 | + var validUntil = Instant.now() |
| 87 | + .plusSeconds(expiresIn) |
| 88 | + .minusSeconds(60); |
| 89 | + |
| 90 | + return AuthToken.of(accessToken, validUntil); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments