a07d753da8
* Update api document in admin/announcements * Update api document in announcements * Update api document in i/read-announcements * Update api document in username/available * Update api document & Fix typo in API 403 error * Update api document * Update api document * Update api document * Fix API permission definition * Update api document * Update api document * Update api document * Update api document * Update api document * Update api document * Update api document * Update api document * Fix bug in users (api) * Apply reviews #6757 * Apply reviews #6757 Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
73 lines
1.3 KiB
TypeScript
73 lines
1.3 KiB
TypeScript
import $ from 'cafy';
|
|
import define from '../../define';
|
|
import { AccessTokens } from '../../../../models';
|
|
import { genId } from '../../../../misc/gen-id';
|
|
import { secureRndstr } from '../../../../misc/secure-rndstr';
|
|
|
|
export const meta = {
|
|
tags: ['auth'],
|
|
|
|
requireCredential: true as const,
|
|
|
|
secure: true,
|
|
|
|
params: {
|
|
session: {
|
|
validator: $.nullable.str
|
|
},
|
|
|
|
name: {
|
|
validator: $.nullable.optional.str
|
|
},
|
|
|
|
description: {
|
|
validator: $.nullable.optional.str,
|
|
},
|
|
|
|
iconUrl: {
|
|
validator: $.nullable.optional.str,
|
|
},
|
|
|
|
permission: {
|
|
validator: $.arr($.str).unique(),
|
|
},
|
|
},
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
export default define(meta, async (ps, user) => {
|
|
// Generate access token
|
|
const accessToken = secureRndstr(32, true);
|
|
|
|
const now = new Date();
|
|
|
|
// Insert access token doc
|
|
await AccessTokens.save({
|
|
id: genId(),
|
|
createdAt: now,
|
|
lastUsedAt: now,
|
|
session: ps.session,
|
|
userId: user.id,
|
|
token: accessToken,
|
|
hash: accessToken,
|
|
name: ps.name,
|
|
description: ps.description,
|
|
iconUrl: ps.iconUrl,
|
|
permission: ps.permission,
|
|
});
|
|
|
|
return {
|
|
token: accessToken
|
|
};
|
|
});
|