UNPKG

smartid-calls

Version:

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

49 lines (40 loc) 1.31 kB
import { RequestI, ModuleConfigI } from '../types/Request'; import AuthHash from '../authhash'; import axios from 'axios'; export class Signing { private config: ModuleConfigI; private hash: string; private request: RequestI; constructor(config: ModuleConfigI, documentNumber: string, digestToSign: string) { this.config = config; this.hash = digestToSign; this.request = { documentNumber: documentNumber }; } async sign(displayText: string): Promise<any> { const response = await axios({ method: 'post', proxy: this.config.proxy || false, url: this.config.host + '/signature/document/' + this.request.documentNumber, responseType: 'json', validateStatus: (status) => status === 200, data: Object.assign({ hash: this.hash, hashType: this.config.algorithm, displayText: (typeof displayText === 'string' ? displayText : undefined) }, this.config.requestParams ) }); let body = response.data; if (typeof body !== 'object' || !body.sessionID) { throw new Error('Invalid response'); } const code = AuthHash.calculateVerificationCode(this.hash, this.config.algorithm); return { verificationCode: code, sessionId: response.data.sessionID, } } }