|
| 1 | +package io.github.primelib.primecodegenlib.java.feign.common.auth; |
| 2 | + |
| 3 | +import com.github.philippheuer.credentialmanager.domain.OAuth2Credential; |
| 4 | +import com.github.philippheuer.credentialmanager.identityprovider.DefaultOAuth2IdentityProvider; |
| 5 | +import com.github.philippheuer.credentialmanager.identityprovider.OAuth2IdentityProvider; |
| 6 | +import lombok.AccessLevel; |
| 7 | +import lombok.Data; |
| 8 | +import lombok.EqualsAndHashCode; |
| 9 | +import lombok.NoArgsConstructor; |
| 10 | +import lombok.experimental.Accessors; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.jetbrains.annotations.ApiStatus; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +import java.time.Instant; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.concurrent.locks.ReentrantLock; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +@Data |
| 23 | +@EqualsAndHashCode(callSuper = true) |
| 24 | +@Accessors(fluent = true) |
| 25 | +@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true) |
| 26 | +@Slf4j |
| 27 | +public class OAuth2ClientCredentialsAuthSpec extends AuthSpecBase { |
| 28 | + private final ReentrantLock refreshLock = new ReentrantLock(); |
| 29 | + |
| 30 | + @NotNull |
| 31 | + private String tokenEndpoint; |
| 32 | + @NotNull |
| 33 | + private String clientId; |
| 34 | + @NotNull |
| 35 | + private String clientSecret; |
| 36 | + |
| 37 | + @NotNull |
| 38 | + private String propertyKey = "Authorization"; |
| 39 | + @NotNull |
| 40 | + private String valueTemplate = "Bearer {token}"; |
| 41 | + |
| 42 | + @ApiStatus.Internal |
| 43 | + @NotNull |
| 44 | + private OAuth2IdentityProvider idp; |
| 45 | + |
| 46 | + @ApiStatus.Internal |
| 47 | + @Nullable |
| 48 | + private OAuth2Credential credential; |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructs a validated implementation of {@link OAuth2ClientCredentialsAuthSpec}. |
| 52 | + * |
| 53 | + * @param spec the specification to process |
| 54 | + */ |
| 55 | + public OAuth2ClientCredentialsAuthSpec(Consumer<OAuth2ClientCredentialsAuthSpec> spec) { |
| 56 | + spec.accept(this); |
| 57 | + validate(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Validates the Spec, will throw a exception if required parameters are missing |
| 62 | + * |
| 63 | + * @throws NullPointerException if a required parameter is missing |
| 64 | + * @throws IllegalArgumentException if a parameter has an invalid value |
| 65 | + */ |
| 66 | + public void validate() { |
| 67 | + Objects.requireNonNull(tokenEndpoint, "tokenEndpoint is a required parameter!"); |
| 68 | + Objects.requireNonNull(clientId, "clientId is a required parameter!"); |
| 69 | + Objects.requireNonNull(clientSecret, "clientSecret is a required parameter!"); |
| 70 | + Objects.requireNonNull(propertyKey, "propertyKey is required"); |
| 71 | + Objects.requireNonNull(valueTemplate, "valueTemplate is required"); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public Map<String, String> getHeaderMap() { |
| 76 | + if (credential == null || credential.getReceivedAt().plusSeconds(credential.getExpiresIn()).isBefore(Instant.now())) { |
| 77 | + refreshLock.lock(); |
| 78 | + try { |
| 79 | + if (credential == null || credential.getReceivedAt().plusSeconds(credential.getExpiresIn()).isBefore(Instant.now())) { |
| 80 | + credential = idp.getAppAccessToken(); |
| 81 | + } |
| 82 | + } finally { |
| 83 | + refreshLock.unlock(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return Map.of(propertyKey, valueTemplate.replace("{token}", credential.getAccessToken())); |
| 88 | + } |
| 89 | +} |
0 commit comments