Repository navigation
Expand file tree
/
Copy pathaccessToken.js
More file actions
67 lines (61 loc) · 1.99 KB
/
Copy pathaccessToken.js
File metadata and controls
67 lines (61 loc) · 1.99 KB
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
const db = require('@arangodb').db;
const google_session = db._collection('google_session');
const aql = require('@arangodb').aql;
const request = require('@arangodb/request');
const jsrsasign = require('jsrsasign');
const iisGoogle = require('./config').iisGoogle;
const privatKey = require('./config').privatKey;
function authorize() {
// Header
const oHeader = {alg: 'RS256', typ: 'JWT'};
// Payload
let oPayload = {};
const tNow = jsrsasign.jws.IntDate.get('now');
const tEnd = jsrsasign.jws.IntDate.get('now + 1hour');
oPayload.iss = iisGoogle;
oPayload.scope = "https://www.googleapis.com/auth/spreadsheets";
oPayload.aud = "https://www.googleapis.com/oauth2/v4/token";
oPayload.exp = tEnd;
oPayload.iat = tNow;
const sHeader = JSON.stringify(oHeader);
const sPayload = JSON.stringify(oPayload);
const sJWT = jsrsasign.jws.JWS.sign("RS256", sHeader, sPayload, privatKey);
const getToken = request(
{
method: 'POST',
url: 'https://www.googleapis.com/oauth2/v4/token',
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: sJWT
}
});
if (getToken && getToken.json && getToken.json.access_token) {
const token = `${getToken.json.token_type} ${getToken.json.access_token}`;
google_session.truncate();
google_session.insert({
accessToken: token,
exp: oPayload.exp,
iat: oPayload.iat
}
);
return token
} else {
return {error: true, getToken, sJWT}
}
}
function getAutorise () {
const dbToken = db._query(aql`
FOR session IN google_session
FILTER session.exp > (DATE_NOW()/1000 + 60)
RETURN session.accessToken
`);
if (dbToken._documents[0]) {
return dbToken._documents[0]
} else {
return authorize()
}
}
module.exports = {
authorize,
getAutorise
};