@nocobase/plugin-api-keys 2.1.0-alpha.14 → 2.1.0-alpha.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,9 +8,9 @@
8
8
  */
9
9
 
10
10
  module.exports = {
11
- "@nocobase/client": "2.1.0-alpha.14",
12
- "@nocobase/database": "2.1.0-alpha.14",
13
- "@nocobase/server": "2.1.0-alpha.14",
11
+ "@nocobase/client": "2.1.0-alpha.15",
12
+ "@nocobase/database": "2.1.0-alpha.15",
13
+ "@nocobase/server": "2.1.0-alpha.15",
14
14
  "@formily/react": "2.3.7",
15
15
  "ahooks": "3.7.8",
16
16
  "antd": "5.24.2",
@@ -18,5 +18,5 @@ module.exports = {
18
18
  "react": "18.2.0",
19
19
  "@formily/shared": "2.3.7",
20
20
  "react-i18next": "11.18.6",
21
- "@nocobase/actions": "2.1.0-alpha.14"
21
+ "@nocobase/actions": "2.1.0-alpha.15"
22
22
  };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import Application from '@nocobase/server';
10
+ export default function generateAPIKeyCommand(app: Application): void;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var generate_exports = {};
28
+ __export(generate_exports, {
29
+ default: () => generateAPIKeyCommand
30
+ });
31
+ module.exports = __toCommonJS(generate_exports);
32
+ function generateAPIKeyCommand(app) {
33
+ app.command("generate-api-key").preload().requiredOption("-n, --name <name>", "The name of the API key").requiredOption("-r, --role <roleName>", "The role of the API key").requiredOption("-u, --username <username>", "The username of the API key").option("-e, --expires-in [expiresIn]", "The expiration time of the API key", "30d").action(async (options) => {
34
+ const plugin = app.pm.get("api-keys");
35
+ const token = await plugin.generateAPIKey(options);
36
+ console.log("-----BEGIN API KEY-----");
37
+ console.log(token);
38
+ console.log("-----END API KEY-----");
39
+ });
40
+ }
@@ -9,6 +9,7 @@
9
9
  import { Plugin } from '@nocobase/server';
10
10
  export declare class PluginAPIKeysServer extends Plugin {
11
11
  resourceName: string;
12
+ generateAPIKey(values: any): Promise<string>;
12
13
  beforeLoad(): Promise<void>;
13
14
  load(): Promise<void>;
14
15
  }
@@ -34,6 +34,37 @@ var import_server = require("@nocobase/server");
34
34
  var import_api_keys = require("./actions/api-keys");
35
35
  class PluginAPIKeysServer extends import_server.Plugin {
36
36
  resourceName = "apiKeys";
37
+ async generateAPIKey(values) {
38
+ const { name, username, role: roleName, expiresIn } = values;
39
+ const user = await this.db.getRepository("users").findOne({
40
+ filter: {
41
+ username
42
+ }
43
+ });
44
+ if (!user) {
45
+ throw new Error("User not found");
46
+ }
47
+ const userId = user.id;
48
+ const repository = this.db.getRepository("users.roles", userId);
49
+ const role = await repository.findOne({
50
+ filter: {
51
+ name: roleName
52
+ }
53
+ });
54
+ if (!role) {
55
+ throw new Error("Role not found");
56
+ }
57
+ const token = this.app.authManager.jwt.sign({ userId, roleName }, { expiresIn });
58
+ await this.db.getRepository(this.resourceName).create({
59
+ values: {
60
+ name,
61
+ roleName,
62
+ token,
63
+ expiresIn
64
+ }
65
+ });
66
+ return token;
67
+ }
37
68
  async beforeLoad() {
38
69
  this.app.resourcer.define({
39
70
  name: this.resourceName,
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "description": "Allows users to use API key to access application's HTTP API",
7
7
  "description.ru-RU": "Позволяет пользователям использовать API ключ для доступа к HTTP API приложения",
8
8
  "description.zh-CN": "允许用户使用 API 密钥访问应用的 HTTP API",
9
- "version": "2.1.0-alpha.14",
9
+ "version": "2.1.0-alpha.15",
10
10
  "license": "Apache-2.0",
11
11
  "main": "./dist/server/index.js",
12
12
  "homepage": "https://docs.nocobase.com/handbook/api-keys",
@@ -35,5 +35,5 @@
35
35
  "@nocobase/test": "2.x",
36
36
  "@nocobase/utils": "2.x"
37
37
  },
38
- "gitHead": "d8735b541de0ff9557bba704de49c799b4962672"
38
+ "gitHead": "7c86e75b0af4b9f532c8ebf5ef96a7423b0ab60e"
39
39
  }