2015-11-02 15:02:50 -06:00
|
|
|
-- Token authentication
|
|
|
|
|
-- Copyright (C) 2015 Atlassian
|
|
|
|
|
|
2016-06-13 16:11:44 -05:00
|
|
|
local jwt = require "jwt";
|
2015-11-02 15:02:50 -06:00
|
|
|
|
|
|
|
|
local _M = {};
|
|
|
|
|
|
2015-12-22 19:51:43 +01:00
|
|
|
local function _get_room_name(token, appSecret)
|
|
|
|
|
local claims, err = jwt.decode(token, appSecret);
|
|
|
|
|
if claims ~= nil then
|
|
|
|
|
return claims["room"];
|
|
|
|
|
else
|
|
|
|
|
return nil, err;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-06-13 16:11:44 -05:00
|
|
|
local function _verify_token(token, appId, appSecret, roomName, disableRoomNameConstraints)
|
2015-11-02 15:02:50 -06:00
|
|
|
|
2015-12-22 19:51:43 +01:00
|
|
|
local claims, err = jwt.decode(token, appSecret, true);
|
2015-11-18 12:49:36 -06:00
|
|
|
if claims == nil then
|
|
|
|
|
return nil, err;
|
2015-11-02 15:02:50 -06:00
|
|
|
end
|
|
|
|
|
|
2015-11-18 12:49:36 -06:00
|
|
|
local issClaim = claims["iss"];
|
|
|
|
|
if issClaim == nil then
|
2016-04-20 16:37:36 -05:00
|
|
|
return nil, "'iss' claim is missing";
|
2015-11-02 15:02:50 -06:00
|
|
|
end
|
2015-11-18 12:49:36 -06:00
|
|
|
if issClaim ~= appId then
|
|
|
|
|
return nil, "Invalid application ID('iss' claim)";
|
2015-11-02 15:02:50 -06:00
|
|
|
end
|
|
|
|
|
|
2015-11-18 12:49:36 -06:00
|
|
|
local roomClaim = claims["room"];
|
2016-06-13 16:11:44 -05:00
|
|
|
if roomClaim == nil and disableRoomNameConstraints ~= true then
|
2016-04-20 16:37:36 -05:00
|
|
|
return nil, "'room' claim is missing";
|
2015-11-18 12:49:36 -06:00
|
|
|
end
|
2016-06-13 16:11:44 -05:00
|
|
|
if roomName ~= nil and roomName ~= roomClaim and disableRoomNameConstraints ~= true then
|
2015-11-18 12:49:36 -06:00
|
|
|
return nil, "Invalid room name('room' claim)";
|
2015-11-02 15:02:50 -06:00
|
|
|
end
|
2015-12-22 19:51:43 +01:00
|
|
|
|
2015-11-18 12:49:36 -06:00
|
|
|
return true;
|
2015-11-02 15:02:50 -06:00
|
|
|
end
|
|
|
|
|
|
2016-06-13 16:11:44 -05:00
|
|
|
function _M.verify_token(token, appId, appSecret, roomName, disableRoomNameConstraints)
|
|
|
|
|
return _verify_token(token, appId, appSecret, roomName, disableRoomNameConstraints);
|
2015-12-22 19:51:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function _M.get_room_name(token, appSecret)
|
|
|
|
|
return _get_room_name(token, appSecret);
|
2015-11-02 15:02:50 -06:00
|
|
|
end
|
|
|
|
|
|
2016-06-13 16:11:44 -05:00
|
|
|
return _M;
|