# IAM

> Manage Projects and Access Policies with IAM Service.

## Install Dependency

Package: @affinidi-tdk/iam-client

```bash
npm install @affinidi-tdk/iam-client --save
```

Check the latest version on the [npm registry](https://www.npmjs.com/package/@affinidi-tdk/iam-client) or view the source on [GitHub](https://github.com/affinidi/affinidi-tdk/tree/main/clients/typescript/iam-client).

## Classes and Methods

### Projects API

Use the Projects API to manage your projects including providing access to other users on your projects.

#### addPrincipalToProject

Adds a principal to the project.

Parameters

    request
    [AddUserToProjectInput](https://github.com/affinidi/affinidi-tdk/blob/main/clients/typescript/iam-client/docs/AddUserToProjectInput.md)
    Required
  Principal ID and principal type to add. principalType is "user" for a user account or "token" for a Personal Access Token.

Example

```typescript
import { ProjectsApi, Configuration, AddUserToProjectInput } from '@affinidi-tdk/iam-client'

const api = new ProjectsApi(
  new Configuration({ apiKey: authProvider.fetchProjectScopedToken.bind(authProvider) })
)

const request: AddUserToProjectInput = {
  principalId: '

',
  principalType: 'token',
}

const { data } = await api.addPrincipalToProject(request)
```

#### createProject

Creates a project for your account.

Parameters

    request
    [CreateProjectInput](https://github.com/affinidi/affinidi-tdk/blob/main/clients/typescript/iam-client/docs/CreateProjectInput.md)
    Required
  Project name and optional description.

Example

```typescript
import { ProjectsApi, Configuration, CreateProjectInput } from '@affinidi-tdk/iam-client'

const api = new ProjectsApi(
  new Configuration({ apiKey: '' })
)

const request: CreateProjectInput = {
  name: 'project_name',
  description: 'project_description',
}

const { data } = await api.createProject(request)
```

#### deletePrincipalFromProject

Removes a principal (user or PAT) from the project.

Parameters

    principalId
    string
    Required
  ID of the user or Personal Access Token (PAT).

    principalType
    string
    Required
  Type of the principal: "user" for a user account or "token" for a Personal Access Token.

Example

```typescript
import { ProjectsApi, Configuration } from '@affinidi-tdk/iam-client'

const api = new ProjectsApi(
  new Configuration({ apiKey: authProvider.fetchProjectScopedToken.bind(authProvider) })
)

await api.deletePrincipalFromProject('

', 'token')
```

#### listPrincipalsOfProject

Returns the list of principals assigned to the project.

No parameters required.

Example

```typescript
import { ProjectsApi, Configuration } from '@affinidi-tdk/iam-client'

const api = new ProjectsApi(
  new Configuration({ apiKey: authProvider.fetchProjectScopedToken.bind(authProvider) })
)

const { data } = await api.listPrincipalsOfProject()
```

#### listProject

Returns the list of projects.

No parameters required.

Example

```typescript
import { ProjectsApi, Configuration } from '@affinidi-tdk/iam-client'

const api = new ProjectsApi(
  new Configuration({ apiKey: '' })
)

const { data } = await api.listProject()
```

#### updateProject

Updates the project details.

Parameters

    projectId
    string
    Required
  ID of the project to update.

    request
    [UpdateProjectInput](https://github.com/affinidi/affinidi-tdk/blob/main/clients/typescript/iam-client/docs/UpdateProjectInput.md)
    Required
  Updated project name and description.

Example

```typescript
import { ProjectsApi, Configuration, UpdateProjectInput } from '@affinidi-tdk/iam-client'

const api = new ProjectsApi(
  new Configuration({ apiKey: '' })
)

const request: UpdateProjectInput = {
  name: 'project_name',
  description: 'project_description',
}

const { data } = await api.updateProject('

', request)
```

### Policies API

Use the Policies API to manage access to your projects and related resources for other users.

#### getPolicies

Retrieves the policy attached to a principal.

Parameters

    principalId
    string
    Required
  ID of the user or Personal Access Token (PAT).

    principalType
    string
    Required
  Type of the principal: "user" for a user account or "token" for a Personal Access Token.

Example

```typescript
import { PoliciesApi, Configuration } from '@affinidi-tdk/iam-client'

const api = new PoliciesApi(
  new Configuration({ apiKey: authProvider.fetchProjectScopedToken.bind(authProvider) })
)

const { data } = await api.getPolicies('

', 'token')
```

#### updatePolicies

Updates the policy for a principal.

Parameters

    principalId
    string
    Required
  ID of the user or Personal Access Token (PAT).

    principalType
    string
    Required
  Type of the principal: "user" for a user account or "token" for a Personal Access Token.

    policyDto
    [PolicyDto](https://github.com/affinidi/affinidi-tdk/blob/main/clients/typescript/iam-client/docs/PolicyDto.md)
    Required
  Policy rules object defining principals, actions, resources, and effect.

Example

```typescript
import { PoliciesApi, Configuration, PolicyDto } from '@affinidi-tdk/iam-client'

const api = new PoliciesApi(
  new Configuration({ apiKey: authProvider.fetchProjectScopedToken.bind(authProvider) })
)

const policyInfo: PolicyDto = {
  version: '2022-12-15',
  statement: [
    {
      principal: ['

'],
      action: [''],
      resource: [''],
      effect: 'Allow',
    },
  ],
}

await api.updatePolicies('

', 'token', policyInfo)
```

### Secure Token Exchange (STS) API

Use the STS (Secure Token Exchange) API to create a Project Scoped Token and get the current session information.

#### createProjectScopedToken

Creates a Project Scoped Token to access resources in the project.

Parameters

    request
    [CreateProjectScopedTokenInput](https://github.com/affinidi/affinidi-tdk/blob/main/clients/typescript/iam-client/docs/CreateProjectScopedTokenInput.md)
    Required
  Project ID to generate the token for.

Example

```typescript
import { StsApi, Configuration, CreateProjectScopedTokenInput } from '@affinidi-tdk/iam-client'

const api = new StsApi(
  new Configuration({ apiKey: '' })
)

const request: CreateProjectScopedTokenInput = {
  projectId: '

',
}

const { data } = await api.createProjectScopedToken(request)
```

#### whoami

Retrieves the principal information of the current session.

No parameters required.

Example

```typescript
import { StsApi, Configuration } from '@affinidi-tdk/iam-client'

const api = new StsApi(
  new Configuration({ apiKey: '' })
)

const { data } = await api.whoami()
```

### Tokens API

Use the Tokens API to create a Personal Access Token (PAT) to call Affinidi services on your behalf. Alternatively, use the Token command from Affinidi CLI.

#### createToken

Creates a Personal Access Token.

Parameters

    request
    [CreateTokenInput](https://github.com/affinidi/affinidi-tdk/blob/main/clients/typescript/iam-client/docs/CreateTokenInput.md)
    Required
  Token name and public key information used to authenticate the token.

Example

```typescript
import { TokensApi, Configuration, CreateTokenInput } from '@affinidi-tdk/iam-client'

const api = new TokensApi(
  new Configuration({ apiKey: '' })
)

const jwks = {
  keys: [
    {
      kid: 'kid',
      alg: algorithm,
      use: 'sig',
      kty: jwk.kty,
      n: jwk.n,
      e: jwk.e,
    },
  ],
}

const request: CreateTokenInput = {
  name: 'token_name',
  authenticationMethod: {
    type: 'PRIVATE_KEY',
    signingAlgorithm: algorithm,
    publicKeyInfo: { jwks },
  },
}

const { data } = await api.createToken(request)
```

#### deleteToken

Deletes a Personal Access Token.

Parameters

    tokenId
    string
    Required
  ID of the Personal Access Token to delete.

Example

```typescript
import { TokensApi, Configuration } from '@affinidi-tdk/iam-client'

const api = new TokensApi(
  new Configuration({ apiKey: '' })
)

await api.deleteToken('')
```

#### getToken

Retrieves the details of a Personal Access Token.

Parameters

    tokenId
    string
    Required
  ID of the Personal Access Token to retrieve.

Example

```typescript
import { TokensApi, Configuration } from '@affinidi-tdk/iam-client'

const api = new TokensApi(
  new Configuration({ apiKey: '' })
)

const { data } = await api.getToken('')
```

#### listToken

Returns the list of Personal Access Tokens.

No parameters required.

Example

```typescript
import { TokensApi, Configuration } from '@affinidi-tdk/iam-client'

const api = new TokensApi(
  new Configuration({ apiKey: '' })
)

const { data } = await api.listToken()
```

#### updateToken

Updates an existing Personal Access Token.

Parameters

    tokenId
    string
    Required
  ID of the Personal Access Token to update.

    request
    [UpdateTokenInput](https://github.com/affinidi/affinidi-tdk/blob/main/clients/typescript/iam-client/docs/UpdateTokenInput.md)
    Required
  Updated token name and public key information.

Example

```typescript
import { TokensApi, Configuration, UpdateTokenInput } from '@affinidi-tdk/iam-client'

const api = new TokensApi(
  new Configuration({ apiKey: '' })
)

const jwks = {
  keys: [
    {
      kid: 'kid',
      alg: algorithm,
      use: 'sig',
      kty: jwk.kty,
      n: jwk.n,
      e: jwk.e,
    },
  ],
}

const request: UpdateTokenInput = {
  name: 'token_name',
  authenticationMethod: {
    type: 'PRIVATE_KEY',
    signingAlgorithm: algorithm,
    publicKeyInfo: { jwks },
  },
}

const { data } = await api.updateToken('', request)
```
