@lotusflare/consent-ts-sdk
v1.0.2
Published
TypeScript SDK for Consent Service
Readme
Consent TypeScript SDK
A TypeScript SDK for interacting with the Consent Service, enabling applications to manage user consent for data access and sharing in a secure and standardized way.
Installation
To install the SDK, first package it locally and then add it to your project:
cd consent-ts-sdk
npm pack
# in your project
yarn add ../consent-ts-sdk/consent-ts-sdk-1.0.0.tgz
# or
npm install ../consent-ts-sdk/consent-ts-sdk-1.0.0.tgzQuick Start
This section provides a step-by-step guide to integrating the Consent SDK into your TypeScript project.
1. Initialize TokenProvider
The SDK supports multiple authentication strategies for obtaining access tokens. Choose the one that best fits your use case:
Client Credentials Token Provider
Use this provider to obtain tokens using Keycloak client credentials. This is recommended for most server-to-server integrations.
import { TokenProviderFactory } from 'consent-ts-sdk';
// Create a token provider using client credentials
envconst tokenProvider = TokenProviderFactory.createClientCredentialsTokenProvider(
'https://keycloak-server.example.com'
'my-realm',
'my-client-id',
'my-client-secret',
);Static Token Provider
Use this provider if you already have a valid access token. This is useful for testing or simple integrations where token management is handled externally.
import { TokenProviderFactory } from 'consent-ts-sdk';
// Create a token provider using a static token
const tokenProvider = TokenProviderFactory.createStaticProvider('your-static-access-token');JWT Token Provider
Use this provider to sign and obtain tokens using a JWT and your private key. This is suitable for advanced scenarios requiring custom token signing.
import { TokenProviderFactory } from 'consent-ts-sdk';
// Create a token provider using JWT
const tokenProvider = TokenProviderFactory.createJwtTokenProvider(
'https://keycloak-server.example.com',
'my-realm',
'my-client-id',
'/privateKeyPath',
'your-key-id'
);2. Create ConsentClient
The ConsentClient is the main entry point for interacting with the Consent Service. You need to provide a TokenProvider and the base URL of your Consent Service instance.
import { createConsentClient } from 'consent-ts-sdk';
const baseUrl = 'https://your-consent-service.example.com';
const consentClient = createConsentClient(tokenProvider, baseUrl);3. Check Consent
To check if a user needs to provide consent for specific operations, use the checkConsent method. This method validates whether the user has already granted the necessary permissions or if they need to go through a consent flow.
Key Points:
- If consent is already granted, the operation can proceed immediately
- If consent is required, you'll need to initiate a consent intent flow
Response Handling:
success: true- Consent check completed successfullyresponse- Contains detailed consent information and statusmessage- Error details if the check failed
import { ConsentCheckRequest } from 'consent-ts-sdk';
const request = new ConsentCheckRequest(
'myuserId', // User ID
'myclientId', // Client ID
'auth_code', // Grant type
'dpv:AccountManagement number-verification-share-read number-verification-verify-read' // Purpose and scopes
);
const operatorName = 'your-operator';
const result = await consentClient.checkConsent(request, operatorName);
if (result.success) {
console.log('Consent check successful!');
console.log('Response:', result.response);
} else {
console.log('Consent check failed:', result.message);
}4. Get Consent Intent
If the user has not yet provided consent, initiate a consent intent to generate the consent intent token.
import { GetConsentIntentRequest } from 'consent-ts-sdk';
const getConsentIntentRequest = new GetConsentIntentRequest(
'myUserId', // User ID
'myUserName', // Username
'myClientId', // Client ID
'dpv:AccountManagement number-verification-share-read number-verification-verify-read', // Purpose and scopes
result.response // Response from consent check
);
const intentResult = await consentClient.getConsentIntent(getConsentIntentRequest, operatorName);
console.log('Consent intent result:', intentResult);5. Confirm Consent
After the user has reviewed and approved the requested consent, confirm their decision by submitting the consent confirmation request.
import { ConsentConfirmRequest } from 'consent-ts-sdk';
const confirmRequest = new ConsentConfirmRequest(
'testUserId', // User ID
'mySpecificationId', // Specification ID
'testUserName', // Username
'myClientId', // Client ID
[
{ name: 'predicate1', agreed: true },
{ name: 'predicate2', agreed: false }
] // List of predicates representing user choices
);
try {
const confirmResult = await consentClient.consentConfirm(confirmRequest, operatorName);
console.log('Consent confirm result:', confirmResult);
} catch (error) {
console.log('Consent confirm failed:', error);
}