Skip to content

Commit 25e1277

Browse files
author
Roman Tretiak
committed
Preload private_key in JwtTokenSource
1 parent 92d7119 commit 25e1277

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ydb/oauth2_token_exchange/token_source.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
except ImportError:
1111
jwt = None
1212

13+
try:
14+
from cryptography.hazmat.primitives.serialization import load_pem_private_key
15+
except ImportError:
16+
load_pem_private_key = None
17+
1318

1419
class Token(abc.ABC):
1520
def __init__(self, token: str, token_type: str):
@@ -48,6 +53,7 @@ def __init__(
4853
token_ttl_seconds: int = 3600,
4954
):
5055
assert jwt is not None, "Install pyjwt library to use jwt tokens"
56+
assert load_pem_private_key is not None, "Install cryptography library to use jwt tokens"
5157
self._signing_method = signing_method
5258
self._key_id = key_id
5359
if private_key and private_key_file:
@@ -70,6 +76,7 @@ def __init__(
7076
raise Exception("JWT: no private key specified")
7177
if self._token_ttl_seconds <= 0:
7278
raise Exception("JWT: invalid jwt token TTL")
79+
self._loaded_private_key = load_pem_private_key(self._private_key.encode(), password=None)
7380

7481
def token(self) -> Token:
7582
now = time.time()
@@ -96,7 +103,7 @@ def token(self) -> Token:
96103
headers["kid"] = self._key_id
97104

98105
token = jwt.encode(
99-
key=self._private_key,
106+
key=self._loaded_private_key,
100107
algorithm=self._signing_method,
101108
headers=headers,
102109
payload=payload,

0 commit comments

Comments
 (0)