Skip to content

Commit a743beb

Browse files
committed
Fix #157 : Update Id token in session after refreshing tokens
1 parent 5a9ac8e commit a743beb

File tree

1 file changed

+48
-27
lines changed

1 file changed

+48
-27
lines changed

lib/resty/openidc.lua

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,44 @@ local function openidc_load_jwt_and_verify_crypto(opts, jwt_string, asymmetric_s
829829
return jwt_obj
830830
end
831831

832+
--
833+
-- Load and validate id token from the id_token properties of the token endpoint response
834+
-- Parameters :
835+
-- - opts the openidc module options
836+
-- - jwt_id_token the id_token from the id_token properties of the token endpoint response
837+
-- - session the current session
838+
-- Return the id_token, nil if valid
839+
-- Return nil, the error if invalid
840+
--
841+
local function openidc_load_and_validate_jwt_id_token(opts, jwt_id_token, session)
842+
843+
local jwt_obj, err = openidc_load_jwt_and_verify_crypto(opts, jwt_id_token, opts.secret, opts.client_secret)
844+
if err then
845+
local is_unsupported_signature_error = jwt_obj and not jwt_obj.verified and not is_algorithm_supported(jwt_obj.header)
846+
if is_unsupported_signature_error then
847+
ngx.log(ngx.WARN, "ignored id_token signature as algorithm '" .. jwt_obj.header.alg .. "' is not supported")
848+
else
849+
local alg = (jwt_obj and jwt_obj.header and jwt_obj.header.alg) or ''
850+
ngx.log(ngx.ERR, "id_token '" .. alg .. "' signature verification failed")
851+
return nil, err
852+
end
853+
end
854+
local id_token = jwt_obj.payload
855+
856+
ngx.log(ngx.DEBUG, "id_token header: ", cjson.encode(jwt_obj.header))
857+
ngx.log(ngx.DEBUG, "id_token payload: ", cjson.encode(jwt_obj.payload))
858+
859+
-- validate the id_token contents
860+
if openidc_validate_id_token(opts, id_token, session.data.nonce) == false then
861+
err = "id_token validation failed"
862+
ngx.log(ngx.ERR, err)
863+
return nil, err
864+
end
865+
866+
return id_token
867+
868+
end
869+
832870
-- handle a "code" authorization response from the OP
833871
local function openidc_authorization_response(opts, session)
834872
local args = ngx.req.get_uri_args()
@@ -879,34 +917,8 @@ local function openidc_authorization_response(opts, session)
879917
return nil, err, session.data.original_url, session
880918
end
881919

882-
local jwt_obj
883-
jwt_obj, err = openidc_load_jwt_and_verify_crypto(opts, json.id_token, opts.secret, opts.client_secret,
884-
opts.discovery.id_token_signing_alg_values_supported)
920+
local id_token, err = openidc_load_and_validate_jwt_id_token(opts, json.id_token, session);
885921
if err then
886-
local alg = (jwt_obj and jwt_obj.header and jwt_obj.header.alg) or ''
887-
local is_unsupported_signature_error = jwt_obj and not jwt_obj.verified and not is_algorithm_supported(jwt_obj.header)
888-
if is_unsupported_signature_error then
889-
if opts.accept_unsupported_alg == nil or opts.accept_unsupported_alg then
890-
ngx.log(ngx.WARN, "ignored id_token signature as algorithm '" .. alg .. "' is not supported")
891-
else
892-
err = "token is signed using algorithm \"" .. alg .. "\" which is not supported by lua-resty-jwt"
893-
ngx.log(ngx.ERR, err)
894-
return nil, err, session.data.original_url, session
895-
end
896-
else
897-
ngx.log(ngx.ERR, "id_token '" .. alg .. "' signature verification failed")
898-
return nil, err, session.data.original_url, session
899-
end
900-
end
901-
local id_token = jwt_obj.payload
902-
903-
ngx.log(ngx.DEBUG, "id_token header: ", cjson.encode(jwt_obj.header))
904-
ngx.log(ngx.DEBUG, "id_token payload: ", cjson.encode(jwt_obj.payload))
905-
906-
-- validate the id_token contents
907-
if openidc_validate_id_token(opts, id_token, session.data.nonce) == false then
908-
err = "id_token validation failed"
909-
ngx.log(ngx.ERR, err)
910922
return nil, err, session.data.original_url, session
911923
end
912924

@@ -1098,6 +1110,15 @@ local function openidc_access_token(opts, session, try_to_renew)
10981110
session.data.refresh_token = json.refresh_token
10991111
end
11001112

1113+
if json.id_token ~= nil then
1114+
local id_token, err = openidc_load_and_validate_jwt_id_token(opts, json.id_token, session)
1115+
if err then
1116+
session:save()
1117+
return nil, err
1118+
end
1119+
session.data.id_token = id_token
1120+
end
1121+
11011122
-- save the session with the new access_token and optionally the new refresh_token
11021123
session:save()
11031124

0 commit comments

Comments
 (0)