Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sources/oauth: add mastodon #13607

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions authentik/sources/oauth/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"authentik.sources.oauth.types.reddit",
"authentik.sources.oauth.types.twitch",
"authentik.sources.oauth.types.twitter",
"authentik.sources.oauth.types.mastodon",
]


Expand Down
10 changes: 10 additions & 0 deletions authentik/sources/oauth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ class Meta:
verbose_name = _("Okta OAuth Source")
verbose_name_plural = _("Okta OAuth Sources")

class MastodonOauthSource(OAuthSource):
"""Social Login using a specific mastodon instance."""

class Meta:

abstract = True
verbose_name = _("Mastodon OAuth Source")
verbose_name_plural = _("Mastodon OAuth Sources")



class RedditOAuthSource(CreatableType, OAuthSource):
"""Social Login using reddit.com."""
Expand Down
55 changes: 55 additions & 0 deletions authentik/sources/oauth/types/mastodon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""OpenID Connect OAuth Views"""
from typing import Any

from authentik.sources.oauth.clients.oauth2 import UserprofileHeaderAuthClient
from authentik.sources.oauth.models import OAuthSource
from authentik.sources.oauth.types.registry import SourceType, registry
from authentik.sources.oauth.views.callback import OAuthCallback
from authentik.sources.oauth.views.redirect import OAuthRedirect


class MastodonClient(OAuth2Client):
"""Mastodon OAuth2 Client"""

def get_access_token(self, **request_kwargs):
"Fetch access token from callback request."
auth = HTTPBasicAuth(self.source.consumer_key, self.source.consumer_secret)
return super().get_access_token(auth=auth)

class MastodonOAuthRedirect(OAuthRedirect):
"""Mastodon OAuth2 Redirect"""

def get_additional_parameters(self, source: OAuthSource): # pragma: no cover
return {
"scope": ["read"]
}


class MastodonOAuth2Callback(OAuthCallback):
"""Mastodon OAuth2 Callback"""

client_class = MastodonClient

def get_user_id(self, info: dict[str, str]) -> str:
return info.get("username", "")

def get_user_enroll_context(
self,
info: dict[str, Any],
) -> dict[str, Any]:
return {
"username": info.get("username"),
"name": info.get("display_name")
}


@registry.register()
class MastodonType(SourceType):
"""Mastodon Type definition"""

callback_view = MastodonOAuth2Callback
redirect_view = MastodonOAuthRedirect
name = "Mastodon"
slug = "Mastodon"

urls_customizable = True
1 change: 1 addition & 0 deletions web/authentik/sources/mastodon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions web/src/admin/sources/oauth/OAuthSourceViewPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export function ProviderToLabel(provider?: ProviderTypeEnum): string {
return "Patreon";
case ProviderTypeEnum.Reddit:
return "Reddit";
case ProviderTypeEnum.Mastodon:
return "Mastodon";
case ProviderTypeEnum.Twitter:
return "Twitter";
case ProviderTypeEnum.Twitch:
Expand Down
47 changes: 47 additions & 0 deletions website/sidebarsIntegrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,52 @@ module.exports = {
},
],
},
{
type: "category",
label: "Federation & Social login",
link: {
type: "generated-index",
title: "Sources",
slug: "sources",
description:
"Sources of users which can be federated with authentik",
},
items: [
{
type: "category",
label: "Directory syncronization",
items: [
"sources/active-directory/index",
"sources/freeipa/index",
],
},
"sources/general",
{
type: "category",
label: "Protocols",
items: [
"sources/ldap/index",
"sources/oauth/index",
"sources/saml/index",
],
},
{
type: "category",
label: "Social Logins",
items: [
"sources/apple/index",
"sources/azure-ad/index",
"sources/discord/index",
"sources/github/index",
"sources/google/index",
"sources/mailcow/index",
"sources/twitch/index",
"sources/plex/index",
"sources/twitter/index",
"sources/mastodon/index",
],
},
],
},
],
};
Loading