Skip to content

Commit

Permalink
fix: support custom alg in jwt header for signing (#729)
Browse files Browse the repository at this point in the history
* fix: support custom alg in jwt header for signing

* lint
  • Loading branch information
arithmetic1728 committed Apr 8, 2021
1 parent 6033e30 commit 0a83706
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
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

0 comments on commit 0a83706

Please sign in to comment.