Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure digest input is bytes #4679

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lemur/auth/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def decode_with_multiple_secrets(encoded_jwt, secrets, algorithms):
continue
if len(secrets) > 1:
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(secret)
if isinstance(secret, str):
digest.update(secret.encode())
else:
digest.update(secret)
metrics.send("jwt_decode", "counter", 1, metric_tags={**dict(kid=index, fingerprint=digest.finalize().hex()), **payload})
return payload
if errors:
Expand Down
30 changes: 30 additions & 0 deletions lemur/tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from unittest.mock import patch

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
import jwt
from lemur.auth.service import decode_with_multiple_secrets


@patch("lemur.auth.service.metrics")
def test_decode_with_multiple_secrets(mock_metrics):
# Given
secret = "my_secret"
encoded_jwt = jwt.encode({"foo": "bar"}, secret, algorithm='HS256')
secrets = [secret, secret + "2"]
algorithms = ['HS256']

# When
payload = decode_with_multiple_secrets(encoded_jwt, secrets, algorithms)

# Then
assert payload == {"foo": "bar"}
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(secret.encode())
mock_metrics.send.assert_called_once_with(
"jwt_decode", "counter", 1,
metric_tags={
**dict(kid=0, fingerprint=digest.finalize().hex()),
**{"foo": "bar"}
}
)