Skip to content

Commit 13b84c0

Browse files
committed
Added JWT auth support
1 parent 7d85e70 commit 13b84c0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

wordpress/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import logging
1111
from json import dumps as jsonencode
1212

13-
from wordpress.auth import BasicAuth, OAuth, OAuth_3Leg
13+
from wordpress.auth import BasicAuth, OAuth, OAuth_3Leg, JWTAuth
1414
from wordpress.helpers import StrUtils, UrlUtils
1515
from wordpress.transport import API_Requests_Wrapper
1616

@@ -34,6 +34,8 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs):
3434
auth_class = BasicAuth
3535
elif kwargs.get('oauth1a_3leg'):
3636
auth_class = OAuth_3Leg
37+
elif kwargs.get('jwt_auth'):
38+
auth_class = JWTAuth
3739

3840
if kwargs.get('version', '').startswith('wc') and kwargs.get('oauth1a_3leg'):
3941
self.logger.warn("WooCommerce JSON Api does not seem to support 3leg")

wordpress/auth.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ def get_auth(self):
9494
return HTTPBasicAuth(self.consumer_key, self.consumer_secret)
9595

9696

97+
class JWTAuth(Auth):
98+
"""
99+
Just injects an already generated and signed JWT token header
100+
in the request headers.
101+
It is up to the user to make sure this is sent via SSL.
102+
"""
103+
def __init__(self, requester, consumer_key, consumer_secret, **kwargs):
104+
super(JWTAuth, self).__init__(requester, **kwargs)
105+
self.consumer_key = consumer_key
106+
self.consumer_secret = consumer_secret
107+
self.jwt_token = kwargs.pop('jwt_token', None)
108+
self.jwt_header_format = kwargs.pop('jwt_header_format',
109+
u'JWT token="%s"')
110+
111+
def get_auth_url(self, endpoint_url, method, **kwargs):
112+
return endpoint_url
113+
114+
def __call__(self, request):
115+
"""
116+
Adds an `Authorization` header to the request.
117+
"""
118+
request.headers['Authorization'] = self._header_format % self.jwt_token
119+
return request
120+
121+
97122
class OAuth(Auth):
98123
""" Signs string with oauth consumer_key and consumer_secret """
99124
oauth_version = '1.0'

0 commit comments

Comments
 (0)