@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
@@ -0,0 +1,17 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { Prisma } from "../db";
3
+ type GetUserRequest = {
4
+ ref: string;
5
+ };
6
+ type User = {
7
+ ref: string;
8
+ email: string;
9
+ name: string;
10
+ avatar: string;
11
+ createdAt: Date;
12
+ updatedAt: Date;
13
+ };
14
+ declare function getUser(prisma: Prisma): (call: {
15
+ request: GetUserRequest;
16
+ }, callback: (error: GrpcErrorMessage, response?: User) => void) => Promise<void>;
17
+ export { getUser };
@@ -0,0 +1,67 @@
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.getUser = 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 grpc_js_1 = require("@grpc/grpc-js");
34
+ const utils_1 = require("../utils");
35
+ const getTokenFromCall_1 = require("../utils/getTokenFromCall");
36
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
37
+ function getUser(prisma) {
38
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
39
+ try {
40
+ const { ref } = call.request;
41
+ const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
42
+ const accessKeyId = (0, utils_1.getAccessKeyIdFromToken)(token);
43
+ logger.verbose("getting user with ref and accessKeyId", {
44
+ ref,
45
+ accessKeyId
46
+ });
47
+ const user = yield prisma.user.findUnique({
48
+ where: {
49
+ ref,
50
+ accessKeyId
51
+ }
52
+ });
53
+ if (!user) {
54
+ callback({
55
+ code: grpc_js_1.status.NOT_FOUND,
56
+ message: `User not found: ${ref}`
57
+ });
58
+ return;
59
+ }
60
+ callback(null, (0, common_1.datesMapper)(user));
61
+ }
62
+ catch (error) {
63
+ (0, common_1.handleError)(error, callback);
64
+ }
65
+ });
66
+ }
67
+ exports.getUser = getUser;
@@ -0,0 +1,5 @@
1
+ export * from "./createUser";
2
+ export * from "./getUser";
3
+ export * from "./deleteUser";
4
+ export * from "./updateUser";
5
+ export * from "./upsertDefaultUser";
@@ -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("./createUser"), exports);
36
+ __exportStar(require("./getUser"), exports);
37
+ __exportStar(require("./deleteUser"), exports);
38
+ __exportStar(require("./updateUser"), exports);
39
+ __exportStar(require("./upsertDefaultUser"), exports);
@@ -0,0 +1,27 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { z } from "zod";
3
+ import { Prisma } from "../db";
4
+ declare const UpdateUserRequestSchema: z.ZodObject<{
5
+ ref: z.ZodString;
6
+ name: z.ZodUnion<[z.ZodString, z.ZodNullable<z.ZodOptional<z.ZodString>>]>;
7
+ password: z.ZodUnion<[z.ZodString, z.ZodNullable<z.ZodOptional<z.ZodString>>]>;
8
+ avatar: z.ZodUnion<[z.ZodString, z.ZodNullable<z.ZodOptional<z.ZodString>>]>;
9
+ }, "strip", z.ZodTypeAny, {
10
+ name?: string;
11
+ ref?: string;
12
+ password?: string;
13
+ avatar?: string;
14
+ }, {
15
+ name?: string;
16
+ ref?: string;
17
+ password?: string;
18
+ avatar?: string;
19
+ }>;
20
+ type UpdateUserRequest = z.infer<typeof UpdateUserRequestSchema>;
21
+ type UpdateUserResponse = {
22
+ ref: string;
23
+ };
24
+ declare function updateUser(prisma: Prisma): (call: {
25
+ request: UpdateUserRequest;
26
+ }, callback: (error: GrpcErrorMessage, response?: UpdateUserResponse) => void) => Promise<void>;
27
+ export { updateUser };
@@ -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.updateUser = 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 utils_1 = require("../utils");
35
+ const getTokenFromCall_1 = require("../utils/getTokenFromCall");
36
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
37
+ const UpdateUserRequestSchema = zod_1.z.object({
38
+ ref: zod_1.z.string(),
39
+ name: zod_1.z.string().min(3).max(50).or(zod_1.z.string().optional().nullable()),
40
+ password: zod_1.z.string().min(8).max(50).or(zod_1.z.string().optional().nullable()),
41
+ avatar: zod_1.z.string().url().or(zod_1.z.string().optional().nullable())
42
+ });
43
+ function updateUser(prisma) {
44
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
45
+ try {
46
+ const validatedRequest = UpdateUserRequestSchema.parse(call.request);
47
+ const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
48
+ const accessKeyId = (0, utils_1.getAccessKeyIdFromToken)(token);
49
+ const { ref, name, avatar, password } = validatedRequest;
50
+ logger.verbose("call to updateUser", { ref, password });
51
+ yield prisma.user.update({
52
+ where: {
53
+ ref,
54
+ accessKeyId
55
+ },
56
+ data: {
57
+ name,
58
+ avatar,
59
+ password: password || undefined,
60
+ updatedAt: new Date()
61
+ }
62
+ });
63
+ const response = {
64
+ ref
65
+ };
66
+ callback(null, response);
67
+ }
68
+ catch (error) {
69
+ (0, common_1.handleError)(error, callback);
70
+ }
71
+ });
72
+ }
73
+ exports.updateUser = updateUser;
@@ -0,0 +1,17 @@
1
+ import { z } from "zod";
2
+ declare const CreateUserRequestSchema: z.ZodObject<{
3
+ name: z.ZodString;
4
+ email: z.ZodString;
5
+ password: z.ZodUnion<[z.ZodString, z.ZodNullable<z.ZodOptional<z.ZodString>>]>;
6
+ }, "strip", z.ZodTypeAny, {
7
+ name?: string;
8
+ email?: string;
9
+ password?: string;
10
+ }, {
11
+ name?: string;
12
+ email?: string;
13
+ password?: string;
14
+ }>;
15
+ type CreateUserRequest = z.infer<typeof CreateUserRequestSchema>;
16
+ declare function upsertDefaultUser(request: CreateUserRequest): Promise<void>;
17
+ export { upsertDefaultUser };
@@ -0,0 +1,87 @@
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.upsertDefaultUser = 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 logger_1 = require("@fonoster/logger");
32
+ const zod_1 = require("zod");
33
+ const db_1 = require("../db");
34
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
35
+ const CreateUserRequestSchema = zod_1.z.object({
36
+ name: zod_1.z.string().min(3).max(50),
37
+ email: zod_1.z.string().email(),
38
+ password: zod_1.z.string().min(8).max(50).or(zod_1.z.string().optional().nullable())
39
+ });
40
+ const USER_REF = "00000000-0000-0000-0000-000000000000";
41
+ const WORKSPACE_REF = "00000000-0000-0000-0000-000000000000";
42
+ const USER_ACCESS_KEY_ID = "US00000000000000000000000000000000";
43
+ const WORKSPACE_ACCESS_KEY_ID = "WO00000000000000000000000000000000";
44
+ function upsertDefaultUser(request) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ try {
47
+ const validatedRequest = CreateUserRequestSchema.parse(request);
48
+ const { name, email, password } = validatedRequest;
49
+ logger.verbose("call to upsertDefaultUser", {
50
+ email,
51
+ accessKeyId: USER_ACCESS_KEY_ID
52
+ });
53
+ yield db_1.prisma.user.upsert({
54
+ where: { ref: USER_REF },
55
+ update: {
56
+ name,
57
+ email,
58
+ password: password || undefined,
59
+ accessKeyId: USER_ACCESS_KEY_ID,
60
+ updatedAt: new Date()
61
+ },
62
+ create: {
63
+ ref: USER_REF,
64
+ name,
65
+ email,
66
+ password,
67
+ accessKeyId: USER_ACCESS_KEY_ID
68
+ }
69
+ });
70
+ yield db_1.prisma.workspace.upsert({
71
+ where: { ref: WORKSPACE_REF },
72
+ update: {},
73
+ create: {
74
+ ref: WORKSPACE_REF,
75
+ name: "Default Workspace",
76
+ ownerRef: USER_REF,
77
+ accessKeyId: WORKSPACE_ACCESS_KEY_ID
78
+ }
79
+ });
80
+ }
81
+ catch (error) {
82
+ logger.error("error on upsertDefaultUser", { error });
83
+ process.exit(1);
84
+ }
85
+ });
86
+ }
87
+ exports.upsertDefaultUser = upsertDefaultUser;
@@ -0,0 +1,6 @@
1
+ import { IdentityConfig } from "../exchanges/types";
2
+ declare function createCallAccessToken(identityConfig: IdentityConfig): (params: {
3
+ accessKeyId: string;
4
+ appRef: string;
5
+ }) => Promise<string>;
6
+ export { createCallAccessToken };
@@ -0,0 +1,65 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.createCallAccessToken = void 0;
16
+ /*
17
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
18
+ * http://github.com/fonoster/fonoster
19
+ *
20
+ * This file is part of Fonoster
21
+ *
22
+ * Licensed under the MIT License (the "License");
23
+ * you may not use this file except in compliance with
24
+ * the License. You may obtain a copy of the License at
25
+ *
26
+ * https://opensource.org/licenses/MIT
27
+ *
28
+ * Unless required by applicable law or agreed to in writing, software
29
+ * distributed under the License is distributed on an "AS IS" BASIS,
30
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31
+ * See the License for the specific language governing permissions and
32
+ * limitations under the License.
33
+ */
34
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
35
+ const TokenUseEnum_1 = require("../exchanges/TokenUseEnum");
36
+ const roles_1 = require("../roles");
37
+ const SIGN_ALGORITHM = "RS256";
38
+ function createCallAccessToken(identityConfig) {
39
+ return (params) => __awaiter(this, void 0, void 0, function* () {
40
+ const { privateKey } = identityConfig;
41
+ const accessTokenSignOptions = {
42
+ algorithm: SIGN_ALGORITHM,
43
+ // Just enough time to validate a request
44
+ expiresIn: "30s"
45
+ };
46
+ const { issuer, audience } = identityConfig;
47
+ const { accessKeyId, appRef } = params;
48
+ const access = [
49
+ {
50
+ accessKeyId,
51
+ role: roles_1.VOICE_SERVICE_ROLE
52
+ }
53
+ ];
54
+ const unsignedToken = {
55
+ iss: issuer,
56
+ sub: appRef,
57
+ aud: audience,
58
+ tokenUse: TokenUseEnum_1.TokenUseEnum.ACCESS,
59
+ accessKeyId,
60
+ access
61
+ };
62
+ return jsonwebtoken_1.default.sign(unsignedToken, privateKey, accessTokenSignOptions);
63
+ });
64
+ }
65
+ exports.createCallAccessToken = createCallAccessToken;
@@ -0,0 +1,4 @@
1
+ import { TokenUseEnum } from "../exchanges/TokenUseEnum";
2
+ import { DecodedToken } from "../exchanges/types";
3
+ declare function decodeToken<T extends TokenUseEnum>(token: string): DecodedToken<T>;
4
+ export { decodeToken };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeToken = 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 jwt_decode_1 = require("jwt-decode");
23
+ function decodeToken(token) {
24
+ return (0, jwt_decode_1.jwtDecode)(token);
25
+ }
26
+ exports.decodeToken = decodeToken;
@@ -0,0 +1,8 @@
1
+ declare enum AccessKeyIdType {
2
+ USER = 0,
3
+ WORKSPACE = 1,
4
+ SERVICE = 2,
5
+ API_KEY = 3
6
+ }
7
+ declare function generateAccessKeyId(type: AccessKeyIdType): string;
8
+ export { generateAccessKeyId, AccessKeyIdType };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AccessKeyIdType = exports.generateAccessKeyId = 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 nanoid_1 = require("nanoid");
23
+ var AccessKeyIdType;
24
+ (function (AccessKeyIdType) {
25
+ AccessKeyIdType[AccessKeyIdType["USER"] = 0] = "USER";
26
+ AccessKeyIdType[AccessKeyIdType["WORKSPACE"] = 1] = "WORKSPACE";
27
+ AccessKeyIdType[AccessKeyIdType["SERVICE"] = 2] = "SERVICE";
28
+ AccessKeyIdType[AccessKeyIdType["API_KEY"] = 3] = "API_KEY";
29
+ })(AccessKeyIdType || (exports.AccessKeyIdType = AccessKeyIdType = {}));
30
+ function generateAccessKeyId(type) {
31
+ const prefix = {
32
+ [AccessKeyIdType.USER]: "US",
33
+ [AccessKeyIdType.WORKSPACE]: "WO",
34
+ [AccessKeyIdType.SERVICE]: "SE",
35
+ [AccessKeyIdType.API_KEY]: "AP"
36
+ };
37
+ return `${prefix[type]}${(0, nanoid_1.customAlphabet)("1234567890abcdefghijklmnopqrstuvwxyz", 32)()}`;
38
+ }
39
+ exports.generateAccessKeyId = generateAccessKeyId;
@@ -0,0 +1,2 @@
1
+ declare function generateAccessKeySecret(): string;
2
+ export { generateAccessKeySecret };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateAccessKeySecret = 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 nanoid_1 = require("nanoid");
23
+ function generateAccessKeySecret() {
24
+ return (0, nanoid_1.customAlphabet)("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 64)();
25
+ }
26
+ exports.generateAccessKeySecret = generateAccessKeySecret;
@@ -0,0 +1,3 @@
1
+ import { ServerInterceptingCall } from "@grpc/grpc-js";
2
+ declare function getAccessKeyIdFromCall(call: ServerInterceptingCall): string;
3
+ export { getAccessKeyIdFromCall };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAccessKeyIdFromCall = void 0;
4
+ function getAccessKeyIdFromCall(call) {
5
+ var _a;
6
+ const metadata = call.metadata.getMap();
7
+ return (_a = metadata["accesskeyid"]) === null || _a === void 0 ? void 0 : _a.toString();
8
+ }
9
+ exports.getAccessKeyIdFromCall = getAccessKeyIdFromCall;
@@ -0,0 +1,3 @@
1
+ import { Prisma } from "../db";
2
+ declare function getAccessKeyIdFromEmail(prisma: Prisma): (email: string) => Promise<string>;
3
+ export { getAccessKeyIdFromEmail };
@@ -0,0 +1,37 @@
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.getAccessKeyIdFromEmail = 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 getUserByEmail_1 = require("./getUserByEmail");
32
+ function getAccessKeyIdFromEmail(prisma) {
33
+ return (email) => __awaiter(this, void 0, void 0, function* () {
34
+ return (yield (0, getUserByEmail_1.getUserByEmail)(prisma)(email)).accessKeyId;
35
+ });
36
+ }
37
+ exports.getAccessKeyIdFromEmail = getAccessKeyIdFromEmail;
@@ -0,0 +1,2 @@
1
+ declare function getAccessKeyIdFromToken(token: string): string;
2
+ export { getAccessKeyIdFromToken };
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAccessKeyIdFromToken = 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 decodeToken_1 = require("./decodeToken");
23
+ const TokenUseEnum_1 = require("../exchanges/TokenUseEnum");
24
+ function getAccessKeyIdFromToken(token) {
25
+ const decodedToken = (0, decodeToken_1.decodeToken)(token);
26
+ if (decodedToken.tokenUse !== TokenUseEnum_1.TokenUseEnum.ACCESS) {
27
+ throw new Error("Invalid token type");
28
+ }
29
+ return decodedToken.accessKeyId;
30
+ }
31
+ exports.getAccessKeyIdFromToken = getAccessKeyIdFromToken;
@@ -0,0 +1,12 @@
1
+ import { Prisma } from "../db";
2
+ declare function getApiKeyByAccessKeyId(prisma: Prisma): (accessKeyId: string) => Promise<{
3
+ ref: string;
4
+ accessKeyId: string;
5
+ createdAt: Date;
6
+ updatedAt: Date;
7
+ role: "WORKSPACE_ADMIN";
8
+ workspaceRef: string;
9
+ accessKeySecret: string;
10
+ expiresAt: Date;
11
+ }>;
12
+ export { getApiKeyByAccessKeyId };
@@ -0,0 +1,26 @@
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.getApiKeyByAccessKeyId = void 0;
13
+ function getApiKeyByAccessKeyId(prisma) {
14
+ return (accessKeyId) => __awaiter(this, void 0, void 0, function* () {
15
+ const key = yield prisma.apiKey.findFirst({
16
+ where: {
17
+ accessKeyId
18
+ }
19
+ });
20
+ if (!key) {
21
+ return null;
22
+ }
23
+ return key;
24
+ });
25
+ }
26
+ exports.getApiKeyByAccessKeyId = getApiKeyByAccessKeyId;