# Auth Provider

> Generate Authorisation Token to call Affinidi Services.

## Install Dependency

Package: pkg:maven/com.affinidi.tdk/auth.provider

```xml

    com.affinidi.tdk
    auth.provider

```

Check the latest version on the [Maven Central repository](https://central.sonatype.com/artifact/com.affinidi.tdk/auth.provider) or view the source on [GitHub](https://github.com/affinidi/affinidi-tdk/tree/main/packages/java/auth.provider).

## API Endpoints

URLs required to initiate integration with Affinidi TDK.

### Token Endpoint

Endpoint to generate access token to call Affinidi Services with TDK.

```bash
https://apse1.auth.developer.affinidi.io/auth/oauth2/token
```

### API Gateway URL

Base API URL of Affinidi Services.

```bash
https://apse1.api.affinidi.io
```

## Classes and Methods

### AuthProvider API

Used to generate Project Scoped Token required for calling Affinidi services (Clients).

When initiating the AuthProvider class, provide the Token information including the public/private key information to generate the Authorisation Token required.

#### AuthProvider (constructor)

Initialises AuthProvider with the Personal Access Token credentials required to generate authorisation tokens.

Parameters

    projectId
    String
    Required
  ID of the project for which to generate the token.

    privateKey
    String
    Required
  Private key string used to sign the token.

    passphrase
    String
    Optional
  Passphrase for the private key, if one is configured.

    tokenId
    String
    Required
  ID of the Personal Access Token (PAT).

Example

```java
import com.affinidi.tdk.authProvider.AuthProvider;

try {
    // Pass values directly
    AuthProvider authProvider = new AuthProvider.Configurations()
        .projectId("

")
        .privateKey("

")
        .passphrase("

")
        .tokenId("

")
        .build();

    // Or read from environment variables
    AuthProvider authProviderFromEnv = new AuthProvider.Configurations().buildWithEnv();
} catch (Exception e) {
    e.printStackTrace();
}
```

#### fetchProjectScopedToken

Returns the generated project-scoped token for calling Affinidi services.

No parameters required.

Example

```java
import com.affinidi.tdk.authProvider.AuthProvider;

try {
    AuthProvider authProvider = new AuthProvider.Configurations()
        .projectId("

")
        .privateKey("

")
        .passphrase("

")
        .tokenId("

")
        .build();

    String projectToken = authProvider.fetchProjectScopedToken();
    System.out.println(projectToken);
} catch (Exception e) {
    e.printStackTrace();
}
```

#### createIotaToken

Creates an Affinidi Iota Framework token to generate Iota Credentials for signing the request token.

Parameters

    iotaConfigId
    String
    Required
  ID of the Iota configuration.

    did
    String
    Required
  DID of the logged-in user, obtained from the ID token provided by [Affinidi Login](/products/affinidi-elements/affinidi-login/how-affinidi-login-works.md).

    iotaSessionId
    String
    Optional
  ID of the initialised Iota session.

Example

```java
import com.affinidi.tdk.authProvider.AuthProvider;
import com.affinidi.tdk.authProvider.types.IotaJwtOutput;

try {
    AuthProvider authProvider = new AuthProvider.Configurations()
        .projectId("

")
        .privateKey("

")
        .passphrase("

")
        .tokenId("

")
        .build();

    IotaJwtOutput iotaToken = authProvider.signIotaJwt(
        "",
        "",
        ""
    );
    System.out.println(iotaToken.getIotaJwt());
} catch (Exception e) {
    e.printStackTrace();
}
```
