2023-01-13 05:40:33 +01:00
|
|
|
import define from "../../../define.js";
|
|
|
|
import { ApiError } from "../../../error.js";
|
|
|
|
import { Apps, AuthSessions, AccessTokens, Users } from "@/models/index.js";
|
2018-11-02 04:49:08 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2023-01-13 05:40:33 +01:00
|
|
|
tags: ["auth"],
|
2019-02-23 03:20:58 +01:00
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2018-11-02 04:49:08 +01:00
|
|
|
|
2019-02-24 20:18:09 +01:00
|
|
|
res: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
2019-02-24 20:18:09 +01:00
|
|
|
properties: {
|
|
|
|
accessToken: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "string",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
2019-02-24 20:18:09 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
user: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
|
|
|
ref: "UserDetailedNotMe",
|
2019-02-24 20:18:09 +01:00
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-24 20:18:09 +01:00
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
noSuchApp: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "No such app.",
|
|
|
|
code: "NO_SUCH_APP",
|
|
|
|
id: "fcab192a-2c5a-43b7-8ad8-9b7054d8d40d",
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchSession: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "No such session.",
|
|
|
|
code: "NO_SUCH_SESSION",
|
|
|
|
id: "5b5a1503-8bc8-4bd0-8054-dc189e8cdcb3",
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
pendingSession: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "This session is not completed yet.",
|
|
|
|
code: "PENDING_SESSION",
|
|
|
|
id: "8c8a4145-02cc-4cca-8e66-29ba60445a8e",
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
2022-02-19 06:05:32 +01:00
|
|
|
properties: {
|
2023-01-13 05:40:33 +01:00
|
|
|
appSecret: { type: "string" },
|
|
|
|
token: { type: "string" },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["appSecret", "token"],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
2023-01-13 05:54:33 +01:00
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
export default define(meta, paramDef, async (ps) => {
|
2017-03-03 20:28:38 +01:00
|
|
|
// Lookup app
|
2022-03-26 07:34:00 +01:00
|
|
|
const app = await Apps.findOneBy({
|
2021-12-09 15:58:30 +01:00
|
|
|
secret: ps.appSecret,
|
2017-03-03 20:28:38 +01:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
if (app == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchApp);
|
2017-03-03 20:28:38 +01:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Fetch token
|
2022-03-26 07:34:00 +01:00
|
|
|
const session = await AuthSessions.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
token: ps.token,
|
2021-12-09 15:58:30 +01:00
|
|
|
appId: app.id,
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (session == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchSession);
|
2017-03-03 20:28:38 +01:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (session.userId == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.pendingSession);
|
2017-03-03 20:28:38 +01:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Lookup access token
|
2022-03-26 07:34:00 +01:00
|
|
|
const accessToken = await AccessTokens.findOneByOrFail({
|
2019-04-07 14:50:36 +02:00
|
|
|
appId: app.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
userId: session.userId,
|
2021-02-13 07:33:38 +01:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Delete session
|
2019-04-07 14:50:36 +02:00
|
|
|
AuthSessions.delete(session.id);
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
return {
|
2018-03-29 07:48:47 +02:00
|
|
|
accessToken: accessToken.token,
|
2019-04-07 14:50:36 +02:00
|
|
|
user: await Users.pack(session.userId, null, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
|
|
|
}),
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|
|
|
|
});
|