-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsession-management.js
72 lines (62 loc) · 2.56 KB
/
session-management.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
const ProtocolVersion = require('./protocol-version');
if (typeof fetch === 'undefined') require('isomorphic-fetch');
module.exports = class SessionManagement {
constructor(options) {
this._options = options;
this._mappings = {};
}
start() {
// Handle case where start is disabled and qr and token are supplied directly
if (!this._options.start) {
Object.keys(this._options.mapping).forEach((val) => (this._mappings[val] = this._options.mapping[val]({})));
return Promise.resolve(this._parseMappings(this._mappings));
}
// Start options are specified, so start a new session
// eslint-disable-next-line compat/compat
return fetch(this._options.start.url(this._options), this._options.start)
.then((r) => {
if (r.status !== 200)
throw new Error(
`Error in fetch: endpoint returned status other than 200 OK. Status: ${r.status} ${r.statusText}`,
);
return r;
})
.then((r) => this._options.start.parseResponse(r))
.then((r) => {
// Execute all mapping functions using the received start response.
Object.keys(this._options.mapping).forEach((val) => (this._mappings[val] = this._options.mapping[val](r)));
return this._parseMappings(this._mappings);
});
}
_parseMappings(mappings) {
if (!mappings.sessionPtr) throw new Error('Missing sessionPtr in mappings');
let frontendRequest = mappings.frontendRequest;
if (!frontendRequest) {
frontendRequest = {
minProtocolVersion: ProtocolVersion.minSupported(),
maxProtocolVersion: ProtocolVersion.minSupported(),
};
}
// Check whether the IRMA server at least has minimum support for this yivi-client version.
if (
ProtocolVersion.above(ProtocolVersion.minSupported(), frontendRequest.maxProtocolVersion) ||
ProtocolVersion.below(ProtocolVersion.maxSupported(), frontendRequest.minProtocolVersion)
) {
throw new Error('Frontend protocol version is not supported');
}
return { ...mappings, frontendRequest };
}
result() {
if (!this._options.result) return Promise.resolve(this._mappings);
// eslint-disable-next-line compat/compat
return fetch(this._options.result.url(this._options, this._mappings), this._options.result)
.then((r) => {
if (r.status !== 200)
throw new Error(
`Error in fetch: endpoint returned status other than 200 OK. Status: ${r.status} ${r.statusText}`,
);
return r;
})
.then((r) => this._options.result.parseResponse(r));
}
};