@@ -52,6 +52,8 @@ local type = type
52
52
local ngx = ngx
53
53
local b64 = ngx .encode_base64
54
54
local unb64 = ngx .decode_base64
55
+ local b64url = require (" ngx.base64" ).encode_base64url
56
+ local unb64url = require (" ngx.base64" ).decode_base64url
55
57
56
58
local log = ngx .log
57
59
local DEBUG = ngx .DEBUG
@@ -278,23 +280,6 @@ local function openidc_get_redirect_uri(opts, session)
278
280
return scheme .. " ://" .. host .. path
279
281
end
280
282
281
- -- perform base64url decoding
282
- local function openidc_base64_url_decode (input )
283
- local reminder = # input % 4
284
- if reminder > 0 then
285
- local padlen = 4 - reminder
286
- input = input .. string.rep (' =' , padlen )
287
- end
288
- input = input :gsub (' %-' , ' +' ):gsub (' _' , ' /' )
289
- return unb64 (input )
290
- end
291
-
292
- -- perform base64url encoding
293
- local function openidc_base64_url_encode (input )
294
- local output = b64 (input , true )
295
- return output :gsub (' %+' , ' -' ):gsub (' /' , ' _' )
296
- end
297
-
298
283
local function openidc_combine_uri (uri , params )
299
284
if params == nil or next (params ) == nil then
300
285
return uri
@@ -310,10 +295,12 @@ local function decorate_request(http_request_decorator, req)
310
295
return http_request_decorator and http_request_decorator (req ) or req
311
296
end
312
297
298
+ local sha256 = (require ' resty.sha256' ):new ()
313
299
local function openidc_s256 (verifier )
314
- local sha256 = (require ' resty.sha256' ):new ()
315
300
sha256 :update (verifier )
316
- return openidc_base64_url_encode (sha256 :final ())
301
+ local s256 = b64url (sha256 :final ())
302
+ sha256 :reset ()
303
+ return s256
317
304
end
318
305
319
306
-- send the browser of to the OP's authorization endpoint
@@ -326,7 +313,7 @@ local function openidc_authorize(opts, session, target_url, prompt)
326
313
local state = resty_string .to_hex (resty_random .bytes (16 ))
327
314
local nonce = (opts .use_nonce == nil or opts .use_nonce )
328
315
and resty_string .to_hex (resty_random .bytes (16 ))
329
- local code_verifier = opts .use_pkce and openidc_base64_url_encode (resty_random .bytes (32 ))
316
+ local code_verifier = opts .use_pkce and b64url (resty_random .bytes (32 ))
330
317
331
318
-- assemble the parameters to the authentication request
332
319
local params = {
@@ -537,8 +524,8 @@ local function openidc_access_token_expires_in(opts, expires_in)
537
524
end
538
525
539
526
local function openidc_load_jwt_none_alg (enc_hdr , enc_payload )
540
- local header = cjson_s .decode (openidc_base64_url_decode (enc_hdr ))
541
- local payload = cjson_s .decode (openidc_base64_url_decode (enc_payload ))
527
+ local header = cjson_s .decode (unb64url (enc_hdr ))
528
+ local payload = cjson_s .decode (unb64url (enc_payload ))
542
529
if header and payload and header .alg == " none" then
543
530
return {
544
531
raw_header = enc_hdr ,
@@ -856,7 +843,7 @@ local function openidc_pem_from_rsa_n_and_e(n, e)
856
843
log (DEBUG , " getting PEM public key from n and e parameters of json public key" )
857
844
858
845
local der_key = {
859
- openidc_base64_url_decode (n ), openidc_base64_url_decode (e )
846
+ unb64url (n ), unb64url (e )
860
847
}
861
848
local encoded_key = encode_sequence_of_integer (der_key )
862
849
local pem = der2pem (encode_sequence ({
@@ -949,8 +936,9 @@ local function is_algorithm_expected(jwt_header, expected_algs)
949
936
return true
950
937
end
951
938
if type (expected_algs ) == ' string' then
952
- expected_algs = { expected_algs }
939
+ return expected_algs == jwt_header . alg
953
940
end
941
+
954
942
for _ , alg in ipairs (expected_algs ) do
955
943
if alg == jwt_header .alg then
956
944
return true
0 commit comments