Skip to content

Commit ddd3dda

Browse files
authored
Merge pull request #34 from Koredotcom/XOP-2118
Added support for HS512 JWT signing algorithm
2 parents d3a118b + 5f4a712 commit ddd3dda

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
"appId": "test_app_id2"
1818
}
1919
},
20+
"jwt": {
21+
"jwtAlgorithm": "HS256",
22+
"jwt-expiry": 60,
23+
"st-67890":{
24+
"jwtAlgorithm": "HS512",
25+
"jwt-expiry": 60
26+
}
27+
},
2028
"redis": {
2129
"options": {
2230
"host": "localhost",

lib/app/middlewares/APIKeyMiddleware/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var jwt = require("jwt-simple");
77
var config = require('../../../../config');
88
var apiPrefix = config.app.apiPrefix;
99
var credentials = config.credentials;
10+
var jwtProps = config.jwt;
1011

1112
function APIKeyMiddleware() {
1213
var botIdregex = /(?<botId>st-[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-5[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})/;
@@ -17,6 +18,7 @@ function APIKeyMiddleware() {
1718

1819
var botId = url.match(botIdregex).groups.botId;
1920
var cred = credentials[botId]?credentials[botId]:credentials;
21+
var jwtAlg = (jwtProps[botId] ? jwtProps[botId].jwtAlgorithm : jwtProps.jwtAlgorithm) || "HS256" ; //Adding HS256 as default algorithm if config is not set.
2022

2123
if(_.has(header, 'apikey')){//DEPRECATED::SOON TO BE REMOVED
2224
if(header.apikey===cred.apikey)
@@ -25,7 +27,7 @@ function APIKeyMiddleware() {
2527
if(_.has(header, 'token')){
2628
var appId;
2729
try {
28-
appId = jwt.decode(header.token, cred.apikey).appId;
30+
appId = jwt.decode(header.token, cred.apikey, false, jwtAlg).appId;
2931
} catch(e){
3032
console.info("invalid jwt token");
3133
}

lib/sdk/lib/invokePlatformAPIs.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,29 @@ var config = require("../../../config");
44
var { makeHttpCall } = require("../../../makeHttpCall");
55

66
function getSignedJWTToken(botId) {
7-
var appId, apiKey;
7+
var appId, apiKey, jwtAlgorithm, jwtExpiry;
8+
var defAlg = "HS256";
9+
810
if (config.credentials[botId]) {
911
appId = config.credentials[botId].appId;
1012
apiKey = config.credentials[botId].apikey;
1113
} else {
1214
appId = config.credentials.appId;
1315
apiKey = config.credentials.apikey;
1416
}
17+
18+
if (config.jwt[botId]) {
19+
jwtAlgorithm = config.jwt[botId].jwtAlgorithm;
20+
jwtExpiry = config.jwt[botId].jwtExpiry;
21+
} else {
22+
jwtAlgorithm = config.jwt.jwtAlgorithm;
23+
jwtExpiry = config.jwt.jwtExpiry;
24+
}
25+
1526
return jwt.encode({
1627
appId: appId,
17-
exp: Date.now()/1000 + (config.jwt_expiry || 60) //set the default expiry as 60 seconds
18-
}, apiKey);
28+
exp: Date.now()/1000 + (jwtExpiry || 60) //set the default expiry as 60 seconds
29+
}, apiKey, (jwtAlgorithm || defAlg));
1930
}
2031

2132
function makeRequest(url, method, body, opts) {
@@ -24,6 +35,7 @@ function makeRequest(url, method, body, opts) {
2435
opts = opts || {};
2536
headers = opts.headers || {};
2637
headers['content-type'] = 'application/json';
38+
2739
headers.auth = getSignedJWTToken(botId);
2840

2941
return new Promise(function(resolve, reject) {

0 commit comments

Comments
 (0)