Skip to content

Commit

Permalink
Drop AsyncJWK to use static JWK public key
Browse files Browse the repository at this point in the history
  • Loading branch information
nkiryanov committed Jun 11, 2024
1 parent 2574939 commit 52fd08a
Show file tree
Hide file tree
Showing 23 changed files with 204 additions and 529 deletions.
3 changes: 1 addition & 2 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ WEBSOCKETS_HOST=localhost
WEBSOCKETS_PORT=6789
WEBSOCKETS_PATH=/v1/test-subscription-websocket

AUTH_JWKS_URL=https://auth-test.contoso.com/auth/realms/test-realm/protocol/openid-connect/certs
AUTH_SUPPORTED_SIGNING_ALGORITHMS=["RS256"]
JWT_PUBLIC_KEY=-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqdnObC47NUAqOOCvOzOg\n4i0KxZaTe2m7rfQe9a+rWbtkJ2TNuakN7eRshvv2UVGP4uSKEKe356v4GwP/yGAi\n92XEGr0Y6ieypnhu1wi0wuK4Z62abRkvsEZdDwKQpcde1rvyuvt0YeDMh9dCi/3P\nBLhcOlgvAu+6M79iWlRTZxzFe3KVzQabU8CIfgG7MXokutHUxT2dsRNfX4VwxMsW\no9o0o1QqSPJ6OOx2DwLEKat5n1w5ysIYYvkgHs36B3nPnZYc2b66uEp9AP9JlRjc\npWuH8vn3/OsvxHMErhyn+h9/H+aXRRIk/JuokqpVbPlOY8l+5z+JG6zn9onWpjcM\njQ19NP8C/CTwvcB8O+s3qEHKECkggyywCOe7EQqrB0uMU7IQ1srH8ENspuY16UQV\nqQPBlYVQOywVW6+25z+ILNyPjEdzukn6Oyh9ChU+m08Tw9SsBAV2vnrkUCT1wJhC\nc2X/i2WqBM4lExJu18tau0X26iKdbRZwx50OKUVn9w8AbW/iglCExYkpDs3VKH80\nallIBcfAOXJ00X6jhWETT2T9U1c1KiTqNHMgBflvG17CbkKkyaFIILdYIfMkW2EA\nngpOxaBC1cLJTvXGuTx4R54wpQNyt8k/7P/r8UY+FxBlA/3Upb9LidmItvd7yvVr\nzUMftEW5MYWWWnm6ZUS3Q6sCAwEAAQ==\n-----END PUBLIC KEY-----\n
Empty file added secrets/.gitkeep
Empty file.
108 changes: 108 additions & 0 deletions src/a12n/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import pytest

import jwt

from app.types import UserId


@pytest.fixture
def jwt_private_key():
return """-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQCp2c5sLjs1QCo4
4K87M6DiLQrFlpN7abut9B71r6tZu2QnZM25qQ3t5GyG+/ZRUY/i5IoQp7fnq/gb
A//IYCL3ZcQavRjqJ7KmeG7XCLTC4rhnrZptGS+wRl0PApClx17Wu/K6+3Rh4MyH
10KL/c8EuFw6WC8C77ozv2JaVFNnHMV7cpXNBptTwIh+AbsxeiS60dTFPZ2xE19f
hXDEyxaj2jSjVCpI8no47HYPAsQpq3mfXDnKwhhi+SAezfoHec+dlhzZvrq4Sn0A
/0mVGNyla4fy+ff86y/EcwSuHKf6H38f5pdFEiT8m6iSqlVs+U5jyX7nP4kbrOf2
idamNwyNDX00/wL8JPC9wHw76zeoQcoQKSCDLLAI57sRCqsHS4xTshDWysfwQ2ym
5jXpRBWpA8GVhVA7LBVbr7bnP4gs3I+MR3O6Sfo7KH0KFT6bTxPD1KwEBXa+euRQ
JPXAmEJzZf+LZaoEziUTEm7Xy1q7RfbqIp1tFnDHnQ4pRWf3DwBtb+KCUITFiSkO
zdUofzRqWUgFx8A5cnTRfqOFYRNPZP1TVzUqJOo0cyAF+W8bXsJuQqTJoUggt1gh
8yRbYQCeCk7FoELVwslO9ca5PHhHnjClA3K3yT/s/+vxRj4XEGUD/dSlv0uJ2Yi2
93vK9WvNQx+0RbkxhZZaebplRLdDqwIDAQABAoICAETiW7BOE58mFbmZjhexeZcg
81RtHAUaPY5wCjpT82dh811yqWiZolePo2AfQad7L6KyUzgr/Q7NFMNIHO1T5/pz
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ou
44fxYKe6UunrLa/MAEy9H1Ev4GeiN4KmB14dqSvV2MKHv79W4J+knEqPcj9VGNGM
KwU5VgPsz/bDn9LJYWggdIsQUZB5OsHl54gTmVPXGjoYRMCDk9jn/BTkEZUkXkHs
tFb7jcKo9XUoe0PG6Bx634wrlLx8clCwHWFfPbji4NdSGm8UTMKDAXKtMVwQyPzk
8qn2Iv/fqfJHIYs6DvxWgLy8ZoWL
-----END PRIVATE KEY-----
"""


