oapi: oauth2

This commit is contained in:
Puyodead1 2023-03-24 21:43:18 -04:00
parent 3a40254ca5
commit 1b1fbce4d3
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
5 changed files with 3744 additions and 98 deletions

View File

@ -6134,6 +6134,17 @@
"stickers" "stickers"
] ]
}, },
"OAuthAuthorizeResponse": {
"type": "object",
"properties": {
"location": {
"type": "string"
}
},
"required": [
"location"
]
},
"TenorTrendingResponse": { "TenorTrendingResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -7762,10 +7773,56 @@
} }
}, },
"responses": { "responses": {
"default": { "200": {
"description": "No description available" "description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OAuthAuthorizeResponse"
}
}
}
},
"400": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/APIErrorResponse"
}
}
}
},
"403": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/APIErrorResponse"
}
}
}
},
"404": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/APIErrorResponse"
}
}
}
} }
}, },
"parameters": [
{
"name": "client_id",
"in": "query",
"schema": {
"type": "string"
}
}
],
"tags": [ "tags": [
"oauth2" "oauth2"
] ]

File diff suppressed because it is too large Load Diff

View File

@ -32,110 +32,152 @@ const router = Router();
// TODO: scopes, other oauth types // TODO: scopes, other oauth types
router.get("/", route({}), async (req: Request, res: Response) => { router.get(
// const { client_id, scope, response_type, redirect_url } = req.query; "/",
const { client_id } = req.query; route({
responses: {
const app = await Application.findOne({ // TODO: I really didn't feel like typing all of it out
where: { 200: {},
id: client_id as string, 400: {
}, body: "APIErrorResponse",
relations: ["bot"], },
}); 404: {
body: "APIErrorResponse",
// TODO: use DiscordApiErrors
// findOneOrFail throws code 404
if (!app) throw DiscordApiErrors.UNKNOWN_APPLICATION;
if (!app.bot) throw DiscordApiErrors.OAUTH2_APPLICATION_BOT_ABSENT;
const bot = app.bot;
delete app.bot;
const user = await User.findOneOrFail({
where: {
id: req.user_id,
bot: false,
},
select: ["id", "username", "avatar", "discriminator", "public_flags"],
});
const guilds = await Member.find({
where: {
user: {
id: req.user_id,
}, },
}, },
relations: ["guild", "roles"], }),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment async (req: Request, res: Response) => {
//@ts-ignore // const { client_id, scope, response_type, redirect_url } = req.query;
// prettier-ignore const { client_id } = req.query;
select: ["guild.id", "guild.name", "guild.icon", "guild.mfa_level", "guild.owner_id", "roles.id"],
});
const guildsWithPermissions = guilds.map((x) => { const app = await Application.findOne({
const perms = where: {
x.guild.owner_id === user.id id: client_id as string,
? new Permissions(Permissions.FLAGS.ADMINISTRATOR) },
: Permissions.finalPermission({ relations: ["bot"],
user: { });
id: user.id,
roles: x.roles?.map((x) => x.id) || [],
},
guild: {
roles: x?.roles || [],
},
});
return { // TODO: use DiscordApiErrors
id: x.guild.id, // findOneOrFail throws code 404
name: x.guild.name, if (!app) throw DiscordApiErrors.UNKNOWN_APPLICATION;
icon: x.guild.icon, if (!app.bot) throw DiscordApiErrors.OAUTH2_APPLICATION_BOT_ABSENT;
mfa_level: x.guild.mfa_level,
permissions: perms.bitfield.toString(),
};
});
return res.json({ const bot = app.bot;
guilds: guildsWithPermissions, delete app.bot;
user: {
id: user.id, const user = await User.findOneOrFail({
username: user.username, where: {
avatar: user.avatar, id: req.user_id,
avatar_decoration: null, // TODO bot: false,
discriminator: user.discriminator, },
public_flags: user.public_flags, select: [
}, "id",
application: { "username",
id: app.id, "avatar",
name: app.name, "discriminator",
icon: app.icon, "public_flags",
description: app.description, ],
summary: app.summary, });
type: app.type,
hook: app.hook, const guilds = await Member.find({
guild_id: null, // TODO support guilds where: {
bot_public: app.bot_public, user: {
bot_require_code_grant: app.bot_require_code_grant, id: req.user_id,
verify_key: app.verify_key, },
flags: app.flags, },
}, relations: ["guild", "roles"],
bot: { // eslint-disable-next-line @typescript-eslint/ban-ts-comment
id: bot.id, //@ts-ignore
username: bot.username, // prettier-ignore
avatar: bot.avatar, select: ["guild.id", "guild.name", "guild.icon", "guild.mfa_level", "guild.owner_id", "roles.id"],
avatar_decoration: null, // TODO });
discriminator: bot.discriminator,
public_flags: bot.public_flags, const guildsWithPermissions = guilds.map((x) => {
bot: true, const perms =
approximated_guild_count: 0, // TODO x.guild.owner_id === user.id
}, ? new Permissions(Permissions.FLAGS.ADMINISTRATOR)
authorized: false, : Permissions.finalPermission({
}); user: {
}); id: user.id,
roles: x.roles?.map((x) => x.id) || [],
},
guild: {
roles: x?.roles || [],
},
});
return {
id: x.guild.id,
name: x.guild.name,
icon: x.guild.icon,
mfa_level: x.guild.mfa_level,
permissions: perms.bitfield.toString(),
};
});
return res.json({
guilds: guildsWithPermissions,
user: {
id: user.id,
username: user.username,
avatar: user.avatar,
avatar_decoration: null, // TODO
discriminator: user.discriminator,
public_flags: user.public_flags,
},
application: {
id: app.id,
name: app.name,
icon: app.icon,
description: app.description,
summary: app.summary,
type: app.type,
hook: app.hook,
guild_id: null, // TODO support guilds
bot_public: app.bot_public,
bot_require_code_grant: app.bot_require_code_grant,
verify_key: app.verify_key,
flags: app.flags,
},
bot: {
id: bot.id,
username: bot.username,
avatar: bot.avatar,
avatar_decoration: null, // TODO
discriminator: bot.discriminator,
public_flags: bot.public_flags,
bot: true,
approximated_guild_count: 0, // TODO
},
authorized: false,
});
},
);
router.post( router.post(
"/", "/",
route({ requestBody: "ApplicationAuthorizeSchema" }), route({
requestBody: "ApplicationAuthorizeSchema",
query: {
client_id: {
type: "string",
},
},
responses: {
200: {
body: "OAuthAuthorizeResponse",
},
400: {
body: "APIErrorResponse",
},
403: {
body: "APIErrorResponse",
},
404: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const body = req.body as ApplicationAuthorizeSchema; const body = req.body as ApplicationAuthorizeSchema;
// const { client_id, scope, response_type, redirect_url } = req.query; // const { client_id, scope, response_type, redirect_url } = req.query;

View File

@ -0,0 +1,3 @@
export interface OAuthAuthorizeResponse {
location: string;
}

View File

@ -31,6 +31,7 @@ export * from "./GuildWidgetJsonResponse";
export * from "./GuildWidgetSettingsResponse"; export * from "./GuildWidgetSettingsResponse";
export * from "./LocationMetadataResponse"; export * from "./LocationMetadataResponse";
export * from "./MemberJoinGuildResponse"; export * from "./MemberJoinGuildResponse";
export * from "./OAuthAuthorizeResponse";
export * from "./Tenor"; export * from "./Tenor";
export * from "./TokenResponse"; export * from "./TokenResponse";
export * from "./UserProfileResponse"; export * from "./UserProfileResponse";