-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayload.js
executable file
·73 lines (61 loc) · 2.53 KB
/
payload.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
#!/usr/bin/env node
const { Command } = require('commander');
const { exit } = require('process');
const program = new Command();
program.name('payload-gen').description('Utility to generate payload data for egendata wallet.').version('0.1.0');
// providerWebId, purpose, documentType, requestorWebId, returnUrl
program
.command('encode')
.description('Encode a new payload from parameters.')
.argument('<providerWebId>', 'Entity that can provide data for the request.')
.argument('<requestorWebId>', 'Entity that requests the data.')
.option('-p, --purpose <string>', 'Purpose of the request.', 'Testing purposes.')
.option(
'-d, --documentType <document type>',
"Type of document that's requested (link to RDF type).",
'http://egendata.se/schema/core/v1#UnemploymentCertificate',
)
.option('-u, --returnUrl <url>', 'Destination to send user to when complete.', 'https://example.com/some/destination')
.option('-b, --base <url>', 'Base URL to deployed system.')
.action((providerWebId, requestorWebId, options) => {
const { purpose, documentType, returnUrl, base } = options;
const data = { providerWebId, requestorWebId, purpose, documentType, returnUrl };
const payload = encodeURIComponent(Buffer.from(JSON.stringify(data), 'utf-8').toString('base64'));
let url;
try {
url = base ? new URL(base) : new URL('http://example.com');
} catch (error) {
console.error('Invalid base URL.');
exit(1);
}
url.pathname = '/request';
url.searchParams.append('payload', payload);
console.log(url.toString());
});
program
.command('decode')
.description("Decode a payload. Either from entire URL or on it's own.")
.argument('<string>', 'URL or string with payload.')
.option('--no-values', 'Only show keys (hide values)')
.action((payload, options) => {
const { values } = options;
payload = payload.startsWith('http') ? payload.split('?payload=')[1] : payload;
let data;
try {
data = JSON.parse(Buffer.from(decodeURIComponent(payload), 'base64').toString('utf-8'));
} catch (error) {
console.error("Couldn't find valid payload");
exit(1);
}
console.log('');
if (values) {
const maxKeyLength =
Math.ceil(Object.keys(data).reduce((prev, value) => (value.length > prev ? value.length : prev), 0) / 4) * 4;
for (const [key, value] of Object.entries(data)) {
console.log(`${key.padStart(maxKeyLength)}: ${value}`);
}
} else {
console.log(Object.keys(data).join(', '));
}
});
program.parse();