1import psycopg
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.select(
20 OAuthConnection.provider_key, flat=True
21 ).distinct()
22 )
23 except psycopg.OperationalError, psycopg.ProgrammingError:
24 # Check runs on plain migrations apply, and the table may not exist yet
25 # or it may not be installed on the particular database intentionally
26 return errors
27
28 keys_in_settings = set(get_provider_keys())
29
30 if keys_in_db - keys_in_settings:
31 errors.append(
32 PreflightResult(
33 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(
34 ", ".join(keys_in_db - keys_in_settings)
35 ),
36 id="oauth.provider_settings_missing",
37 )
38 )
39
40 return errors