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

fix MPP-3805: get FxA SocialApp from DB at startup #4634

Merged
merged 2 commits into from
Apr 23, 2024
Merged
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
13 changes: 12 additions & 1 deletion privaterelay/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import os
from pathlib import Path
from typing import Any
from typing import TYPE_CHECKING, Any

from django.apps import AppConfig
from django.conf import settings
Expand All @@ -13,6 +13,10 @@
ROOT_DIR = os.path.abspath(os.curdir)


if TYPE_CHECKING:
from allauth.socialaccount.models import SocialApp


def get_profiler_startup_data() -> tuple[str | None, str | None]:
from .utils import get_version_info

Expand Down Expand Up @@ -81,6 +85,7 @@ def ready(self) -> None:

try:
del self.fxa_verifying_keys # Clear cache
del self.fxa_social_app # Clear cache
except AttributeError:
pass

Expand All @@ -93,3 +98,9 @@ def fxa_verifying_keys(self) -> list[dict[str, Any]]:
keys: list[dict[str, Any]] = resp.json()["keys"]
return keys
return []

@cached_property
def fxa_social_app(self) -> "SocialApp":
from allauth.socialaccount.models import SocialApp

return SocialApp.objects.get(provider="fxa")
2 changes: 1 addition & 1 deletion privaterelay/tests/views_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def setup_fxa_rp_events(
profile.save()

# Create FxA app, account, token, etc.
fxa_app: SocialApp = baker.make(SocialApp, provider="fxa")
fxa_app: SocialApp = baker.make(SocialApp, provider="fxa", client_id="test-fxa")
fxa_profile_data = {
"email": user.email,
"locale": "en-US,en;q=0.5",
Expand Down
14 changes: 13 additions & 1 deletion privaterelay/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ def fxa_verifying_keys(reload: bool = False) -> list[dict[str, Any]]:
return private_relay_config.fxa_verifying_keys


def fxa_social_app(reload: bool = False) -> SocialApp:
"""Get FxA SocialApp from app config or DB."""
private_relay_config = apps.get_app_config("privaterelay")
assert isinstance(private_relay_config, PrivateRelayConfig)
if reload:
private_relay_config.ready()
return private_relay_config.fxa_social_app


class FxAEvent(TypedDict):
"""
FxA Security Event Token (SET) payload, sent to relying parties.
Expand Down Expand Up @@ -201,7 +210,10 @@ def _verify_jwt_with_fxa_key(
) -> FxAEvent | None:
if not verifying_keys:
raise Exception("FXA verifying keys are not available.")
social_app = SocialApp.objects.get(provider="fxa")
social_app = fxa_social_app()
if not social_app:
raise Exception("FXA SocialApp is not available.")
assert isinstance(social_app, SocialApp)
for verifying_key in verifying_keys:
if verifying_key["alg"] == "RS256":
public_key = jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(verifying_key))
Expand Down