# Auth Provider

> Generate Authorisation Token to call Affinidi Services.

## Install Dependency

Package: affinidi_tdk_auth_provider

```bash
dart pub add affinidi_tdk_auth_provider
```

Check the latest version on [pub.dev](https://pub.dev/packages/affinidi_tdk_auth_provider) or view the source on [GitHub](https://github.com/affinidi/affinidi-tdk-dart/tree/main/packages/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.

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

### API Gateway URL

Base API URL of Affinidi Services.

```dart
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.

#### Required Parameters

- tokenId: ID of the Personal Access Token (PAT). Use TOKEN_ID as the environment variable for Java.

- passphrase: Passphrase of the Private key if configured. Use PASSPHRASE as the environment variable for Java.

- privateKey: Private key string to use to sign the token. Use PRIVATE_KEY as the environment variable for Java.

- projectId: Project ID to generate the Token. Use PROJECT_ID as the environment variable for Java.

##### Example

```dart
import 'package:affinidi_tdk_auth_provider/affinidi_tdk_auth_provider.dart';

// NOTE: set your variables for PAT
final privateKey = '

';
final passphrase = '

';
final tokenId = '

';
final projectId = '

';

final authProvider = AuthProvider(
    privateKey: privateKey,
    passphrase: passphrase,
    tokenId: tokenId,
    projectId: projectId,    
);
```

#### fetchProjectScopedToken

Return the generated Project Scoped Token to use for calling the Affinidi Services.

No parameters required.

Example

```dart
import 'package:affinidi_tdk_auth_provider/affinidi_tdk_auth_provider.dart';

final privateKey = '

';
final passphrase = '

';
final tokenId = '

';
final projectId = '

';

final authProvider = AuthProvider(
    privateKey: privateKey,
    passphrase: passphrase,
    tokenId: tokenId,
    projectId: projectId,    
);

final projectScopedToken = await provider.fetchProjectScopedToken();
```

#### createIotaToken

Create an Affinidi Iota Framework token to generate the Iota Credentials to sign the request token.

Parameters

    iotaConfigId
    String
    Required
  The ID of the Iota configuration.

    did
    String
    Required
  The 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
  The ID of the initialised Iota session.

Example

```dart
import 'package:affinidi_tdk_auth_provider/affinidi_tdk_auth_provider.dart';

final privateKey = '

';
final passphrase = '

';
final tokenId = '

';
final projectId = '

';

final authProvider = AuthProvider(
    privateKey: privateKey,
    passphrase: passphrase,
    tokenId: tokenId,
    projectId: projectId,    
);

final iotaToken = provider.createIotaToken(iotaConfigId: "", did: "");
```
