Last active
June 30, 2016 16:53
-
-
Save thiagopnts/8bb510124b8a016689a129fa34b0138f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local hmac = require "resty.hmac" | |
local setmetatable = setmetatable | |
local _M = { _VERSION = '0.0.1' } | |
local mt = { __index = _M } | |
function _M.new(self, secret) | |
return setmetatable({_secret = secret}, mt) | |
end | |
function _M.is_valid(self, auth_header, url, custom_header) | |
if not auth_header or not url or not custom_header then | |
return false | |
end | |
local hmac512 = hmac:new() | |
local string_to_sign = url .. ":" .. custom_header | |
local digest = hmac:digest("sha512", self._secret, string_to_sign) | |
local signed_b64 = ngx.encode_base64(digest) | |
local result = "NYTV " .. signed_b64 | |
return result == auth_header | |
end | |
return _M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment