IAM

Manage Projects and Access Policies with IAM Service.

Install Dependency

Package: @affinidi-tdk/iam-client

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

Check the latest version on the npm registry or view the source on GitHub.

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 Required
Principal ID and principal type to add. principalType is "user" for a user account or "token" for a Personal Access Token.

Example

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

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

const request: AddUserToProjectInput = {
  principalId: '<PAT_ID>',
  principalType: 'token',
}

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

createProject

Creates a project for your account.

Parameters

request CreateProjectInput Required
Project name and optional description.

Example

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

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

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

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

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

await api.deletePrincipalFromProject('<PAT_ID>', 'token')

listPrincipalsOfProject

Returns the list of principals assigned to the project.

No parameters required.

Example

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

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

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

const { data } = await api.listProject()

updateProject

Updates the project details.

Parameters

projectId string Required
ID of the project to update.
request UpdateProjectInput Required
Updated project name and description.

Example

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

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

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

const { data } = await api.updateProject('<PROJECT_ID>', 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

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

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

const { data } = await api.getPolicies('<PAT_ID>', '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 Required
Policy rules object defining principals, actions, resources, and effect.

Example

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: ['<PRINCIPAL_ID>'],
      action: [''],
      resource: [''],
      effect: 'Allow',
    },
  ],
}

await api.updatePolicies('<PAT_ID>', '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

Project ID to generate the token for.

Example

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

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

const request: CreateProjectScopedTokenInput = {
  projectId: '<PROJECT_ID>',
}

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

whoami

Retrieves the principal information of the current session.

No parameters required.

Example

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

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

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 Required
Token name and public key information used to authenticate the token.

Example

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

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

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

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

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

await api.deleteToken('<TOKEN_ID>')

getToken

Retrieves the details of a Personal Access Token.

Parameters

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

Example

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

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

const { data } = await api.getToken('<TOKEN_ID>')

listToken

Returns the list of Personal Access Tokens.

No parameters required.

Example

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

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

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 Required
Updated token name and public key information.

Example

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

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

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('<TOKEN_ID>', request)