Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
118 lines (112 loc) · 4.13 KB
/
Copy pathauth.ts
File metadata and controls
118 lines (112 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import NextAuth from "next-auth";
import type { OAuthConfig, OAuthUserConfig } from "next-auth/providers";
import { findUserByProvider, upsertUser } from "@/db/users";
/** Mastodon の verify_credentials が返すプロフィール(必要分のみ) */
interface MastodonProfile {
id: string;
username: string;
acct: string;
display_name: string;
avatar: string;
url: string;
}
/** コミュニティの Mastodon インスタンス向けカスタム OAuth プロバイダ */
function Mastodon(
options: OAuthUserConfig<MastodonProfile> & { instance: string },
): OAuthConfig<MastodonProfile> {
const instance = options.instance.replace(/\/$/, "");
return {
id: "mastodon",
// "mastodon" はOAuthプロトコル/内部ID。表示名はコミュニティでの呼称「PGrit」に合わせる。
name: "PGrit",
type: "oauth",
authorization: {
url: `${instance}/oauth/authorize`,
// read:accounts=プロフィール取得 / write:statuses=本人名義の投稿(自動トゥート)
params: { scope: "read:accounts write:statuses" },
},
token: `${instance}/oauth/token`,
userinfo: `${instance}/api/v1/accounts/verify_credentials`,
// state(暗号化JWE)はURLが長くなりMastodon側の認可処理が502になることがあるため、
// 短いcode_challengeで済むPKCEを使う(Mastodon 4.3+対応)。
checks: ["pkce"],
profile(profile) {
return {
id: profile.id,
name: profile.display_name || profile.username,
image: profile.avatar,
};
},
options,
};
}
/**
* ログインプロバイダを構築する。コミュニティの Mastodon(PGrit)のみを許可する。
* PGritアカウントの保有=メンバーであることを認証ゲートとして機能させ、
* 部外者のオープンサインアップを防ぐ(Google等の外部IdPは意図的に非対応)。
*/
function buildProviders() {
const providers = [];
const { MASTODON_INSTANCE, MASTODON_CLIENT_ID, MASTODON_CLIENT_SECRET } =
process.env;
if (MASTODON_INSTANCE && MASTODON_CLIENT_ID && MASTODON_CLIENT_SECRET) {
providers.push(
Mastodon({
instance: MASTODON_INSTANCE,
clientId: MASTODON_CLIENT_ID,
clientSecret: MASTODON_CLIENT_SECRET,
}),
);
}
return providers;
}
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: buildProviders(),
session: { strategy: "jwt" },
callbacks: {
// 除名(アーカイブ)済みユーザーの再ログインを拒否する。
async signIn({ account }) {
if (!account) return false;
const existing = await findUserByProvider(
account.provider,
account.providerAccountId,
);
if (existing?.archivedAt) return false;
return true;
},
// サインイン時にユーザーをDBへupsertし、DBのユーザーID・provider・acctをtokenに持たせる
async jwt({ token, user, account, profile }) {
if (account && user) {
const acct =
account.provider === "mastodon" && profile
? (profile as unknown as MastodonProfile).acct
: undefined;
token.provider = account.provider;
token.acct = acct;
const dbUser = await upsertUser({
provider: account.provider,
providerUid: account.providerAccountId,
displayName: user.name ?? "(no name)",
avatarUrl: user.image,
mastodonAcct: acct,
email: user.email,
// Mastodonログイン時のみアクセストークンを保存(自動トゥート用)
mastodonAccessToken:
account.provider === "mastodon"
? (account.access_token as string | undefined)
: undefined,
});
token.uid = dbUser.id;
token.role = dbUser.role;
}
return token;
},
session({ session, token }) {
session.user.id = token.uid as string;
session.user.role = token.role as string | undefined;
session.user.provider = token.provider as string | undefined;
session.user.acct = token.acct as string | undefined;
return session;
},
},
});