Skip to content

Commit 7d65da5

Browse files
committed
Merge branch 'cdman/use-logging-module'
fixes openid#42
2 parents fab30b0 + 95aa2a9 commit 7d65da5

12 files changed

Lines changed: 98 additions & 102 deletions

File tree

openid/consumer/consumer.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189

190190
import cgi
191191
import copy
192+
import logging
192193
from urlparse import urlparse, urldefrag
193194

194195
from 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

openid/consumer/discover.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
]
1515

1616
import urlparse
17+
import logging
1718

18-
from openid import oidutil, fetchers, urinorm
19+
from openid import fetchers, urinorm
1920

2021
from openid import yadis
2122
from openid.yadis.etxrd import nsTag, XRDSError, XRD_NS_2_0
@@ -425,7 +426,7 @@ def discoverXRI(iname):
425426
for service_element in services:
426427
endpoints.extend(flt.getServiceEndpoints(iname, service_element))
427428
except XRDSError:
428-
oidutil.log('xrds error on ' + iname)
429+
logging.exception('xrds error on ' + iname)
429430

430431
for endpoint in endpoints:
431432
# Is there a way to pass this through the filter to the endpoint

openid/extensions/sreg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from openid.message import registerNamespaceAlias, \
3939
NamespaceAliasRegistrationError
4040
from openid.extension import Extension
41-
from openid import oidutil
41+
import logging
4242

4343
try:
4444
basestring #pylint:disable-msg=W0104
@@ -95,7 +95,7 @@ def checkFieldName(field_name):
9595
try:
9696
registerNamespaceAlias(ns_uri_1_1, 'sreg')
9797
except NamespaceAliasRegistrationError, e:
98-
oidutil.log('registerNamespaceAlias(%r, %r) failed: %s' % (ns_uri_1_1,
98+
logging.exception('registerNamespaceAlias(%r, %r) failed: %s' % (ns_uri_1_1,
9999
'sreg', str(e),))
100100

101101
def supportsSReg(endpoint):

openid/kvform.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
__all__ = ['seqToKV', 'kvToSeq', 'dictToKV', 'kvToDict']
22

3-
from openid import oidutil
4-
53
import types
4+
import logging
65

76
class KVFormError(ValueError):
87
pass
@@ -22,7 +21,7 @@ def err(msg):
2221
if strict:
2322
raise KVFormError(formatted)
2423
else:
25-
oidutil.log(formatted)
24+
logging.warn(formatted)
2625

2726
lines = []
2827
for k, v in seq:
@@ -73,7 +72,7 @@ def err(msg):
7372
if strict:
7473
raise KVFormError(formatted)
7574
else:
76-
oidutil.log(formatted)
75+
logging.warn(formatted)
7776

7877
lines = data.split('\n')
7978
if lines[-1]:

openid/oidutil.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import binascii
1111
import sys
1212
import urlparse
13+
import logging
1314

1415
from urllib import urlencode
1516

@@ -77,9 +78,8 @@ def importElementTree(module_names=None):
7778
except (SystemExit, MemoryError, AssertionError):
7879
raise
7980
except:
80-
why = sys.exc_info()[1]
81-
log('Not using ElementTree library %r because it failed to '
82-
'parse a trivial document: %s' % (mod_name, why))
81+
logging.exception('Not using ElementTree library %r because it failed to '
82+
'parse a trivial document: %s' % mod_name)
8383
else:
8484
return ElementTree
8585
else:
@@ -91,20 +91,8 @@ def importElementTree(module_names=None):
9191
def log(message, level=0):
9292
"""Handle a log message from the OpenID library.
9393
94-
This implementation writes the string it to C{sys.stderr},
95-
followed by a newline.
96-
97-
Currently, the library does not use the second parameter to this
98-
function, but that may change in the future.
99-
100-
To install your own logging hook::
101-
102-
from openid import oidutil
103-
104-
def myLoggingFunction(message, level):
105-
...
106-
107-
oidutil.log = myLoggingFunction
94+
This is a legacy function which redirects to logging.error.
95+
The logging module should be used instead of this
10896
10997
@param message: A string containing a debugging message from the
11098
OpenID library
@@ -118,8 +106,8 @@ def myLoggingFunction(message, level):
118106
@returns: Nothing.
119107
"""
120108

121-
sys.stderr.write(message)
122-
sys.stderr.write('\n')
109+
logging.error("This is a legacy log message, please use the "
110+
"logging module. Message: %s", message)
123111

124112
def appendArgs(url, args):
125113
"""Append query arguments to a HTTP(s) URL. If the URL already has

openid/server/server.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"""
118118

119119
import time, warnings
120+
import logging
120121
from copy import deepcopy
121122

122123
from openid import cryptutil
@@ -420,7 +421,7 @@ def fromMessage(klass, message, op_endpoint=UNUSED):
420421
if message.isOpenID1():
421422
session_type = message.getArg(OPENID_NS, 'session_type')
422423
if session_type == 'no-encryption':
423-
oidutil.log('Received OpenID 1 request with a no-encryption '
424+
logging.warn('Received OpenID 1 request with a no-encryption '
424425
'assocaition session type. Continuing anyway.')
425426
elif not session_type:
426427
session_type = 'no-encryption'
@@ -1170,15 +1171,15 @@ def verify(self, assoc_handle, message):
11701171
"""
11711172
assoc = self.getAssociation(assoc_handle, dumb=True)
11721173
if not assoc:
1173-
oidutil.log("failed to get assoc with handle %r to verify "
1174+
logging.error("failed to get assoc with handle %r to verify "
11741175
"message %r"
11751176
% (assoc_handle, message))
11761177
return False
11771178

11781179
try:
11791180
valid = assoc.checkMessageSignature(message)
11801181
except ValueError, ex:
1181-
oidutil.log("Error in verifying %s with %s: %s" % (message,
1182+
logging.exception("Error in verifying %s with %s: %s" % (message,
11821183
assoc,
11831184
ex))
11841185
return False
@@ -1285,7 +1286,7 @@ def getAssociation(self, assoc_handle, dumb, checkExpiration=True):
12851286
key = self._normal_key
12861287
assoc = self.store.getAssociation(key, assoc_handle)
12871288
if assoc is not None and assoc.expiresIn <= 0:
1288-
oidutil.log("requested %sdumb key %r is expired (by %s seconds)" %
1289+
logging.info("requested %sdumb key %r is expired (by %s seconds)" %
12891290
((not dumb) and 'not-' or '',
12901291
assoc_handle, assoc.expiresIn))
12911292
if checkExpiration:

0 commit comments

Comments
 (0)