Affinidi TDK
The Affinidi Trust Development Kit (TDK) provides clients that represent each available Affinidi service, such as Login Configurations and Credential Issuance, including useful libraries and packages. These modules expose service endpoints as methods that enable developers to easily call these services from their applications.
The TDK is an open-source developer tools piublished on GitHub that is built using Typescript and utilises the JSII library to transpile and support different languages.
You can read our contribution guide to learn more about how you can contribute to the project.
Supported Languages
Affinidi TDK supports the integration with the following programming languages.
- TypeScript
- Python
- PHP
How to use Affinidi TDK
The Affinidi TDK provides three types of modules that developers can utilise to integrate their application:
Clients, which offer methods to access Affinidi Elements services like Credential Issuance, Credential Verification, and Login Configurations, among others.
Packages are commonly used utilities/helpers that are self-contained and composable.
Libraries are high-level abstractions that combine logic and data to perform necessary business logic functionalities.
To start integrating with Affinidi TDK, you have to create a Project. To create a project, simply login to the Affinidi Portal using your Affinidi Vault account. On your first login, the Affinidi Portal will create a default project.
Once you have an active project, you can use it to create a Personal Access Token (PAT), a machine user used to authenticate and generate an Authorisation Token to call the Affinidi services.
Sample Code using Affinidi TDK
To integrate Affinidi TDK into your application, import the client and the auth provider into the project.
- Install the required libraries (in our example, we will use Login Configuration client.
npm install -S @affinidi-tdk/auth-provider @affinidi-tdk/login-configuration-client
pip install affinidi_tdk_auth_provider affinidi_tdk_login_configuration_client
composer require affinidi-tdk/affinidi-tdk-php
- Import the libraries into the code. We are importing the Login Configuration client to create a Login Configuration and the Auth Provider to generate the Project Scoped Token for the Authorisation header.
import { AuthProvider } from '@affinidi-tdk/auth-provider'
import { ConfigurationApi, Configuration as AuthConfiguration, CreateLoginConfigurationInput } from '@affinidi-tdk/login-configuration-client'
import affinidi_tdk_auth_provider
import affinidi_tdk_login_configuration_client
require_once 'vendor/autoload.php';
use AffinidiTdk\AuthProvider\AuthProvider;
use AffinidiTdk\Clients\LoginConfigurationClient;
- Generate an Authorisation token to call the client using the Personal Access Token for the specific project.
Use the Affinidi CLI Token command to generate the Personal Access Token (PAT) for the Auth Provider.
If you have created the Personal Access Token (PAT) with the
--key-id
flag in Affinidi CLI, you must also set thekeyId
with the supplied value 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": "<PRIVATE_KEY_STRING>",
"passphrase": "<KEY_PAIR_PASSPHRASE>",
"tokenId": "<PAT_ID>",
"projectId": "<PROJECT_ID>"
}
authProvider = affinidi_tdk_auth_provider.AuthProvider(stats)
projectScopedToken = authProvider.fetch_project_scoped_token()
$params = [
'privateKey' => '<PAT_PRIVATE_KEY_STRING>',
'passphrase' => '<PAT_KEY_PAIR_PASSPHRASE>',
'tokenId' => '<PAT_ID>',
'projectId' => '<PROJECT_ID>'
];
$authProvider = new AuthProvider($params);
- Initiate the Login Configuration module with the authorisation header and call the client method.
async function createLoginConfig() {
const api = new ConfigurationApi(authConfiguration)
const request: CreateLoginConfigurationInput = {
name: 'Sample App',
redirectUris: ['http://localhost:3000/auth/callback']
}
const { data } = await api.createLoginConfigurations(request)
return data
}
createLoginConfig()
.then((data) => console.log(data))
.catch((error) => console.log(error))
# Defining the host is optional and defaults to http://localhost
# See configuration.py for a list of all supported configuration parameters.
configuration = affinidi_tdk_login_configuration_client.Configuration()
# Pass the projectScopedToken generated from AuthProvider package
configuration.api_key['ProjectTokenAuth'] = projectScopedToken
with affinidi_tdk_login_configuration_client.ApiClient(configuration) as api_client:
api_instance = affinidi_tdk_login_configuration_client.ConfigurationApi(api_client)
request_json = {
name: "Sample App",
redirectUris: ["http://localhost:3000/auth/callback"]
}
create_login_configuration_input = affinidi_tdk_login_configuration_client.CreateLoginConfigurationInput.from_dict(request_json)
api_response = api_instance.create_login_configurations(create_login_configuration_input=create_login_configuration_input)
$tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
// Configure API key authorization: ProjectTokenAuth
$config = LoginConfigurationClient\Configuration::getDefaultConfiguration()->setApiKey('authorization', '', $tokenCallback);
$apiInstance = new LoginConfigurationClient\Api\ConfigurationApi(
new GuzzleHttp\Client(),
$config
);
try {
$result = $apiInstance->createLoginConfigurations([
"name" => "Sample App",
"redirectUris" => array(
"http://localhost:3000/auth/callback"
)
]);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConfigurationApi->createLoginConfigurations: ', $e->getMessage(), PHP_EOL;
}
In the above example, we called Create Login Configuration method and extracted the data
that contains the response from the endpoint.
Explore the Affinidi TDK documentation to learn how to use it to onboard your application into the Affinidi Trust Network.
Glad to hear it! Please tell us how we can improve more.
Sorry to hear that. Please tell us how we can improve.
Thank you for sharing your feedback so we can improve your experience.