-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenBuilder.gs
More file actions
142 lines (120 loc) · 3.68 KB
/
tokenBuilder.gs
File metadata and controls
142 lines (120 loc) · 3.68 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* GAS library for generating user OAuth Tokens via Google service account. see
* @param {String} rsaKey Your private RSA key in PEM64
* @param {Array} Scopes An Array of scopes you want to authenticate
* @param {String} saEmail The service account Email
* @return {object} self for chaining
*/
function tokenBuilder(rsaKey, Scopes, saEmail){
var self = this;
var rsaKey_ = Utilities.newBlob(Utilities.base64DecodeWebSafe(rsaKey)).getDataAsString();
var Scopes_ = Scopes;
var saEmail_ = saEmail;
var jwts_ ;
var tokens_ = {};
var expireTime_;
var subAccounts_;
var signingServer_ = "https://130.211.114.57:8080/";
if (!rsaKey_) {
throw 'You must provide the private rsa key';
}
if(!(Scopes_.constructor === Array)){
throw "The Scopes must be in a valid array"
}
if (!Scopes_) {
throw 'You must provide atleast one scope';
}
if (!saEmail_) {
throw 'You must provide the service account email';
}
self.setSigningServer = function(serverUrl){
signingServer_ = serverUrl;
}
self.addUser = function(userEmail){
if(!subAccounts_){
subAccounts_ = [];
}
subAccounts_.push(userEmail);
return self;
}
self.generateJWT = function(){
if(!subAccounts_){
throw new Error("You must add at least one user account");
}
var sPayloads = [];
for(var i=0; i<subAccounts_.length;i++){
sPayloads.push({"claim":makeClaim(subAccounts_[i]),"user":subAccounts_[i],"expire":expireTime_});
}
var payload = {"payloads":sPayloads, "key":rsaKey_, "algorithm": "RS256"};
var stringifiedPayload = JSON.stringify(payload);
var sResult = null;
try {
sResult = UrlFetchApp
.fetch(signingServer_,
{"validateHttpsCertificates":false,
"method":"post",
"payload":JSON.stringify(payload),
"contentType":"application/json"}
)
.getContentText();
jwts_ = JSON.parse(sResult);
} catch (ex) {
throw new Error("Error generating JWT: " + ex);
}
return self;
}
self.getToken = function(userEmail){
if(!(userEmail in tokens_)){
throw new Error("User not found");
}else{
return tokens_[userEmail];
}
}
self.getTokens = function(){
return tokens_;
}
self.requestToken = function(){
if(!jwts_){
throw 'You must run generateJWT'
}
var params = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: ''
}
var url = "https://accounts.google.com/o/oauth2/token"
var parameters = { 'method' : 'post',
'payload' : params,
muteHttpExceptions:true};
var response = "";
for(var user in jwts_){
params.assertion = jwts_[user].claim;
response = JSON.parse(UrlFetchApp.fetch(url,parameters).getContentText());
if(response.error){
throw new Error('There was an error requesting a Token from the OAuth server: '+ response.error);
}
if(response.access_token)
{
tokens_[user]={};
tokens_[user].token = response.access_token;
tokens_[user].expire = jwts_[user].expire;
}
}
return self;
}
return self;
function makeClaim(subAccount){
var now = (Date.now()/1000).toString().substr(0,10);
var exp = (parseInt(now) + 3600).toString().substr(0,10);
expireTime_ = exp;
var claim =
{
"iss": saEmail_,
"sub": subAccount,
"scope": Scopes_.join(" "),
"aud":"https://accounts.google.com/o/oauth2/token",
"iat": now,
"exp": exp
};
return claim;
}
}