UNPKG

smartid-calls

Version:

Smart-ID client module for Node.JS with proxy layer

95 lines (76 loc) 3.28 kB
import { Authentication } from './class/authentication'; import { GetCertificate } from './class/getCertificate'; import { ModuleConfigI } from './types/Request'; import { Session } from './class/session'; import { Signing } from './class/signing'; export class SmartID { private config: ModuleConfigI; constructor(config: ModuleConfigI) { if (!config.host || !config.requestParams) { throw new TypeError('Invalid configuration'); } if (!config.algorithm) { config.algorithm = 'SHA256'; } this.config = config; } /** * Initiates the authentification with SmartID * @param country string country ISO name * @param idNumber string personal number of the individual * @param displayText string text to show for the enduser */ async authenticate(country: string, idNumber: string, displayText: string): Promise<any> { if (!country || !idNumber) throw new TypeError('Missing mandatory parameters'); const auth = new Authentication(this.config, country, idNumber); const response = await auth.authenticate(displayText); return response; } /** * Queries SmartID for desired certificates * @param document object|string either document number or object with country and personal nubmer * @param level string Level of the certificate to look for */ async getSigningCert(document: { country: string, personalNumber: string }|string, level = 'QUALIFIED'): Promise<any> { let certs = new GetCertificate(this.config, level); let certResp; if (typeof document === 'string') { certResp = await certs.certificateByDocumentNumber(document); } else { certResp = await certs.certificateByPersonalNumber(document.country, document.personalNumber); } return certResp; } /** * Initiates the signing procedure on the users device * @param documentNumber string document identifier to use for signing * @param digestToSign string base64 encoded hash for signing * @param displayText string text to show for the end user */ async sign(documentNumber: string, digestToSign: string, displayText: string): Promise<any> { if (!documentNumber || !digestToSign) { throw new TypeError('Missing mandatory parameters'); } let signing = new Signing(this.config, documentNumber, digestToSign); return await signing.sign(displayText); } /** * Verifies whether certificate is valid and optionally can check whether the signed data is received properly * @param sessionId string smartid session identifier * @param prevRequest previous request for verification * @param authHash original data used to create digest for signing */ async verifyRequest(sessionId: string, prevRequest: object, authHash?: string): Promise<any> { const session = new Session(this.config, sessionId); return await session.verifyRequest(prevRequest, authHash); } /** * Polls and waits for user action and afterwards returns the information associated with previous request. * @param sessionId string SmartID session identifier */ async getSession(sessionId: string): Promise<any> { const session = new Session(this.config, sessionId); return await session.pollStatus(); } } export default SmartID;