@@ -66,6 +66,7 @@ local function retrieve_credentials(header_name, conf)
66
66
return username , password
67
67
end
68
68
69
+
69
70
--- Validate a credential in the Authorization header against one fetched from the database.
70
71
-- @param credential The retrieved credential from the username passed in the request
71
72
-- @param given_password The password as given in the Authorization header
@@ -79,6 +80,7 @@ local function validate_credentials(credential, given_password)
79
80
return credential .password == digest
80
81
end
81
82
83
+
82
84
local function load_credential_into_memory (username )
83
85
local credential , err = kong .db .basicauth_credentials :select_by_username (username )
84
86
if err then
@@ -87,6 +89,7 @@ local function load_credential_into_memory(username)
87
89
return credential
88
90
end
89
91
92
+
90
93
local function load_credential_from_db (username )
91
94
if not username then
92
95
return
@@ -104,7 +107,10 @@ local function load_credential_from_db(username)
104
107
return credential
105
108
end
106
109
110
+
107
111
local function set_consumer (consumer , credential )
112
+ kong .client .authenticate (consumer , credential )
113
+
108
114
local set_header = kong .service .request .set_header
109
115
local clear_header = kong .service .request .clear_header
110
116
@@ -126,23 +132,22 @@ local function set_consumer(consumer, credential)
126
132
clear_header (constants .HEADERS .CONSUMER_USERNAME )
127
133
end
128
134
129
- kong .client .authenticate (consumer , credential )
135
+ if credential and credential .username then
136
+ set_header (constants .HEADERS .CREDENTIAL_IDENTIFIER , credential .username )
137
+ set_header (constants .HEADERS .CREDENTIAL_USERNAME , credential .username )
138
+ else
139
+ clear_header (constants .HEADERS .CREDENTIAL_IDENTIFIER )
140
+ clear_header (constants .HEADERS .CREDENTIAL_USERNAME )
141
+ end
130
142
131
143
if credential then
132
- if credential .username then
133
- set_header (constants .HEADERS .CREDENTIAL_USERNAME , credential .username )
134
- else
135
- clear_header (constants .HEADERS .CREDENTIAL_USERNAME )
136
- end
137
-
138
144
clear_header (constants .HEADERS .ANONYMOUS )
139
-
140
145
else
141
- clear_header (constants .HEADERS .CREDENTIAL_USERNAME )
142
146
set_header (constants .HEADERS .ANONYMOUS , true )
143
147
end
144
148
end
145
149
150
+
146
151
local function do_authentication (conf )
147
152
-- If both headers are missing, return 401
148
153
if not (kong .request .get_header (" authorization" ) or kong .request .get_header (" proxy-authorization" )) then
@@ -156,18 +161,18 @@ local function do_authentication(conf)
156
161
end
157
162
158
163
local credential
159
- local given_username , given_password = retrieve_credentials (" proxy-authorization" , conf )
160
- if given_username then
161
- credential = load_credential_from_db (given_username )
164
+ local username , password = retrieve_credentials (" proxy-authorization" , conf )
165
+ if username then
166
+ credential = load_credential_from_db (username )
162
167
end
163
168
164
169
-- Try with the authorization header
165
170
if not credential then
166
- given_username , given_password = retrieve_credentials (" authorization" , conf )
167
- credential = load_credential_from_db (given_username )
171
+ username , password = retrieve_credentials (" authorization" , conf )
172
+ credential = load_credential_from_db (username )
168
173
end
169
174
170
- if not credential or not validate_credentials (credential , given_password ) then
175
+ if not credential or not validate_credentials (credential , password ) then
171
176
return false , { status = 401 , message = " Invalid authentication credentials" }
172
177
end
173
178
@@ -207,7 +212,7 @@ function _M.execute(conf)
207
212
return kong .response .exit (500 , { message = " An unexpected error occurred" })
208
213
end
209
214
210
- set_consumer (consumer , nil )
215
+ set_consumer (consumer )
211
216
212
217
else
213
218
return kong .response .exit (err .status , { message = err .message }, err .headers )
0 commit comments