Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Keycloak for JWT validation #214

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/mainframe/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ class Mainframe(EnvConfig):

job_timeout: int = 60 * 2


mainframe_settings = Mainframe() # pyright: ignore

class Keycloak(EnvConfig, env_prefix="kc_"): # pyright: ignore
issuer_url: str = "https://keycloak.vipyrsec.com/realms/dragonfly"
audience: str = "Dragonfly Mainframe"
jwks_uri: str = "https://keycloak.vipyrsec.com/realms/dragonfly/protocol/openid-connect/certs"

keycloak_settings = Keycloak()

class _Sentry(EnvConfig, env_prefix="sentry_"): # pyright: ignore
dsn: str = ""
Expand Down
1 change: 0 additions & 1 deletion src/mainframe/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def validate_token_override():
audience="DEVELOPMENT AUDIENCE",
issued_at=datetime.now() - timedelta(seconds=10),
expires_at=datetime.now() + timedelta(seconds=10),
grant_type="DEVELOPMENT GRANT TYPE",
)


Expand Down
14 changes: 4 additions & 10 deletions src/mainframe/json_web_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@

import jwt

from mainframe.constants import mainframe_settings
from mainframe.constants import keycloak_settings
from mainframe.custom_exceptions import (
BadCredentialsException,
UnableCredentialsException,
)


@dataclass
class AuthenticationData:
issuer: str
subject: str
audience: str
issued_at: datetime
expires_at: datetime
grant_type: str

@classmethod
def from_dict(cls, data: dict[Any, Any]):
Expand All @@ -28,7 +26,6 @@ def from_dict(cls, data: dict[Any, Any]):
audience=data["aud"],
issued_at=datetime.fromtimestamp(data["iat"]),
expires_at=datetime.fromtimestamp(data["exp"]),
grant_type=data["gty"],
)


Expand All @@ -37,21 +34,18 @@ class JsonWebToken:
"""Perform JSON Web Token (JWT) validation using PyJWT"""

jwt_access_token: str
auth0_issuer_url: str = f"https://{mainframe_settings.auth0_domain}/"
auth0_audience: str = mainframe_settings.auth0_audience
algorithm: str = "RS256"
jwks_uri: str = f"{auth0_issuer_url}.well-known/jwks.json"

def validate(self) -> AuthenticationData:
try:
jwks_client = jwt.PyJWKClient(self.jwks_uri)
jwks_client = jwt.PyJWKClient(keycloak_settings.jwks_uri, headers={"User-Agent": "Dragonfly Mainframe"})
jwt_signing_key = jwks_client.get_signing_key_from_jwt(self.jwt_access_token).key
payload = jwt.decode(
self.jwt_access_token,
jwt_signing_key,
algorithms=self.algorithm, # type: ignore
audience=self.auth0_audience,
issuer=self.auth0_issuer_url,
audience=keycloak_settings.audience,
issuer=keycloak_settings.issuer_url,
)
except jwt.exceptions.PyJWKClientError:
raise UnableCredentialsException
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def auth() -> AuthenticationData:
audience="DEVELOPMENT AUDIENCE",
issued_at=datetime.now() - timedelta(seconds=10),
expires_at=datetime.now() + timedelta(seconds=10),
grant_type="DEVELOPMENT GRANT TYPE",
)


Expand Down