authentik.sources.oauth.types.twitch

Twitch OAuth Views

 1"""Twitch OAuth Views"""
 2
 3from json import dumps
 4from typing import Any
 5
 6from authentik.sources.oauth.clients.oauth2 import UserprofileHeaderAuthClient
 7from authentik.sources.oauth.models import AuthorizationCodeAuthMethod
 8from authentik.sources.oauth.types.oidc import OpenIDConnectOAuth2Callback
 9from authentik.sources.oauth.types.registry import SourceType, registry
10from authentik.sources.oauth.views.redirect import OAuthRedirect
11
12
13class TwitchClient(UserprofileHeaderAuthClient):
14    """Twitch needs the token_type to be capitalized for the request header."""
15
16    def get_profile_info(self, token: dict[str, str]) -> dict[str, Any] | None:
17        token["token_type"] = token["token_type"].capitalize()
18        return super().get_profile_info(token)
19
20
21class TwitchOAuthRedirect(OAuthRedirect):
22    """Twitch OAuth2 Redirect"""
23
24    def get_additional_parameters(self, source):  # pragma: no cover
25        claims = {"userinfo": {"email": None, "preferred_username": None}}
26        return {
27            "scope": ["openid"],
28            "claims": dumps(claims),
29        }
30
31
32class TwitchOAuth2Callback(OpenIDConnectOAuth2Callback):
33    """Twitch OAuth2 Callback"""
34
35    client_class = TwitchClient
36
37
38@registry.register()
39class TwitchType(SourceType):
40    """Twitch Type definition"""
41
42    callback_view = TwitchOAuth2Callback
43    redirect_view = TwitchOAuthRedirect
44    verbose_name = "Twitch"
45    name = "twitch"
46
47    authorization_url = "https://id.twitch.tv/oauth2/authorize"
48    access_token_url = "https://id.twitch.tv/oauth2/token"  # nosec
49    profile_url = "https://id.twitch.tv/oauth2/userinfo"
50
51    authorization_code_auth_method = AuthorizationCodeAuthMethod.POST_BODY
52
53    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
54        return {
55            "username": info.get("preferred_username"),
56            "email": info.get("email"),
57            "name": info.get("preferred_username"),
58        }
14class TwitchClient(UserprofileHeaderAuthClient):
15    """Twitch needs the token_type to be capitalized for the request header."""
16
17    def get_profile_info(self, token: dict[str, str]) -> dict[str, Any] | None:
18        token["token_type"] = token["token_type"].capitalize()
19        return super().get_profile_info(token)

Twitch needs the token_type to be capitalized for the request header.

def get_profile_info(self, token: dict[str, str]) -> dict[str, Any] | None:
17    def get_profile_info(self, token: dict[str, str]) -> dict[str, Any] | None:
18        token["token_type"] = token["token_type"].capitalize()
19        return super().get_profile_info(token)

Fetch user profile information.

class TwitchOAuthRedirect(authentik.sources.oauth.views.redirect.OAuthRedirect):
22class TwitchOAuthRedirect(OAuthRedirect):
23    """Twitch OAuth2 Redirect"""
24
25    def get_additional_parameters(self, source):  # pragma: no cover
26        claims = {"userinfo": {"email": None, "preferred_username": None}}
27        return {
28            "scope": ["openid"],
29            "claims": dumps(claims),
30        }

Twitch OAuth2 Redirect

def get_additional_parameters(self, source):
25    def get_additional_parameters(self, source):  # pragma: no cover
26        claims = {"userinfo": {"email": None, "preferred_username": None}}
27        return {
28            "scope": ["openid"],
29            "claims": dumps(claims),
30        }

Return additional redirect parameters for this source.

class TwitchOAuth2Callback(authentik.sources.oauth.types.oidc.OpenIDConnectOAuth2Callback):
33class TwitchOAuth2Callback(OpenIDConnectOAuth2Callback):
34    """Twitch OAuth2 Callback"""
35
36    client_class = TwitchClient

Twitch OAuth2 Callback

client_class = <class 'TwitchClient'>
@registry.register()
class TwitchType(authentik.sources.oauth.types.registry.SourceType):
39@registry.register()
40class TwitchType(SourceType):
41    """Twitch Type definition"""
42
43    callback_view = TwitchOAuth2Callback
44    redirect_view = TwitchOAuthRedirect
45    verbose_name = "Twitch"
46    name = "twitch"
47
48    authorization_url = "https://id.twitch.tv/oauth2/authorize"
49    access_token_url = "https://id.twitch.tv/oauth2/token"  # nosec
50    profile_url = "https://id.twitch.tv/oauth2/userinfo"
51
52    authorization_code_auth_method = AuthorizationCodeAuthMethod.POST_BODY
53
54    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
55        return {
56            "username": info.get("preferred_username"),
57            "email": info.get("email"),
58            "name": info.get("preferred_username"),
59        }

Twitch Type definition

callback_view = <class 'TwitchOAuth2Callback'>
redirect_view = <class 'TwitchOAuthRedirect'>
verbose_name = 'Twitch'
name = 'twitch'
authorization_url = 'https://idauthentik.sources.oauth.types.twitch.tv/oauth2/authorize'
access_token_url = 'https://idauthentik.sources.oauth.types.twitch.tv/oauth2/token'
profile_url = 'https://idauthentik.sources.oauth.types.twitch.tv/oauth2/userinfo'
authorization_code_auth_method = AuthorizationCodeAuthMethod.POST_BODY
def get_base_user_properties(self, info: dict[str, typing.Any], **kwargs) -> dict[str, typing.Any]:
54    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
55        return {
56            "username": info.get("preferred_username"),
57            "email": info.get("email"),
58            "name": info.get("preferred_username"),
59        }

Get base user properties for enrollment/update