-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.js
209 lines (202 loc) · 6.7 KB
/
verifier.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { createServer, request } from 'node:http'
import { config, jsonHeaders } from './init.js'
import { auth } from './auth.js'
const auth_token = await auth(config, config.verificationTemplate)
jsonHeaders.Authorization = auth_token
async function checkStatus(sessionId) {
const statusUrl = `${config.sessions_url}/${sessionId}`
const statusParams = {
method: 'GET',
headers: jsonHeaders
}
// console.log(statusUrl, JSON.stringify(statusParams, null, 1))
const statusResp = await fetch(statusUrl, statusParams)
if (statusResp.status == 403) {
const { auth } = await import('./auth.js')
jsonHeaders.Authorization = await auth(config, config.verificationTemplate)
return checkStatus(sessionId)
}
let status = 2
const json = await statusResp.json()
if (statusResp.status == 202) {
status = 0
}
else if (statusResp.status == 200) {
status = 1
}
// console.log(statusResp.status, status, JSON.stringify(json, null, 1))
const statusObj = {
result: status,
data: json
}
return statusObj
}
async function showVerifierPage(res) {
if (!jsonHeaders.Authorization) {
res.setHeader("Content-Type", "text/html")
res.writeHead(500)
res.end(`<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>Virhe</title>
<h1>Tunnistustiedot eivät kelpaa!</h1>
<p>Olisikohan ilmainen kokeilujakso päättynyt?</p>
</html>`)
return false
}
const sessionsUrl = config.sessions_url
const sessionsBody = {
"ssiConfigId": config.verificationTemplate
}
const sessionsParams = {
method: 'POST',
headers: jsonHeaders,
body: JSON.stringify(sessionsBody)
}
// console.log(sessionsUrl, JSON.stringify(sessionsParams, null, 1))
const sessionsResp = await fetch(sessionsUrl, sessionsParams)
console.log(sessionsResp.status, sessionsUrl)
if (sessionsResp.status == 403) {
const { auth } = await import('./auth.js')
jsonHeaders.Authorization = await auth(config, config.verificationTemplate)
return showVerifierPage(res)
}
else if (sessionsResp.status == 400 || sessionsResp.status == 500) {
const error = await sessionsResp.text()
console.error(error)
console.log(JSON.stringify(sessionsParams, null, 2))
res.setHeader("Content-Type", "text/plain")
res.writeHead(200)
res.end(error)
return false
}
else if (sessionsResp != 200 || !json) {
console.log(JSON.stringify(sessionsParams, null, 2))
}
const json = await sessionsResp.json()
// console.log(JSON.stringify(json.data, null, 1))
const sessionId = json.presentation_definition.id
const authRequest = json.authentication_request
console.log(sessionsResp.status, sessionId, authRequest)
res.setHeader("Content-Type", "text/html")
res.writeHead(200)
res.end(`<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>Gataca tarkastaa eläketodisteen</title>
<script type="module" src="https://unpkg.com/@gataca/qr@2.0.4/dist/gatacaqr/gatacaqr.esm.js"></script>
<script nomodule="" src="https://unpkg.com/@gataca/qr@2.0.4/dist/index.js"></script>
<style>
table {
max-width: 30em;
margin: 1em auto;
}
th, td {
text-align: left;
}
pre {
background-color: black;
color: green;
display: none;
text-align: left;
}
#content.full pre {
display: block;
}
</style>
<body style="text-align: center;">
<img src="https://cdn-assets-cloud.frontify.com/s3/frontify-cloud-files-us/eyJwYXRoIjoiZnJvbnRpZnlcL2FjY291bnRzXC8yZFwvMTkyOTA4XC9wcm9qZWN0c1wvMjQ5NjY1XC9hc3NldHNcLzAwXC80NTY4NzI2XC81MjA2ODk2MDdmZGRkYjBlMDEwMDhiOTVlMTk1OTRjMS0xNTk1NDI3ODE5LnN2ZyJ9:frontify:ToCDM7NDPWebZDLJcmAgDwA_EsA9XJBl3YroZI1XhA0?width=240" alt="HSL" />
<h1>Heippa vahvasti tunnistettu asiakas!</h1>
<div id="content">
<p>Lähetäpä eläketodiste niin tsekataan, että sinulla on oikeus eläkealennukseen...</p>
<gataca-qr id="gataca-qr" polling-frequency="3" session-timeout="300" autostart="true" style="display: block; width: 256px; margin: 0 auto;" v-2="true"></gataca-qr>
</div>
<script>
const qr = document.getElementById('gataca-qr')
var count = 0
var ok = true
/*
const processData = (data) => {
// console.log(JSON.stringify(data, null, 2))
let result = {}
for (let vc of data?.verifiableCredential) {
for (let key of Object.keys(vc.credentialSubject)) {
if (key != "id") {
result[key] = vc.credentialSubject[key]
}
}
}
return result
}
*/
qr.errorCallback = (e) => {
console.error(e)
}
qr.successCallback = (data, token) => {
const c = document.querySelector('#content')
console.log(JSON.stringify(data, null, 2))
// console.log(JSON.stringify(qr.sessionData, null, 4))
console.log(JSON.stringify(data.PresentationSubmission.verifiableCredential, null, 4))
for (const ps of data?.PresentationSubmission) {
for (const vc of ps.verifiableCredential) {
console.log(JSON.stringify(vc, null, 1))
const credential = vc.credentialSubject
const html = \`<p>Todisteen tarkistuksen tila: <strong>OK</strong></p>
<table>
<tr><th>Nimi</th><td>\${credential.person?.firstName} \${credential.person?.lastName}</td></tr>
<tr><th>Eläke</th><td>\${credential.pension?.type} \${credential.pension?.startDate}–\${credential.pension?.endDate || ''}</td></tr>
</table>
<pre>\${JSON.stringify(status, null, 2)}</pre>\`
c.innerHTML = html
c.ondblclick = function(e) {
this.classList.toggle('full')
}
break
}
}
}
qr.createSession = () => {
return {
sessionId: '${sessionId}',
authenticationRequest: '${authRequest}'
}
}
qr.checkStatus = async () => {
const statusUrl = '/status/${sessionId}'
const response = await fetch(statusUrl)
const obj = await response.json()
// console.log(JSON.stringify(obj, null, 2))
console.log(response.status, obj.result)
if (obj.result == 1) {
// qr.sessionData = processData(obj)
qr.sessionData = obj
}
return obj
}
</script>
</body>
</html>`)
}
const handleRequests = async function (req, res) {
let match
if (match = req.url.match(/^\/status\/(.*)/)) {
const sessionId = match[1]
const statusObject = await checkStatus(sessionId)
// console.log(statusObject)
res.setHeader("Content-Type", "'application/json'")
res.writeHead(200)
res.end(JSON.stringify(statusObject))
return false
}
if (req.url !== '/') {
res.setHeader("Content-Type", "text/plain")
res.writeHead(404)
res.end(`Not Found`)
return false
}
await showVerifierPage(res)
}
const server = createServer(handleRequests)
server.listen(config.verifier_port, config.server_host, () => {
console.log(`Verifier server is running on http://${config.server_host}:${config.verifier_port}`)
})