2019-08-18 05:42:58 +02:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2021-08-19 14:55:45 +02:00
|
|
|
import config from '@/config/index';
|
|
|
|
import define from '../../../define';
|
|
|
|
import { ApiError } from '../../../error';
|
|
|
|
import { Apps, AuthSessions } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2018-11-02 04:49:08 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['auth'],
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
|
2018-11-02 04:49:08 +01:00
|
|
|
params: {
|
|
|
|
appSecret: {
|
2019-02-24 04:40:17 +01:00
|
|
|
validator: $.str,
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
2019-02-24 20:18:09 +01:00
|
|
|
res: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-24 20:18:09 +01:00
|
|
|
properties: {
|
|
|
|
token: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-24 20:18:09 +01:00
|
|
|
},
|
|
|
|
url: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
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',
|
|
|
|
},
|
|
|
|
},
|
2018-11-02 04:49:08 +01:00
|
|
|
};
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps) => {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Lookup app
|
2019-04-07 14:50:36 +02:00
|
|
|
const app = await Apps.findOne({
|
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
|
2019-04-07 14:50:36 +02:00
|
|
|
const doc = await AuthSessions.save({
|
|
|
|
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,
|
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
|
|
|
};
|
|
|
|
});
|