|
| 1 | +package com.yoti.auth; |
| 2 | + |
| 3 | +import static com.yoti.validation.Validation.notNull; |
| 4 | +import static com.yoti.validation.Validation.notNullOrEmpty; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.MalformedURLException; |
| 8 | +import java.net.URL; |
| 9 | +import java.security.KeyPair; |
| 10 | +import java.time.OffsetDateTime; |
| 11 | +import java.util.Date; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.UUID; |
| 16 | +import java.util.function.Supplier; |
| 17 | + |
| 18 | +import com.yoti.api.client.InitialisationException; |
| 19 | +import com.yoti.api.client.KeyPairSource; |
| 20 | +import com.yoti.api.client.spi.remote.KeyStreamVisitor; |
| 21 | +import com.yoti.api.client.spi.remote.call.ResourceException; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import io.jsonwebtoken.Jwts; |
| 26 | + |
| 27 | +/** |
| 28 | + * The {@link AuthenticationTokenGenerator} is used for generation authorization tokens |
| 29 | + * that can be used for accessing Yoti services. An authorization token must have |
| 30 | + * a unique identifier, and an expiry timestamp. One or more scopes can be provided |
| 31 | + * to allow the authorization token access to different parts of Yoti systems. |
| 32 | + * <p> |
| 33 | + * The {@link AuthenticationTokenGenerator.Builder} can be accessed via {@code AuthorizationTokenGenerator.builder()} |
| 34 | + * method, and then configured via the fluent API. |
| 35 | + */ |
| 36 | +public class AuthenticationTokenGenerator { |
| 37 | + |
| 38 | + private final String sdkId; |
| 39 | + private final KeyPair keyPair; |
| 40 | + private final Supplier<String> jwtIdSupplier; |
| 41 | + private final FormRequestClient formRequestClient; |
| 42 | + |
| 43 | + private final URL authApiUrl; |
| 44 | + private final ObjectMapper objectMapper; |
| 45 | + |
| 46 | + AuthenticationTokenGenerator( |
| 47 | + String sdkId, |
| 48 | + KeyPair keyPair, |
| 49 | + Supplier<String> jwtIdSupplier, |
| 50 | + FormRequestClient formRequestClient, |
| 51 | + ObjectMapper objectMapper) { |
| 52 | + this.sdkId = sdkId; |
| 53 | + this.keyPair = keyPair; |
| 54 | + this.jwtIdSupplier = jwtIdSupplier; |
| 55 | + this.formRequestClient = formRequestClient; |
| 56 | + this.objectMapper = objectMapper; |
| 57 | + |
| 58 | + try { |
| 59 | + authApiUrl = new URL(System.getProperty(Properties.PROPERTY_YOTI_AUTH_URL, Properties.DEFAULT_YOTI_AUTH_URL)); |
| 60 | + } catch (MalformedURLException e) { |
| 61 | + throw new IllegalStateException("Invalid Yoti auth url", e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new instance of {@link AuthenticationTokenGenerator.Builder} |
| 67 | + * |
| 68 | + * @return the builder |
| 69 | + */ |
| 70 | + public static AuthenticationTokenGenerator.Builder builder() { |
| 71 | + return new AuthenticationTokenGenerator.Builder(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Creates a new authentication token, using the supplied scopes and comment. |
| 76 | + * |
| 77 | + * @param scopes a list of scopes to be used by the authentication token |
| 78 | + * @return a {@link CreateAuthenticationTokenResponse} containing information about the created token. |
| 79 | + * @throws ResourceException if something was incorrect with the request to the Yoti authentication service |
| 80 | + * @throws IOException |
| 81 | + */ |
| 82 | + public CreateAuthenticationTokenResponse generate(List<String> scopes) throws ResourceException, IOException { |
| 83 | + notNullOrEmpty(scopes, "scopes"); |
| 84 | + |
| 85 | + String jwts = createSignedJwt(sdkId, keyPair, jwtIdSupplier, authApiUrl); |
| 86 | + |
| 87 | + Map<String, String> formParams = new HashMap<>(); |
| 88 | + formParams.put("grant_type", "client_credentials"); |
| 89 | + formParams.put("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"); |
| 90 | + formParams.put("scope", String.join(" ", scopes)); |
| 91 | + formParams.put("client_assertion", jwts); |
| 92 | + |
| 93 | + byte[] responseBody = formRequestClient.performRequest(authApiUrl, "POST", formParams); |
| 94 | + |
| 95 | + return objectMapper.readValue(responseBody, CreateAuthenticationTokenResponse.class); |
| 96 | + } |
| 97 | + |
| 98 | + private String createSignedJwt(String sdkId, KeyPair keyPair, Supplier<String> jwtIdSupplier, URL authApiUrl) { |
| 99 | + String sdkIdProperty = String.format("sdk:%s", sdkId); |
| 100 | + OffsetDateTime now = OffsetDateTime.now(); |
| 101 | + return Jwts.builder() |
| 102 | + .issuer(sdkIdProperty) |
| 103 | + .subject(sdkIdProperty) |
| 104 | + .id(jwtIdSupplier.get()) |
| 105 | + .audience() |
| 106 | + .add(authApiUrl.toString()) |
| 107 | + .and() |
| 108 | + .expiration(new Date(now.plusMinutes(5).toInstant().toEpochMilli())) |
| 109 | + .issuedAt(new Date(now.toInstant().toEpochMilli())) |
| 110 | + .header() |
| 111 | + .add("alg", "PS384") |
| 112 | + .add("typ", "JWT") |
| 113 | + .and() |
| 114 | + .signWith(keyPair.getPrivate(), Jwts.SIG.PS384) |
| 115 | + .compact(); |
| 116 | + } |
| 117 | + |
| 118 | + public static final class Builder { |
| 119 | + |
| 120 | + private String sdkId; |
| 121 | + private KeyPairSource keyPairSource; |
| 122 | + private Supplier<String> jwtIdSupplier = () -> UUID.randomUUID().toString(); |
| 123 | + |
| 124 | + private Builder() { |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Sets the SDK ID that the authorization token will be generated against. |
| 129 | + * |
| 130 | + * @param sdkId the SDK ID |
| 131 | + * @return the builder for method chaining. |
| 132 | + */ |
| 133 | + public Builder withSdkId(String sdkId) { |
| 134 | + this.sdkId = sdkId; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Sets the {@link KeyPairSource} that will be used to load the {@link KeyPair} |
| 140 | + * |
| 141 | + * @param keyPairSource the key pair source that will be used to load the {@link KeyPair} |
| 142 | + * @return the builder for method chaining. |
| 143 | + */ |
| 144 | + public Builder withKeyPairSource(KeyPairSource keyPairSource) { |
| 145 | + this.keyPairSource = keyPairSource; |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Sets the supplier that will be used to generate a unique ID for the |
| 151 | + * authorization token. By default, this will be a UUID v4. |
| 152 | + * |
| 153 | + * @param jwtIdSupplier the supplier used for generating authorization token ID |
| 154 | + * @return the builder for method chaining. |
| 155 | + */ |
| 156 | + public Builder withJwtIdSupplier(Supplier<String> jwtIdSupplier) { |
| 157 | + this.jwtIdSupplier = jwtIdSupplier; |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Builds an {@link AuthenticationTokenGenerator} using the values supplied to the {@link Builder}. |
| 163 | + * |
| 164 | + * @return the configured {@link AuthenticationTokenGenerator} |
| 165 | + */ |
| 166 | + public AuthenticationTokenGenerator build() { |
| 167 | + notNullOrEmpty(sdkId, "sdkId"); |
| 168 | + notNull(keyPairSource, "keyPairSource"); |
| 169 | + notNull(jwtIdSupplier, "jwtIdSupplier"); |
| 170 | + |
| 171 | + ObjectMapper objectMapper = new ObjectMapper() |
| 172 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 173 | + |
| 174 | + return new AuthenticationTokenGenerator( |
| 175 | + sdkId, |
| 176 | + loadKeyPair(keyPairSource), |
| 177 | + jwtIdSupplier, |
| 178 | + new FormRequestClient(), |
| 179 | + objectMapper |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + private KeyPair loadKeyPair(KeyPairSource kpSource) throws InitialisationException { |
| 184 | + try { |
| 185 | + return kpSource.getFromStream(new KeyStreamVisitor()); |
| 186 | + } catch (IOException e) { |
| 187 | + throw new InitialisationException("Cannot load key pair", e); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments