2019-08-18 12:42:58 +09:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2017-03-09 03:50:09 +09:00
|
|
|
import $ from 'cafy';
|
2021-08-19 21:55:45 +09: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 12:49:08 +09:00
|
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['auth'],
|
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2019-04-23 22:35:26 +09:00
|
|
|
|
2018-11-02 12:49:08 +09:00
|
|
|
params: {
|
|
|
|
|
appSecret: {
|
2019-02-24 12:40:17 +09:00
|
|
|
validator: $.str,
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
2019-02-25 04:18:09 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2019-02-25 04:18:09 +09:00
|
|
|
properties: {
|
|
|
|
|
token: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'string',
|
|
|
|
|
optional: false, nullable: false,
|
2019-02-25 04:18:09 +09:00
|
|
|
},
|
|
|
|
|
url: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'string',
|
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'url',
|
2019-02-25 04:18:09 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2019-02-25 04:18:09 +09:00
|
|
|
},
|
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
errors: {
|
|
|
|
|
noSuchApp: {
|
|
|
|
|
message: 'No such app.',
|
|
|
|
|
code: 'NO_SUCH_APP',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '92f93e63-428e-4f2f-a5a4-39e1407fe998',
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps) => {
|
2016-12-29 07:49:51 +09:00
|
|
|
// Lookup app
|
2019-04-07 21:50:36 +09:00
|
|
|
const app = await Apps.findOne({
|
2021-12-09 23:58:30 +09:00
|
|
|
secret: ps.appSecret,
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (app == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.noSuchApp);
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate token
|
2019-08-18 12:42:58 +09:00
|
|
|
const token = uuid();
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
|
// Create session token document
|
2022-01-03 02:20:30 +09:00
|
|
|
const doc = await AuthSessions.insert({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: genId(),
|
2018-03-29 14:48:47 +09:00
|
|
|
createdAt: new Date(),
|
2019-04-07 21:50:36 +09:00
|
|
|
appId: app.id,
|
2021-12-09 23:58:30 +09:00
|
|
|
token: token,
|
2022-01-03 02:20:30 +09:00
|
|
|
}).then(x => AuthSessions.findOneOrFail(x.identifiers[0]));
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
return {
|
2016-12-29 07:49:51 +09:00
|
|
|
token: doc.token,
|
2021-12-09 23:58:30 +09:00
|
|
|
url: `${config.authUrl}/${doc.token}`,
|
2019-02-22 11:46:58 +09:00
|
|
|
};
|
|
|
|
|
});
|