Skip to content

Commit 8df6e37

Browse files
authored
Merge pull request #15 from haproxytech/issue-13
Fixes #13: Adding HS512 algorithm support.
2 parents 1d1bdd7 + 3a98baf commit 8df6e37

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ A sample application can be found at https://github.com/haproxytechblog/haproxy-
4848

4949
* RS256
5050
* HS256
51+
* HS512

lib/jwtverify.lua

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ local function algorithmIsValid(token)
101101
if token.headerdecoded.alg == nil then
102102
log("No 'alg' provided in JWT header.")
103103
return false
104-
elseif token.headerdecoded.alg ~= 'HS256' and token.headerdecoded.alg ~= 'RS256' then
105-
log("HS256 and RS256 supported. Incorrect alg in JWT: " .. token.headerdecoded.alg)
104+
elseif token.headerdecoded.alg ~= 'HS256' and token.headerdecoded.alg ~= 'HS512' and token.headerdecoded.alg ~= 'RS256' then
105+
log("HS256, HS512 and RS256 supported. Incorrect alg in JWT: " .. token.headerdecoded.alg)
106106
return false
107107
end
108108

@@ -123,6 +123,12 @@ local function hs256SignatureIsValid(token, secret)
123123
return checksum == token.signaturedecoded
124124
end
125125

126+
local function hs512SignatureIsValid(token, secret)
127+
local hmac = openssl.hmac.new(secret, 'SHA512')
128+
local checksum = hmac:final(token.header .. '.' .. token.payload)
129+
return checksum == token.signaturedecoded
130+
end
131+
126132
local function expirationIsValid(token)
127133
return os.difftime(token.payloaddecoded.exp, core.now().sec) > 0
128134
end
@@ -149,7 +155,7 @@ function jwtverify(txn)
149155
goto out
150156
end
151157

152-
-- 2. Verify the signature algorithm is supported (HS256, RS256)
158+
-- 2. Verify the signature algorithm is supported (HS256, HS512, RS256)
153159
if algorithmIsValid(token) == false then
154160
log("Algorithm not valid.")
155161
goto out
@@ -166,6 +172,11 @@ function jwtverify(txn)
166172
log("Signature not valid.")
167173
goto out
168174
end
175+
elseif token.headerdecoded.alg == 'HS512' then
176+
if hs512SignatureIsValid(token, hmacSecret) == false then
177+
log("Signature not valid.")
178+
goto out
179+
end
169180
end
170181

171182
-- 4. Verify that the token is not expired
@@ -217,7 +228,7 @@ local publicKeyPath = os.getenv("OAUTH_PUBKEY_PATH")
217228
local pem = readAll(publicKeyPath)
218229
config.publicKey = pem
219230

220-
-- when using an HS256 signature
231+
-- when using an HS256 or HS512 signature
221232
config.hmacSecret = os.getenv("OAUTH_HMAC_SECRET")
222233

223234
log("PublicKeyPath: " .. publicKeyPath)

0 commit comments

Comments
 (0)