8000 moved auth stuff from api to auth for clarity, added appropriate test… · boostsup/wp-api-python@88d0678 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88d0678

Browse files
author
derwentx
committed
moved auth stuff from api to auth for clarity, added appropriate test methods, clarify basic_auth option
1 parent e0f84c6 commit 88d0678

File tree

6 files changed

+89
-19
lines changed

6 files changed

+89
-19
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ Options
158158
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
159159
| ``verify_ssl`` | ``bool`` | no | Verify SSL when connect, use this option as ``False`` when need to test with self-signed certificates |
160160
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
161+
| ``basic_auth`` | ``bool`` | no | Force Basic Authentication, can be through query string or headers (default) |
162+
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
161163
| ``query_string_auth`` | ``bool`` | no | Force Basic Authentication as query string when ``True`` and using under HTTPS, default is ``False`` |
162164
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
163165

tests.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,58 @@ def woo_test_mock(*args, **kwargs):
351351
self.assertEqual(response.status_code, 200)
352352
self.assertEqual(response.request.url, 'https://woo.test:8888/wp-json/wp/v2/posts')
353353

354+
class BasicAuthTestcases(unittest.TestCase):
355+
def setUp(self):
356+
self.base_url = "http://localhost:8888/wp-api/"
357+
self.api_name = 'wc-api'
358+
self.api_ver = 'v3'
359+
self.endpoint = 'products/26'
360+
self.signature_method = "HMAC-SHA1"
361+
362+
self.consumer_key = "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
363+
self.consumer_secret = "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
364+
self.api_params = dict(
365+
url=self.base_url,
366+
consumer_key=self.consumer_key,
367+
consumer_secret=self.consumer_secret,
368+
basic_auth=True,
369+
api=self.api_name,
370+
version=self.api_ver
371+
)
372+
373+
def test_endpoint_url(self):
374+
basic_api_params = dict(**self.api_params)
375+
api = API(
376+
**basic_api_params
377+
)
378+
endpoint_url = api.requester.endpoint_url(self.endpoint)
379+
endpoint_url = api.auth.get_oauth_url(endpoint_url, 'GET')
380+
self.assertEqual(
381+
endpoint_url,
382+
UrlUtils.join_components([self.base_url, self.api_name, self.api_ver, self.endpoint])
383+
)
384+
385+
def test_query_string_endpoint_url(self):
386+
query_string_api_params = dict(**self.api_params)
387+
query_string_api_params.update(dict(query_string_auth=True))
388+
api = API(
389+
**query_string_api_params
390+
)
391+
endpoint_url = api.requester.endpoint_url(self.endpoint)
392+
endpoint_url = api.auth.get_oauth_url(endpoint_url, 'GET')
393+
expected_endpoint_url = '%s?consumer_key=%s&consumer_secret=%s' % (self.endpoint, self.consumer_key, self.consumer_secret)
394+
expected_endpoint_url = UrlUtils.join_components([self.base_url, self.api_name, self.api_ver, expected_endpoint_url])
395+
self.assertEqual(
396+
endpoint_url,
397+
expected_endpoint_url
398+
)
399+
endpoint_url = api.requester.endpoint_url(self.endpoint)
400+
endpoint_url = api.auth.get_oauth_url(endpoint_url, 'GET')
401+
402+
354403
class OAuthTestcases(unittest.TestCase):
355404

405+
356406
def setUp(self):
357407
self.base_url = "http://localhost:8888/wordpress/"
358408
self.api_name = 'wc-api'

