189189
190190import cgi
191191import copy
192+ import logging
192193from urlparse import urlparse , urldefrag
193194
194195from openid import fetchers
@@ -662,7 +663,7 @@ def _checkReturnTo(self, message, return_to):
662663 try :
663664 self ._verifyReturnToArgs (message .toPostArgs ())
664665 except ProtocolError , why :
665- oidutil . log ("Verifying return_to arguments: %s" % (why [0 ],))
666+ logging . exception ("Verifying return_to arguments: %s" % (why [0 ],))
666667 return False
667668
668669 # Check the return_to base URL against the one in the message.
@@ -728,7 +729,7 @@ def _doIdRes(self, message, endpoint, return_to):
728729
729730 # Verify discovery information:
730731 endpoint = self ._verifyDiscoveryResults (message , endpoint )
731- oidutil . log ("Received id_res response from %s using association %s" %
732+ logging . info ("Received id_res response from %s using association %s" %
732733 (endpoint .server_url ,
733734 message .getArg (OPENID_NS , 'assoc_handle' )))
734735
@@ -922,7 +923,7 @@ def _verifyDiscoveryResultsOpenID2(self, resp_msg, endpoint):
922923 # endpoints and responses that didn't match the original
923924 # request.
924925 if not endpoint :
925- oidutil . log ('No pre-discovered information supplied.' )
926+ logging . info ('No pre-discovered information supplied.' )
926927 endpoint = self ._discoverAndVerify (to_match .claimed_id , [to_match ])
927928 else :
928929 # The claimed ID matches, so we use the endpoint that we
@@ -931,10 +932,10 @@ def _verifyDiscoveryResultsOpenID2(self, resp_msg, endpoint):
931932 try :
932933 self ._verifyDiscoverySingle (endpoint , to_match )
933934 except ProtocolError , e :
934- oidutil . log (
935+ logging . exception (
935936 "Error attempting to use stored discovery information: " +
936937 str (e ))
937- oidutil . log ("Attempting discovery to verify endpoint" )
938+ logging . info ("Attempting discovery to verify endpoint" )
938939 endpoint = self ._discoverAndVerify (
939940 to_match .claimed_id , [to_match ])
940941
@@ -976,9 +977,9 @@ def _verifyDiscoveryResultsOpenID1(self, resp_msg, endpoint):
976977 except TypeURIMismatch :
977978 self ._verifyDiscoverySingle (endpoint , to_match_1_0 )
978979 except ProtocolError , e :
979- oidutil . log ("Error attempting to use stored discovery information: " +
980+ logging . exception ("Error attempting to use stored discovery information: " +
980981 str (e ))
981- oidutil . log ("Attempting discovery to verify endpoint" )
982+ logging . info ("Attempting discovery to verify endpoint" )
982983 else :
983984 return endpoint
984985
@@ -1048,7 +1049,7 @@ def _discoverAndVerify(self, claimed_id, to_match_endpoints):
10481049
10491050 @raises DiscoveryFailure: when discovery fails.
10501051 """
1051- oidutil . log ('Performing discovery on %s' % (claimed_id ,))
1052+ logging . info ('Performing discovery on %s' % (claimed_id ,))
10521053 _ , services = self ._discover (claimed_id )
10531054 if not services :
10541055 raise DiscoveryFailure ('No OpenID information found at %s' %
@@ -1075,10 +1076,10 @@ def _verifyDiscoveredServices(self, claimed_id, services, to_match_endpoints):
10751076 # succeeded. Return this endpoint.
10761077 return endpoint
10771078 else :
1078- oidutil . log ('Discovery verification failure for %s' %
1079+ logging . error ('Discovery verification failure for %s' %
10791080 (claimed_id ,))
10801081 for failure_message in failure_messages :
1081- oidutil . log (' * Endpoint mismatch: ' + failure_message )
1082+ logging . error (' * Endpoint mismatch: ' + failure_message )
10821083
10831084 raise DiscoveryFailure (
10841085 'No matching endpoint found after discovering %s'
@@ -1090,14 +1091,14 @@ def _checkAuth(self, message, server_url):
10901091 @returns: True if the request is valid.
10911092 @rtype: bool
10921093 """
1093- oidutil . log ('Using OpenID check_authentication' )
1094+ logging . info ('Using OpenID check_authentication' )
10941095 request = self ._createCheckAuthRequest (message )
10951096 if request is None :
10961097 return False
10971098 try :
10981099 response = self ._makeKVPost (request , server_url )
10991100 except (fetchers .HTTPFetchingError , ServerError ), e :
1100- oidutil . log ('check_authentication failed: %s' % (e [0 ],))
1101+ logging . exception ('check_authentication failed: %s' % (e [0 ],))
11011102 return False
11021103 else :
11031104 return self ._processCheckAuthResponse (response , server_url )
@@ -1109,12 +1110,12 @@ def _createCheckAuthRequest(self, message):
11091110 signed = message .getArg (OPENID_NS , 'signed' )
11101111 if signed :
11111112 for k in signed .split (',' ):
1112- oidutil . log (k )
1113+ logging . info (k )
11131114 val = message .getAliasedArg (k )
11141115
11151116 # Signed value is missing
11161117 if val is None :
1117- oidutil . log ('Missing signed field %r' % (k ,))
1118+ logging . info ('Missing signed field %r' % (k ,))
11181119 return None
11191120
11201121 check_auth_message = message .copy ()
@@ -1129,18 +1130,18 @@ def _processCheckAuthResponse(self, response, server_url):
11291130
11301131 invalidate_handle = response .getArg (OPENID_NS , 'invalidate_handle' )
11311132 if invalidate_handle is not None :
1132- oidutil . log (
1133+ logging . info (
11331134 'Received "invalidate_handle" from server %s' % (server_url ,))
11341135 if self .store is None :
1135- oidutil . log ('Unexpectedly got invalidate_handle without '
1136+ logging . error ('Unexpectedly got invalidate_handle without '
11361137 'a store!' )
11371138 else :
11381139 self .store .removeAssociation (server_url , invalidate_handle )
11391140
11401141 if is_valid == 'true' :
11411142 return True
11421143 else :
1143- oidutil . log ('Server responds that checkAuth call is not valid' )
1144+ logging . error ('Server responds that checkAuth call is not valid' )
11441145 return False
11451146
11461147 def _getAssociation (self , endpoint ):
@@ -1193,7 +1194,7 @@ def _negotiateAssociation(self, endpoint):
11931194 except ServerError , why :
11941195 # Do not keep trying, since it rejected the
11951196 # association type that it told us to use.
1196- oidutil . log ('Server %s refused its suggested association '
1197+ logging . error ('Server %s refused its suggested association '
11971198 'type: session_type=%s, assoc_type=%s'
11981199 % (endpoint .server_url , session_type ,
11991200 assoc_type ))
@@ -1216,15 +1217,15 @@ def _extractSupportedAssociationType(self, server_error, endpoint,
12161217 # should be considered a total failure.
12171218 if server_error .error_code != 'unsupported-type' or \
12181219 server_error .message .isOpenID1 ():
1219- oidutil . log (
1220+ logging . error (
12201221 'Server error when requesting an association from %r: %s'
12211222 % (endpoint .server_url , server_error .error_text ))
12221223 return None
12231224
12241225 # The server didn't like the association/session type
12251226 # that we sent, and it sent us back a message that
12261227 # might tell us how to handle it.
1227- oidutil . log (
1228+ logging . error (
12281229 'Unsupported association type %s: %s' % (assoc_type ,
12291230 server_error .error_text ,))
12301231
@@ -1234,13 +1235,13 @@ def _extractSupportedAssociationType(self, server_error, endpoint,
12341235 session_type = server_error .message .getArg (OPENID_NS , 'session_type' )
12351236
12361237 if assoc_type is None or session_type is None :
1237- oidutil . log ('Server responded with unsupported association '
1238+ logging . error ('Server responded with unsupported association '
12381239 'session but did not supply a fallback.' )
12391240 return None
12401241 elif not self .negotiator .isAllowed (assoc_type , session_type ):
12411242 fmt = ('Server sent unsupported session/association type: '
12421243 'session_type=%s, assoc_type=%s' )
1243- oidutil . log (fmt % (session_type , assoc_type ))
1244+ logging . error (fmt % (session_type , assoc_type ))
12441245 return None
12451246 else :
12461247 return assoc_type , session_type
@@ -1261,17 +1262,17 @@ def _requestAssociation(self, endpoint, assoc_type, session_type):
12611262 try :
12621263 response = self ._makeKVPost (args , endpoint .server_url )
12631264 except fetchers .HTTPFetchingError , why :
1264- oidutil . log ('openid.associate request failed: %s' % (why [0 ],))
1265+ logging . exception ('openid.associate request failed: %s' % (why [0 ],))
12651266 return None
12661267
12671268 try :
12681269 assoc = self ._extractAssociation (response , assoc_session )
12691270 except KeyError , why :
1270- oidutil . log ('Missing required parameter in response from %s: %s'
1271+ logging . exception ('Missing required parameter in response from %s: %s'
12711272 % (endpoint .server_url , why [0 ]))
12721273 return None
12731274 except ProtocolError , why :
1274- oidutil . log ('Protocol error parsing response from %s: %s' % (
1275+ logging . exception ('Protocol error parsing response from %s: %s' % (
12751276 endpoint .server_url , why [0 ]))
12761277 return None
12771278 else :
@@ -1348,7 +1349,7 @@ def _getOpenID1SessionType(self, assoc_response):
13481349 # OpenID 1, but we'll accept it anyway, while issuing a
13491350 # warning.
13501351 if session_type == 'no-encryption' :
1351- oidutil . log ( 'WARNING: OpenID server sent "no-encryption"'
1352+ logging . warn ( ' OpenID server sent "no-encryption"'
13521353 'for OpenID 1.X' )
13531354
13541355 # Missing or empty session type is the way to flag a
@@ -1599,7 +1600,7 @@ def getMessage(self, realm, return_to=None, immediate=False):
15991600 else :
16001601 assoc_log_msg = 'using stateless mode.'
16011602
1602- oidutil . log ("Generated %s request to %s %s" %
1603+ logging . info ("Generated %s request to %s %s" %
16031604 (mode , self .endpoint .server_url , assoc_log_msg ))
16041605
16051606 return message
@@ -1779,7 +1780,7 @@ def getSignedNS(self, ns_uri):
17791780
17801781 for key in msg_args .iterkeys ():
17811782 if not self .isSigned (ns_uri , key ):
1782- oidutil . log ("SuccessResponse.getSignedNS: (%s, %s) not signed."
1783+ logging . info ("SuccessResponse.getSignedNS: (%s, %s) not signed."
17831784 % (ns_uri , key ))
17841785 return None
17851786
0 commit comments