-
Notifications
You must be signed in to change notification settings - Fork 1
/
OAuthAuthentication.js
98 lines (71 loc) · 2.21 KB
/
OAuthAuthentication.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const logger = require(`@geek/logger`).createLogger(`@titanium/authentication-oauth`, { meta: { filename: __filename } });
const TOKEN = Symbol(`token`);
const AuthToken = require(`@geek/jwt/AuthToken`);
class OAuthAuthentication {
constructor(flowName, options = {}) {
logger.track(`🔒 you are here → OAuthAuthentication.constructor`);
let Flow;
switch (flowName) {
case `code`:
// TIBUG: https://jira.appcelerator.org/browse/TIMOB-28037
// Flow = require('./flows/CodeFlow');
Flow = require(`./flows/Code`);
break;
case `password`:
// TIBUG: https://jira.appcelerator.org/browse/TIMOB-28037
// Flow = require('./flows/OwnerResourceFlow');
Flow = require(`./flows/Password`);
break;
default:
}
if (options.token) {
if (! (options.token instanceof AuthToken)) {
logger.warn(`Token is not an instance of AuthToken.`);
options.token = new AuthToken(options.token);
}
this[TOKEN] = options.token;
}
this.flow = new Flow(options);
Object.defineProperty(this, `token`, {
enumerable: true,
get () {
return this[TOKEN];
},
});
}
async logout(...args) {
logger.track(`🔒 You are here → OAuthAuthentication.logout()`);
this[TOKEN] = null;
await this.flow.logout(...args);
}
async renew(...args) {
logger.track(`🔒 you are here → OAuthAuthentication.renew()`);
this[TOKEN] = await this.flow.renewAccessToken(...args);
return this[TOKEN];
}
async authenticate(...args) {
logger.track(`🔒 you are here → OAuthAuthentication.authenticate()`);
this[TOKEN] = await this.flow.authenticate(...args);
return this[TOKEN];
}
async isAuthenticated(token) {
logger.track(`🔒 You are here → OAuthAuthentication.isAuthenticated()`);
if (_.isNil(token)) {
if (_.isNil(this.token)) {
return false;
}
token = this.token;
}
if (! (token instanceof AuthToken)) {
logger.warn(`Token is not an instance of AuthToken.`);
// token = new OAuthAuthentication.AuthToken(token);
return false;
}
return token.isAccessTokenExpired();
}
renewAccessToken(...args) {
return this.flow.renewAccessToken(...args);
}
}
OAuthAuthentication.AuthToken = AuthToken;
module.exports = OAuthAuthentication;