authentik.sources.oauth.types.patreon

Patreon OAuth Views

 1"""Patreon OAuth Views"""
 2
 3from typing import Any
 4from urllib.parse import urlencode
 5
 6from authentik.sources.oauth.clients.oauth2 import UserprofileHeaderAuthClient
 7from authentik.sources.oauth.models import AuthorizationCodeAuthMethod, OAuthSource
 8from authentik.sources.oauth.types.registry import SourceType, registry
 9from authentik.sources.oauth.views.callback import OAuthCallback
10from authentik.sources.oauth.views.redirect import OAuthRedirect
11
12
13class PatreonOAuthRedirect(OAuthRedirect):
14    """Patreon OAuth2 Redirect"""
15
16    def get_additional_parameters(self, source: OAuthSource):  # pragma: no cover
17        # https://docs.patreon.com/#scopes
18        return {
19            "scope": ["identity", "identity[email]"],
20        }
21
22
23class PatreonOAuthCallback(OAuthCallback):
24    """Patreon OAuth2 Callback"""
25
26    client_class = UserprofileHeaderAuthClient
27
28    def get_user_id(self, info: dict[str, Any]) -> str | None:
29        return info.get("data", {}).get("id")
30
31
32@registry.register()
33class PatreonType(SourceType):
34    """OpenIDConnect Type definition"""
35
36    callback_view = PatreonOAuthCallback
37    redirect_view = PatreonOAuthRedirect
38    verbose_name = "Patreon"
39    name = "patreon"
40
41    authorization_url = "https://www.patreon.com/oauth2/authorize"
42    access_token_url = "https://www.patreon.com/api/oauth2/token"  # nosec
43    profile_url = f"https://www.patreon.com/api/oauth2/v2/identity?{urlencode({
44        "fields[user]": ",".join(["vanity","email","full_name"])
45    })}"
46
47    authorization_code_auth_method = AuthorizationCodeAuthMethod.POST_BODY
48
49    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
50        return {
51            "username": info.get("data", {}).get("attributes", {}).get("vanity")
52            or info.get("data", {}).get("id"),
53            "email": info.get("data", {}).get("attributes", {}).get("email"),
54            "name": info.get("data", {}).get("attributes", {}).get("full_name"),
55        }
class PatreonOAuthRedirect(authentik.sources.oauth.views.redirect.OAuthRedirect):
14class PatreonOAuthRedirect(OAuthRedirect):
15    """Patreon OAuth2 Redirect"""
16
17    def get_additional_parameters(self, source: OAuthSource):  # pragma: no cover
18        # https://docs.patreon.com/#scopes
19        return {
20            "scope": ["identity", "identity[email]"],
21        }

Patreon OAuth2 Redirect

def get_additional_parameters(self, source: authentik.sources.oauth.models.OAuthSource):
17    def get_additional_parameters(self, source: OAuthSource):  # pragma: no cover
18        # https://docs.patreon.com/#scopes
19        return {
20            "scope": ["identity", "identity[email]"],
21        }

Return additional redirect parameters for this source.

class PatreonOAuthCallback(authentik.sources.oauth.views.callback.OAuthCallback):
24class PatreonOAuthCallback(OAuthCallback):
25    """Patreon OAuth2 Callback"""
26
27    client_class = UserprofileHeaderAuthClient
28
29    def get_user_id(self, info: dict[str, Any]) -> str | None:
30        return info.get("data", {}).get("id")

Patreon OAuth2 Callback

def get_user_id(self, info: dict[str, typing.Any]) -> str | None:
29    def get_user_id(self, info: dict[str, Any]) -> str | None:
30        return info.get("data", {}).get("id")

Return unique identifier from the profile info.

@registry.register()
class PatreonType(authentik.sources.oauth.types.registry.SourceType):
33@registry.register()
34class PatreonType(SourceType):
35    """OpenIDConnect Type definition"""
36
37    callback_view = PatreonOAuthCallback
38    redirect_view = PatreonOAuthRedirect
39    verbose_name = "Patreon"
40    name = "patreon"
41
42    authorization_url = "https://www.patreon.com/oauth2/authorize"
43    access_token_url = "https://www.patreon.com/api/oauth2/token"  # nosec
44    profile_url = f"https://www.patreon.com/api/oauth2/v2/identity?{urlencode({
45        "fields[user]": ",".join(["vanity","email","full_name"])
46    })}"
47
48    authorization_code_auth_method = AuthorizationCodeAuthMethod.POST_BODY
49
50    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
51        return {
52            "username": info.get("data", {}).get("attributes", {}).get("vanity")
53            or info.get("data", {}).get("id"),
54            "email": info.get("data", {}).get("attributes", {}).get("email"),
55            "name": info.get("data", {}).get("attributes", {}).get("full_name"),
56        }

OpenIDConnect Type definition

callback_view = <class 'PatreonOAuthCallback'>
redirect_view = <class 'PatreonOAuthRedirect'>
verbose_name = 'Patreon'
name = 'patreon'
authorization_url = 'https://wwwauthentik.sources.oauth.types.patreon.com/oauth2/authorize'
access_token_url = 'https://wwwauthentik.sources.oauth.types.patreon.com/api/oauth2/token'
profile_url = 'https://wwwauthentik.sources.oauth.types.patreon.com/api/oauth2/v2/identity?fields%5Buser%5D=vanity%2Cemail%2Cfull_name'
authorization_code_auth_method = AuthorizationCodeAuthMethod.POST_BODY
def get_base_user_properties(self, info: dict[str, typing.Any], **kwargs) -> dict[str, typing.Any]:
50    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
51        return {
52            "username": info.get("data", {}).get("attributes", {}).get("vanity")
53            or info.get("data", {}).get("id"),
54            "email": info.get("data", {}).get("attributes", {}).get("email"),
55            "name": info.get("data", {}).get("attributes", {}).get("full_name"),
56        }

Get base user properties for enrollment/update