summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/miauth
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
commit0e4a111f81cceed275d9bec2695f6e401fb654d8 (patch)
tree40874799472fa07416f17b50a398ac33b7771905 /packages/backend/src/server/api/endpoints/miauth
parentupdate deps (diff)
downloadsharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.gz
sharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.bz2
sharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.zip
refactoring
Resolve #7779
Diffstat (limited to 'packages/backend/src/server/api/endpoints/miauth')
-rw-r--r--packages/backend/src/server/api/endpoints/miauth/gen-token.ts72
1 files changed, 72 insertions, 0 deletions
diff --git a/packages/backend/src/server/api/endpoints/miauth/gen-token.ts b/packages/backend/src/server/api/endpoints/miauth/gen-token.ts
new file mode 100644
index 0000000000..321fa42fc9
--- /dev/null
+++ b/packages/backend/src/server/api/endpoints/miauth/gen-token.ts
@@ -0,0 +1,72 @@
+import $ from 'cafy';
+import define from '../../define';
+import { AccessTokens } from '@/models/index';
+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.insert({
+ 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
+ };
+});