-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthentication-static.js
60 lines (45 loc) · 1.61 KB
/
Authentication-static.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
const _ = require('lodash');
let authenticationService;
class Authentication {
static initialize({ provider, options }) {
if (_.isNil(authenticationService)) {
if (_.isNil(provider)) {
throw new Error('Must provide a valid Authentication service provider');
}
if (_.isString(provider)) {
authenticationService = new (require(provider))(options);
} else {
authenticationService = provider;
}
} else {
throw new Error('Authentication service already initialized');
}
}
static async authenticate({ username, password, options = {} } = {}) {
logger.track('🔒 you are here → Authentication.authenticate');
if (_.isNil(authenticationService)) {
throw new Error('Authentication service has not been initialized');
}
const authenticationResponse = await authenticationService.authenticate({ username, password, options });
return authenticationResponse;
}
static async isAuthenticated() {
logger.track('🔒 you are here → Authentication.isAuthenticated');
if (_.isNil(authenticationService)) {
throw new Error('Authentication service has not been initialized');
}
return await authenticationService.isAuthenticated();
}
static async getPublicKey({ kid } = {}) {
logger.track(`🔒 you are here → Authentication.getPublicKey({kid:${kid}})`);
if (_.isNil(authenticationService)) {
throw new Error('Authentication service has not been initialized');
}
let public_key;
if (_.isFunction(authenticationService.getPublicKey)) {
public_key = await authenticationService.getPublicKey({ kid });
}
return public_key;
}
}
module.exports = Authentication;