This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
feat: Use cryptography based JWT parser for increased speed #854
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import base64 | ||
import binascii | ||
import json | ||
|
||
from cryptography.exceptions import InvalidSignature | ||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives.asymmetric import ec, utils | ||
from cryptography.hazmat.primitives import hashes | ||
from pyasn1.error import PyAsn1Error | ||
from twisted.logger import Logger | ||
|
||
|
||
def repad(string): | ||
# type: (str) -> str | ||
"""Adds padding to strings for base64 decoding""" | ||
if len(string) % 4: | ||
string += '===='[len(string) % 4:] | ||
return string | ||
|
||
|
||
class VerifyJWT(object): | ||
"""Minimally verify a Vapid JWT object. | ||
|
||
Why hand roll? Most python JWT libraries either use a python elliptic | ||
curve library directly, or call one that does, or is abandoned, or a | ||
dozen other reasons. | ||
|
||
After spending half a day looking for reasonable replacements, I | ||
decided to just write the functions we need directly. | ||
|
||
THIS IS NOT A FULL JWT REPLACEMENT. | ||
|
||
""" | ||
|
||
@staticmethod | ||
def extract_signature(auth): | ||
# type: (str) -> tuple() | ||
"""Fix the JWT auth token. | ||
|
||
The JWA spec defines the signature to be a pair of 32octet encoded | ||
longs. | ||
The `ecdsa` library signs using a raw, 32octet pair of values (s, r). | ||
Cryptography, which uses OpenSSL, uses a DER sequence of (s, r). | ||
This function converts the raw ecdsa to DER. | ||
|
||
:param auth: A JWT authorization token. | ||
:type auth: str | ||
|
||
:return tuple containing the signature material and signature | ||
|
||
""" | ||
payload, asig = auth.encode('utf8').rsplit(".", 1) | ||
sig = base64.urlsafe_b64decode(repad(asig)) | ||
if len(sig) != 64: | ||
return payload, sig | ||
|
||
encoded = utils.encode_dss_signature( | ||
s=int(binascii.hexlify(sig[32:]), 16), | ||
r=int(binascii.hexlify(sig[:32]), 16) | ||
) | ||
return payload, encoded | ||
|
||
@staticmethod | ||
def decode(token, key): | ||
# type (str, str) -> dict() | ||
"""Decode a web token into a assertion dictionary. | ||
|
||
This attempts to rectify both ecdsa and openssl generated | ||
signatures. We use the built-in cryptography library since it wraps | ||
libssl and is faster than the python only approach. | ||
|
||
:param token: VAPID auth token | ||
:type token: str | ||
:param key: bitarray containing public key | ||
:type key: str or bitarray | ||
|
||
:return dict of the VAPID claims | ||
|
||
:raise InvalidSignature | ||
|
||
""" | ||
# convert the signature if needed. | ||
try: | ||
sig_material, signature = VerifyJWT.extract_signature(token) | ||
pkey = ec.EllipticCurvePublicNumbers.from_encoded_point( | ||
ec.SECP256R1(), | ||
key | ||
).public_key(default_backend()) | ||
# NOTE: verify() will take any string as the signature. It appears | ||
# to be doing lazy verification and matching strings rather than | ||
# comparing content values. If the signatures start failing for | ||
# some unknown reason in the future, decode the signature and | ||
# make sure it matches how we're reconstructing it. | ||
# This will raise an InvalidSignature exception if failure. | ||
# It will be captured externally. | ||
pkey.verify( | ||
signature, | ||
sig_material.encode('utf8'), | ||
ec.ECDSA(hashes.SHA256())) | ||
return json.loads( | ||
base64.urlsafe_b64decode( | ||
repad(sig_material.split('.')[1]).encode('utf8'))) | ||
except (ValueError, TypeError, binascii.Error, PyAsn1Error): | ||
raise InvalidSignature() | ||
except Exception: # pragma: no cover | ||
Logger().failure("Unexpected error processing JWT") | ||
raise InvalidSignature() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ documentation is organized alphabetically by module name. | |
api/ssl | ||
api/utils | ||
api/websocket | ||
api/jwt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.. _jwt_module: | ||
|
||
:mod:`autopush.jwt` | ||
------------------------ | ||
|
||
.. automodule:: autopush.jwt | ||
|
||
.. autoclass:: VerifyJWT | ||
:members: | ||
:member-order: bysource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
-r requirements.txt | ||
nose | ||
coverage | ||
ecdsa==0.13 | ||
python-jose==1.3.2 | ||
mock>=1.0.1 | ||
funcsigs==1.0.2 | ||
pbr==1.10.0 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception here definitely not needed