@@ -4,12 +4,16 @@ local utils = require("kong.plugins.oidc.utils")
44local filter = require (" kong.plugins.oidc.filter" )
55local session = require (" kong.plugins.oidc.session" )
66
7- local singletons = require " kong.singletons"
87local constants = require " kong.constants"
9- local responses = require " kong.tools.responses"
8+
9+ local kong = kong
1010
1111OidcHandler .PRIORITY = 1000
1212
13+ local function internal_server_error (err )
14+ kong .log .err (err )
15+ return kong .response .exit (500 , { message = " An unexpected error occurred" })
16+ end
1317
1418function OidcHandler :new ()
1519 OidcHandler .super .new (self , " oidc" )
1822function OidcHandler :access (config )
1923 OidcHandler .super .access (self )
2024
21- if ngx . ctx . authenticated_credential and config .anonymous ~= " " then
25+ if config .anonymous and kong . client . get_credential () then
2226 -- we're already authenticated, and we're configured for using anonymous,
2327 -- hence we're in a logical OR between auth methods and we're already done.
2428 return
@@ -69,19 +73,20 @@ function make_oidc(oidcConfig)
6973 ngx .log (ngx .DEBUG , " Entering recovery page: " .. oidcConfig .recovery_page_path )
7074 ngx .redirect (oidcConfig .recovery_page_path )
7175 end
72- if oidcConfig .anonymous ~= " " then
76+ if oidcConfig .anonymous then
7377 -- get anonymous user
74- local consumer_cache_key = singletons .db .consumers :cache_key (oidcConfig .anonymous )
75- local consumer , err = singletons .cache :get (consumer_cache_key , nil ,
76- load_consumer_into_memory ,
77- oidcConfig .anonymous , true )
78+ local consumer_cache_key = kong .db .consumers :cache_key (oidcConfig .anonymous )
79+ local consumer , err = kong .cache :get (consumer_cache_key , nil ,
80+ load_consumer_into_memory ,
81+ oidcConfig .anonymous , true )
7882 if err then
79- return responses . send_HTTP_INTERNAL_SERVER_ERROR (err )
83+ return internal_server_error (err )
8084 end
85+
8186 set_consumer (consumer , nil , nil )
8287
8388 else
84- utils . exit (500 , err , ngx . HTTP_INTERNAL_SERVER_ERROR )
89+ return kong . response . exit (err . status , err . message , err . headers )
8590 end
8691 end
8792 return res
@@ -93,19 +98,20 @@ function introspect(oidcConfig)
9398 if err then
9499 if oidcConfig .bearer_only == " yes" then
95100 ngx .header [" WWW-Authenticate" ] = ' Bearer realm="' .. oidcConfig .realm .. ' ",error="' .. err .. ' "'
96- if oidcConfig .anonymous ~= " " then
101+ if oidcConfig .anonymous then
97102 -- get anonymous user
98- local consumer_cache_key = singletons .db .consumers :cache_key (oidcConfig .anonymous )
99- local consumer , err = singletons .cache :get (consumer_cache_key , nil ,
100- load_consumer_into_memory ,
101- oidcConfig .anonymous , true )
103+ local consumer_cache_key = kong .db .consumers :cache_key (oidcConfig .anonymous )
104+ local consumer , err = kong .cache :get (consumer_cache_key , nil ,
105+ load_consumer_into_memory ,
106+ oidcConfig .anonymous , true )
102107 if err then
103- return responses . send_HTTP_INTERNAL_SERVER_ERROR (err )
108+ return internal_server_error (err )
104109 end
110+
105111 set_consumer (consumer , nil , nil )
106112
107113 else
108- utils . exit (ngx . HTTP_UNAUTHORIZED , err , ngx . HTTP_UNAUTHORIZED )
114+ return kong . response . exit (err . status , err . message , err . headers )
109115 end
110116
111117 end
@@ -120,17 +126,48 @@ end
120126-- TESTING
121127
122128local function set_consumer (consumer , credential , token )
123- ngx_set_header (constants .HEADERS .CONSUMER_ID , consumer .id )
124- ngx_set_header (constants .HEADERS .CONSUMER_CUSTOM_ID , consumer .custom_id )
125- ngx_set_header (constants .HEADERS .CONSUMER_USERNAME , consumer .username )
126- ngx .ctx .authenticated_consumer = consumer
129+ local set_header = kong .service .request .set_header
130+ local clear_header = kong .service .request .clear_header
131+
132+ if consumer and consumer .id then
133+ set_header (constants .HEADERS .CONSUMER_ID , consumer .id )
134+ else
135+ clear_header (constants .HEADERS .CONSUMER_ID )
136+ end
137+
138+ if consumer and consumer .custom_id then
139+ set_header (constants .HEADERS .CONSUMER_CUSTOM_ID , consumer .custom_id )
140+ else
141+ clear_header (constants .HEADERS .CONSUMER_CUSTOM_ID )
142+ end
143+
144+ if consumer and consumer .username then
145+ set_header (constants .HEADERS .CONSUMER_USERNAME , consumer .username )
146+ else
147+ clear_header (constants .HEADERS .CONSUMER_USERNAME )
148+ end
149+
150+ kong .client .authenticate (consumer , credential )
151+
127152 if credential then
128- ngx_set_header (" x-authenticated-scope" , token .scope )
129- ngx_set_header (" x-authenticated-userid" , token .authenticated_userid )
130- ngx .ctx .authenticated_credential = credential
131- ngx_set_header (constants .HEADERS .ANONYMOUS , nil ) -- in case of auth plugins concatenation
153+ if token .scope then
154+ set_header (" x-authenticated-scope" , token .scope )
155+ else
156+ clear_header (" x-authenticated-scope" )
157+ end
158+
159+ if token .authenticated_userid then
160+ set_header (" x-authenticated-userid" , token .authenticated_userid )
161+ else
162+ clear_header (" x-authenticated-userid" )
163+ end
164+
165+ clear_header (constants .HEADERS .ANONYMOUS ) -- in case of auth plugins concatenation
166+
132167 else
133- ngx_set_header (constants .HEADERS .ANONYMOUS , true )
168+ set_header (constants .HEADERS .ANONYMOUS , true )
169+ clear_header (" x-authenticated-scope" )
170+ clear_header (" x-authenticated-userid" )
134171 end
135172
136173end
0 commit comments