forked from ArmedGuy/discourse_sso_node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (49 loc) · 1.39 KB
/
index.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
50
51
52
53
54
55
56
57
var crypto = require("crypto"),
querystring = require("querystring");
(function() {
"use strict";
var discourse_sso = function(secret) {
this.sso_secret = secret;
}
discourse_sso.prototype.getHmac = function() {
return crypto.createHmac("sha256", this.sso_secret);
}
discourse_sso.prototype.validate = function(payload, sig) {
var hmac = this.getHmac();
hmac.update(querystring.unescape(payload));
if(hmac.digest("hex") === sig) {
return true;
} else {
return false;
}
}
discourse_sso.prototype.getNonce = function(payload) {
var q = querystring.parse(
new Buffer( querystring.unescape(payload) , 'base64').toString()
);
if("nonce" in q) {
return q["nonce"];
} else {
throw new Exception("Missing Nonce in payload!");
}
}
discourse_sso.prototype.buildLoginString = function(params) {
if(!("external_id" in params)) {
throw new Exception("Missing required parameter 'external_id'");
}
if(!("nonce" in params)) {
throw new Exception("Missing required parameter 'nonce'");
}
if(!("email" in params)) {
throw new Exception("Missing required parameter 'email'");
}
var payload = new Buffer( querystring.stringify(params) , 'utf8').toString("base64");
var hmac = this.getHmac();
hmac.update(payload);
return querystring.stringify({
'sso': payload,
'sig': hmac.digest('hex')
});
}
module.exports = discourse_sso;
})();