smartid-calls
Version:
Smart-ID client module for Node.JS with proxy layer
48 lines (41 loc) • 1.43 kB
text/typescript
import { RequestI, ModuleConfigI } from '../types/Request';
import AuthHash from '../authhash';
import axios from 'axios';
export class Authentication {
private config: ModuleConfigI;
private request: RequestI;
constructor(config: ModuleConfigI, country: string, idNumber: string) {
this.config = config;
this.request = {
idNumber: idNumber,
country: country.toUpperCase()
};
}
async authenticate(displayText: string): Promise<any> {
const hash = await AuthHash.generateRandomHash(this.config.algorithm);
const response = await axios({
method: 'post',
url: this.config.host + '/authentication/pno/' + this.request.country + '/' + this.request.idNumber,
proxy: this.config.proxy || false,
responseType: 'json',
validateStatus: (status) => status === 200,
data: Object.assign({
hash: hash.digest,
hashType: this.config.algorithm,
displayText: (typeof displayText === 'string' ? displayText : undefined)
},
this.config.requestParams
)
});
const body = response.data;
if (typeof body !== 'object' || !body.sessionID) {
throw new Error('Invalid response')
}
const code = AuthHash.calculateVerificationCode(hash.digest, this.config.algorithm);
return {
verificationCode: code,
sessionId: response.data.sessionID,
authHash: hash.raw.toString('hex'),
}
}
}