|
| 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.security.KeyPair; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.UUID; |
| 12 | +import java.util.function.Supplier; |
| 13 | + |
| 14 | +import com.yoti.api.client.InitialisationException; |
| 15 | +import com.yoti.api.client.KeyPairSource; |
| 16 | +import com.yoti.api.client.spi.remote.KeyStreamVisitor; |
| 17 | +import com.yoti.api.client.spi.remote.call.ResourceException; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import com.fasterxml.jackson.databind.PropertyNamingStrategies; |
| 22 | + |
| 23 | +/** |
| 24 | + * The {@link AuthenticationTokenGenerator} is used for generation authorization tokens |
| 25 | + * that can be used for accessing Yoti services. An authorization token must have |
| 26 | + * a unique identifier, and an expiry timestamp. One or more scopes can be provided |
| 27 | + * to allow the authorization token access to different parts of Yoti systems. |
| 28 | + * <p> |
| 29 | + * The {@link AuthenticationTokenGenerator.Builder} can be accessed via {@code AuthorizationTokenGenerator.builder()} |
| 30 | + * method, and then configured via the fluent API. |
| 31 | + */ |
| 32 | +public class AuthenticationTokenGenerator { |
| 33 | + |
| 34 | + private static final Map<String, String> DEFAULT_FORM_VALUES; |
| 35 | + |
| 36 | + static { |
| 37 | + DEFAULT_FORM_VALUES = new HashMap<>(); |
| 38 | + DEFAULT_FORM_VALUES.put("grant_type", "client_credentials"); |
| 39 | + DEFAULT_FORM_VALUES.put("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"); |
| 40 | + } |
| 41 | + |
| 42 | + private final String sdkId; |
| 43 | + private final KeyPair keyPair; |
| 44 | + private final Supplier<String> jwtIdSupplier; |
| 45 | + private final FormRequestClient formRequestClient; |
| 46 | + private final SignedJwtGenerator signedJwtGenerator; |
| 47 | + |
| 48 | + private final String authApiUrl; |
| 49 | + private final ObjectMapper objectMapper; |
| 50 | + |
| 51 | + AuthenticationTokenGenerator( |
| 52 | + String sdkId, |
| 53 | + KeyPair keyPair, |
| 54 | + Supplier<String> jwtIdSupplier, |
| 55 | + FormRequestClient formRequestClient, |
| 56 | + SignedJwtGenerator signedJwtGenerator, |
| 57 | + ObjectMapper objectMapper) { |
| 58 | + this.sdkId = sdkId; |
| 59 | + this.keyPair = keyPair; |
| 60 | + this.jwtIdSupplier = jwtIdSupplier; |
| 61 | + this.formRequestClient = formRequestClient; |
| 62 | + this.signedJwtGenerator = signedJwtGenerator; |
| 63 | + this.objectMapper = objectMapper; |
| 64 | + |
| 65 | + authApiUrl = System.getProperty(Properties.PROPERTY_YOTI_AUTH_URL, Properties.DEFAULT_YOTI_AUTH_URL); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a new instance of {@link AuthenticationTokenGenerator.Builder} |
| 70 | + * |
| 71 | + * @return the builder |
| 72 | + */ |
| 73 | + public static AuthenticationTokenGenerator.Builder builder() { |
| 74 | + return new AuthenticationTokenGenerator.Builder(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a new authentication token, using the supplied scopes and comment. |
| 79 | + * |
| 80 | + * @param scopes a list of scopes to be used by the authentication token |
| 81 | + * @return a {@link CreateAuthenticationTokenResponse} containing information about the created token. |
| 82 | + * @throws Exception |
| 83 | + */ |
| 84 | + public CreateAuthenticationTokenResponse generate(List<String> scopes) throws ResourceException, IOException { |
| 85 | + String jwts = signedJwtGenerator.create(sdkId, keyPair, jwtIdSupplier, authApiUrl); |
| 86 | + |
| 87 | + Map<String, String> formParams = new HashMap<>(DEFAULT_FORM_VALUES); |
| 88 | + formParams.put("scope", String.join(" ", scopes)); |
| 89 | + formParams.put("client_assertion", jwts); |
| 90 | + |
| 91 | + String responseBody = formRequestClient.performRequest(authApiUrl, "POST", formParams); |
| 92 | + |
| 93 | + return objectMapper.readValue(responseBody, CreateAuthenticationTokenResponse.class); |
| 94 | + } |
| 95 | + |
| 96 | + public static final class Builder { |
| 97 | + |
| 98 | + private String sdkId; |
| 99 | + private KeyPairSource keyPairSource; |
| 100 | + private Supplier<String> jwtIdSupplier = () -> UUID.randomUUID().toString(); |
| 101 | + |
| 102 | + private Builder() {} |
| 103 | + |
| 104 | + /** |
| 105 | + * Sets the SDK ID that the authorization token will be generated against. |
| 106 | + * |
| 107 | + * @param sdkId the SDK ID |
| 108 | + * @return the builder for method chaining. |
| 109 | + */ |
| 110 | + public Builder withSdkId(String sdkId) { |
| 111 | + this.sdkId = sdkId; |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Sets the {@link KeyPairSource} that will be used to load the {@link KeyPair} |
| 117 | + * |
| 118 | + * @param keyPairSource the key pair source that will be used to load the {@link KeyPair} |
| 119 | + * @return the builder for method chaining. |
| 120 | + */ |
| 121 | + public Builder withKeyPairSource(KeyPairSource keyPairSource) { |
| 122 | + this.keyPairSource = keyPairSource; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Sets the supplier that will be used to generate a unique ID for the |
| 128 | + * authorization token. By default, this will be a UUID v4. |
| 129 | + * |
| 130 | + * @param jwtIdSupplier the supplier used for generating authorization token ID |
| 131 | + * @return the builder for method chaining. |
| 132 | + */ |
| 133 | + public Builder withJwtIdSupplier(Supplier<String> jwtIdSupplier) { |
| 134 | + this.jwtIdSupplier = jwtIdSupplier; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Builds an {@link AuthenticationTokenGenerator} using the values supplied to the {@link Builder}. |
| 140 | + * |
| 141 | + * @return the configured {@link AuthenticationTokenGenerator} |
| 142 | + */ |
| 143 | + public AuthenticationTokenGenerator build() { |
| 144 | + notNullOrEmpty(sdkId, "sdkId"); |
| 145 | + notNull(keyPairSource, "keyPairSource"); |
| 146 | + notNull(jwtIdSupplier, "jwtIdSupplier"); |
| 147 | + |
| 148 | + ObjectMapper objectMapper = new ObjectMapper() |
| 149 | + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE) |
| 150 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 151 | + |
| 152 | + return new AuthenticationTokenGenerator(sdkId, loadKeyPair(keyPairSource), jwtIdSupplier, new FormRequestClient(), new SignedJwtGenerator(), objectMapper); |
| 153 | + } |
| 154 | + |
| 155 | + private KeyPair loadKeyPair(KeyPairSource kpSource) throws InitialisationException { |
| 156 | + try { |
| 157 | + return kpSource.getFromStream(new KeyStreamVisitor()); |
| 158 | + } catch (IOException e) { |
| 159 | + throw new InitialisationException("Cannot load key pair", e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments