Merge lp:~cjwatson/lazr.restfulclient/oauthlib into lp:lazr.restfulclient

Proposed by Colin Watson
Status: Merged
Merged at revision: 154
Proposed branch: lp:~cjwatson/lazr.restfulclient/oauthlib
Merge into: lp:lazr.restfulclient
Prerequisite: lp:~cjwatson/lazr.restfulclient/tox
Diff against target: 144 lines (+43/-23)
4 files modified
setup.py (+3/-1)
src/lazr/restfulclient/__init__.py (+2/-0)
src/lazr/restfulclient/authorize/oauth.py (+35/-21)
src/lazr/restfulclient/docs/NEWS.rst (+3/-1)
To merge this branch: bzr merge lp:~cjwatson/lazr.restfulclient/oauthlib
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+344716@code.launchpad.net

Commit message

Port from oauth to oauthlib.

Description of the change

Some tests still need to use oauth until lazr.authentication is ported, which is a more difficult job.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'setup.py'
2--- setup.py 2018-04-27 13:50:16 +0000
3+++ setup.py 2018-04-27 13:50:17 +0000
4@@ -38,6 +38,7 @@
5 tests_require = [
6 'lazr.authentication',
7 'lazr.restful>=0.11.0',
8+ 'oauth',
9 'wsgi_intercept',
10 'zope.testrunner',
11 ]
12@@ -60,8 +61,9 @@
13 license='LGPL v3',
14 install_requires=[
15 'httplib2',
16- 'oauth',
17+ 'oauthlib',
18 'setuptools',
19+ 'six',
20 'wadllib>=1.1.4',
21 ],
22 url='https://launchpad.net/lazr.restfulclient',
23
24=== modified file 'src/lazr/restfulclient/__init__.py'
25--- src/lazr/restfulclient/__init__.py 2009-08-06 14:02:10 +0000
26+++ src/lazr/restfulclient/__init__.py 2018-04-27 13:50:17 +0000
27@@ -19,3 +19,5 @@
28 import pkg_resources
29 __version__ = pkg_resources.resource_string(
30 "lazr.restfulclient", "version.txt").strip()
31+if not isinstance(__version__, str):
32+ __version__ = __version__.decode('ASCII')
33
34=== modified file 'src/lazr/restfulclient/authorize/oauth.py'
35--- src/lazr/restfulclient/authorize/oauth.py 2014-07-28 15:35:35 +0000
36+++ src/lazr/restfulclient/authorize/oauth.py 2018-04-27 13:50:17 +0000
37@@ -28,12 +28,9 @@
38 import platform
39 import stat
40 import socket
41-# Work around relative import behavior. The below is equivalent to
42-# from oauth import oauth
43-oauth = __import__('oauth.oauth', {}).oauth
44-(OAuthConsumer, OAuthRequest, OAuthSignatureMethod_PLAINTEXT,
45- OAuthToken) = (oauth.OAuthConsumer, oauth.OAuthRequest,
46- oauth.OAuthSignatureMethod_PLAINTEXT, oauth.OAuthToken)
47+
48+from oauthlib import oauth1
49+import six
50
51 from lazr.restfulclient.authorize import HttpAuthorizer
52 from lazr.restfulclient.errors import CredentialsFileError
53@@ -50,12 +47,9 @@
54 CREDENTIALS_FILE_VERSION = '1'
55
56
57-# These two classes are provided for convenience (so applications
58-# don't need to import from oauth.oauth), and to provide
59-# lazr.restfulclient-specific enhancements like a default argument for
60-# secret (which shouldn't be used), and an application name separate
61-# from the consumer key.
62-class Consumer(oauth.OAuthConsumer):
63+# For compatibility, Consumer and AccessToken are defined using terminology
64+# from the older oauth library rather than the newer oauthlib.
65+class Consumer:
66 """An OAuth consumer (application)."""
67
68 def __init__(self, key, secret='', application_name=None):
69@@ -68,18 +62,29 @@
70 from the consumer key. If present, this will be used in
71 the User-Agent header.
72 """
73- OAuthConsumer.__init__(self, key, secret)
74+ self.key = key
75+ self.secret = secret
76 self.application_name = application_name
77
78
79-class AccessToken(oauth.OAuthToken):
80+class AccessToken:
81 """An OAuth access token."""
82
83 def __init__(self, key, secret='', context=None):
84- OAuthToken.__init__(self, key, secret)
85+ self.key = key
86+ self.secret = secret
87 self.context = context
88
89
90+class TruthyString(six.text_type):
91+ """A Unicode string which is always true."""
92+
93+ def __bool__(self):
94+ return True
95+
96+ __nonzero__ = __bool__
97+
98+
99 class SystemWideConsumer(Consumer):
100 """A consumer associated with the logged-in user rather than an app.
101
102@@ -240,9 +245,18 @@
103
104 def authorizeRequest(self, absolute_uri, method, body, headers):
105 """Sign a request with OAuth credentials."""
106- oauth_request = OAuthRequest.from_consumer_and_token(
107- self.consumer, self.access_token, http_url=absolute_uri)
108- oauth_request.sign_request(
109- OAuthSignatureMethod_PLAINTEXT(),
110- self.consumer, self.access_token)
111- headers.update(oauth_request.to_header(self.oauth_realm))
112+ client = oauth1.Client(
113+ self.consumer.key,
114+ client_secret=self.consumer.secret,
115+ resource_owner_key=TruthyString(self.access_token.key or ''),
116+ resource_owner_secret=self.access_token.secret,
117+ signature_method=oauth1.SIGNATURE_PLAINTEXT,
118+ realm=self.oauth_realm)
119+ # The older oauth library (which may still be used on the server)
120+ # requires the oauth_token parameter to be present and will fail
121+ # authentication if it isn't. This hack forces it to be present
122+ # even if its value is the empty string.
123+ client.resource_owner_key = TruthyString(client.resource_owner_key)
124+ _, signed_headers, _ = client.sign(absolute_uri)
125+ for key, value in signed_headers.items():
126+ headers[key.encode('UTF-8')] = value.encode('UTF-8')
127
128=== modified file 'src/lazr/restfulclient/docs/NEWS.rst'
129--- src/lazr/restfulclient/docs/NEWS.rst 2018-04-27 13:50:16 +0000
130+++ src/lazr/restfulclient/docs/NEWS.rst 2018-04-27 13:50:17 +0000
131@@ -2,10 +2,12 @@
132 NEWS for lazr.restfulclient
133 ===========================
134
135-0.13.6
136+0.14.0
137 ======
138
139 - Switch from buildout to tox.
140+ - Port from oauth to oauthlib. Some tests still need to use oauth until
141+ lazr.authentication is ported. [bug=1672458]
142
143 0.13.5 (2017-09-04)
144 ===================

Subscribers

People subscribed via source and target branches