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

fix: support custom alg in jwt header for signing #729

Merged
merged 2 commits into from Apr 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions google/auth/jwt.py
Expand Up @@ -95,10 +95,11 @@ def encode(signer, payload, header=None, key_id=None):

header.update({"typ": "JWT"})

if es256 is not None and isinstance(signer, es256.ES256Signer):
header.update({"alg": "ES256"})
else:
header.update({"alg": "RS256"})
if "alg" not in header:
if es256 is not None and isinstance(signer, es256.ES256Signer):
header.update({"alg": "ES256"})
else:
header.update({"alg": "RS256"})

if key_id is not None:
header["kid"] = key_id
Expand Down
6 changes: 6 additions & 0 deletions tests/test_jwt.py
Expand Up @@ -73,6 +73,12 @@ def test_encode_extra_headers(signer):
}


def test_encode_custom_alg_in_headers(signer):
encoded = jwt.encode(signer, {}, header={"alg": "foo"})
header = jwt.decode_header(encoded)
assert header == {"typ": "JWT", "alg": "foo", "kid": signer.key_id}


@pytest.fixture
def es256_signer():
return crypt.ES256Signer.from_string(EC_PRIVATE_KEY_BYTES, "1")
Expand Down