2019-08-18 05:42:58 +02:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2022-02-27 03:07:39 +01:00
|
|
|
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 = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['auth'],
|
|
|
|
|
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: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 20:18:09 +01:00
|
|
|
properties: {
|
|
|
|
token: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'string',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 20:18:09 +01:00
|
|
|
},
|
|
|
|
url: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'string',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
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: {
|
|
|
|
message: 'No such app.',
|
|
|
|
code: 'NO_SUCH_APP',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '92f93e63-428e-4f2f-a5a4-39e1407fe998',
|
|
|
|
},
|
|
|
|
},
|
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 = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
appSecret: { type: 'string' },
|
|
|
|
},
|
|
|
|
required: ['appSecret'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
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,
|
2022-03-26 07:34:00 +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
|
|
|
};
|
|
|
|
});
|