Get Access Token
The Affinidi TDK uses Personal Access Token (PAT) to authenticate and obtain the authorisation token required to initialise the clients and call the API endpoints.
Using the AuthProvider package provided in Affinidi TDK, it generates a Project-scoped token that allows you to integrate your application and access resources within your projects.
Create a Personal Access Token (PAT)
A Personal Access Token (PAT) is like a machine user who acts on your behalf to call API endpoints using Affinidi TDK. Using the IAM service, a PAT can be granted access to other projects.
Follow the command below to create a Personal Access Token using the Affinidi CLI. It creates a PAT with an auto-generated private/public key pair and IAM policy granting access to project resources.
affinidi token create-token -n IotaToken --auto-generate-key --passphrase "MySecretPassphrase" --with-permissions
The command will display details such as the privateKey
, tokenId
, projectId
, and the passphrase
if provided to instantiate the AuthProvider package.
Generate a Project-scoped token
The Project-scoped token authorises your application to access and perform actions on the resources within the project where you generate the token.
Instantiate the AuthProvider package with the Personal Access Token (PAT) details to generate the project-scoped token.
import { AuthProvider } from '@affinidi-tdk/auth-provider'
const authProvider = new AuthProvider({
privateKey: '<PAT_PRIVATE_KEY_STRING>',
passphrase: '<PAT_KEY_PAIR_PASSPHRASE>',
tokenId: '<PAT_ID>',
projectId: '<PROJECT_ID>'
})
const projectScopedToken = await authProvider.fetchProjectScopedToken()
import affinidi_tdk_auth_provider
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()
require_once 'vendor/autoload.php';
use AffinidiTdk\AuthProvider\AuthProvider;
$params = [
'privateKey' => '<PAT_PRIVATE_KEY_STRING>',
'passphrase' => '<PAT_KEY_PAIR_PASSPHRASE>',
'tokenId' => '<PAT_ID>',
'projectId' => '<PROJECT_ID>'
];
$authProvider = new AuthProvider($params);
// this is optional, another option to fetch the ProjectScopedToken is through callback
$projectScopedToken = $authProvider->fetchProjectScopedToken();
// $tokenCallback = [$authProvider, 'fetchProjectScopedToken'];
try{
// Use this method by passing the values directly on initialisation
// Initialise AuthProvider class to generate projectScopedToken
AuthProvider authProviderWithPassedValues = new AuthProvider.Configurations()
.projectId("PROJECT_ID")
.privateKey("PRIVATE_KEY")
.passphrase("PASSPHRASE")
.tokenId("TOKEN_ID")
.build();
String projectToken = authProviderWithPassedValues.fetchProjectScopedToken();
} catch(Exception e) {
e.printStackTrace();
}
What’s next
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.