@fonoster/identity 0.6.1-alpha.0

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.
Files changed (147) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/dist/JsonWebErrorEnum.d.ts +5 -0
  4. package/dist/JsonWebErrorEnum.js +26 -0
  5. package/dist/apikeys/ApiRoleEnum.d.ts +4 -0
  6. package/dist/apikeys/ApiRoleEnum.js +25 -0
  7. package/dist/apikeys/createApiKey.d.ts +24 -0
  8. package/dist/apikeys/createApiKey.js +73 -0
  9. package/dist/apikeys/deleteApiKey.d.ts +18 -0
  10. package/dist/apikeys/deleteApiKey.js +58 -0
  11. package/dist/apikeys/index.d.ts +5 -0
  12. package/dist/apikeys/index.js +39 -0
  13. package/dist/apikeys/listApiKeys.d.ts +23 -0
  14. package/dist/apikeys/listApiKeys.js +50 -0
  15. package/dist/apikeys/regenerateApiKey.d.ts +20 -0
  16. package/dist/apikeys/regenerateApiKey.js +64 -0
  17. package/dist/createAuthInterceptor.d.ts +15 -0
  18. package/dist/createAuthInterceptor.js +75 -0
  19. package/dist/db.d.ts +14 -0
  20. package/dist/db.js +31 -0
  21. package/dist/envs.d.ts +1 -0
  22. package/dist/envs.js +24 -0
  23. package/dist/errors.d.ts +4 -0
  24. package/dist/errors.js +35 -0
  25. package/dist/exchanges/TokenUseEnum.d.ts +6 -0
  26. package/dist/exchanges/TokenUseEnum.js +27 -0
  27. package/dist/exchanges/exchangeApiKey.d.ts +24 -0
  28. package/dist/exchanges/exchangeApiKey.js +86 -0
  29. package/dist/exchanges/exchangeCredentials.d.ts +24 -0
  30. package/dist/exchanges/exchangeCredentials.js +86 -0
  31. package/dist/exchanges/exchangeRefreshToken.d.ts +21 -0
  32. package/dist/exchanges/exchangeRefreshToken.js +62 -0
  33. package/dist/exchanges/exchangeTokens.d.ts +8 -0
  34. package/dist/exchanges/exchangeTokens.js +92 -0
  35. package/dist/exchanges/index.d.ts +5 -0
  36. package/dist/exchanges/index.js +39 -0
  37. package/dist/exchanges/payloads/apikeys/getAccessTokenPayload.d.ts +4 -0
  38. package/dist/exchanges/payloads/apikeys/getAccessTokenPayload.js +45 -0
  39. package/dist/exchanges/payloads/apikeys/getRefreshTokenPayload.d.ts +4 -0
  40. package/dist/exchanges/payloads/apikeys/getRefreshTokenPayload.js +32 -0
  41. package/dist/exchanges/payloads/apikeys/index.d.ts +2 -0
  42. package/dist/exchanges/payloads/apikeys/index.js +36 -0
  43. package/dist/exchanges/payloads/buildRefreshTokenPayload.d.ts +7 -0
  44. package/dist/exchanges/payloads/buildRefreshTokenPayload.js +34 -0
  45. package/dist/exchanges/payloads/users/getAccessTokenPayload.d.ts +4 -0
  46. package/dist/exchanges/payloads/users/getAccessTokenPayload.js +55 -0
  47. package/dist/exchanges/payloads/users/getIdTokenPayload.d.ts +4 -0
  48. package/dist/exchanges/payloads/users/getIdTokenPayload.js +39 -0
  49. package/dist/exchanges/payloads/users/getRefreshTokenPayload.d.ts +4 -0
  50. package/dist/exchanges/payloads/users/getRefreshTokenPayload.js +35 -0
  51. package/dist/exchanges/payloads/users/index.d.ts +3 -0
  52. package/dist/exchanges/payloads/users/index.js +37 -0
  53. package/dist/exchanges/types.d.ts +56 -0
  54. package/dist/exchanges/types.js +2 -0
  55. package/dist/getPublicKey.d.ts +6 -0
  56. package/dist/getPublicKey.js +21 -0
  57. package/dist/getPublicKeyClient.d.ts +5 -0
  58. package/dist/getPublicKeyClient.js +62 -0
  59. package/dist/index.d.ts +10 -0
  60. package/dist/index.js +44 -0
  61. package/dist/invites/EmailTemplatesEnum.d.ts +5 -0
  62. package/dist/invites/EmailTemplatesEnum.js +26 -0
  63. package/dist/invites/createInviteBody.d.ts +8 -0
  64. package/dist/invites/createInviteBody.js +44 -0
  65. package/dist/invites/index.d.ts +3 -0
  66. package/dist/invites/index.js +37 -0
  67. package/dist/invites/sendInvite.d.ts +11 -0
  68. package/dist/invites/sendInvite.js +29 -0
  69. package/dist/invites/templates/inviteExistingUser.hbs +54 -0
  70. package/dist/invites/templates/inviteNewUser.hbs +55 -0
  71. package/dist/roles.d.ts +5 -0
  72. package/dist/roles.js +129 -0
  73. package/dist/service.d.ts +200 -0
  74. package/dist/service.js +63 -0
  75. package/dist/users/createUser.d.ts +27 -0
  76. package/dist/users/createUser.js +66 -0
  77. package/dist/users/deleteUser.d.ts +12 -0
  78. package/dist/users/deleteUser.js +59 -0
  79. package/dist/users/getUser.d.ts +17 -0
  80. package/dist/users/getUser.js +67 -0
  81. package/dist/users/index.d.ts +5 -0
  82. package/dist/users/index.js +39 -0
  83. package/dist/users/updateUser.d.ts +27 -0
  84. package/dist/users/updateUser.js +73 -0
  85. package/dist/users/upsertDefaultUser.d.ts +17 -0
  86. package/dist/users/upsertDefaultUser.js +87 -0
  87. package/dist/utils/createCallAccessToken.d.ts +6 -0
  88. package/dist/utils/createCallAccessToken.js +65 -0
  89. package/dist/utils/decodeToken.d.ts +4 -0
  90. package/dist/utils/decodeToken.js +26 -0
  91. package/dist/utils/generateAccessKeyId.d.ts +8 -0
  92. package/dist/utils/generateAccessKeyId.js +39 -0
  93. package/dist/utils/generateAccessKeySecret.d.ts +2 -0
  94. package/dist/utils/generateAccessKeySecret.js +26 -0
  95. package/dist/utils/getAccessKeyIdFromCall.d.ts +3 -0
  96. package/dist/utils/getAccessKeyIdFromCall.js +9 -0
  97. package/dist/utils/getAccessKeyIdFromEmail.d.ts +3 -0
  98. package/dist/utils/getAccessKeyIdFromEmail.js +37 -0
  99. package/dist/utils/getAccessKeyIdFromToken.d.ts +2 -0
  100. package/dist/utils/getAccessKeyIdFromToken.js +31 -0
  101. package/dist/utils/getApiKeyByAccessKeyId.d.ts +12 -0
  102. package/dist/utils/getApiKeyByAccessKeyId.js +26 -0
  103. package/dist/utils/getTokenFromCall.d.ts +3 -0
  104. package/dist/utils/getTokenFromCall.js +9 -0
  105. package/dist/utils/getUserByEmail.d.ts +15 -0
  106. package/dist/utils/getUserByEmail.js +26 -0
  107. package/dist/utils/getUserRefFromToken.d.ts +2 -0
  108. package/dist/utils/getUserRefFromToken.js +31 -0
  109. package/dist/utils/hasAccess.d.ts +3 -0
  110. package/dist/utils/hasAccess.js +10 -0
  111. package/dist/utils/hasAccessToResource.d.ts +5 -0
  112. package/dist/utils/hasAccessToResource.js +53 -0
  113. package/dist/utils/index.d.ts +11 -0
  114. package/dist/utils/index.js +45 -0
  115. package/dist/utils/isValidToken.d.ts +2 -0
  116. package/dist/utils/isValidToken.js +52 -0
  117. package/dist/utils/tokenHasAccessKeyId.d.ts +2 -0
  118. package/dist/utils/tokenHasAccessKeyId.js +29 -0
  119. package/dist/utils/withAccess.d.ts +6 -0
  120. package/dist/utils/withAccess.js +77 -0
  121. package/dist/workspaces/WorkspaceRoleEnum.d.ts +6 -0
  122. package/dist/workspaces/WorkspaceRoleEnum.js +27 -0
  123. package/dist/workspaces/createSendEmail.d.ts +3 -0
  124. package/dist/workspaces/createSendEmail.js +38 -0
  125. package/dist/workspaces/createWorkspace.d.ts +18 -0
  126. package/dist/workspaces/createWorkspace.js +65 -0
  127. package/dist/workspaces/deleteWorkspace.d.ts +12 -0
  128. package/dist/workspaces/deleteWorkspace.js +59 -0
  129. package/dist/workspaces/getWorkspace.d.ts +16 -0
  130. package/dist/workspaces/getWorkspace.js +65 -0
  131. package/dist/workspaces/index.d.ts +9 -0
  132. package/dist/workspaces/index.js +43 -0
  133. package/dist/workspaces/inviteUserToWorkspace.d.ts +31 -0
  134. package/dist/workspaces/inviteUserToWorkspace.js +152 -0
  135. package/dist/workspaces/isAdminMember.d.ts +3 -0
  136. package/dist/workspaces/isAdminMember.js +53 -0
  137. package/dist/workspaces/isWorkspaceMember.d.ts +3 -0
  138. package/dist/workspaces/isWorkspaceMember.js +32 -0
  139. package/dist/workspaces/listWorkspaces.d.ts +16 -0
  140. package/dist/workspaces/listWorkspaces.js +55 -0
  141. package/dist/workspaces/removeUserFromWorkspace.d.ts +12 -0
  142. package/dist/workspaces/removeUserFromWorkspace.js +84 -0
  143. package/dist/workspaces/resendWorkspaceMembershipInvitation.d.ts +14 -0
  144. package/dist/workspaces/resendWorkspaceMembershipInvitation.js +98 -0
  145. package/dist/workspaces/updateWorkspace.d.ts +21 -0
  146. package/dist/workspaces/updateWorkspace.js +76 -0
  147. package/package.json +50 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Fonoster Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ <a href="https://gitpod.io/#https://github.com/fonoster/fonoster"> <img src="https://img.shields.io/badge/Contribute%20with-Gitpod-908a85?logo=gitpod" alt="Contribute with Gitpod" />