@pytest.fixture
def jwt_public_key():
return """-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqdnObC47NUAqOOCvOzOg
4i0KxZaTe2m7rfQe9a+rWbtkJ2TNuakN7eRshvv2UVGP4uSKEKe356v4GwP/yGAi
92XEGr0Y6ieypnhu1wi0wuK4Z62abRkvsEZdDwKQpcde1rvyuvt0YeDMh9dCi/3P
BLhcOlgvAu+6M79iWlRTZxzFe3KVzQabU8CIfgG7MXokutHUxT2dsRNfX4VwxMsW
o9o0o1QqSPJ6OOx2DwLEKat5n1w5ysIYYvkgHs36B3nPnZYc2b66uEp9AP9JlRjc
pWuH8vn3/OsvxHMErhyn+h9/H+aXRRIk/JuokqpVbPlOY8l+5z+JG6zn9onWpjcM
jQ19NP8C/CTwvcB8O+s3qEHKECkggyywCOe7EQqrB0uMU7IQ1srH8ENspuY16UQV
qQPBlYVQOywVW6+25z+ILNyPjEdzukn6Oyh9ChU+m08Tw9SsBAV2vnrkUCT1wJhC
c2X/i2WqBM4lExJu18tau0X26iKdbRZwx50OKUVn9w8AbW/iglCExYkpDs3VKH80
allIBcfAOXJ00X6jhWETT2T9U1c1KiTqNHMgBflvG17CbkKkyaFIILdYIfMkW2EA
ngpOxaBC1cLJTvXGuTx4R54wpQNyt8k/7P/r8UY+FxBlA/3Upb9LidmItvd7yvVr
zUMftEW5MYWWWnm6ZUS3Q6sCAwEAAQ==
-----END PUBLIC KEY-----
"""


@pytest.fixture
def set_jwt_public_key(settings, jwt_public_key):
settings.JWT_PUBLIC_KEY = jwt_public_key
return settings


@pytest.fixture
def create_jwt_for_user(jwt_private_key):
def create_jwt(user_id: UserId, timestamp_expired_at: int) -> str:
payload = {
"sub": user_id,
"exp": timestamp_expired_at,
}

return jwt.encode(payload=payload, key=jwt_private_key, algorithm="RS256")

return create_jwt


@pytest.fixture
def jwt_user_valid_token(create_jwt_for_user):
return create_jwt_for_user(
user_id="user",
timestamp_expired_at=4700000000, # year of expiration 2118
)
157 changes: 0 additions & 157 deletions src/a12n/jwk_client.py

This file was deleted.

15 changes: 15 additions & 0 deletions src/a12n/jwt_decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Any

import jwt

from app.conf import get_app_settings
from app.types import DecodedValidToken


def decode(jwt_token: str, **kwargs: Any) -> DecodedValidToken:
"""Validate and decode a JWT token with public key.
Adjust validation parameters to project requirements (like algorithms, required claims, etc).
"""
decoded = jwt.decode(jwt=jwt_token, key=get_app_settings().JWT_PUBLIC_KEY, algorithms=["RS256"], **kwargs)
return DecodedValidToken(**decoded)
73 changes: 0 additions & 73 deletions src/a12n/tests/async_jwk_client/conftest.py

This file was deleted.

9 changes: 0 additions & 9 deletions src/a12n/tests/async_jwk_client/tests_async_jwk_common.py

This file was deleted.

Loading

0 comments on commit 52fd08a

Please sign in to comment.