|
1 | 1 | package com.compassion.commons.rest.client.auth; |
2 | 2 |
|
3 | | -import java.util.concurrent.TimeUnit; |
| 3 | +import java.io.IOException; |
4 | 4 |
|
5 | 5 | import com.compassion.commons.rest.client.WebParam.WebHeader; |
6 | 6 | import com.compassion.commons.rest.client.WebParam.WebQueryParam; |
|
11 | 11 |
|
12 | 12 | import jakarta.ws.rs.client.ClientBuilder; |
13 | 13 | import lombok.Getter; |
| 14 | +import lombok.NoArgsConstructor; |
14 | 15 | import lombok.extern.log4j.Log4j2; |
15 | 16 |
|
16 | 17 | @Log4j2 |
17 | | -@Getter |
18 | | -@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) |
19 | | -public class AccessToken { |
20 | | - private static final String SCOPE_KEY = "scope", GRANT_TYPE = "grant_type", |
21 | | - CLIENT_CREDS = "client_credentials", DEF_ENDPOINT = "/oauth2/token"; |
| 18 | +public abstract class AccessToken { |
22 | 19 |
|
23 | | - private String accessToken; |
24 | | - private int expiresIn; |
25 | | - private String tokenType; |
26 | | - |
27 | | - @JsonIgnore |
28 | | - private long expiresAfter; |
| 20 | + public static IAccessToken acquire(OAuthMethod method, OAuthAwareConfig config) throws IOException { |
| 21 | + log.info("Acquiring new access token for {}", config.baseUrl()); |
| 22 | + |
| 23 | + try (var client = new RestClient(ClientBuilder.newBuilder())) { |
| 24 | + var ret = client.request(RecursiveTarget.newTarget(config.baseUrl()), config.authCreds()) |
| 25 | + .post(Entity.json(null)); |
| 26 | + |
| 27 | + if (ret.getStatus() < 200 || ret.getStatus() >= 300) { |
| 28 | + throw new IOException( |
| 29 | + String.format("Retrieving access token failed with status code %d and details: %s", ret.getStatus(), ret.readEntity(String.class))); |
| 30 | + } |
| 31 | + return ret.readEntity(method.getPojo()); |
| 32 | + } |
| 33 | + } |
29 | 34 |
|
30 | | - public void setExpiresIn(int expiresIn) { |
31 | | - this.expiresIn = expiresIn; |
32 | | - this.expiresAfter = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expiresIn - 5); |
33 | | - log.info("Access token valid until {}", String.format("%1$tm/%1$td %1$tI:%1$tM:%1$tS", expiresAfter)); |
| 35 | + public interface IAccessToken { |
| 36 | + Boolean checkActive(); |
| 37 | + WebParam providedSessionCreds(); |
34 | 38 | } |
35 | 39 |
|
36 | | - public static AccessToken acquire(AuthEnvironment env, String scope, WebHeader httpAuth) { |
37 | | - log.info("Acquiring new access token for {}", env.getAuthSubdomain()); |
| 40 | + @NoArgsConstructor @AllArgsConstructor @Getter |
| 41 | + public enum OAuthMethod { |
| 42 | + SALESFORCE(SFSessionResponse.class), |
| 43 | + OATMEAL(OatmealSessionResponse.class); |
38 | 44 |
|
39 | | - try (var client = new RestClient(ClientBuilder.newBuilder())) { |
40 | | - client.setBase(String.format("https://%s.ci.org", env.getAuthSubdomain())) |
41 | | - .addPerpetualParams(httpAuth); |
42 | | - |
43 | | - return client.request(RecursiveTarget.newTarget(DEF_ENDPOINT), WebQueryParam.of(GRANT_TYPE, CLIENT_CREDS), |
44 | | - WebQueryParam.of(SCOPE_KEY, scope)) |
45 | | - .get(AccessToken.class); |
46 | | - } |
| 45 | + private Class<? extends IAccessToken> pojo; |
| 46 | + } |
| 47 | + |
| 48 | + public interface OAuthAwareConfig { |
| 49 | + WebParam authCreds(); |
| 50 | + WebParam sessionCreds(); |
| 51 | + String getScope(); |
| 52 | + String baseUrl(); |
47 | 53 | } |
48 | 54 | } |
0 commit comments