# IAM

> Manage Projects and Access Policies with IAM Service.

## Install Dependency

Package: affinidi-tdk/affinidi-tdk-php

```bash
composer require affinidi-tdk/affinidi-tdk-php
```

Check the latest version on the [Packagist repository](https://packagist.org/packages/affinidi-tdk/affinidi-tdk-php) or view the source on [GitHub](https://github.com/affinidi/affinidi-tdk-php/tree/main/src/Clients/IamClient).

## 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 to grant access.

Parameters

    addUserToProjectInput
    [AddUserToProjectInput](https://github.com/affinidi/affinidi-tdk-php/tree/main/src/Clients/IamClient/docs/Model/AddUserToProjectInput.md)
    Required
  Principal ID and principal type (user or token).

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\ProjectsApi(new GuzzleHttp\Client(), $config);

try {
    $request = array(
        'principalId' => '

',
        'principalType' => 'token',
    );
    $result = $apiInstance->addPrincipalToProject($request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### createProject

Creates a project for your account.

Parameters

    createProjectInput
    [CreateProjectInput](https://github.com/affinidi/affinidi-tdk-php/tree/main/src/Clients/IamClient/docs/Model/CreateProjectInput.md)
    Required
  Project name and description.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\ProjectsApi(new GuzzleHttp\Client(), $config);

try {
    $request = array(
        'name' => 'project_name',
        'description' => 'project_description',
    );
    $result = $apiInstance->createProject($request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### deletePrincipalFromProject

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

Parameters

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

    principalType
    string
    Required
  Type of the principal: user for a user or token for a PAT.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\ProjectsApi(new GuzzleHttp\Client(), $config);

try {
    $result = $apiInstance->deletePrincipalFromProject('

', 'token');
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### listPrincipalsOfProject

Returns the list of principals assigned to the project.

No parameters required.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\ProjectsApi(new GuzzleHttp\Client(), $config);

try {
    $result = $apiInstance->listPrincipalsOfProject();
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### listProject

Returns the list of projects.

No parameters required.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\ProjectsApi(new GuzzleHttp\Client(), $config);

try {
    $result = $apiInstance->listProject();
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### updateProject

Updates the project details.

Parameters

    projectId
    string
    Required
  ID of the project to update.

    updateProjectInput
    [UpdateProjectInput](https://github.com/affinidi/affinidi-tdk-php/tree/main/src/Clients/IamClient/docs/Model/UpdateProjectInput.md)
    Required
  Updated project name and description.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\ProjectsApi(new GuzzleHttp\Client(), $config);

try {
    $request = array(
        'name' => 'project_name',
        'description' => 'project_description',
    );
    $result = $apiInstance->updateProject('

', $request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

### 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 or token for a PAT.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\PoliciesApi(new GuzzleHttp\Client(), $config);

try {
    $result = $apiInstance->getPolicies('

', 'token');
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### 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 or token for a PAT.

    policyDto
    [PolicyDto](https://github.com/affinidi/affinidi-tdk-php/tree/main/src/Clients/IamClient/docs/Model/PolicyDto.md)
    Required
  Policy rules, including principals, actions, resources, and effect.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\PoliciesApi(new GuzzleHttp\Client(), $config);

try {
    $request = array(
        'version' => '2022-12-15',
        'statement' => array(
            array(
                'principal' => array('

'),
                'action' => array(),
                'resource' => array(),
                'effect' => 'Allow',
            ),
        ),
    );
    $result = $apiInstance->updatePolicies('

', 'token', $request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

### 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

    createProjectScopedTokenInput
    [CreateProjectScopedTokenInput](https://github.com/affinidi/affinidi-tdk-php/tree/main/src/Clients/IamClient/docs/Model/CreateProjectScopedTokenInput.md)
    Required
  Project ID for which to generate the token.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\StsApi(new GuzzleHttp\Client(), $config);

try {
    $request = array(
        'projectId' => '

',
    );
    $result = $apiInstance->createProjectScopedToken($request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### whoami

Retrieves the principal information of the current user.

No parameters required.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\StsApi(new GuzzleHttp\Client(), $config);

try {
    $result = $apiInstance->whoami();
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

### 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

    createTokenInput
    [CreateTokenInput](https://github.com/affinidi/affinidi-tdk-php/tree/main/src/Clients/IamClient/docs/Model/CreateTokenInput.md)
    Required
  Token name and public key authentication method details.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\TokensApi(new GuzzleHttp\Client(), $config);

try {
    $jwks = array(
        'keys' => array(
            array(
                'kty' => '',
                'n' => '',
                'e' => '',
                'alg' => '',
                'kid' => '',
                'use' => 'sig',
            ),
        ),
    );
    $request = array(
        'name' => 'token_name',
        'authenticationMethod' => array(
            'type' => 'PRIVATE_KEY',
            'signingAlgorithm' => '',
            'publicKeyInfo' => array(
                'jwks' => $jwks,
            ),
        ),
    );
    $result = $apiInstance->createToken($request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### deleteToken

Deletes a Personal Access Token.

Parameters

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

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\TokensApi(new GuzzleHttp\Client(), $config);

try {
    $result = $apiInstance->deleteToken('');
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### getToken

Retrieves the Personal Access Token details.

Parameters

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

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\TokensApi(new GuzzleHttp\Client(), $config);

try {
    $result = $apiInstance->getToken('');
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### listToken

Returns the list of Personal Access Tokens.

No parameters required.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\TokensApi(new GuzzleHttp\Client(), $config);

try {
    $result = $apiInstance->listToken();
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```

#### updateToken

Updates an existing Personal Access Token.

Parameters

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

    updateTokenInput
    [UpdateTokenInput](https://github.com/affinidi/affinidi-tdk-php/tree/main/src/Clients/IamClient/docs/Model/UpdateTokenInput.md)
    Required
  Updated token name and authentication method details.

Example

```php
require_once 'vendor/autoload.php';
use AffinidiTdk\Clients\IamClient;

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
$config = IamClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new IamClient\Api\TokensApi(new GuzzleHttp\Client(), $config);

try {
    $jwks = array(
        'keys' => array(
            array(
                'kty' => '',
                'n' => '',
                'e' => '',
                'alg' => '',
                'kid' => '',
                'use' => 'sig',
            ),
        ),
    );
    $request = array(
        'name' => 'token_name',
        'authenticationMethod' => array(
            'type' => 'PRIVATE_KEY',
            'signingAlgorithm' => '',
            'publicKeyInfo' => array(
                'jwks' => $jwks,
            ),
        ),
    );
    $result = $apiInstance->updateToken('', $request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage(), PHP_EOL;
}
```
