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