-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (46 loc) · 1.6 KB
/
index.js
File metadata and controls
55 lines (46 loc) · 1.6 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
'use strict';
var KJUR = require('jsrsasign');
var axios = require('axios');
var EasyGoogleToken = {
getTokenWithServiceAccountFile: function ( creds, scopes, callback ) {
if (creds === undefined) creds = {};
if (Object.keys(creds).length == 0) { return 'Need Service Account File to continue' };
if (creds.private_key == null) { return 'Need private_key in your File to continue' };
if (creds.private_key_id == null) { return 'Need private_key_id in your File to continue' };
if (creds.client_email == null) { return 'Need client_email in your File to continue' };
// Header
const header = {
alg: 'RS256',
typ: 'JWT',
kid: creds.private_key_id
};
// Payload
const payload = {
iss: creds.client_email,
sub: creds.client_email,
scope: scopes,
iat: KJUR.jws.IntDate.get('now'),
exp: KJUR.jws.IntDate.get('now + 1hour'),
aud: 'https://www.googleapis.com/oauth2/v4/token'
};
const stringHeader = JSON.stringify(header);
const stringPayload = JSON.stringify(payload);
var token = KJUR.jws.JWS.sign('RS256', stringHeader, stringPayload, creds.private_key);
axios.post('https://www.googleapis.com/oauth2/v4/token', {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: token
})
.then(function (response) {
// handle success
callback(response);
})
.catch(function (error) {
// handle error
callback(error);
})
.finally(function () {
// always executed
});
},
}
module.exports = EasyGoogleToken;