@@ -1073,47 +1073,17 @@ def BEGIN(self):
10731073 self .authenticated = False
10741074 self .sspcontext = None
10751075
1076- @ATMT .condition (BEGIN , prio = 0 )
1077- def should_authenticate (self ):
1076+ @ATMT .receive_condition (BEGIN , prio = 1 )
1077+ def should_authenticate (self , pkt ):
10781078 if self .authmethod == HTTP_AUTH_MECHS .NONE .value :
1079- raise self .SERVE ()
1079+ raise self .SERVE (pkt )
10801080 else :
1081- raise self .AUTH ()
1081+ raise self .AUTH (pkt )
10821082
10831083 @ATMT .state ()
1084- def AUTH (self ):
1085- pass
1086-
1087- @ATMT .state ()
1088- def AUTH_ERROR (self , proxy ):
1089- self .sspcontext = None
1090- self ._ask_authorization (proxy , self .authmethod )
1091- self .vprint ("AUTH ERROR" )
1092-
1093- @ATMT .condition (AUTH_ERROR )
1094- def allow_reauth (self ):
1095- raise self .AUTH ()
1096-
1097- def _ask_authorization (self , proxy , data ):
1098- if proxy :
1099- self .send (
1100- HTTPResponse (
1101- Status_Code = b"407" ,
1102- Reason_Phrase = b"Proxy Authentication Required" ,
1103- Proxy_Authenticate = data ,
1104- )
1105- )
1106- else :
1107- self .send (
1108- HTTPResponse (
1109- Status_Code = b"401" ,
1110- Reason_Phrase = b"Unauthorized" ,
1111- WWW_Authenticate = data ,
1112- )
1113- )
1114-
1115- @ATMT .receive_condition (AUTH , prio = 1 )
1116- def received_unauthenticated (self , pkt ):
1084+ def AUTH (self , pkt = None ):
1085+ if pkt is None :
1086+ return
11171087 if HTTPRequest in pkt :
11181088 self .vprint (pkt .summary ())
11191089 if pkt .Method == b"CONNECT" :
@@ -1137,10 +1107,12 @@ def received_unauthenticated(self, pkt):
11371107 # Parse authorization
11381108 method , data = authorization .split (b" " , 1 )
11391109 if plain_str (method ) != self .authmethod :
1110+ self .debug (3 , "Bad auth method." )
11401111 raise self .AUTH_ERROR (proxy )
11411112 try :
11421113 data = base64 .b64decode (data )
11431114 except Exception :
1115+ self .debug (3 , "Couldn't unpack base64 of auth." )
11441116 raise self .AUTH_ERROR (proxy )
11451117 # Now process the authorization
11461118 if not self .basic :
@@ -1149,6 +1121,7 @@ def received_unauthenticated(self, pkt):
11491121 except Exception :
11501122 self .sspcontext = None
11511123 self ._ask_authorization (proxy , self .authmethod )
1124+ self .debug (3 , "Couldn't unpack GSSAPI_BLOB of auth." )
11521125 raise self .AUTH_ERROR (proxy )
11531126 # And call the SSP
11541127 self .sspcontext , tok , status = self .ssp .GSS_Accept_sec_context (
@@ -1164,9 +1137,11 @@ def received_unauthenticated(self, pkt):
11641137 )
11651138 tok , status = None , GSS_S_COMPLETE
11661139 except StopIteration :
1140+ self .debug (3 , "Basic authentication failed with 'unknown user'." )
11671141 tok , status = None , GSS_S_FAILURE
11681142 # Send answer
11691143 if status not in [GSS_S_COMPLETE , GSS_S_CONTINUE_NEEDED ]:
1144+ self .debug (3 , "Authentication failed." )
11701145 raise self .AUTH_ERROR (proxy )
11711146 elif status == GSS_S_CONTINUE_NEEDED :
11721147 data = self .authmethod .encode ()
@@ -1180,6 +1155,38 @@ def received_unauthenticated(self, pkt):
11801155 self .vprint ("AUTH OK" )
11811156 raise self .SERVE (pkt )
11821157
1158+ @ATMT .state ()
1159+ def AUTH_ERROR (self , proxy ):
1160+ self .sspcontext = None
1161+ self ._ask_authorization (proxy , self .authmethod )
1162+ self .vprint ("AUTH ERROR" )
1163+
1164+ @ATMT .condition (AUTH_ERROR )
1165+ def allow_reauth (self ):
1166+ raise self .AUTH ()
1167+
1168+ def _ask_authorization (self , proxy , data ):
1169+ if proxy :
1170+ self .send (
1171+ HTTPResponse (
1172+ Status_Code = b"407" ,
1173+ Reason_Phrase = b"Proxy Authentication Required" ,
1174+ Proxy_Authenticate = data ,
1175+ )
1176+ )
1177+ else :
1178+ self .send (
1179+ HTTPResponse (
1180+ Status_Code = b"401" ,
1181+ Reason_Phrase = b"Unauthorized" ,
1182+ WWW_Authenticate = data ,
1183+ )
1184+ )
1185+
1186+ @ATMT .receive_condition (AUTH , prio = 1 )
1187+ def received_unauthenticated (self , pkt ):
1188+ raise self .AUTH (pkt )
1189+
11831190 @ATMT .eof (AUTH )
11841191 def auth_eof (self ):
11851192 raise self .CLOSED ()
0 commit comments