Skip to content

Commit 1ca84af

Browse files
authored
Merge pull request #265 from digital-asset/python-fix-jwt-decoding
python: Fix an error in the way that dazl interprets JWTs.
2 parents 8e7b5a8 + 96d0191 commit 1ca84af

File tree

7 files changed

+28
-6
lines changed

7 files changed

+28
-6
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.5.6
1+
7.5.7

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[tool.poetry]
55
name = "dazl"
6-
version = "7.5.6"
6+
version = "7.5.7"
77
description = "high-level Ledger API client for DAML ledgers"
88
license = "Apache-2.0"
99
authors = ["Davin K. Tanabe <davin.tanabe@digitalasset.com>"]

python/dazl/ledger/config/access.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,9 @@ def decode_token(token: str) -> Mapping[str, Any]:
388388
components = token.split(".", 3)
389389
if len(components) != 3:
390390
raise ValueError("not a JWT")
391-
claim_str = base64.urlsafe_b64decode(components[1])
391+
392+
pad_bytes = "=" * (-len(components[1]) % 4)
393+
claim_str = base64.urlsafe_b64decode(components[1] + pad_bytes)
392394
claims = json.loads(claim_str)
393395
claims_dict = claims.get(DamlLedgerApiNamespace)
394396
if claims_dict is None:
@@ -418,9 +420,9 @@ def encode_unsigned_token(
418420
}
419421

420422
return (
421-
base64.urlsafe_b64encode(json.dumps(header).encode("utf-8"))
423+
base64.urlsafe_b64encode(json.dumps(header).encode("utf-8")).rstrip(b"=")
422424
+ b"."
423-
+ base64.urlsafe_b64encode(json.dumps(payload).encode("utf-8"))
425+
+ base64.urlsafe_b64encode(json.dumps(payload).encode("utf-8")).rstrip(b"=")
424426
+ b"."
425427
)
426428

python/dazl/ledger/grpc/channel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def __call__(self, context: "AuthMetadataContext", callback: "AuthMetadataPlugin
6666
# TODO: Add support here for refresh tokens
6767
token = self._config.access.token
6868
if token:
69-
options.append(("Authorization", "Bearer " + self._config.access.token))
69+
# note: gRPC headers MUST be lowercased
70+
options.append(("authorization", "Bearer " + self._config.access.token))
7071

7172
callback(tuple(options), None)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2017-2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from dazl.ledger.config import PropertyBasedAccessConfig
4+
from dazl.ledger.config.access import DamlLedgerApiNamespace
5+
from dazl.prim import Party
6+
import jwt
7+
8+
9+
def test_access_jwts_are_valid():
10+
config = PropertyBasedAccessConfig(act_as=[Party("Alice")])
11+
claims = jwt.decode(config.token, algorithms=["none"], options={"verify_signature": False})
12+
13+
assert claims[DamlLedgerApiNamespace]["actAs"] == ["Alice"]

python/tests/unit/test_ledger_config_argv.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Copyright (c) 2017-2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
import argparse
25
from io import StringIO
36
import logging

python/tests/unit/test_ledger_config_url.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Copyright (c) 2017-2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
from urllib.parse import urlparse
25

36
from dazl.ledger.config import create_url

0 commit comments

Comments
 (0)