Skip to content

Commit

Permalink
Merge pull request #4634 from mozilla/get-fxa-socialapp-from-config
Browse files Browse the repository at this point in the history
fix MPP-3805: get FxA SocialApp from DB at startup
  • Loading branch information
groovecoder authored Apr 23, 2024
2 parents b1c96b0 + 06661e9 commit 3358c86
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
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 @@ -174,6 +174,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 @@ -210,7 +219,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

0 comments on commit 3358c86

Please sign in to comment.