Skip to content

Commit f1dbb5e

Browse files
committed
Validate credential via zenroom
1 parent b895c44 commit f1dbb5e

File tree

5 files changed

+94
-26
lines changed

5 files changed

+94
-26
lines changed

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"dependencies": {
1616
"cors": "^2.8.5",
1717
"express": "^4.17.1",
18-
"uuid": "^3.3.3"
18+
"node-fetch": "^2.6.0",
19+
"ramda": "^0.26.1",
20+
"uuid": "^3.3.3",
21+
"zenroom": "^1.0.0"
1922
}
2023
}

src/index.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express';
22
import cors from 'cors';
3-
import sessions, { newSession, getSession, verify } from './sessions';
3+
import sessions, { getSession, verify } from './sessions';
44

55
const app = express();
66

@@ -12,24 +12,28 @@ app.get('/sessions', (req, res) => {
1212
res.send(sessions);
1313
});
1414

15-
app.post('/new-session', (req, res) => {
16-
res.send({
17-
sessionId: newSession(),
18-
})
19-
});
20-
2115
app.get('/session/:sessionId', (req, res) => {
16+
const status = getSession(req.params.sessionId);
17+
console.log('getSession ', req.params.sessionId, status);
2218
res.send({
23-
status: getSession(req.params.sessionId) || 'notfound',
19+
sessionStatus: getSession(req.params.sessionId) || 'notfound',
2420
})
2521
});
2622

