@@ -37,17 +37,15 @@ def __call__(self, environ, start_response):
37
37
)
38
38
signed = Signed .from_headers (self ._extract_headers (environ ))
39
39
authenticator = LocalAuthenticator (signable , signed , logger )
40
- is_authentic , status , message = authenticator .is_authentic ()
40
+ is_authentic , code , message = authenticator .is_authentic ()
41
41
42
42
if is_authentic :
43
43
environ [ENV_APP_UUID ] = signed .app_uuid
44
44
environ [ENV_AUTHENTIC ] = True
45
45
environ [ENV_PROTOCOL_VERSION ] = signed .protocol_version ()
46
46
return self .app (environ , start_response )
47
47
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 )
51
49
52
50
def _validate_configs (self ):
53
51
# Validate the client settings (APP_UUID, PRIVATE_KEY)
@@ -135,3 +133,21 @@ def _extract_url(self, environ):
135
133
url_parts .append (f"?{ quote (qs , safe = self .SAFE_CHARS )} " )
136
134
137
135
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