Skip to content

Commit 9862e8c

Browse files
committed
Merge branch 'adapt-to-pyjwt2' into dev
2 parents 17af762 + e110a63 commit 9862e8c

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

oauth2cli/assertion.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99

1010
logger = logging.getLogger(__name__)
1111

12+
13+
def _str2bytes(raw):
14+
# A conversion based on duck-typing rather than six.text_type
15+
try: # Assuming it is a string
16+
return raw.encode(encoding="utf-8")
17+
except: # Otherwise we treat it as bytes and return it as-is
18+
return raw
19+
20+
1221
class AssertionCreator(object):
1322
def create_normal_assertion(
1423
self, audience, issuer, subject, expires_at=None, expires_in=600,
@@ -103,8 +112,9 @@ def create_normal_assertion(
103112
payload['nbf'] = not_before
104113
payload.update(additional_claims or {})
105114
try:
106-
return jwt.encode(
115+
str_or_bytes = jwt.encode( # PyJWT 1 returns bytes, PyJWT 2 returns str
107116
payload, self.key, algorithm=self.algorithm, headers=self.headers)
117+
return _str2bytes(str_or_bytes) # We normalize them into bytes
108118
except:
109119
if self.algorithm.startswith("RS") or self.algorithm.starswith("ES"):
110120
logger.exception(

oauth2cli/oauth2.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def __init__(
9999
client_secret (str): Triggers HTTP AUTH for Confidential Client
100100
client_assertion (bytes, callable):
101101
The client assertion to authenticate this client, per RFC 7521.
102-
It can be a raw SAML2 assertion (this method will encode it for you),
103-
or a raw JWT assertion.
102+
It can be a raw SAML2 assertion (we will base64 encode it for you),
103+
or a raw JWT assertion in bytes (which we will relay to http layer).
104104
It can also be a callable (recommended),
105105
so that we will do lazy creation of an assertion.
106106
client_assertion_type (str):
@@ -198,7 +198,9 @@ def _obtain_token( # The verb "obtain" is influenced by OAUTH2 RFC 6749
198198
self.default_body["client_assertion_type"], lambda a: a)
199199
_data["client_assertion"] = encoder(
200200
self.client_assertion() # Do lazy on-the-fly computation
201-
if callable(self.client_assertion) else self.client_assertion)
201+
if callable(self.client_assertion) else self.client_assertion
202+
) # The type is bytes, which is preferrable. See also:
203+
# https://github.com/psf/requests/issues/4503#issuecomment-455001070
202204

203205
_data.update(self.default_body) # It may contain authen parameters
204206
_data.update(data or {}) # So the content in data param prevails

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
],
3131
packages=['oauth2cli'],
3232
install_requires=[
33-
'requests>=2.0.0',
34-
'PyJWT>=1.0.0',
33+
'requests>=2.0.0,<3',
34+
'PyJWT>=1.0.0,<3',
3535
]
3636
)

0 commit comments

Comments
 (0)