wordpress/api.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs):
2525
consumer_secret=consumer_secret,
2626
)
2727
if kwargs.get('basic_auth'):
28+
if 'query_string_auth' in kwargs:
29+
auth_kwargs.update(dict(
30+
query_string_auth=kwargs.get("query_string_auth")
31+
))
2832
self.auth = BasicAuth(**auth_kwargs)
2933
else:
3034
auth_kwargs.update(dict(
@@ -84,20 +88,8 @@ def __request(self, method, endpoint, data):
8488
""" Do requests """
8589

8690
endpoint_url = self.requester.endpoint_url(endpoint)
87-
# endpoint_params = UrlUtils.get_query_dict_singular(endpoint_url)
88-
endpoint_params = {}
89-
auth = None
90-
91-
if self.requester.is_ssl or isinstance(self.auth, BasicAuth):
92-
if self.requester.query_string_auth:
93-
endpoint_params.update({
94-
"consumer_key": self.auth.consumer_key,
95-
"consumer_secret": self.auth.consumer_secret
96-
})
97-
else:
98-
auth = (self.auth.consumer_key, self.auth.consumer_secret)
99-
else:
100-
endpoint_url = self.auth.get_oauth_url(endpoint_url, method)
91+
endpoint_url = self.auth.get_oauth_url(endpoint_url, method)
92+
auth = self.auth.get_auth()
10193

10294
if data is not None:
10395
data = jsonencode(data, ensure_ascii=False).encode('utf-8')
@@ -106,7 +98,6 @@ def __request(self, method, endpoint, data):
10698
method=method,
10799
url=endpoint_url,
108100
auth=auth,
109-
params=endpoint_params,
110101
data=data
111102
)
112103

wordpress/auth.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
from random import randint
1111
from hmac import new as HMAC
1212
from hashlib import sha1, sha256
13-
from base64 import b64encode
13+
# from base64 import b64encode
1414
import binascii
15-
import webbrowser
15+
# import webbrowser
1616
import requests
1717
from bs4 import BeautifulSoup
1818

@@ -93,11 +93,37 @@ def flatten_params(cls, params):
9393
params = cls.sorted_params(params)
9494
return "&".join(["%s=%s"%(key, value) for key, value in params])
9595

96+
def get_oauth_url(self, endpoint_url, method):
97+
""" Returns the URL with added Auth params """
98+
return endpoint_url
99+
100+
def get_auth(self):
101+
""" Returns the auth parameter used in requests """
102+
pass
103+
96104
class BasicAuth(Auth):
97105
def __init__(self, requester, consumer_key, consumer_secret, **kwargs):
98106
super(BasicAuth, self).__init__(requester)
99107
self.consumer_key = consumer_key
100108
self.consumer_secret = consumer_secret
109+
self.query_string_auth = kwargs.get("query_string_auth", False)
110+
111+
def get_oauth_url(self, endpoint_url, method):
112+
if self.query_string_auth:
113+
endpoint_params = UrlUtils.get_query_dict_singular(endpoint_url)
114+
endpoint_params.update({
115+
"consumer_key": self.consumer_key,
116+
"consumer_secret": self.consumer_secret
117+
})
118+
endpoint_url = UrlUtils.substitute_query(
119+
endpoint_url,
120+
self.flatten_params(endpoint_params)
121+
)
122+
return endpoint_url
123+
124+
def get_auth(self):
125+
if not self.query_string_auth:
126+
return (self.consumer_key, self.consumer_secret)
101127

102128

103129
class OAuth(Auth):
@@ -164,7 +190,7 @@ def get_params(self):
164190
]
165191

166192
def get_oauth_url(self, endpoint_url, method):
167-
""" Returns the URL with OAuth params """
193+
""" Returns the URL with added Auth params """
168194
params = self.get_params()
169195

170196
return self.add_params_sign(method, endpoint_url, params)

wordpress/helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def is_ssl(cls, url):
144144
def join_components(cls, components):
145145
return reduce(posixpath.join, SeqUtils.filter_true(components))
146146

147+
# TODO: move flatten_params, sorted_params, normalize_params out of auth into here
148+
147149
@staticmethod
148150
def get_value_like_as_php(val):
149151
""" Prepare value for quote """

wordpress/transport.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def __init__(self, url, **kwargs):
3030
self.api_version = kwargs.get("version", __default_api_version__)
3131
self.timeout = kwargs.get("timeout", 5)
3232
self.verify_ssl = kwargs.get("verify_ssl", True)
33-
self.query_string_auth = kwargs.get("query_string_auth", False)
3433
self.session = Session()
3534

3635
@property

0 commit comments

Comments
 (0)
0