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 1 commit
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: 7 additions & 0 deletions privaterelay/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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 +94,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) -> Any:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I couldn't quite figure out how to annotate this with -> SocialApp because Django kept complaining that I couldn't load the SocialApp model until the app registry was full.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at https://mypy.readthedocs.io/en/stable/runtime_troubles.html#annotation-issues-at-runtime and pick one of those solutions (that isn't # type)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about 06661e9 ? (I tried from __future__ import annotations first, but it didn't work. 😢

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