Get Consent Logs from your Application

Retrieve consent logs stored in your project using Affinidi Iota Framework.

When consent logging is enabled in your Iota configuration, Affinidi TDK provides a client to fetch and parse these logs from your application.

We will use the DefaultApi from the Affinidi Iota Framework client. Follow the steps below:

  1. Install Required Libraries (in our example, we will use Affinidi Iota Framework Service):
npm install -S @affinidi-tdk/auth-provider @affinidi-tdk/iota-client
pip install affinidi_tdk_auth_provider affinidi_tdk_iota_client
composer require affinidi-tdk/affinidi-tdk-php
<dependency>
    <groupId>com.affinidi.tdk</groupId>
    <artifactId>auth.provider</artifactId>
    <version><version_number></version>
</dependency>
<dependency>
    <groupId>com.affinidi.tdk</groupId>
    <artifactId>iota.client</artifactId>
    <version><version_number></version>
</dependency>
dotnet add package AffinidiTdk.AuthProvider
dotnet add package AffinidiTdk.IotaClient
  1. Import Libraries
  • Import Iota Client (to get the list of consents)
  • Import Auth Provider (to generate the Project Scoped Token for the Authorisation header)
import { DefaultApi, Configuration as AuthConfiguration } from '@affinidi-tdk/iota-client'
import { AuthProvider } from '@affinidi-tdk/auth-provider'
import affinidi_tdk_auth_provider
import affinidi_tdk_iota_client
require_once 'vendor/autoload.php';

use AffinidiTdk\AuthProvider\AuthProvider;
use AffinidiTdk\Clients\IotaClient;
import com.affinidi.tdk.authProvider.AuthProvider;
import com.affinidi.tdk.iota.client.Configuration;
import com.affinidi.tdk.iota.client.apis.DefaultApi;
import com.affinidi.tdk.iota.client.auth.ApiKeyAuth;
import com.affinidi.tdk.iota.client.models.ListLoggedConsentsOK;
import com.affinidi.tdk.iota.client.ApiClient;
using AffinidiTdk.AuthProvider;
using AffinidiTdk.IotaClient.Api;
using AffinidiTdk.IotaClient.Client;
using AffinidiTdk.IotaClient.Model;
  1. Generate Authorisation Token Use the Personal Access Token (PAT) for your project. Run the Affinidi CLI Token command to generate the Personal Access Token (PAT) required by the auth-provider module.

If you created the PAT with --key-id, set keyId in the AuthProvider class.

// NOTE: set your variables for PAT
const privateKey = '<PAT_PRIVATE_KEY_STRING>'
const passphrase = '<PAT_KEY_PAIR_PASSPHRASE>'
const tokenId = '<PAT_ID>'
const projectId = '<PROJECT_ID>'

const authProvider = new AuthProvider({
    privateKey,
    passphrase,
    tokenId,
    projectId
})

const authConfiguration = new AuthConfiguration({
  apiKey: authProvider.fetchProjectScopedToken.bind(authProvider)
})
stats = {
  'privateKey': '<PAT_PRIVATE_KEY_STRING>',
  'passphrase': '<PAT_KEY_PAIR_PASSPHRASE>',
  'tokenId': '<PAT_ID>',
  'projectId': '<PROJECT_ID>'
}

authProvider = affinidi_tdk_auth_provider.AuthProvider(stats)

projectScopedToken = authProvider.fetch_project_scoped_token()

configuration = affinidi_tdk_credential_verification_client.Configuration()

# Configure API key authorization: ProjectTokenAuth
configuration.api_key['ProjectTokenAuth'] = projectScopedToken
$params = [
  'privateKey' => "<PAT_PRIVATE_KEY_STRING>",
  'passphrase' => '<PAT_KEY_PAIR_PASSPHRASE>',
  'tokenId' => '<PAT_ID>',
  'projectId' => '<PROJECT_ID>'
];

$authProvider = new AuthProvider($params);

$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
AuthProvider authProvider = new AuthProvider.Configurations()
    .projectId(dotenv.get("<PROJECT_ID>"))
    .privateKey(dotenv.get("<PRIVATE_KEY_STRING>"))
    .passphrase(dotenv.get("<KEY_PAIR_PASSPHRASE>"))
    .tokenId(dotenv.get("<PAT_ID>"))
    .build();

ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiKeyAuth ProjectTokenAuth = (ApiKeyAuth) defaultClient.getAuthentication("ProjectTokenAuth");
ProjectTokenAuth.setApiKey(authProvider.fetchProjectScopedToken());
var authProvider = new AuthProvider(new AuthProviderParams
{
    TokenId = "YOUR_TOKEN_ID",
    PrivateKey = "YOUR_PRIVATE_KEY",
    ProjectId = "YOUR_PROJECT_ID"
    Passphrase = "YOUR_PASSPHRASE",
});

string projectScopedToken = await authProvider.FetchProjectScopedTokenAsync();
  1. Initialise Iota Client: Pass the authorisation header and call the client method to retrieve consent logs.
const api = new DefaultApi(authConfiguration)

const { data } = await api.listLoggedConsents()
with affinidi_tdk_iota_client.ApiClient(configuration) as api_client:

    api_instance = affinidi_tdk_iota_client.DefaultApi(api_client)

    api_response = api_instance.list_logged_consents()
// Configure API key authorization: ProjectTokenAuth
$config = IotaClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);

$apiInstance = new IotaClient\Api\DefaultApi(
    new GuzzleHttp\Client(),
    $config
);

try {

    $result = $apiInstance->listLoggedConsents();

    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling method: ', $e->getMessage(), PHP_EOL;
}
DefaultApi apiInstance = new DefaultApi(defaultClient);

String configurationId = "<Config_ID>";
String userId = "<User_ID>";
Integer limit = 56;
String exclusiveStartKey = null;

ListLoggedConsentsOK result = apiInstance.listLoggedConsents(configurationId, userId, limit, exclusiveStartKey);
Configuration config = new Configuration();

config.AddApiKey("authorization", projectScopedToken);

DefaultApi api = new DefaultApi(config);

var configurationId = "<CONFIG_ID>";
var userId = "<USER_ID>";
var limit = 56;  
var exclusiveStartKey = "exclusiveStartKey_example";  

ListLoggedConsentsOK result  = api.ListLoggedConsents(configurationId, userId, limit, exclusiveStartKey);

The Iota client returns a JSON response containing the list of consents:

{ "consents": [ { "projectId": "af338c39-2201-49a6-90d2-106c6cbbbcb7", "id": "4e0cb4cb-e7fc-4a06-bc1e-8724dffff4db", "userId": "did:key:zQ3shnYYaYpKHEEsE8HirxBM3MFFR6Ubpjixdm5EET4ihug6l", "vcType": "Membership", "status": "GIVEN", "modifiedAt": "2024-07-02T05:12:22.000Z", "modifiedBy": "", "createdAt": "2024-07-02T05:12:22.000Z", "createdBy": "" } ], "lastEvaluatedKey": "" }