-
Notifications
You must be signed in to change notification settings - Fork 5
/
guestissuer-util.js
84 lines (68 loc) · 2.56 KB
/
guestissuer-util.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
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
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
const debug = require('debug')('guest:util');
//
// Builds a Guest Token from the specifed user info
//
module.exports.createGuestToken = function (issuer, secret, userid, username, expiresInSeconds) {
debug(`generating Guest token for user with id: ${userid}, name: ${username}, in Guest Issuer: ${issuer}`);
try {
// sign with HMAC SHA256
const jwt = require('jsonwebtoken');
const payload = {
"sub": userid,
"name": username,
"iss": issuer,
"exp": expiresInSeconds
};
const decoded = Buffer.from(secret, 'base64');
const guestToken = jwt.sign(payload, decoded, { algorithm: 'HS256', noTimestamp: true });
debug("successfully built Guest token: " + guestToken.substring(0,30));
return guestToken;
}
catch (err) {
console.error("failed to generate a Guest token, exiting...");
debug("err: " + err.message);
process.exit(1);
}
}
//
// Request an access token for the specified Guest User's Issuer Token
//
module.exports.fetchToken = function (guestToken) {
debug("requesting new access token");
debug('contacting Webex API endpoint: /jwt/login');
const axios = require('axios');
axios.post('https://api.ciscospark.com/v1/jwt/login', '',
{ headers: { 'Authorization': 'Bearer ' + guestToken } })
.then(response => {
if (!response.data || !response.data.token) {
debug("no token found in response: " + response);
console.log("failed to generate an access token: bad response");
console.log("exiting...");
process.exit(1);
}
let accessToken = response.data.token;
console.log(`Here is an access token, valid for: ${response.data.expiresIn} seconds`);
console.log(accessToken);
})
.catch(err => {
switch (err.code) {
case 'ENOTFOUND':
debug("could not contact the Webex API");
break
default:
debug("error accessing /jwt/login, err: " + err.message);
if (err.response && (err.response.status >= 400) && (err.response.status < 500)) {
console.log(`Invalid Guest token: ${err.response.data.message}`);
process.exit(1);
}
break;
}
console.log("failed to generate an access token");
console.log("exiting...");
process.exit(1);
})
}