2022-02-27 02:07:39 +00:00
|
|
|
import Koa from 'koa';
|
|
|
|
import Router from '@koa/router';
|
2018-11-15 10:15:04 +00:00
|
|
|
import { OAuth2 } from 'oauth';
|
2022-04-17 08:30:27 +00:00
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
import { IsNull } from 'typeorm';
|
|
|
|
import { getJson } from '@/misc/fetch.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { publishMainStream } from '@/services/stream.js';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import { Users, UserProfiles } from '@/models/index.js';
|
|
|
|
import { ILocalUser } from '@/models/entities/user.js';
|
2022-08-03 11:18:33 +00:00
|
|
|
import { redisClient } from '@/db/redis.js';
|
2022-04-17 08:30:27 +00:00
|
|
|
import signin from '../common/signin.js';
|
2018-11-15 10:15:04 +00:00
|
|
|
|
2022-02-03 08:12:51 +00:00
|
|
|
function getUserToken(ctx: Koa.BaseContext): string | null {
|
2020-03-20 04:56:22 +00:00
|
|
|
return ((ctx.headers['cookie'] || '').match(/igi=(\w+)/) || [null, null])[1];
|
2018-11-15 10:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:12:51 +00:00
|
|
|
function compareOrigin(ctx: Koa.BaseContext): boolean {
|
|
|
|
function normalizeUrl(url?: string): string {
|
2018-11-15 10:15:04 +00:00
|
|
|
return url ? url.endsWith('/') ? url.substr(0, url.length - 1) : url : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const referer = ctx.headers['referer'];
|
|
|
|
|
2022-02-03 08:12:51 +00:00
|
|
|
return (normalizeUrl(referer) === normalizeUrl(config.url));
|
2018-11-15 10:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init router
|
|
|
|
const router = new Router();
|
|
|
|
|
|
|
|
router.get('/disconnect/discord', async ctx => {
|
|
|
|
if (!compareOrigin(ctx)) {
|
|
|
|
ctx.throw(400, 'invalid origin');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const userToken = getUserToken(ctx);
|
|
|
|
if (!userToken) {
|
|
|
|
ctx.throw(400, 'signin required');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const user = await Users.findOneByOrFail({
|
|
|
|
host: IsNull(),
|
2021-12-09 14:58:30 +00:00
|
|
|
token: userToken,
|
2021-02-13 06:33:38 +00:00
|
|
|
});
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
|
2020-01-31 22:16:52 +00:00
|
|
|
|
|
|
|
delete profile.integrations.discord;
|
|
|
|
|
|
|
|
await UserProfiles.update(user.id, {
|
|
|
|
integrations: profile.integrations,
|
2018-11-15 10:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-04-17 08:30:27 +00:00
|
|
|
ctx.body = 'Discordの連携を解除しました :v:';
|
2018-11-15 10:15:04 +00:00
|
|
|
|
|
|
|
// Publish i updated event
|
2019-04-07 12:50:36 +00:00
|
|
|
publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, {
|
2018-11-15 10:15:04 +00:00
|
|
|
detail: true,
|
2021-12-09 14:58:30 +00:00
|
|
|
includeSecrets: true,
|
2018-11-15 10:15:04 +00:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
async function getOAuth2() {
|
2019-04-23 23:11:19 +00:00
|
|
|
const meta = await fetchMeta(true);
|
2018-11-15 10:15:04 +00:00
|
|
|
|
|
|
|
if (meta.enableDiscordIntegration) {
|
|
|
|
return new OAuth2(
|
2019-04-12 16:43:22 +00:00
|
|
|
meta.discordClientId!,
|
|
|
|
meta.discordClientSecret!,
|
2021-02-06 02:44:46 +00:00
|
|
|
'https://discord.com/',
|
2018-11-15 10:15:04 +00:00
|
|
|
'api/oauth2/authorize',
|
|
|
|
'api/oauth2/token');
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
router.get('/connect/discord', async ctx => {
|
|
|
|
if (!compareOrigin(ctx)) {
|
|
|
|
ctx.throw(400, 'invalid origin');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const userToken = getUserToken(ctx);
|
|
|
|
if (!userToken) {
|
|
|
|
ctx.throw(400, 'signin required');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
redirect_uri: `${config.url}/api/dc/cb`,
|
|
|
|
scope: ['identify'],
|
|
|
|
state: uuid(),
|
2021-12-09 14:58:30 +00:00
|
|
|
response_type: 'code',
|
2018-11-15 10:15:04 +00:00
|
|
|
};
|
|
|
|
|
2021-03-23 05:54:09 +00:00
|
|
|
redisClient.set(userToken, JSON.stringify(params));
|
2018-11-15 10:15:04 +00:00
|
|
|
|
|
|
|
const oauth2 = await getOAuth2();
|
2019-04-12 16:43:22 +00:00
|
|
|
ctx.redirect(oauth2!.getAuthorizeUrl(params));
|
2018-11-15 10:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/signin/discord', async ctx => {
|
|
|
|
const sessid = uuid();
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
redirect_uri: `${config.url}/api/dc/cb`,
|
|
|
|
scope: ['identify'],
|
|
|
|
state: uuid(),
|
2021-12-09 14:58:30 +00:00
|
|
|
response_type: 'code',
|
2018-11-15 10:15:04 +00:00
|
|
|
};
|
|
|
|
|
2020-03-20 04:56:22 +00:00
|
|
|
ctx.cookies.set('signin_with_discord_sid', sessid, {
|
2018-11-15 10:15:04 +00:00
|
|
|
path: '/',
|
|
|
|
secure: config.url.startsWith('https'),
|
2021-12-09 14:58:30 +00:00
|
|
|
httpOnly: true,
|
2018-11-15 10:15:04 +00:00
|
|
|
});
|
|
|
|
|
2021-03-23 05:54:09 +00:00
|
|
|
redisClient.set(sessid, JSON.stringify(params));
|
2018-11-15 10:15:04 +00:00
|
|
|
|
|
|
|
const oauth2 = await getOAuth2();
|
2019-04-12 16:43:22 +00:00
|
|
|
ctx.redirect(oauth2!.getAuthorizeUrl(params));
|
2018-11-15 10:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/dc/cb', async ctx => {
|
|
|
|
const userToken = getUserToken(ctx);
|
|
|
|
|
|
|
|
const oauth2 = await getOAuth2();
|
|
|
|
|
|
|
|
if (!userToken) {
|
2020-03-20 04:56:22 +00:00
|
|
|
const sessid = ctx.cookies.get('signin_with_discord_sid');
|
2018-11-15 10:15:04 +00:00
|
|
|
|
|
|
|
if (!sessid) {
|
|
|
|
ctx.throw(400, 'invalid session');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const code = ctx.query.code;
|
|
|
|
|
2022-04-17 08:30:27 +00:00
|
|
|
if (!code || typeof code !== 'string') {
|
2018-11-15 10:15:04 +00:00
|
|
|
ctx.throw(400, 'invalid session');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { redirect_uri, state } = await new Promise<any>((res, rej) => {
|
2021-03-23 05:54:09 +00:00
|
|
|
redisClient.get(sessid, async (_, state) => {
|
2018-11-15 10:15:04 +00:00
|
|
|
res(JSON.parse(state));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ctx.query.state !== state) {
|
|
|
|
ctx.throw(400, 'invalid session');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { accessToken, refreshToken, expiresDate } = await new Promise<any>((res, rej) =>
|
2019-04-12 16:43:22 +00:00
|
|
|
oauth2!.getOAuthAccessToken(code, {
|
|
|
|
grant_type: 'authorization_code',
|
2021-12-09 14:58:30 +00:00
|
|
|
redirect_uri,
|
2019-04-12 16:43:22 +00:00
|
|
|
}, (err, accessToken, refreshToken, result) => {
|
|
|
|
if (err) {
|
|
|
|
rej(err);
|
|
|
|
} else if (result.error) {
|
|
|
|
rej(result.error);
|
|
|
|
} else {
|
2018-11-15 10:15:04 +00:00
|
|
|
res({
|
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
2021-12-09 14:58:30 +00:00
|
|
|
expiresDate: Date.now() + Number(result.expires_in) * 1000,
|
2018-11-15 10:15:04 +00:00
|
|
|
});
|
2019-04-12 16:43:22 +00:00
|
|
|
}
|
|
|
|
}));
|
2018-11-15 10:15:04 +00:00
|
|
|
|
2022-04-17 08:30:27 +00:00
|
|
|
const { id, username, discriminator } = (await getJson('https://discord.com/api/users/@me', '*/*', 10 * 1000, {
|
2020-04-09 14:42:23 +00:00
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
2022-04-17 08:30:27 +00:00
|
|
|
})) as Record<string, unknown>;
|
2018-11-15 10:15:04 +00:00
|
|
|
|
2022-04-17 08:30:27 +00:00
|
|
|
if (typeof id !== 'string' || typeof username !== 'string' || typeof discriminator !== 'string') {
|
2018-11-15 10:15:04 +00:00
|
|
|
ctx.throw(400, 'invalid session');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-10 06:04:27 +00:00
|
|
|
const profile = await UserProfiles.createQueryBuilder()
|
2022-08-03 12:49:55 +00:00
|
|
|
.where('"integrations"->\'discord\'->>\'id\' = :id', { id })
|
2019-05-03 09:38:19 +00:00
|
|
|
.andWhere('"userHost" IS NULL')
|
2019-04-07 12:50:36 +00:00
|
|
|
.getOne();
|
2018-11-15 10:15:04 +00:00
|
|
|
|
2019-04-10 06:04:27 +00:00
|
|
|
if (profile == null) {
|
2022-09-11 04:20:43 +00:00
|
|
|
ctx.throw(404, `There were no FoundKey accounts linked to @${username}#${discriminator}...`);
|
2018-11-15 10:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-31 22:16:52 +00:00
|
|
|
await UserProfiles.update(profile.userId, {
|
|
|
|
integrations: {
|
|
|
|
...profile.integrations,
|
|
|
|
discord: {
|
2022-08-03 12:49:55 +00:00
|
|
|
id,
|
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
|
|
|
expiresDate,
|
|
|
|
username,
|
|
|
|
discriminator,
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2020-01-31 22:16:52 +00:00
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
2018-11-15 10:15:04 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
signin(ctx, await Users.findOneBy({ id: profile.userId }) as ILocalUser, true);
|
2018-11-15 10:15:04 +00:00
|
|
|
} else {
|
|
|
|
const code = ctx.query.code;
|
|
|
|
|
2022-04-17 08:30:27 +00:00
|
|
|
if (!code || typeof code !== 'string') {
|
2018-11-15 10:15:04 +00:00
|
|
|
ctx.throw(400, 'invalid session');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { redirect_uri, state } = await new Promise<any>((res, rej) => {
|
2021-03-23 05:54:09 +00:00
|
|
|
redisClient.get(userToken, async (_, state) => {
|
2018-11-15 10:15:04 +00:00
|
|
|
res(JSON.parse(state));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ctx.query.state !== state) {
|
|
|
|
ctx.throw(400, 'invalid session');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { accessToken, refreshToken, expiresDate } = await new Promise<any>((res, rej) =>
|
2019-04-12 16:43:22 +00:00
|
|
|
oauth2!.getOAuthAccessToken(code, {
|
|
|
|
grant_type: 'authorization_code',
|
2021-12-09 14:58:30 +00:00
|
|
|
redirect_uri,
|
2019-04-12 16:43:22 +00:00
|
|
|
}, (err, accessToken, refreshToken, result) => {
|
|
|
|
if (err) {
|
|
|
|
rej(err);
|
|
|
|
} else if (result.error) {
|
|
|
|
rej(result.error);
|
|
|
|
} else {
|
|
|
|
res({
|
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
2021-12-09 14:58:30 +00:00
|
|
|
expiresDate: Date.now() + Number(result.expires_in) * 1000,
|
2019-04-12 16:43:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}));
|
2018-11-15 10:15:04 +00:00
|
|
|
|
2022-04-17 08:30:27 +00:00
|
|
|
const { id, username, discriminator } = (await getJson('https://discord.com/api/users/@me', '*/*', 10 * 1000, {
|
2020-04-09 14:42:23 +00:00
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
2022-04-17 08:30:27 +00:00
|
|
|
})) as Record<string, unknown>;
|
|
|
|
if (typeof id !== 'string' || typeof username !== 'string' || typeof discriminator !== 'string') {
|
2018-11-15 10:15:04 +00:00
|
|
|
ctx.throw(400, 'invalid session');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const user = await Users.findOneByOrFail({
|
|
|
|
host: IsNull(),
|
2021-12-09 14:58:30 +00:00
|
|
|
token: userToken,
|
2021-02-13 06:33:38 +00:00
|
|
|
});
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
|
2020-01-31 22:16:52 +00:00
|
|
|
|
|
|
|
await UserProfiles.update(user.id, {
|
|
|
|
integrations: {
|
|
|
|
...profile.integrations,
|
|
|
|
discord: {
|
2022-08-03 12:49:55 +00:00
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
|
|
|
expiresDate,
|
|
|
|
id,
|
|
|
|
username,
|
|
|
|
discriminator,
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
|
|
|
},
|
2018-11-15 10:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-09-11 04:20:43 +00:00
|
|
|
ctx.body = `Discord: @${username}#${discriminator} connected to FoundKey: @${user.username}!`;
|
2018-11-15 10:15:04 +00:00
|
|
|
|
|
|
|
// Publish i updated event
|
2019-04-07 12:50:36 +00:00
|
|
|
publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, {
|
2018-11-15 10:15:04 +00:00
|
|
|
detail: true,
|
2021-12-09 14:58:30 +00:00
|
|
|
includeSecrets: true,
|
2018-11-15 10:15:04 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-31 14:32:58 +00:00
|
|
|
export default router;
|