@@ -148,12 +148,12 @@ def anonymous(self):
148
148
149
149
return None
150
150
151
- def authenticate (self , request , rm ):
151
+ def authenticate (self , request , rm ): # noqa: C901
152
152
actor , anonymous , error = False , True , ""
153
153
# workaround for django header sillyness
154
154
if "HTTP_AUTHORIZATION" in request .META :
155
155
request .META ["AUTHORIZATION" ] = request .META ["HTTP_AUTHORIZATION" ]
156
-
156
+ logger . info ( "ev=dda_resource method=authenticate state=begin" )
157
157
# first we're going to try any authenticators that might match header hints. then, we'll try
158
158
# any catch-all registered under None as a hint
159
159
potential_authenticators = []
@@ -167,6 +167,7 @@ def authenticate(self, request, rm):
167
167
potential_authenticators .extend (authenticators )
168
168
continue
169
169
except KeyError :
170
+ logger .exception ("ev=dda_resource method=authenticate state=KeyError" )
170
171
pass
171
172
172
173
try :
@@ -175,24 +176,29 @@ def authenticate(self, request, rm):
175
176
except KeyError :
176
177
pass
177
178
178
- if len (potential_authenticators ) <= 0 :
179
- actor , anonymous = _no_authenticators_found , CHALLENGE
180
- else :
181
- for authenticator in potential_authenticators :
182
- authentication_result = authenticator .is_authenticated (request )
179
+ try :
180
+ if len (potential_authenticators ) <= 0 :
181
+ actor , anonymous = _no_authenticators_found , CHALLENGE
182
+ else :
183
+ for authenticator in potential_authenticators :
184
+ authentication_result = authenticator .is_authenticated (request )
183
185
184
- if not authentication_result :
185
- error = authentication_result
186
- if self .anonymous and rm in self .anonymous .allowed_methods :
186
+ if not authentication_result :
187
+ error = authentication_result
188
+ if self .anonymous and rm in self .anonymous .allowed_methods :
187
189
188
- actor , anonymous = self .anonymous (), True
190
+ actor , anonymous = self .anonymous (), True
191
+ else :
192
+ actor , anonymous = authenticator .challenge , CHALLENGE
189
193
else :
190
- actor , anonymous = authenticator .challenge , CHALLENGE
191
- else :
192
- return self .handler , False , error
194
+ return self .handler , False , error
193
195
194
- # XXX: this might be a little weird as it'll contain information about the last executed authenticator
195
- return actor , anonymous , error
196
+ # XXX: this might be a little weird as it'll contain information about the last executed authenticator
197
+ return actor , anonymous , error
198
+ except Exception :
199
+ logger .exception (
200
+ "ev=dda_resource method=authenticate state=authentication_exception"
201
+ )
196
202
197
203
# TODO: make this method less complex and remove the `noqa`
198
204
@vary_on_headers ("Authorization" ) # noqa: C901
@@ -202,6 +208,9 @@ def __call__(self, request, *args, **kwargs): # noqa: C901
202
208
that are different (OAuth stuff in `Authorization` header.)
203
209
"""
204
210
rm = request .method .upper ()
211
+ logger .info (
212
+ f'ev=dda_resource method=__call__ content_type="{ request .headers .get ("content-type" )} " body="{ request .body } "' # noqa: E501
213
+ )
205
214
206
215
# Django's internal mechanism doesn't pick up
207
216
# PUT request, so we trick it a little here.
@@ -258,12 +267,18 @@ def __call__(self, request, *args, **kwargs): # noqa: C901
258
267
_ = request .POST if request .method == "POST" else request .GET
259
268
status_code , result = meth (request , * args , ** kwargs )
260
269
except Exception as e :
270
+ logger .exception (
271
+ "ev=dda_resource method=__call__ state=exception_during_endpoint_processing"
272
+ )
261
273
status_code = http .client .BAD_REQUEST
262
274
result = self .error_handler (e , request , meth , em_format )
263
275
264
276
try :
265
277
emitter , ct = Emitter .get (em_format )
266
278
except ValueError : # pragma: nocover
279
+ logger .error (
280
+ "ev=dda_resource method=__call__ state=bad_emitter emitter={emitter}"
281
+ )
267
282
result = rc .BAD_REQUEST
268
283
result .content = "Invalid output format specified '%s'." % em_format
269
284
return result
0 commit comments