Skip to content

Commit 8268886

Browse files
DzeranovBobronium
andauthored
[CVAT][Exchange Oracle] Changed JWT algo from HS256 to ES256 (#2617)
* fix: changed JWT algo from HS256 to ES256 * Update tests and config to use ES256 algorithm * Add comment clarifying source of jwt_public_key --------- Co-authored-by: Arseny Boykov <[email protected]>
1 parent ce35f40 commit 8268886

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

packages/examples/cvat/exchange-oracle/src/core/config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,12 @@ class CoreConfig:
244244

245245

246246
class HumanAppConfig:
247-
jwt_key = os.environ.get("HUMAN_APP_JWT_KEY", "sample")
247+
# jwt_public_key is obtained from the Human App.
248+
# To generate a key pair for testing purposes:
249+
# openssl ecparam -name prime256v1 -genkey -noout -out ec_private.pem
250+
# openssl ec -in ec_private.pem -pubout -out ec_public.pem
251+
# HUMAN_APP_JWT_KEY=$(cat ec_public.pem)
252+
jwt_public_key = os.environ.get("HUMAN_APP_JWT_KEY")
248253

249254

250255
class ApiConfig:

packages/examples/cvat/exchange-oracle/src/endpoints/authentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def authenticate_token(
6868

6969
try:
7070
payload = jwt.decode(
71-
token.credentials, Config.human_app_config.jwt_key, algorithms=["HS256"]
71+
token.credentials, Config.human_app_config.jwt_public_key, algorithms=["ES256"]
7272
)
7373
return self._auth_data_class.model_validate(payload)
7474
except (jwt.PyJWTError, pydantic.ValidationError) as e:

packages/examples/cvat/exchange-oracle/tests/api/test_exchange_api.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from unittest.mock import patch
77

88
import jwt
9+
from cryptography.hazmat.primitives import serialization
10+
from cryptography.hazmat.primitives.asymmetric import ec
911
from fastapi.responses import Response
1012
from fastapi.testclient import TestClient
1113
from sqlalchemy.orm import Session
@@ -31,6 +33,25 @@
3133
cvat_email = "[email protected]"
3234

3335

36+
def generate_ecdsa_keys() -> tuple[str, str]:
37+
private_key = ec.generate_private_key(ec.SECP256R1())
38+
pem_private = private_key.private_bytes(
39+
encoding=serialization.Encoding.PEM,
40+
format=serialization.PrivateFormat.PKCS8,
41+
encryption_algorithm=serialization.NoEncryption(),
42+
)
43+
pem_public = private_key.public_key().public_bytes(
44+
encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo
45+
)
46+
return pem_private.decode(), pem_public.decode()
47+
48+
49+
PRIVATE_KEY, PUBLIC_KEY = generate_ecdsa_keys()
50+
51+
# Exchange Oracle doesn't have access to the private key
52+
Config.human_app_config.jwt_public_key = PUBLIC_KEY
53+
54+
3455
def generate_jwt_token(
3556
*,
3657
wallet_address: str | None = user_address,
@@ -41,7 +62,8 @@ def generate_jwt_token(
4162
**({"wallet_address": wallet_address} if wallet_address else {"role": "HUMAN_APP"}),
4263
"email": email,
4364
},
44-
Config.human_app_config.jwt_key,
65+
PRIVATE_KEY,
66+
algorithm="ES256",
4567
)
4668

4769

0 commit comments

Comments
 (0)