2
+
3
+ This module is part of the [Fonoster](https://fonoster.com) project. By itself, it does not do much. It is intended to be used as a dependency for other modules. For more information about the project, please visit [https://github.com/fonoster/fonoster](https://github.com/fonoster/fonoster).
@@ -0,0 +1,5 @@
1
+ declare enum JsonWebErrorEnum {
2
+ JsonWebTokenError = "JsonWebTokenError",
3
+ TokenExpiredError = "TokenExpiredError"
4
+ }
5
+ export { JsonWebErrorEnum };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonWebErrorEnum = void 0;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ var JsonWebErrorEnum;
23
+ (function (JsonWebErrorEnum) {
24
+ JsonWebErrorEnum["JsonWebTokenError"] = "JsonWebTokenError";
25
+ JsonWebErrorEnum["TokenExpiredError"] = "TokenExpiredError";
26
+ })(JsonWebErrorEnum || (exports.JsonWebErrorEnum = JsonWebErrorEnum = {}));
@@ -0,0 +1,4 @@
1
+ declare enum ApiRoleEnum {
2
+ WORKSPACE_ADMIN = "WORKSPACE_ADMIN"
3
+ }
4
+ export { ApiRoleEnum };
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiRoleEnum = void 0;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ var ApiRoleEnum;
23
+ (function (ApiRoleEnum) {
24
+ ApiRoleEnum["WORKSPACE_ADMIN"] = "WORKSPACE_ADMIN";
25
+ })(ApiRoleEnum || (exports.ApiRoleEnum = ApiRoleEnum = {}));
@@ -0,0 +1,24 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { z } from "zod";
3
+ import { ApiRoleEnum } from "./ApiRoleEnum";
4
+ import { Prisma } from "../db";
5
+ declare const CreatApiKeyRequestSchema: z.ZodObject<{
6
+ role: z.ZodEnum<[ApiRoleEnum]>;
7
+ expiresAt: z.ZodEffects<z.ZodNumber, number, number>;
8
+ }, "strip", z.ZodTypeAny, {
9
+ role?: ApiRoleEnum;
10
+ expiresAt?: number;
11
+ }, {
12
+ role?: ApiRoleEnum;
13
+ expiresAt?: number;
14
+ }>;
15
+ type CreateApiKeyRequest = z.infer<typeof CreatApiKeyRequestSchema>;
16
+ type CreateApiKeyResponse = {
17
+ ref: string;
18
+ accessKeyId: string;
19
+ accessKeySecret: string;
20
+ };
21
+ declare function createApiKey(prisma: Prisma): (call: {
22
+ request: CreateApiKeyRequest;
23
+ }, callback: (error: GrpcErrorMessage, response?: CreateApiKeyResponse) => void) => Promise<void>;
24
+ export { createApiKey };
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createApiKey = void 0;
13
+ /*
14
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const common_1 = require("@fonoster/common");
32
+ const logger_1 = require("@fonoster/logger");
33
+ const zod_1 = require("zod");
34
+ const ApiRoleEnum_1 = require("./ApiRoleEnum");
35
+ const utils_1 = require("../utils");
36
+ const generateAccessKeyId_1 = require("../utils/generateAccessKeyId");
37
+ const generateAccessKeySecret_1 = require("../utils/generateAccessKeySecret");
38
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
39
+ const CreatApiKeyRequestSchema = zod_1.z.object({
40
+ role: zod_1.z.enum([ApiRoleEnum_1.ApiRoleEnum.WORKSPACE_ADMIN]),
41
+ expiresAt: zod_1.z.number().transform((value) => (value === 0 ? null : value))
42
+ });
43
+ function createApiKey(prisma) {
44
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
45
+ try {
46
+ const validatedRequest = CreatApiKeyRequestSchema.parse(call.request);
47
+ const accessKeyId = (0, utils_1.getAccessKeyIdFromCall)(call);
48
+ const { role, expiresAt } = validatedRequest;
49
+ logger.info("creating new ApiKey", { accessKeyId, role, expiresAt });
50
+ const workspace = yield prisma.workspace.findUnique({
51
+ where: { accessKeyId }
52
+ });
53
+ const response = yield prisma.apiKey.create({
54
+ data: {
55
+ workspaceRef: workspace.ref,
56
+ role: validatedRequest.role,
57
+ accessKeyId: (0, generateAccessKeyId_1.generateAccessKeyId)(generateAccessKeyId_1.AccessKeyIdType.API_KEY),
58
+ accessKeySecret: (0, generateAccessKeySecret_1.generateAccessKeySecret)(),
59
+ expiresAt: expiresAt ? new Date(expiresAt) : null
60
+ }
61
+ });
62
+ callback(null, {
63
+ ref: response.ref,
64
+ accessKeyId: response.accessKeyId,
65
+ accessKeySecret: response.accessKeySecret
66
+ });
67
+ }
68
+ catch (error) {
69
+ (0, common_1.handleError)(error, callback);
70
+ }
71
+ });
72
+ }
73
+ exports.createApiKey = createApiKey;
@@ -0,0 +1,18 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { z } from "zod";
3
+ import { Prisma } from "../db";
4
+ declare const DeleteApiKeyRequestSchema: z.ZodObject<{
5
+ ref: z.ZodString;
6
+ }, "strip", z.ZodTypeAny, {
7
+ ref?: string;
8
+ }, {
9
+ ref?: string;
10
+ }>;
11
+ type DeleteApiKeyRequest = z.infer<typeof DeleteApiKeyRequestSchema>;
12
+ type DeleteApiKeyResponse = {
13
+ ref: string;
14
+ };
15
+ declare function deleteApiKey(prisma: Prisma): (call: {
16
+ request: DeleteApiKeyRequest;
17
+ }, callback: (error: GrpcErrorMessage, response?: DeleteApiKeyResponse) => void) => Promise<void>;
18
+ export { deleteApiKey };
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.deleteApiKey = void 0;
13
+ /*
14
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const common_1 = require("@fonoster/common");
32
+ const logger_1 = require("@fonoster/logger");
33
+ const zod_1 = require("zod");
34
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
35
+ const DeleteApiKeyRequestSchema = zod_1.z.object({
36
+ ref: zod_1.z.string()
37
+ });
38
+ function deleteApiKey(prisma) {
39
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
40
+ try {
41
+ const validatedRequest = DeleteApiKeyRequestSchema.parse(call.request);
42
+ const { ref } = validatedRequest;
43
+ logger.info("deleting ApiKey", { ref });
44
+ const response = yield prisma.apiKey.delete({
45
+ where: {
46
+ ref
47
+ }
48
+ });
49
+ callback(null, {
50
+ ref: response.ref
51
+ });
52
+ }
53
+ catch (error) {
54
+ (0, common_1.handleError)(error, callback);
55
+ }
56
+ });
57
+ }
58
+ exports.deleteApiKey = deleteApiKey;
@@ -0,0 +1,5 @@
1
+ export * from "./createApiKey";
2
+ export * from "./deleteApiKey";
3
+ export * from "./listApiKeys";
4
+ export * from "./regenerateApiKey";
5
+ export * from "./ApiRoleEnum";
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /*
18
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
19
+ * http://github.com/fonoster/fonoster
20
+ *
21
+ * This file is part of Fonoster
22
+ *
23
+ * Licensed under the MIT License (the "License");
24
+ * you may not use this file except in compliance with
25
+ * the License. You may obtain a copy of the License at
26
+ *
27
+ * https://opensource.org/licenses/MIT
28
+ *
29
+ * Unless required by applicable law or agreed to in writing, software
30
+ * distributed under the License is distributed on an "AS IS" BASIS,
31
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
+ * See the License for the specific language governing permissions and
33
+ * limitations under the License.
34
+ */
35
+ __exportStar(require("./createApiKey"), exports);
36
+ __exportStar(require("./deleteApiKey"), exports);
37
+ __exportStar(require("./listApiKeys"), exports);
38
+ __exportStar(require("./regenerateApiKey"), exports);
39
+ __exportStar(require("./ApiRoleEnum"), exports);
@@ -0,0 +1,23 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { ApiRoleEnum } from "./ApiRoleEnum";
3
+ import { Prisma } from "../db";
4
+ type ListApiKeysRequest = {
5
+ pageSize: number;
6
+ pageToken: string;
7
+ };
8
+ type ApiKey = {
9
+ ref: string;
10
+ accessKeyId: string;
11
+ role: ApiRoleEnum;
12
+ expiresAt: Date;
13
+ createdAt: Date;
14
+ updatedAt: Date;
15
+ };
16
+ type ListApiKeysResponse = {
17
+ items: ApiKey[];
18
+ nextPageToken?: string;
19
+ };
20
+ declare function listApiKeys(prisma: Prisma): (call: {
21
+ request: ListApiKeysRequest;
22
+ }, callback: (error: GrpcErrorMessage, response?: ListApiKeysResponse) => void) => Promise<void>;
23
+ export { listApiKeys };
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.listApiKeys = void 0;
13
+ const logger_1 = require("@fonoster/logger");
14
+ const utils_1 = require("../utils");
15
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
16
+ function listApiKeys(prisma) {
17
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
18
+ var _a;
19
+ const { pageSize, pageToken } = call.request;
20
+ const accessKeyId = (0, utils_1.getAccessKeyIdFromCall)(call);
21
+ logger.verbose("list keys for workspace", { accessKeyId });
22
+ const workspace = yield prisma.workspace.findUnique({
23
+ where: {
24
+ accessKeyId
25
+ }
26
+ });
27
+ const keys = yield prisma.apiKey.findMany({
28
+ where: {
29
+ workspaceRef: workspace.ref
30
+ },
31
+ take: pageSize,
32
+ skip: pageToken ? 1 : 0,
33
+ cursor: pageToken ? { ref: pageToken } : undefined
34
+ });
35
+ const items = keys.map((key) => ({
36
+ ref: key.ref,
37
+ accessKeyId: key.accessKeyId,
38
+ role: key.role,
39
+ expiresAt: key.expiresAt,
40
+ createdAt: key.createdAt,
41
+ updatedAt: key.updatedAt
42
+ }));
43
+ const response = {
44
+ items,
45
+ nextPageToken: (_a = items[items.length - 1]) === null || _a === void 0 ? void 0 : _a.ref
46
+ };
47
+ callback(null, response);
48
+ });
49
+ }
50
+ exports.listApiKeys = listApiKeys;
@@ -0,0 +1,20 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { z } from "zod";
3
+ import { Prisma } from "../db";
4
+ declare const RegenerateApiKeyRequestSchema: z.ZodObject<{
5
+ ref: z.ZodString;
6
+ }, "strip", z.ZodTypeAny, {
7
+ ref?: string;
8
+ }, {
9
+ ref?: string;
10
+ }>;
11
+ type RegenerateApiKeyRequest = z.infer<typeof RegenerateApiKeyRequestSchema>;
12
+ type RegenerateApiKeyResponse = {
13
+ ref: string;
14
+ accessKeyId: string;
15
+ accessKeySecret: string;
16
+ };
17
+ declare function regenerateApiKey(prisma: Prisma): (call: {
18
+ request: RegenerateApiKeyRequest;
19
+ }, callback: (error: GrpcErrorMessage, response?: RegenerateApiKeyResponse) => void) => Promise<void>;
20
+ export { regenerateApiKey };
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.regenerateApiKey = void 0;
13
+ /*
14
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const common_1 = require("@fonoster/common");
32
+ const logger_1 = require("@fonoster/logger");
33
+ const zod_1 = require("zod");
34
+ const generateAccessKeySecret_1 = require("../utils/generateAccessKeySecret");
35
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
36
+ const RegenerateApiKeyRequestSchema = zod_1.z.object({
37
+ ref: zod_1.z.string()
38
+ });
39
+ function regenerateApiKey(prisma) {
40
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
41
+ try {
42
+ const validatedRequest = RegenerateApiKeyRequestSchema.parse(call.request);
43
+ const { ref } = validatedRequest;
44
+ logger.info("regenerating ApiKey", { ref });
45
+ const response = yield prisma.apiKey.update({
46
+ where: {
47
+ ref
48
+ },
49
+ data: {
50
+ accessKeySecret: (0, generateAccessKeySecret_1.generateAccessKeySecret)()
51
+ }
52
+ });
53
+ callback(null, {
54
+ ref: response.ref,
55
+ accessKeyId: response.accessKeyId,
56
+ accessKeySecret: response.accessKeySecret
57
+ });
58
+ }
59
+ catch (error) {
60
+ (0, common_1.handleError)(error, callback);
61
+ }
62
+ });
63
+ }
64
+ exports.regenerateApiKey = regenerateApiKey;
@@ -0,0 +1,15 @@
1
+ import { ServerInterceptingCall } from "@grpc/grpc-js";
2
+ /**
3
+ * This function is a gRPC interceptor that checks if the request is valid
4
+ * and if the user has the right permissions to access the resource. When
5
+ * validating the request, the function will check if the request is in the
6
+ * skip list, if the token is valid and if the role is allowed by the RBAC.
7
+ *
8
+ * @param {string} identityPublicKey - The public key to validate the token
9
+ * @param {string[]} publicPath - The list of public paths
10
+ * @return {Function} - The gRPC interceptor
11
+ */
12
+ declare function createAuthInterceptor(identityPublicKey: string, publicPath: string[]): (methodDefinition: {
13
+ path: string;
14
+ }, call: ServerInterceptingCall) => ServerInterceptingCall;
15
+ export { createAuthInterceptor };
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createAuthInterceptor = void 0;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const logger_1 = require("@fonoster/logger");
23
+ const errors_1 = require("./errors");
24
+ const roles_1 = require("./roles");
25
+ const utils_1 = require("./utils");
26
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
27
+ /**
28
+ * This function is a gRPC interceptor that checks if the request is valid
29
+ * and if the user has the right permissions to access the resource. When
30
+ * validating the request, the function will check if the request is in the
31
+ * skip list, if the token is valid and if the role is allowed by the RBAC.
32
+ *
33
+ * @param {string} identityPublicKey - The public key to validate the token
34
+ * @param {string[]} publicPath - The list of public paths
35
+ * @return {Function} - The gRPC interceptor
36
+ */
37
+ function createAuthInterceptor(identityPublicKey, publicPath) {
38
+ /**
39
+ * Inner function that will be called by the gRPC server.
40
+ *
41
+ * @param {object} methodDefinition - The method definition
42
+ * @param {string} methodDefinition.path - The path of the gRPC method
43
+ * @param {ServerInterceptingCall} call - The call object
44
+ * @return {ServerInterceptingCall} - The modified call object
45
+ */
46
+ return (methodDefinition, call) => {
47
+ const { path } = methodDefinition;
48
+ const accessKeyId = (0, utils_1.getAccessKeyIdFromCall)(call);
49
+ logger.verbose("intercepting api call to path", { accessKeyId, path });
50
+ if (publicPath.includes(methodDefinition.path)) {
51
+ logger.verbose("skipping auth for public path", { path });
52
+ return call;
53
+ }
54
+ const token = (0, utils_1.getTokenFromCall)(call);
55
+ logger.verbose("validating token", { accessKeyId, path });
56
+ if (!(0, utils_1.isValidToken)(token, identityPublicKey)) {
57
+ return (0, errors_1.unauthenticatedError)(call);
58
+ }
59
+ const decodedToken = (0, utils_1.decodeToken)(token);
60
+ logger.verbose("checking access for accessKeyId", {
61
+ accessKeyId,
62
+ path,
63
+ hasAccess: (0, utils_1.hasAccess)(decodedToken.access, path),
64
+ pathIsWorkspacePath: roles_1.workspaceAccess.includes(path),
65
+ tokenHasAccessKeyId: (0, utils_1.tokenHasAccessKeyId)(token, accessKeyId)
66
+ });
67
+ if (!(0, utils_1.hasAccess)(decodedToken.access, path) ||
68
+ (roles_1.workspaceAccess.includes(path) &&
69
+ !(0, utils_1.tokenHasAccessKeyId)(token, accessKeyId))) {
70
+ return (0, errors_1.permissionDeniedError)(call);
71
+ }
72
+ return call;
73
+ };
74
+ }
75
+ exports.createAuthInterceptor = createAuthInterceptor;
package/dist/db.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { Prisma as DMMF } from "@prisma/identity-client";
2
+ declare const prisma: import("@prisma/identity-client/runtime/library").DynamicClientExtensionThis<DMMF.TypeMap<import("@prisma/identity-client/runtime/library").InternalArgs & {
3
+ result: {};
4
+ model: {};
5
+ query: {};
6
+ client: {};
7
+ }>, DMMF.TypeMapCb, {
8
+ result: {};
9
+ model: {};
10
+ query: {};
11
+ client: {};
12
+ }>;
13
+ type Prisma = typeof prisma;
14
+ export { prisma, Prisma };
package/dist/db.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prisma = void 0;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const identity_client_1 = require("@prisma/identity-client");
23
+ const prisma_field_encryption_1 = require("prisma-field-encryption");
24
+ const envs_1 = require("./envs");
25
+ // Check the link for details on dmff:
26
+ // https://www.npmjs.com/package/prisma-field-encryption#custom-prisma-client-location
27
+ const prisma = new identity_client_1.PrismaClient().$extends((0, prisma_field_encryption_1.fieldEncryptionExtension)({
28
+ encryptionKey: envs_1.CLOAK_ENCRYPTION_KEY,
29
+ dmmf: identity_client_1.Prisma.dmmf
30
+ }));
31
+ exports.prisma = prisma;
package/dist/envs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const CLOAK_ENCRYPTION_KEY: string;
package/dist/envs.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CLOAK_ENCRYPTION_KEY = void 0;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const common_1 = require("@fonoster/common");
23
+ (0, common_1.assertEnvsAreSet)(["CLOAK_ENCRYPTION_KEY"]);
24
+ exports.CLOAK_ENCRYPTION_KEY = process.env.CLOAK_ENCRYPTION_KEY;
@@ -0,0 +1,4 @@
1
+ import { ServerInterceptingCall } from "@grpc/grpc-js";
2
+ declare const unauthenticatedError: (call: ServerInterceptingCall) => ServerInterceptingCall;
3
+ declare const permissionDeniedError: (call: ServerInterceptingCall) => ServerInterceptingCall;
4
+ export { unauthenticatedError, permissionDeniedError };