authentik.sources.oauth.types.twitter

Twitter OAuth Views

 1"""Twitter OAuth Views"""
 2
 3from typing import Any
 4
 5from authentik.sources.oauth.clients.oauth2 import (
 6    UserprofileHeaderAuthClient,
 7)
 8from authentik.sources.oauth.models import PKCEMethod
 9from authentik.sources.oauth.types.registry import SourceType, registry
10from authentik.sources.oauth.views.callback import OAuthCallback
11from authentik.sources.oauth.views.redirect import OAuthRedirect
12
13
14class TwitterOAuthRedirect(OAuthRedirect):
15    """Twitter OAuth2 Redirect"""
16
17    def get_additional_parameters(self, source):  # pragma: no cover
18        return {
19            "scope": ["users.read", "tweet.read"],
20        }
21
22
23class TwitterOAuthCallback(OAuthCallback):
24    """Twitter OAuth2 Callback"""
25
26    client_class = UserprofileHeaderAuthClient
27
28    def get_user_id(self, info: dict[str, str]) -> str:
29        return info.get("data", {}).get("id", "")
30
31
32@registry.register()
33class TwitterType(SourceType):
34    """Twitter Type definition"""
35
36    callback_view = TwitterOAuthCallback
37    redirect_view = TwitterOAuthRedirect
38    verbose_name = "Twitter"
39    name = "twitter"
40
41    authorization_url = "https://twitter.com/i/oauth2/authorize"
42    access_token_url = "https://api.twitter.com/2/oauth2/token"  # nosec
43    profile_url = "https://api.twitter.com/2/users/me"
44    pkce = PKCEMethod.S256
45
46    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
47        data = info.get("data", {})
48        return {
49            "username": data.get("username"),
50            "email": None,
51            "name": data.get("name"),
52        }
class TwitterOAuthRedirect(authentik.sources.oauth.views.redirect.OAuthRedirect):
15class TwitterOAuthRedirect(OAuthRedirect):
16    """Twitter OAuth2 Redirect"""
17
18    def get_additional_parameters(self, source):  # pragma: no cover
19        return {
20            "scope": ["users.read", "tweet.read"],
21        }

Twitter OAuth2 Redirect

def get_additional_parameters(self, source):
18    def get_additional_parameters(self, source):  # pragma: no cover
19        return {
20            "scope": ["users.read", "tweet.read"],
21        }

Return additional redirect parameters for this source.

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

Twitter OAuth2 Callback

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

Return unique identifier from the profile info.

@registry.register()
class TwitterType(authentik.sources.oauth.types.registry.SourceType):
33@registry.register()
34class TwitterType(SourceType):
35    """Twitter Type definition"""
36
37    callback_view = TwitterOAuthCallback
38    redirect_view = TwitterOAuthRedirect
39    verbose_name = "Twitter"
40    name = "twitter"
41
42    authorization_url = "https://twitter.com/i/oauth2/authorize"
43    access_token_url = "https://api.twitter.com/2/oauth2/token"  # nosec
44    profile_url = "https://api.twitter.com/2/users/me"
45    pkce = PKCEMethod.S256
46
47    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
48        data = info.get("data", {})
49        return {
50            "username": data.get("username"),
51            "email": None,
52            "name": data.get("name"),
53        }

Twitter Type definition

callback_view = <class 'TwitterOAuthCallback'>
redirect_view = <class 'TwitterOAuthRedirect'>
verbose_name = 'Twitter'
name = 'twitter'
authorization_url = 'https://twitter.com/i/oauth2/authorize'
access_token_url = 'https://apiauthentik.sources.oauth.types.twitter.com/2/oauth2/token'
profile_url = 'https://apiauthentik.sources.oauth.types.twitter.com/2/users/me'
pkce = PKCEMethod.S256
def get_base_user_properties(self, info: dict[str, typing.Any], **kwargs) -> dict[str, typing.Any]:
47    def get_base_user_properties(self, info: dict[str, Any], **kwargs) -> dict[str, Any]:
48        data = info.get("data", {})
49        return {
50            "username": data.get("username"),
51            "email": None,
52            "name": data.get("name"),
53        }

Get base user properties for enrollment/update