-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
49 lines (45 loc) · 1.48 KB
/
auth.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
//auth.js
var config = require("./SACSConfig");
var base64 = require("./base64");
var Client = require('node-rest-client').Client;
require("datejs");
var tokenString = "";
var expirationDate;
module.exports = {
getAuthString : function() {
if (tokenString.length == 0 || expirationDate == null || expirationDate.isBefore()) {
var client = new Client();
var args = {
data : "grant_type=client_credentials",
headers : {"Authorization" : "Basic " + this._buildAuthString(),
"Content-Type" : "application/x-www-form-urlencoded"}
}
var promise = new Promise(function(accept, reject) {
client.post(config.environment + "/v2/auth/token", args, function(data) {
tokenString = data.access_token;
expirationDate = Date.today().setTimeToNow().addSeconds(data.expires_in);
console.log("Token aquired; string: "+tokenString);
console.log("Token aquired; token's expiration date: "+expirationDate);
accept(data.access_token);
});
});
return promise;
} else {
return new Promise(function(accept, reject) {
console.log("Just reading the previously aquired token...");
accept(tokenString);
})
}
},
_buildAuthString : function() {
var credentials = config.formatVersion
+ ":"
+ config.userId
+ ":"
+ config.group
+ ":"
+ config.domain;
var secret = base64.encode(config.clientSecret);
return base64.encode(base64.encode(credentials)+":"+secret);
}
};