Skip to content

Commit

Permalink
32818 Add dina-client module
Browse files Browse the repository at this point in the history
Initial classes to support OpenId Connect configuration and requests.
  • Loading branch information
cgendreau committed Jan 29, 2024
1 parent 4c8731c commit 88e1210
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dina-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DINA Client


51 changes: 51 additions & 0 deletions dina-client/pom.xml
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>
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;

}
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;

}
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);

}
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;
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module>dina-messaging</module>
<module>dina-search</module>
<module>dina-workbook</module>
<module>dina-client</module>
<module>dina-test-support</module>
</modules>

Expand Down

0 comments on commit 88e1210

Please sign in to comment.