-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.js
65 lines (59 loc) · 1.95 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(async function() {
const usage = () => {
return console.log(
'Usage: npm run demo [country] [idcode]. Example: npm run demo EE 12345678901'
);
};
if (!process.argv || process.argv.length !== 4) return usage();
let idNumber = process.argv.pop();
let country = process.argv.pop().toUpperCase();
console.log('Running authentication for: ' + country + ' - ' + idNumber);
const SmartIDAuth = require('./index.js');
const smartauth = new SmartIDAuth({
host: 'https://sid.demo.sk.ee/smart-id-rp/v1',
requestParams: {
relyingPartyUUID: '00000000-0000-0000-0000-000000000000',
relyingPartyName: 'DEMO',
certificateLevel: 'QUALIFIED',
},
});
try {
const session = await smartauth.authenticate(
country,
idNumber,
'Hello World'
);
// This is the verification code you should display to the user (on your site):
console.log('Verification code: ' + session.verificationCode);
console.log('Waiting for user action...');
const response = await session.getResponse();
// full Smart-ID response:
console.log(response.data);
if (response.result === 'OK') {
console.log('Authentication OK!');
// Certificate subject (name, country, id number):
console.log(response.subject);
} else {
console.log('Authentication failed!:');
switch (response.result) {
case 'USER_REFUSED':
console.error('User refused the request');
break;
case 'TIMEOUT':
console.error('Authentication request timed out');
break;
case 'DOCUMENT_UNUSABLE':
console.error('Request cannot be completed');
break;
case 'WRONG_VC':
console.error('User chose wrong verification code');
break;
default:
console.error(`Unknown result: ${response.result}`);
}
}
} catch (err) {
console.error('Authentication error');
console.error(err);
}
})();