Auth0に「Mastodonでログイン」を追加
Auth0がMastodon公式サポートを提供していない現状において、開発者がConnections APIを活用して任意のMastodonサーバーに対応する「ログインwith Mastodon」の実装手法を解説している。
キーポイント
Auth0におけるMastodonサポートの欠如
Auth0はFacebookやXなどの主要ソーシャルログインをサポートしているが、Mastodonには公式に対応しておらず、単一インスタンスのみの手動追加はFediverseの分散性に対して不十分である。
Connections APIを用いた汎用実装
Auth0のConnections APIを使用することで、特定のサーバーに限定せず、任意のMastodonインスタンスをOAuth2認証サーバーとして識別子プロバイダーに追加できる。
Mastodon APIを用いた動的アプリ登録
ユーザーのログイン時に、MastodonサーバーのAPI経由で動的にアプリケーションを登録し、読み取り専用のAPIキーを取得することで、分散型ネットワーク全体での認証を可能にする仕組みを実装している。
影響分析・編集コメントを表示
影響分析
本記事は、分散型SNSであるMastodonの普及に伴い、既存の認証基盤(Auth0など)との統合における技術的課題と解決策を示すものである。特に、単一のインスタンスに依存しない「Fediverse対応」の認証フローを構築する実装例は、Web開発者にとって参考になる実践的な知見であり、分散型アイデンティティの標準化に向けた一歩となる可能性がある。
編集コメント
分散型SNSの認証統合は技術的に複雑だが、この実装例はOpenID ConnectやOAuth2の応用として非常に参考になる。Auth0のような商用サービスが公式サポートを遅らせる場合、開発者のカスタム実装力が重要になることを示唆している。
私はOpenBenchesウェブサイトにソーシャルログインを提供するためにAuth0を使用しています。ユーザーアカウントの作成やパスワードの管理などを扱いたくないので、Auth0は私のニーズに最適です。Auth0が提供するソーシャルメディアログインの選択肢は幅広く、Facebook、Twitter、WordPress、Discordなどの定番サービスも含まれています。残念ながら、Mastodonのサポートはありません。しかし、すべてが失われたわけではありません…
原文を表示
I use Auth0 to provide social logins for the OpenBenches website. I don't want to deal with creating user accounts, managing passwords, or anything like that, so Auth0 is perfect for my needs.
There are a wide range of social media logins provided by Auth0 - including the usual suspects like Facebook, Twitter, WordPress, Discord, etc. Sadly, there's no support for Mastodon0.
All is not lost though. The Auth0 documentation says:
However, you can use Auth0’s Connections API to add any OAuth2 Authorization Server as an identity provider.
You can manually add a *single* Mastodon instance, but that doesn't work with the decentralised nature of the Fediverse. Instead, I've come up with a manual solution which works with *any* Mastodon server!
Background
Every Mastodon1 server is independent. I have an account on mastodon.social you have an account on whatever.chaos. They are separate servers, albeit running similar software. A generic authenticator needs to work with *all* these servers. There's no point only allowing log ins from a single server.
Fortuitously, Mastodon allows app developers to automatically create new apps. A few simple lines of code and you will have an API key suitable for *read-only* access to that server. You can read how to instantly create Mastodon API keys or you can steal my PHP code.
User Experience
The user clicks the sign-in button on OpenBenches. They're taken to the Auth0 social login screen:

The user clicks on Mastodon. This is where Auth0's involvement ends!
The user is asked to provide the URl of their instance:

In the background, my server contacts the Mastodon instance and creates a read-only API key.
The user is asked to sign in to Mastodon.

The user is asked to authorise read-only access.

The user is now signed in and OpenBenches can retrieve their name, avatar image, and other useful information. Hurrah!
Auth0
Once you have created a service to generate API keys, it will need to run on a publicly accessible web server. For example https://example.com/mastodon_login.
Here's what you need to do within your Auth0 tennant:
- Authentication → Social → Create Connection
- At the bottom, choose "Create Custom".
- Choose "Authentication" only.
- Give your connection a name. This will be visible to users.
- "Authorization URL" and "Token URL" have the same value - the URl of your service.
- "Client ID" is only visible to you.
- "Client Secret" any random password; it won't be used for anything.
- Leave everything else in the default state.
It should look something like this:

Click the "Create" button and you're (almost) done.
Auth0 Icon
You will need to add a custom icon to the social integration. Annoyingly, there's no way to do it through the web interface, so follow that guide to use the command line.
Done!
I'll admit, this isn't the most straightforward thing to implement. Auth0 could make this easier - but it would still rely on users knowing the URl of their home instance.
That said, the Mastodon API is a delight to work with and the read-only permissions reduce risk for all parties.
- Auth0 did blog about Mastodon a few years ago but never bothered implementing it! ↩︎
- I do mean Mastodon; not the wider Fediverse. This only works with sites which have implemented Mastodon's APIs. ↩︎
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み