Skip to content

Commit 844223b

Browse files
committed
refactor exceptions, add code linting and more tests
1 parent 44e9d37 commit 844223b

File tree

7 files changed

+99
-5
lines changed

7 files changed

+99
-5
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
# pycharm
44
.idea
55
# bytecode
6-
__pycache__
6+
__pycache__
7+
# pytest-cov
8+
.coverage

paseto/exceptions.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
class PasetoException(Exception):
4+
""" Base exception for all paseto related errors"""
5+
pass
6+
7+
8+
class InvalidFooter(PasetoException):
9+
pass
10+
11+
12+
class InvalidHeader(PasetoException):
13+
pass

paseto/protocol/version2.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hmac
33
import hashlib
44
from .util import pae, b64, b64decode
5+
from paseto.exceptions import InvalidHeader, InvalidFooter
56
from nacl.bindings import (
67
crypto_aead_xchacha20poly1305_ietf_encrypt,
78
crypto_aead_xchacha20poly1305_ietf_decrypt,
@@ -66,13 +67,13 @@ def decrypt(message: bytes, key: bytes, footer: bytes = None):
6667
# they do so using a constant-time string compare function.
6768
if footer is not None:
6869
if not hmac.compare_digest(b64(footer), message.split(b".")[-1]):
69-
raise Exception("Unexpected footer found")
70+
raise InvalidFooter("Invalid message footer")
7071

7172
# 2. Verify that the message begins with "v2.local.", otherwise throw
7273
# an exception. This constant will be referred to as "h".
7374
header = Version2.HEADER_LOCAL
7475
if not message.startswith(header):
75-
raise Exception("Unexpected header found")
76+
raise InvalidHeader("Invalid message header")
7677

7778
# 3. Decode the payload ("m" sans "h", "f", and the optional trailing
7879
# period between "m" and "f") from base64url to raw binary. Set:

poetry.lock

+65-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pylama.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pylama:pycodestyle]
2+
max_line_length = 90

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pynacl = "^1.3"
1313
pytest = "^3.9"
1414
pytest-cov = "^2.6"
1515
black = "^18.3-alpha.0"
16+
pylama = "^7.6"
1617

1718
[build-system]
1819
requires = ["poetry>=0.12"]

tests/protocol/test_version2.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import pytest
12
from paseto.protocol.version2 import Version2
3+
from paseto.exceptions import InvalidFooter, InvalidHeader
24

35

46
class TestVersion2(object):
5-
def test_encrpy_decrypt(self):
7+
8+
def test_encrypt_decrypt(self):
69
key = b"0" * 32
710
message = b"foo"
811
footer = b"baz"
@@ -11,3 +14,11 @@ def test_encrpy_decrypt(self):
1114

1215
plain_text = Version2.decrypt(token, key, footer)
1316
assert plain_text == message
17+
18+
def test_decrypt_invalid_footer(self):
19+
with pytest.raises(InvalidFooter):
20+
Version2.decrypt(b'header.message.footer', b'a key', b'some_other_footer')
21+
22+
def test_decrypt_invalid_header(self):
23+
with pytest.raises(InvalidHeader):
24+
Version2.decrypt(b'some_incorrect_header.message.footer', b'a key')

0 commit comments

Comments
 (0)