-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial classes to support OpenId Connect configuration and requests.
- Loading branch information
Showing
7 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# DINA Client | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.github.aafc-bicoe</groupId> | ||
<artifactId>dina-base-parent</artifactId> | ||
<version>0.116-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>dina-client</artifactId> | ||
<name>dina-client</name> | ||
<description>DINA client</description> | ||
<url>https://github.com/AAFC-BICoE/dina-base-api</url> | ||
|
||
<properties> | ||
<okhttp.version>4.9.0</okhttp.version> | ||
<retrofit.version>2.9.0</retrofit.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>${okhttp.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.retrofit2</groupId> | ||
<artifactId>retrofit</artifactId> | ||
<version>${retrofit.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.squareup.retrofit2</groupId> | ||
<artifactId>converter-jackson</artifactId> | ||
<version>${retrofit.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<licenses> | ||
<license> | ||
<name>MIT License</name> | ||
<url>https://opensource.org/licenses/mit-license</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
dina-client/src/main/java/ca/gc/aafc/dina/client/config/OpenIdConnectConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ca.gc.aafc.dina.client.config; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class OpenIdConnectConfig { | ||
|
||
/** | ||
* for Keycloak it should look like .../realms/dina/protocol/openid-connect/ | ||
*/ | ||
private String openIdConnectBaseUrl; | ||
|
||
private String clientId; | ||
private String username; | ||
private String password; | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
dina-client/src/main/java/ca/gc/aafc/dina/client/token/AccessToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ca.gc.aafc.dina.client.token; | ||
|
||
import lombok.Data; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
||
/** | ||
* Represents an access token response from an OpenId Connect endpoint. | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
@Data | ||
public class AccessToken { | ||
|
||
private String clientId; | ||
private String tokenType; | ||
private String accessToken; | ||
|
||
private String refreshToken; | ||
private int expiresIn; | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
dina-client/src/main/java/ca/gc/aafc/dina/client/token/AccessTokenApiCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ca.gc.aafc.dina.client.token; | ||
|
||
import java.util.Map; | ||
import retrofit2.Call; | ||
import retrofit2.http.FieldMap; | ||
import retrofit2.http.FormUrlEncoded; | ||
import retrofit2.http.POST; | ||
|
||
/** | ||
* Retrofit based API call to an OpenId Connect endpoint to get or refresh tokens. | ||
*/ | ||
public interface AccessTokenApiCall { | ||
|
||
@FormUrlEncoded | ||
@POST("token") | ||
Call<AccessToken> callAccessTokenEndpoint(@FieldMap Map<String, Object> accessTokenRequest); | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
dina-client/src/main/java/ca/gc/aafc/dina/client/token/AccessTokenRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ca.gc.aafc.dina.client.token; | ||
|
||
import java.util.Map; | ||
|
||
import ca.gc.aafc.dina.client.config.OpenIdConnectConfig; | ||
|
||
public class AccessTokenRequest { | ||
|
||
private enum GrantType {PASSWORD, REFRESH_TOKEN} | ||
|
||
private final String clientId; | ||
private final GrantType grantType; | ||
|
||
private final String username; | ||
private final String password; | ||
|
||
private final String refreshToken; | ||
|
||
public static AccessTokenRequest newPasswordBased(String clientId, String username, String password) { | ||
return new AccessTokenRequest(GrantType.PASSWORD, clientId, username, password, null); | ||
} | ||
|
||
public static AccessTokenRequest newPasswordBased(OpenIdConnectConfig config) { | ||
return new AccessTokenRequest(GrantType.PASSWORD, config.getClientId(), config.getUsername(), config.getPassword(), null); | ||
} | ||
|
||
public static AccessTokenRequest newRefreshTokenBased(String clientId, String refreshToken) { | ||
return new AccessTokenRequest(GrantType.REFRESH_TOKEN, clientId, null, null, refreshToken); | ||
} | ||
|
||
private AccessTokenRequest(GrantType grantType, String clientId, String username, String password, String refreshToken) { | ||
this.grantType = grantType; | ||
this.clientId = clientId; | ||
this.username = username; | ||
this.password = password; | ||
this.refreshToken = refreshToken; | ||
} | ||
|
||
public Map<String, Object> toFieldMap() { | ||
if (grantType == GrantType.PASSWORD) { | ||
return Map.of( | ||
"client_id", clientId, | ||
"grant_type", grantType.name().toLowerCase(), | ||
"username", username, | ||
"password", password); | ||
} else if (grantType == GrantType.REFRESH_TOKEN) { | ||
return Map.of( | ||
"client_id", clientId, | ||
"grant_type", grantType.name().toLowerCase(), | ||
"refresh_token", refreshToken); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters