forked from grahamearley/FirestoreGoogleAppsScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticate.js
More file actions
61 lines (51 loc) · 1.9 KB
/
Authenticate.js
File metadata and controls
61 lines (51 loc) · 1.9 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
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "_" }] */
/* global FirestoreRequest_, Utilities, base64EncodeSafe_ */
/**
* Auth token is formatted to {@link https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests}
*
* @private
* @param email the database service account email address
* @param key the database service account private key
* @param authUrl the authorization url
* @returns {string} the access token needed for making future requests
*/
function getAuthToken_ (email, key, authUrl) {
const jwt = createJwt_(email, key, authUrl)
var options = {
payload: 'grant_type=' + decodeURIComponent('urn:ietf:params:oauth:grant-type:jwt-bearer') + '&assertion=' + jwt
}
const responseObj = new FirestoreRequest_(authUrl, null, options).post()
return responseObj.access_token
}
/**
* Creates the JSON Web Token for OAuth 2.0
*
* @private
* @param email the database service account email address
* @param key the database service account private key
* @param authUrl the authorization url
* @returns {string} JWT to utilize
*/
function createJwt_ (email, key, authUrl) {
const jwtHeader = {
'alg': 'RS256',
'typ': 'JWT'
}
const now = new Date()
const nowSeconds = now.getTime() / 1000
now.setHours(now.getHours() + 1)
const oneHourFromNowSeconds = now.getTime() / 1000
const jwtClaim = {
'iss': email,
'scope': 'https://www.googleapis.com/auth/datastore',
'aud': authUrl,
'exp': oneHourFromNowSeconds,
'iat': nowSeconds
}
const jwtHeaderBase64 = base64EncodeSafe_(JSON.stringify(jwtHeader))
const jwtClaimBase64 = base64EncodeSafe_(JSON.stringify(jwtClaim))
const signatureInput = jwtHeaderBase64 + '.' + jwtClaimBase64
const signature = Utilities.computeRsaSha256Signature(signatureInput, key)
const encodedSignature = base64EncodeSafe_(signature)
return signatureInput + '.' + encodedSignature
}