2022-02-27 03:07:39 +01:00
|
|
|
import define from '../../define.js';
|
|
|
|
import { AccessTokens } from '@/models/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { secureRndstr } from '@/misc/secure-rndstr.js';
|
2020-03-28 03:24:37 +01:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['auth'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2020-03-28 03:24:37 +01:00
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
2021-03-06 14:34:11 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 14:34:11 +01:00
|
|
|
properties: {
|
|
|
|
token: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'string',
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2020-03-28 03:24:37 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
session: { type: 'string', nullable: true },
|
|
|
|
name: { type: 'string', nullable: true },
|
|
|
|
description: { type: 'string', nullable: true },
|
|
|
|
iconUrl: { type: 'string', nullable: true },
|
|
|
|
permission: { type: 'array', uniqueItems: true, items: {
|
|
|
|
type: 'string',
|
|
|
|
} },
|
|
|
|
},
|
|
|
|
required: ['session', 'permission'],
|
|
|
|
} 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, user) => {
|
2020-03-28 03:24:37 +01:00
|
|
|
// Generate access token
|
2020-03-29 16:16:36 +02:00
|
|
|
const accessToken = secureRndstr(32, true);
|
2020-03-28 03:24:37 +01:00
|
|
|
|
2020-08-04 16:09:48 +02:00
|
|
|
const now = new Date();
|
|
|
|
|
2020-03-28 03:24:37 +01:00
|
|
|
// Insert access token doc
|
2021-03-21 13:27:09 +01:00
|
|
|
await AccessTokens.insert({
|
2020-03-28 03:24:37 +01:00
|
|
|
id: genId(),
|
2020-08-04 16:09:48 +02:00
|
|
|
createdAt: now,
|
|
|
|
lastUsedAt: now,
|
2020-03-28 03:24:37 +01:00
|
|
|
session: ps.session,
|
|
|
|
userId: user.id,
|
|
|
|
token: accessToken,
|
|
|
|
hash: accessToken,
|
|
|
|
name: ps.name,
|
|
|
|
description: ps.description,
|
|
|
|
iconUrl: ps.iconUrl,
|
|
|
|
permission: ps.permission,
|
|
|
|
});
|
2020-07-18 05:12:10 +02:00
|
|
|
|
|
|
|
return {
|
2021-12-09 15:58:30 +01:00
|
|
|
token: accessToken,
|
2020-07-18 05:12:10 +02:00
|
|
|
};
|
2020-03-28 03:24:37 +01:00
|
|
|
});
|