27-
app.post('/verify', (req, res) => {
28-
res.send({
29-
status: verify(req.body),
30-
})
23+
app.post('/verify', async (req, res) => {
24+
try {
25+
const verified = await verify(req.body);
26+
return res.send({
27+
sessionStatus: verified,
28+
});
29+
} catch (error) {
30+
console.log('Error: ', error);
31+
return res.status(412).send({
32+
message: error.message,
33+
});
34+
}
3135
});
3236

33-
app.listen(3000, () =>
34-
console.log('DECODE example backend listening on port 3000!'),
37+
app.listen(process.env.PORT, () =>
38+
console.log(`DECODE example backend listening on port ${process.env.PORT}!`),
3539
);

src/sessions.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import uuid from 'uuid/v4';
1+
import zenroom from 'zenroom';
2+
import { isNil } from 'ramda';
3+
import fetch from 'node-fetch';
4+
import verifyProof from './verify-proof.zen';
25

36
const SESSION_STATUS = {
47
NEW: 'new',
@@ -8,16 +11,41 @@ const SESSION_STATUS = {
811

912
let sessions = {};
1013

11-
export const newSession = () => {
12-
const sessionId = uuid();
13-
sessions[sessionId] = SESSION_STATUS.NEW;
14-
return sessionId;
15-
};
16-
1714
export const getSession = id => sessions[id];
1815

19-
export const verify = ({ sessionId, credentials }) => {
20-
console.log('args: ', sessionId, credentials);
16+
export const verify = async ({ sessionId, credential, optionalAttributes }) => {
17+
console.log('args: ', sessionId, credential, optionalAttributes);
18+
sessions[sessionId] = SESSION_STATUS.NEW;
19+
if (isNil(credential)) throw new Error('credential');
20+
const { authorizable_attribute_id: attributeId, value, credential_issuer_endpoint_address: credentialIssuerUrl } = credential;
21+
if (isNil(attributeId)) throw new Error('attributeId');
22+
if (isNil(value)) throw new Error('value');
23+
if (isNil(credentialIssuerUrl)) throw new Error('credentialIssuerUrl');
24+
const { proof } = value;
25+
if (isNil(proof)) throw new Error('proof');
26+
const credentialProof = { credential_proof: proof };
27+
console.log('Attribute id: ', attributeId);
28+
console.log('Credential issuer URL: ', credentialIssuerUrl);
29+
console.log('Proof: ', credentialProof);
30+
const uidResp = await fetch(`${credentialIssuerUrl}/uid`);
31+
if (!uidResp.ok) throw new Error('no uid');
32+
const { credential_issuer_id: credentialIssuerId } = await uidResp.json();
33+
if (isNil(credentialIssuerId)) throw new Error('credentialIssuerId');
34+
console.log('Credential issuer id: ', credentialIssuerId);
35+
const verfierResp = await fetch(`${credentialIssuerUrl}/authorizable_attribute/${attributeId}`);
36+
if (!verfierResp.ok) throw new Error('no verifier');
37+
const { verification_key: verificationKey } = await verfierResp.json();
38+
console.log('verificationKey: ', verificationKey);
39+
if (isNil(verificationKey)) throw new Error('verification key');
40+
zenroom
41+
.success(() => console.log('Success'))
42+
.error(() => { throw new Error('not verified') })
43+
.script(verifyProof(credentialIssuerId))
44+
.data(JSON.stringify(verificationKey))
45+
.keys(credentialProof)
46+
.zenroom_exec()
47+
.reset();
48+
sessions[sessionId] = SESSION_STATUS.VALID;
2149
return SESSION_STATUS.VALID;
2250
};
2351

src/verify-proof.zen.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default uniqueId => `
2+
ZEN:begin(0)
3+
4+
ZEN:parse([[
5+
Scenario coconut: verify proof
6+
Given that I have a valid 'verifier' from '${uniqueId}'
7+
and I have a valid 'credential proof'
8+
When I aggregate the verifiers
9+
and I verify the credential proof
10+
Then print 'Success' 'OK' as 'string'
11+
]])
12+
13+
ZEN:run()
14+
`;

yarn.lock

+21-2
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ core-js-compat@^3.1.1:
10481048
browserslist "^4.7.1"
10491049
semver "^6.3.0"
10501050

1051-
core-js@^3.2.1:
1051+
core-js@^3.1.4, core-js@^3.2.1:
10521052
version "3.3.3"
10531053
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.3.3.tgz#b7048d3c6c1a52b5fe55a729c1d4ccdffe0891bb"
10541054
integrity sha512-0xmD4vUJRY8nfLyV9zcpC17FtSie5STXzw+HyYw2t8IIvmDnbq7RJUULECCo+NstpJtwK9kx8S+898iyqgeUow==
@@ -2128,6 +2128,11 @@ node-environment-flags@^1.0.5:
21282128
object.getownpropertydescriptors "^2.0.3"
21292129
semver "^5.7.0"
21302130

2131+
node-fetch@^2.6.0:
2132+
version "2.6.0"
2133+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
2134+
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==
2135+
21312136
node-modules-regexp@^1.0.0:
21322137
version "1.0.0"
21332138
resolved "https://registry.yarnpkg.com/node-modules-regexp/-/node-modules-regexp-1.0.0.tgz#8d9dbe28964a4ac5712e9131642107c71e90ec40"
@@ -2473,6 +2478,11 @@ [email protected]:
24732478
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
24742479
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==
24752480

2481+
ramda@^0.26.1:
2482+
version "0.26.1"
2483+
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.26.1.tgz#8d41351eb8111c55353617fc3bbffad8e4d35d06"
2484+
integrity sha512-hLWjpy7EnsDBb0p+Z3B7rPi3GDeRG5ZtiI33kJhTt+ORCd38AbAIjB/9zRIUoeTbE/AVX5ZkU7m6bznsvrf8eQ==
2485+
24762486
range-parser@~1.2.1:
24772487
version "1.2.1"
24782488
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031"
@@ -2532,7 +2542,7 @@ regenerate@^1.4.0:
25322542
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11"
25332543
integrity sha512-1G6jJVDWrt0rK99kBjvEtziZNCICAuvIPkSiUFIQxVP06RCVpq3dmDo2oi6ABpYaDYaTRr67BEhL8r1wgEZZKg==
25342544

2535-
regenerator-runtime@^0.13.3:
2545+
regenerator-runtime@*, regenerator-runtime@^0.13.3:
25362546
version "0.13.3"
25372547
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5"
25382548
integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==
@@ -3149,3 +3159,12 @@ yallist@^3.0.0, yallist@^3.0.3:
31493159
version "3.1.1"
31503160
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
31513161
integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==
3162+
3163+
zenroom@^1.0.0:
3164+
version "1.0.0"
3165+
resolved "https://registry.yarnpkg.com/zenroom/-/zenroom-1.0.0.tgz#8dc863cc30f1b0e193cc5cd689c9e808e6d6e4d8"
3166+
integrity sha512-khZLsXUs+qL+LrN3GlL0BvwwoP6KXnkDQatLUT5+OrRmspJCkutfCAQiFSEmagyi44m9m4fi+Xta9uTVe7VDlQ==
3167+
dependencies:
3168+
"@babel/node" "^7.6.3"
3169+
core-js "^3.1.4"
3170+
regenerator-runtime "*"

0 commit comments

Comments
 (0)