@@ -52,6 +52,8 @@ local type = type
5252local ngx = ngx
5353local b64 = ngx .encode_base64
5454local unb64 = ngx .decode_base64
55+ local b64url = require (" ngx.base64" ).encode_base64url
56+ local unb64url = require (" ngx.base64" ).decode_base64url
5557
5658local log = ngx .log
5759local DEBUG = ngx .DEBUG
@@ -278,23 +280,6 @@ local function openidc_get_redirect_uri(opts, session)
278280 return scheme .. " ://" .. host .. path
279281end
280282
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-
298283local function openidc_combine_uri (uri , params )
299284 if params == nil or next (params ) == nil then
300285 return uri
@@ -310,10 +295,12 @@ local function decorate_request(http_request_decorator, req)
310295 return http_request_decorator and http_request_decorator (req ) or req
311296end
312297
298+ local sha256 = (require ' resty.sha256' ):new ()
313299local function openidc_s256 (verifier )
314- local sha256 = (require ' resty.sha256' ):new ()
315300 sha256 :update (verifier )
316- return openidc_base64_url_encode (sha256 :final ())
301+ local s256 = b64url (sha256 :final ())
302+ sha256 :reset ()
303+ return s256
317304end
318305
319306-- send the browser of to the OP's authorization endpoint
@@ -326,7 +313,7 @@ local function openidc_authorize(opts, session, target_url, prompt)
326313 local state = resty_string .to_hex (resty_random .bytes (16 ))
327314 local nonce = (opts .use_nonce == nil or opts .use_nonce )
328315 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 ))
330317
331318 -- assemble the parameters to the authentication request
332319 local params = {
@@ -537,8 +524,8 @@ local function openidc_access_token_expires_in(opts, expires_in)
537524end
538525
539526local 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 ))
542529 if header and payload and header .alg == " none" then
543530 return {
544531 raw_header = enc_hdr ,
@@ -856,7 +843,7 @@ local function openidc_pem_from_rsa_n_and_e(n, e)
856843 log (DEBUG , " getting PEM public key from n and e parameters of json public key" )
857844
858845 local der_key = {
859- openidc_base64_url_decode (n ), openidc_base64_url_decode (e )
846+ unb64url (n ), unb64url (e )
860847 }
861848 local encoded_key = encode_sequence_of_integer (der_key )
862849 local pem = der2pem (encode_sequence ({
@@ -949,8 +936,9 @@ local function is_algorithm_expected(jwt_header, expected_algs)
949936 return true
950937 end
951938 if type (expected_algs ) == ' string' then
952- expected_algs = { expected_algs }
939+ return expected_algs == jwt_header . alg
953940 end
941+
954942 for _ , alg in ipairs (expected_algs ) do
955943 if alg == jwt_header .alg then
956944 return true
0 commit comments