2020-03-28 04:24:37 +02:00
|
|
|
import $ from 'cafy';
|
|
|
|
import define from '../../define';
|
|
|
|
import { AccessTokens } from '../../../../models';
|
|
|
|
import { genId } from '../../../../misc/gen-id';
|
2020-03-29 17:16:36 +03:00
|
|
|
import { secureRndstr } from '../../../../misc/secure-rndstr';
|
2020-03-28 04:24:37 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['auth'],
|
|
|
|
|
|
|
|
requireCredential: true as const,
|
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
session: {
|
2020-07-18 06:12:10 +03:00
|
|
|
validator: $.nullable.str
|
2020-03-28 04:24:37 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
name: {
|
|
|
|
validator: $.nullable.optional.str
|
|
|
|
},
|
|
|
|
|
|
|
|
description: {
|
|
|
|
validator: $.nullable.optional.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
iconUrl: {
|
|
|
|
validator: $.nullable.optional.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
permission: {
|
|
|
|
validator: $.arr($.str).unique(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
// Generate access token
|
2020-03-29 17:16:36 +03:00
|
|
|
const accessToken = secureRndstr(32, true);
|
2020-03-28 04:24:37 +02:00
|
|
|
|
|
|
|
// Insert access token doc
|
|
|
|
await AccessTokens.save({
|
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
2020-03-28 12:44:57 +02:00
|
|
|
lastUsedAt: new Date(),
|
2020-03-28 04:24:37 +02: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 06:12:10 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
token: accessToken
|
|
|
|
};
|
2020-03-28 04:24:37 +02:00
|
|
|
});
|