UNPKG

877 BJavaScriptView Raw
1const UnAuthenticated = require('./UnAuthenticated');
2const Aborted = require('./Aborted');
3
4/**
5 * A gate to determine of in the given context, the given Credential is
6 * authenticated or not.
7 */
8class Gateway {
9
10 /**
11 *
12 * @param {Protocol} protocol
13 * @param {IdentityProvider} provider
14 */
15 constructor(protocol, provider) {
16 this.protocol = protocol;
17 this.provider = provider;
18 }
19
20 /**
21 * Perform the authentication process with a given context.
22 *
23 * @return {Promise<*>}
24 */
25 async authenticate(context) {
26 const credential = await this.protocol.resolve(context);
27 const identity = await this.provider.provide(credential);
28
29 if (!identity) {
30 throw new UnAuthenticated("Identity not found");
31 }
32
33 return identity;
34 }
35}
36
37module.exports = Gateway;