Skip to content

Commit

Permalink
feat: Java TDK auth & common packages (#403)
Browse files Browse the repository at this point in the history
* feat: boilerplate for java tdk

* feat: segregating commons and auth-provider

* feat: Adding AuthProvider

* feat: merge main

* feat: Added Jwt utility functions

* feat: Implementation of AuthProvider and JwtUtil functions

* feat: added javadoc and exception handling

* docs: update readme

* test: auth provider unit tests [FTL-18596] (#464)

* test: add more junits dependencies

* test: add 2 failing test scenarios

* refactor: add nested property with name

* test: add test with static method stub

* test: add invalid private-key with apikey endpoint stub

* refactor: move error message to constants file

* test: add happy path

* fix: check proper exception

* docs: added vault-data-manager-client to the
list

* Manual Integration tests and fix for authprovider junits

* fix: util => static

* fix: dotenv fixed

* feat: docs, linting, alignments

* test: auth provider shouldRefreshToken unit tests (#493)

* test: add first shouldRefreshToken function test

* test: when api-key endpoint call fails

* test: when api-key endpint succeeds but the project-score-token is invalid

* fix: remove line breaks

---------

Co-authored-by: maratsh <533533+maratsh@users.noreply.github.com>
Co-authored-by: Francis Pineda <francis.p@affinidi.com>
Co-authored-by: aeffinidi <86773100+aeffinidi@users.noreply.github.com>
Co-authored-by: Francis Pineda <135792176+affrncsp@users.noreply.github.com>
Co-authored-by: Robert Kwolek <robert.k@affinidi.com>
  • Loading branch information
6 people authored Jan 22, 2025
1 parent b0287b3 commit 35842bc
Show file tree
Hide file tree
Showing 28 changed files with 2,251 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Thumbs.db
.nx
.vscode

# Java
**/target
# Created by `dart pub`
.dart_tool/

Expand Down
7 changes: 7 additions & 0 deletions packages/java/auth.provider/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Optional values needed to generate auth token. These values can also be directly passsed to the AuthProvider build fuunction
PROJECT_ID=
KEY_ID=
TOKEN_ID=
PASSPHRASE=
PRIVATE_KEY=

90 changes: 90 additions & 0 deletions packages/java/auth.provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# auth.provider

AuthProvider package enables you to generate the required access token like Project Scoped Token to authenticate and access Affinidi Trust Network services.

For more information, please visit [https://github.com/affinidi/affinidi-tdk](https://github.com/affinidi/affinidi-tdk)

## Requirements

Building the package requires:

1. Java 1.8+
2. Maven (3.8.3+)

## Installation

To install the API client library to your local Maven repository, simply execute:

```shell
mvn clean install
```

To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:

```shell
mvn clean deploy
```

### Maven users

Add this dependency to your project's POM:

```xml
<dependency>
<groupId>com.affinidi.tdk</groupId>
<artifactId>auth.provider</artifactId>
<version>1.0.0</version>
</dependency>
```

### Others

At first generate the JAR by executing:

```shell
mvn clean package
```

## Usage

Sample usage to generate Project Scoped Token to call Affinidi TDK clients.

> You can store the required parameters like the Token details into an environment file.
```java

// Import classes:
import com.affinidi.tdk.authProvider.helper.JwtUtil;
import com.affinidi.tdk.authProvider.helper.AuthProvider;

public class AuthProviderConsumer {
public static void main(String arg[]) {
try{
// Create an authprovider from the values configured in the environment file
AuthProvider authProviderFromEnvFile = new AuthProvider.Configurations().buildWithEnv();
String projectToken = authProviderFromEnvFile.fetchProjectScopedToken();
System.out.println(projectToken);

boolean isExistingProjectScopeTokenValid = JwtUtil.validProjectTokenPresent(projectToken, authProviderFromEnvFile.apiGatewayUrl);
System.out.println(isExistingProjectScopeTokenValid);


// Alternatively you can create an auth provider by explicitly passing the configurations
AuthProvider authProviderWithPassedValues = new AuthProvider.Configurations()
.keyId("")
.projectId("")
.passphrase("")
.projectId("")
.tokenId("")
.build();
String projectToken = authProvider.fetchProjectScopedToken();
System.out.println(projectToken);


}catch(Exception e){
e.printStackTrace();
}
}
}

```
175 changes: 175 additions & 0 deletions packages/java/auth.provider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?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>
<packaging>jar</packaging>
<groupId>com.affinidi.tdk</groupId>
<artifactId>auth.provider</artifactId>
<version>1.0</version>
<name>auth.provider</name>
<url>https://github.com/affinidi/affinidi-tdk</url>
<licenses>
<license>
<name>Apache-2.0</name>
<url>https://github.com/affinidi/affinidi-tdk/blob/main/LICENSE</url>
<distribution>repo</distribution>
</license>
</licenses>

<developers>
<developer>
<name>Affinidi</name>
<email>...</email>
<organization>Affinidi</organization>
<organizationUrl>https://affinidi.com</organizationUrl>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<apache-http-version>5.4.1</apache-http-version>
<junit-version>5.11.0</junit-version>
<cdimascio-version>2.2.0</cdimascio-version>
<jersey-version>3.1.7</jersey-version>
<affinidi-common-version>1.0</affinidi-common-version>
<jsonwebtoken-version>0.12.6</jsonwebtoken-version>
<mockito-version>3.12.4</mockito-version>
<wiremock-version>3.10.0</wiremock-version>
<gson-version>2.10.1</gson-version>
<slf4j.version>2.0.16</slf4j.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>${cdimascio-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>${wiremock-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.affinidi.tdk</groupId>
<artifactId>common</artifactId>
<version>${affinidi-common-version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jsonwebtoken-version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jsonwebtoken-version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jsonwebtoken-version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${apache-http-version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson-version}</version>
</dependency>

</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to
parent pom) -->
<plugins>
<!-- clean lifecycle, see
https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see
https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.5.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see
https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Loading

0 comments on commit 35842bc

Please sign in to comment.