2023-01-13 05:40:33 +01:00
|
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
import config from "@/config/index.js";
|
|
|
|
import define from "../../../define.js";
|
|
|
|
import { ApiError } from "../../../error.js";
|
|
|
|
import { Apps, AuthSessions } from "@/models/index.js";
|
|
|
|
import { genId } from "@/misc/gen-id.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,
|
2019-04-23 15:35:26 +02: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: {
|
|
|
|
token: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "string",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
2019-02-24 20:18:09 +01:00
|
|
|
},
|
|
|
|
url: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "string",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
|
|
|
format: "url",
|
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: "92f93e63-428e-4f2f-a5a4-39e1407fe998",
|
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" },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["appSecret"],
|
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) => {
|
2016-12-28 23:49:51 +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,
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (app == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchApp);
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate token
|
2019-08-18 05:42:58 +02:00
|
|
|
const token = uuid();
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Create session token document
|
2022-01-02 18:20:30 +01:00
|
|
|
const doc = await AuthSessions.insert({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: genId(),
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: new Date(),
|
2019-04-07 14:50:36 +02:00
|
|
|
appId: app.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
token: token,
|
2023-01-13 05:40:33 +01:00
|
|
|
}).then((x) => AuthSessions.findOneByOrFail(x.identifiers[0]));
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
return {
|
2016-12-28 23:49:51 +01:00
|
|
|
token: doc.token,
|
2021-12-09 15:58:30 +01:00
|
|
|
url: `${config.authUrl}/${doc.token}`,
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|
|
|
|
});
|