# Auth Provider

> Generate Authorisation Token to call Affinidi Services.

## Install Dependency

Package: @affinidi-tdk/auth-provider

```bash
npm install @affinidi-tdk/auth-provider --save
```

Check the latest version on the [npm registry](https://www.npmjs.com/package/@affinidi-tdk/auth-provider) or view the source on [GitHub](https://github.com/affinidi/affinidi-tdk/tree/main/packages/jsii/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.

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

### API Gateway URL

Base API URL of Affinidi Services.

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

## Classes and Methods

### AuthProvider API

Generates the Project Scoped Token required for calling Affinidi services.

When instantiating the AuthProvider class, provide the Personal Access Token (PAT) information including the private key to generate the authorisation token.

Constructor parameters

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

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

    passphrase
    string
    Optional
  Passphrase of the private key, if configured.

    projectId
    string
    Required
  Project ID to generate the token for.

Example

```typescript
import { AuthProvider } from '@affinidi-tdk/auth-provider'

const authProvider = new AuthProvider({
  privateKey: '

',
  passphrase: '

',
  tokenId: '

',
  projectId: '

',
})
```

#### fetchProjectScopedToken

Returns the Project Scoped Token used to call Affinidi Services.

No parameters required.

Example

```typescript
import { AuthProvider } from '@affinidi-tdk/auth-provider'

const authProvider = new AuthProvider({
  privateKey: '

',
  passphrase: '

',
  tokenId: '

',
  projectId: '

',
})

const projectScopedToken = await authProvider.fetchProjectScopedToken()
```

#### createIotaToken

Creates an Affinidi Iota Framework token to generate the Iota Credentials used to sign 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

```typescript
import { AuthProvider } from '@affinidi-tdk/auth-provider'

const authProvider = new AuthProvider({
  privateKey: '

',
  passphrase: '

',
  tokenId: '

',
  projectId: '

',
})

const iotaToken = await authProvider.createIotaToken('', '')
```
