forked from zendesk/zendesk_jwt_sso_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_jwt.js
38 lines (29 loc) · 1004 Bytes
/
node_jwt.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
// requires jwt-simple, uuid
// install with: npm install jwt-simple uuid
// or from: https://github.com/hokaccha/node-jwt-simple and https://github.com/broofa/node-uuid
var http = require('http');
var jwt = require('jwt-simple');
var uuid = require('uuid');
var url = require('url');
var subdomain = '{my zendesk subdomain}';
var shared_key = '{my zendesk token}';
http.createServer(function (request, response) {
var payload = {
iat: (new Date().getTime() / 1000),
jti: uuid.v4()
// name: user.name(),
// email: user.email()
};
// encode
var token = jwt.encode(payload, shared_key);
var redirect = 'https://' + subdomain + '.zendesk.com/access/jwt?jwt=' + token;
var query = url.parse(request.url, true).query;
if(query['return_to']) {
redirect += '&return_to=' + encodeURIComponent(query['return_to']);
}
response.writeHead(302, {
'Location': redirect
});
response.end();
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');