Skip to content

Commit df8516b

Browse files
authored
Merge pull request #225 from Vonage/jwt-auth-fix
Jwt auth fix
2 parents f992b63 + 67db346 commit df8516b

File tree

2 files changed

+3
-9
lines changed

2 files changed

+3
-9
lines changed

src/vonage/client.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def __init__(
9393
self.signature_method = getattr(hashlib, signature_method)
9494

9595
self._jwt_auth_params = {}
96-
self.jwt = None
9796

9897
if private_key is not None and application_id is not None:
9998
self._application_id = application_id
@@ -103,8 +102,6 @@ def __init__(
103102
with open(self._private_key, "rb") as key_file:
104103
self._private_key = key_file.read()
105104

106-
self.jwt = self._generate_application_jwt()
107-
108105
self._host = "rest.nexmo.com"
109106
self._api_host = "api.nexmo.com"
110107

@@ -115,7 +112,6 @@ def __init__(
115112

116113
self.headers = {"User-Agent": user_agent, "Accept": "application/json"}
117114

118-
119115
self.account = Account(self)
120116
self.application = Application(self)
121117
self.messages = Messages(self)
@@ -152,7 +148,6 @@ def api_host(self, value=None):
152148

153149
def auth(self, params=None, **kwargs):
154150
self._jwt_auth_params = params or kwargs
155-
self.jwt = self._generate_application_jwt()
156151

157152
def check_signature(self, params):
158153
params = dict(params)
@@ -293,7 +288,7 @@ def parse(self, host, response):
293288
logger.debug(f"Response headers {repr(response.headers)}")
294289
if response.status_code == 401:
295290
raise AuthenticationError(
296-
"Check you're using a valid authentication method for the API you want to use"
291+
"Authentication failed. Check you're using a valid authentication method."
297292
)
298293
elif response.status_code == 204:
299294
return None
@@ -313,7 +308,6 @@ def parse(self, host, response):
313308

314309
# Test for standard error format:
315310
try:
316-
317311
error_data = response.json()
318312
if (
319313
"type" in error_data
@@ -334,7 +328,7 @@ def parse(self, host, response):
334328
raise ServerError(message)
335329

336330
def _add_jwt_to_request_headers(self):
337-
return dict(self.headers, Authorization=b"Bearer " + self.jwt)
331+
return dict(self.headers, Authorization=b"Bearer " + self._generate_application_jwt())
338332

339333
def _generate_application_jwt(self):
340334
iat = int(time.time())

src/vonage/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, client):
1919
def send_message(self, params: dict):
2020
self.validate_send_message_input(params)
2121

22-
if self._client.jwt is None:
22+
if self._client._application_id is None:
2323
self._auth_type='header'
2424
return self._client.post(
2525
self._client.api_host(),

0 commit comments

Comments
 (0)