Skip to content

Commit 36c2ead

Browse files
authored
[TSDK-483] Fix authentication for integrations (#233)
In Python 2.7 the 'b' prefix ahead of a string is ignored but in Python 3 it indicates a byte literal versus a string. So on Python 3 apps implementing our SDK they would encounter an error as the Nylas API is expecting a string instead of a byte string. Now we ensure that it gets decoded back into a string to ensure it works on Python 3.
1 parent f766d11 commit 36c2ead

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

nylas/client/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -817,19 +817,19 @@ def _request(self, method, url, cls=None, headers=None, **kwargs):
817817

818818
def _add_auth_header(self, auth_method):
819819
authorization = None
820-
if auth_method is AuthMethod.BEARER:
820+
if auth_method == AuthMethod.BEARER:
821821
authorization = (
822822
"Bearer {token}".format(token=self.access_token)
823823
if self.access_token
824824
else None
825825
)
826-
elif auth_method is AuthMethod.BASIC_CLIENT_ID_AND_SECRET:
826+
elif auth_method == AuthMethod.BASIC_CLIENT_ID_AND_SECRET:
827827
if self.client_id and self.client_secret:
828828
credential = "{client_id}:{client_secret}".format(
829829
client_id=self.client_id, client_secret=self.client_secret
830830
)
831831
authorization = "Basic {credential}".format(
832-
credential=b64encode(credential.encode("utf8"))
832+
credential=b64encode(credential.encode("utf8")).decode("utf8")
833833
)
834834
else:
835835
if self.client_secret:

tests/test_client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import responses
77
from nylas.client import APIClient
88
from nylas.client.restful_models import Contact
9+
from nylas.utils import AuthMethod
910

1011

1112
def urls_equal(url1, url2):
@@ -345,3 +346,22 @@ def test_count(mocked_responses, api_client, api_url):
345346

346347
contact_count = api_client.contacts.count()
347348
assert contact_count == 721
349+
350+
351+
def test_add_auth_header_bearer(api_client):
352+
api_client.access_token = "access_token"
353+
auth_header = api_client._add_auth_header(AuthMethod.BEARER)
354+
assert auth_header == {"Authorization": "Bearer access_token"}
355+
356+
357+
def test_add_auth_header_basic_client_id_and_secret(api_client):
358+
api_client.client_id = "client_id"
359+
api_client.client_secret = "client_secret"
360+
auth_header = api_client._add_auth_header(AuthMethod.BASIC_CLIENT_ID_AND_SECRET)
361+
assert auth_header == {"Authorization": "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ="}
362+
363+
364+
def test_add_auth_header_basic(api_client):
365+
api_client.client_secret = "client_secret"
366+
auth_header = api_client._add_auth_header(AuthMethod.BASIC)
367+
assert auth_header == {"Authorization": "Basic Y2xpZW50X3NlY3JldDo="}

0 commit comments

Comments
 (0)