@reixtech/smartid-ts
v0.0.4
Published
Typescript client for Smart-ID V2
Downloads
5
Maintainers
Readme
SMART-ID Node typescript client for version 2
This is a Node client for the SMART-ID API version 2.
Important
- Note that this package is in early development.
- The API for this package is subject to change.
Updates
Version 0.0.4 - To provide flexibility, added two new methods that can be used separately from the initial authenticateWithPNO.
- createAuthData - Creates the hash and verification code for the authentication request. You can store and use them later to trigger actual authentication flow using triggerAuth.
- triggerAuth - Triggers the actual authentication flow using the hash and verification code created by createAuthData.
Installation
pnpm install @reixtech/smartid-ts
Smart-ID Documentation
https://github.com/SK-EID/smart-id-documentation
Usage
Create smartid client
import {
createSmartIDClient,
makeAllowedInteractionsBuilder,
} from '@reixtech/smartid-ts';
// Helper function to create allowed interactions.
// Keep in mind that duplicate interactions are not allowed and will throw an error.
const allowedInteractionsOrder = makeAllowedInteractionsBuilder()
.add(
'confirmationMessageAndVerificationCodeChoice',
'Message to display in the Smart-ID app.',
)
.add('confirmationMessage', 'Message to display in the Smart-ID app.')
.add('verificationCodeChoice', 'Message to display in the Smart-ID app.')
.build();
const smartIdClient = createSmartIDClient(
{
// URL of the Smart-ID API
host: 'https://sid.demo.sk.ee/smart-id-rp/v2',
// UUID of the relying party
relyingPartyUUID: '00000000-0000-0000-0000-000000000000',
// Name of the relying party
relyingPartyName: 'DEMO',
// Allowed interactions
allowedInteractionsOrder,
},
);
Authenticate with personal identification code and PIN callback
import {
createSmartIDClient,
makeAllowedInteractionsBuilder,
isEndResultError,
isRequestError,
} from '@reixtech/smartid-ts';
// ... create smartid client
try {
const response = await smartIdClient.authenticateWithPNO(
{
// Country code for person (EE, LT, LV)
countryCode: 'EE',
// National identity number for person
nationalIdentityNumber: '39303292707',
},
async (pin) => {
// You can use this callback to show the verification code to the user
// once this promise resolves, then actual request is sent to the users phone.
// Without it Smart ID app will popup a dialog before user can see the verification code.
await new Promise((resolve) => setTimeout(resolve, 5000));
},
);
// You can use the response to do something with the authentication result
console.log('Authentication response:', response);
} catch (e) {
if (isEndResultError(e)) {
// Error is now in the form of EndResultError
// Triggered when the user cancels the request, or the request times out etc
}
if (isRequestError(e)) {
// Error is now in the form of RequestError
// Triggered when the request to the API fails, NOT FOUND, UNDER MAINTENANCE, etc.
}
// Handle other errors
// Like certificate validation errors, etc.
}
Create authentication data and trigger authentication request separately
import {
createSmartIDClient,
makeAllowedInteractionsBuilder,
isEndResultError,
isRequestError,
} from '@reixtech/smartid-ts';
// ... create smartid client
try {
const { hash, verificationCode, randomBytes, identifier } =
smartIdClient.createAuthData({
countryCode: 'EE',
nationalIdentityNumber: '39303292707',
});
// Show the verificationCode to user and do any magic you like...
// ...
// Trigger the authentication request using previously generated data.
const { parsedSubject, response } = await smartIdClient.triggerAuth({
hash,
randomBytes,
identifier,
});
// Do something with the parsedSubject and response...
} catch (e) {
if (isEndResultError(e)) {
// Error is now in the form of EndResultError
// Triggered when the user cancels the request, or the request times out etc
}
if (isRequestError(e)) {
// Error is now in the form of RequestError
// Triggered when the request to the API fails, NOT FOUND, UNDER MAINTENANCE, etc.
}
// Handle other errors
// Like certificate validation errors, etc.
}