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