-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
106 lines (89 loc) · 3 KB
/
app.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
99
100
101
102
103
104
105
106
let axios = require('axios');
let credentialsOAuth = Symbol('credentialsOAuth');
let urlsOAuth = Symbol('urlsOAuth');
let postOAuth = Symbol('postOAuth');
let querystring = require('querystring');
class OAuth2 {
constructor(clientId, clientSecret, redirecturl, scopes, accessToken, urlBase, urlAuthorizate='authorize', urlToken='token', urlRevoke='revoke') {
this[credentialsOAuth] = {
clientId: clientId,
clientSecret: clientSecret,
redirecturl: redirecturl,
scopes: scopes,
accessToken: accessToken,
refreshToken: '',
expiresIn: ''
};
this[urlsOAuth] = {
base: urlBase,
authorizate: urlAuthorizate,
token: urlToken,
revoke: urlRevoke
};
this.axiosOAuth = axios.create({
baseURL: urlBase
});
this.axiosOAuth.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
}
authorizationUrl() {
return `${this[urlsOAuth].base}${this[urlsOAuth].authorizate}?response_type=code&client_id=${this[credentialsOAuth].clientId}&redirect_uri=${this[credentialsOAuth].redirecturl}&scope=${this[credentialsOAuth].scopes}`;
}
getCredentials() {
return {
accessToken: this[credentialsOAuth].accessToken,
refreshToken: this[credentialsOAuth].refreshToken,
expiresIn: this[credentialsOAuth].expiresIn
};
}
connect(code) {
let url = `${this[urlsOAuth].token}`;
let data = {
grant_type: 'authorization_code',
client_id: this[credentialsOAuth].clientId,
client_secret: this[credentialsOAuth].clientSecret,
redirect_uri: this[credentialsOAuth].redirecturl,
code: code
};
return this[postOAuth](url, data).then((result) => {
this[credentialsOAuth].accessToken = result.data.access_token;
this[credentialsOAuth].refreshToken = result.data.refresh_token;
this[credentialsOAuth].expiresIn = result.data.expires_in;
return result;
});
}
reconnect(refreshToken) {
let url = `${this[urlsOAuth].token}`;
let data = {
grant_type: 'refresh_token',
client_id: this[credentialsOAuth].clientId,
client_secret: this[credentialsOAuth].clientSecret,
refresh_token: refreshToken
};
return this[postOAuth](url, data).then((result) => {
this[credentialsOAuth].accessToken = result.data.access_token;
this[credentialsOAuth].refreshToken = result.data.refresh_token || refreshToken;
this[credentialsOAuth].expiresIn = result.data.expires_in;
return result;
});
}
revoke() {
let url = `${this[urlsOAuth].revoke}`;
let data = {
token: this[credentialsOAuth].accessToken
};
return this[postOAuth](url, data).then((result) => {
this[credentialsOAuth].accessToken = '';
this[credentialsOAuth].refreshToken = '';
this[credentialsOAuth].expiresIn = 0;
return result;
});
}
[postOAuth](url, data) {
return this.axiosOAuth.post(url, querystring.stringify(data))
.catch((err) => {
console.log(`status: ${err.response.status}, url: ${err.response.config.url}, data: ${err.response.config.data}, message: ${JSON.stringify(err.response.data)}`);
return Promise.reject(err);
});
}
}
module.exports = OAuth2;