@@ -37,17 +37,15 @@ def __call__(self, environ, start_response):
3737 )
3838 signed = Signed .from_headers (self ._extract_headers (environ ))
3939 authenticator = LocalAuthenticator (signable , signed , logger )
40- is_authentic , status , message = authenticator .is_authentic ()
40+ is_authentic , code , message = authenticator .is_authentic ()
4141
4242 if is_authentic :
4343 environ [ENV_APP_UUID ] = signed .app_uuid
4444 environ [ENV_AUTHENTIC ] = True
4545 environ [ENV_PROTOCOL_VERSION ] = signed .protocol_version ()
4646 return self .app (environ , start_response )
4747
48- start_response (status , [("content-type" , "application/json" )])
49- body = {"errors" : {"mauth" : [message ]}}
50- return [json .dumps (body ).encode ("utf-8" )]
48+ return self ._send_response (code , message , start_response )
5149
5250 def _validate_configs (self ):
5351 # Validate the client settings (APP_UUID, PRIVATE_KEY)
@@ -135,3 +133,21 @@ def _extract_url(self, environ):
135133 url_parts .append (f"?{ quote (qs , safe = self .SAFE_CHARS )} " )
136134
137135 return "" .join (url_parts )
136+
137+ _STATUS_STRS = {
138+ 401 : "401 Unauthorized" ,
139+ 500 : "500 Internal Server Error" ,
140+ }
141+
142+ def _send_response (self , code , msg , start_response ):
143+ status = self ._STATUS_STRS [code ]
144+ body = {"errors" : {"mauth" : [msg ]}}
145+ body_bytes = json .dumps (body ).encode ("utf-8" )
146+
147+ headers = [
148+ ("Content-Type" , "application/json" ),
149+ ("Content-Length" , str (len (body_bytes ))),
150+ ]
151+ start_response (status , headers )
152+
153+ return [body_bytes ]
0 commit comments