v0.151.1
 1import psycopg
 2
 3from plain.preflight import PreflightCheck, PreflightResult, register_check
 4
 5
 6@register_check(name="oauth.provider_keys")
 7class CheckOAuthProviderKeys(PreflightCheck):
 8    """
 9    Check for OAuth provider keys in the database that are not present in settings.
10    """
11
12    def run(self) -> list[PreflightResult]:
13        from .models import OAuthConnection
14        from .providers import get_provider_keys
15
16        errors = []
17
18        try:
19            keys_in_db = set(
20                OAuthConnection.query.values_list("provider_key", flat=True).distinct()
21            )
22        except (psycopg.OperationalError, psycopg.ProgrammingError):
23            # Check runs on plain migrations apply, and the table may not exist yet
24            # or it may not be installed on the particular database intentionally
25            return errors
26
27        keys_in_settings = set(get_provider_keys())
28
29        if keys_in_db - keys_in_settings:
30            errors.append(
31                PreflightResult(
32                    fix="The following OAuth providers are in the database but not in the settings: {}. Add these providers to your OAUTH_LOGIN_PROVIDERS setting or remove the corresponding OAuthConnection records.".format(
33                        ", ".join(keys_in_db - keys_in_settings)
34                    ),
35                    id="oauth.provider_settings_missing",
36                )
37            )
38
39        return errors