2020-03-28 03:24:37 +01:00
|
|
|
import $ from 'cafy';
|
2021-08-19 11:33:41 +02: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'],
|
|
|
|
|
|
|
|
requireCredential: true as const,
|
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
session: {
|
2020-07-18 05:12:10 +02:00
|
|
|
validator: $.nullable.str
|
2020-03-28 03:24:37 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
name: {
|
|
|
|
validator: $.nullable.optional.str
|
|
|
|
},
|
|
|
|
|
|
|
|
description: {
|
|
|
|
validator: $.nullable.optional.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
iconUrl: {
|
|
|
|
validator: $.nullable.optional.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
permission: {
|
|
|
|
validator: $.arr($.str).unique(),
|
|
|
|
},
|
|
|
|
},
|
2021-03-06 14:34:11 +01:00
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
properties: {
|
|
|
|
token: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 03:24:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
// 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 {
|
|
|
|
token: accessToken
|
|
|
|
};
|
2020-03-28 03:24:37 +01:00
|
|
|
});
|