Skip to content

Commit a7a686c

Browse files
committed
refactor jwt package, get tests passing
1 parent 445cab8 commit a7a686c

File tree

12 files changed

+60
-33
lines changed

12 files changed

+60
-33
lines changed

jwt/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
resource(name='pyproject', source='pyproject.toml')
2+
file(name='readme', source='README.md')
3+
4+
files(sources=['tests/data/*'])
5+
6+
python_distribution(
7+
name='vonage-jwt',
8+
dependencies=[
9+
':pyproject',
10+
':readme',
11+
'jwt/src/vonage_jwt',
12+
],
13+
provides=python_artifact(),
14+
generate_setup=False,
15+
repositories=['@pypi'],
16+
)

jwt/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.1
2+
- Exceptions inherit from `VonageError`
3+
- Moving the package into the Vonage Python SDK monorepo
4+
15
# 1.1.0
26
- Add new module with method to verify JWT signatures, `verify_jwt.verify_signature`
37

jwt/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
This package (`vonage-jwt`) provides functionality to generate a JWT in Python code.
44

5-
It is used by the [Vonage Python SDK](https://github.com/Vonage/vonage-python-sdk), specifically by the `vonage-http-client` package, to generate JWTs for authentication. Thus, it doesn't require manual installation or configuration unless you're using this package independently of a SDK.
5+
It is used by the [Vonage Python SDK](https://github.com/Vonage/vonage-python-sdk), specifically by the `vonage-http-client` package, to generate JWTs for authentication. Thus, it doesn't require manual installation or configuration unless you're using this package independently of an SDK.
66

7-
For full API documentation, refer to the [Vonage Developer documentation](https://developer.vonage.com).
7+
For full API documentation, refer to the [Vonage developer documentation](https://developer.vonage.com).
88

99
- [Installation](#installation)
1010
- [Generating JWTs](#generating-jwts)
@@ -27,7 +27,7 @@ It can also be used as a standalone JWT generator for use with Vonage APIs, like
2727
### Import the `JwtClient` object
2828

2929
```python
30-
from vonage_jwt.jwt import JwtClient
30+
from vonage_jwt import JwtClient
3131
```
3232

3333
### Create a `JwtClient` object
@@ -54,7 +54,7 @@ jwt_client.generate_application_jwt(claims)
5454
You can use the `verify_jwt.verify_signature` method to verify a JWT signature is valid.
5555

5656
```python
57-
from vonage_jwt.verify_jwt import verify_signature
57+
from vonage_jwt import verify_signature
5858

5959
verify_signature(TOKEN, SIGNATURE_SECRET) # Returns a boolean
6060
```

jwt/pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ description = "Tooling for working with JWTs for Vonage APIs in Python."
55
readme = "README.md"
66
authors = [{ name = "Vonage", email = "[email protected]" }]
77
requires-python = ">=3.8"
8-
dependencies = [
9-
"pyjwt[crypto] >=1.6.4"
10-
]
8+
dependencies = ["vonage-utils>=1.1.2", "pyjwt[crypto]>=1.6.4"]
119
classifiers = [
1210
"Programming Language :: Python",
1311
"Programming Language :: Python :: 3",

jwt/src/vonage_jwt/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from .jwt import JwtClient, VonageJwtError
1+
from .errors import VonageJwtError, VonageVerifyJwtError
2+
from .jwt import JwtClient
23
from .verify_jwt import verify_signature
34

4-
__all__ = ['JwtClient', 'VonageJwtError', 'verify_signature']
5+
__all__ = ['JwtClient', 'VonageJwtError', 'VonageVerifyJwtError', 'verify_signature']

jwt/src/vonage_jwt/errors.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from vonage_utils import VonageError
2+
3+
4+
class VonageJwtError(VonageError):
5+
"""An error relating to the Vonage JWT Generator."""
6+
7+
8+
class VonageVerifyJwtError(VonageError):
9+
"""The signature could not be verified."""

jwt/src/vonage_jwt/jwt.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import re
22
from time import time
3-
from jwt import encode
4-
from uuid import uuid4
53
from typing import Union
4+
from uuid import uuid4
5+
6+
from jwt import encode
7+
8+
from .errors import VonageJwtError
69

710

811
class JwtClient:
@@ -22,8 +25,8 @@ def __init__(self, application_id: str, private_key: str):
2225
)
2326

2427
def generate_application_jwt(self, jwt_options: dict = {}):
25-
"""
26-
Generates a JWT for the specified Vonage application.
28+
"""Generates a JWT for the specified Vonage application.
29+
2730
You can override values for application_id and private_key on the JWTClient object by
2831
specifying them in the `jwt_options` dict if required.
2932
"""
@@ -51,7 +54,3 @@ def _set_private_key(self, key: Union[str, bytes]):
5154
)
5255
else:
5356
self._private_key = key
54-
55-
56-
class VonageJwtError(Exception):
57-
"""An error relating to the Vonage JWT Generator."""

jwt/src/vonage_jwt/verify_jwt.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from jwt import InvalidSignatureError, decode
22

3+
from .errors import VonageVerifyJwtError
4+
35

46
def verify_signature(token: str, signature_secret: str = None) -> bool:
5-
"""
6-
Method to verify that an incoming JWT was sent by Vonage.
7-
"""
7+
"""Method to verify that an incoming JWT was sent by Vonage."""
88

99
try:
1010
decode(token, signature_secret, algorithms='HS256')
@@ -13,7 +13,3 @@ def verify_signature(token: str, signature_secret: str = None) -> bool:
1313
return False
1414
except Exception as e:
1515
raise VonageVerifyJwtError(repr(e))
16-
17-
18-
class VonageVerifyJwtError(Exception):
19-
"""The signature could not be verified."""

jwt/tests/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
python_tests(dependencies=['messages', 'testutils'])
1+
python_tests(dependencies=['jwt', 'testutils'])

jwt/tests/test_jwt_generator.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
from vonage_jwt.jwt import JwtClient, VonageJwtError
1+
from os import environ
2+
from os.path import dirname, join
3+
from time import time
24

3-
import os
5+
from jwt.exceptions import ImmatureSignatureError
46
from pytest import raises
7+
from vonage_jwt.jwt import JwtClient, VonageJwtError
8+
59
from jwt import decode
6-
from jwt.exceptions import ImmatureSignatureError
7-
from time import time
810

911
# Ensure the client isn't being configured with real values
10-
os.environ.clear()
12+
environ.clear()
1113

1214

1315
def read_file(path):
14-
with open(os.path.join(os.path.dirname(__file__), path)) as input_file:
16+
with open(join(dirname(__file__), path)) as input_file:
1517
return input_file.read()
1618

1719

1820
application_id = 'asdf1234'
1921
private_key_string = read_file('data/private_key.txt')
20-
private_key_file_path = './tests/data/private_key.txt'
22+
private_key_file_path = 'jwt/tests/data/private_key.txt'
2123
jwt_client = JwtClient(application_id, private_key_file_path)
2224

2325
public_key = read_file('data/public_key.txt')

0 commit comments

Comments
 (0)