@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,98 @@
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.resendWorkspaceMembershipInvitation = 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 createSendEmail_1 = require("./createSendEmail");
35
+ const isAdminMember_1 = require("./isAdminMember");
36
+ const utils_1 = require("../utils");
37
+ const getTokenFromCall_1 = require("../utils/getTokenFromCall");
38
+ const getUserRefFromToken_1 = require("../utils/getUserRefFromToken");
39
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
40
+ function resendWorkspaceMembershipInvitation(prisma, identityConfig, sendInvite) {
41
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
42
+ try {
43
+ const { userRef: inviteeRef } = call.request;
44
+ const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
45
+ const adminRef = (0, getUserRefFromToken_1.getUserRefFromToken)(token);
46
+ const accessKeyId = (0, utils_1.getAccessKeyIdFromCall)(call);
47
+ const workspace = yield prisma.workspace.findUnique({
48
+ where: {
49
+ accessKeyId
50
+ }
51
+ });
52
+ const workspaceRef = workspace.ref;
53
+ logger.verbose("resending workspace membership invitation", {
54
+ workspaceRef,
55
+ inviteeRef,
56
+ adminRef
57
+ });
58
+ const isAdmin = yield (0, isAdminMember_1.isAdminMember)(prisma)(workspace.ref, adminRef);
59
+ if (!isAdmin) {
60
+ return callback({
61
+ code: grpc_js_1.status.PERMISSION_DENIED,
62
+ message: "Only admins and owners can resend workspace invitations"
63
+ });
64
+ }
65
+ const member = yield prisma.workspaceMember.findFirst({
66
+ where: {
67
+ workspaceRef,
68
+ userRef: inviteeRef
69
+ },
70
+ include: {
71
+ user: true,
72
+ workspace: true
73
+ }
74
+ });
75
+ if (!member) {
76
+ return callback({
77
+ code: grpc_js_1.status.NOT_FOUND,
78
+ message: `Original invitation not found for userRef: ${inviteeRef}`
79
+ });
80
+ }
81
+ yield sendInvite((0, createSendEmail_1.createSendEmail)(identityConfig), {
82
+ recipient: member.user.email,
83
+ oneTimePassword: member.user.password,
84
+ workspaceName: member.workspace.name,
85
+ isExistingUser: true,
86
+ // TODO: Create inviteUrl with invite token
87
+ inviteUrl: "https://placehold.it?token=jwt"
88
+ });
89
+ callback(null, {
90
+ userRef: inviteeRef
91
+ });
92
+ }
93
+ catch (error) {
94
+ (0, common_1.handleError)(error, callback);
95
+ }
96
+ });
97
+ }
98
+ exports.resendWorkspaceMembershipInvitation = resendWorkspaceMembershipInvitation;
@@ -0,0 +1,21 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { z } from "zod";
3
+ import { Prisma } from "../db";
4
+ declare const UpdateWorkspaceRequestSchema: z.ZodObject<{
5
+ ref: z.ZodString;
6
+ name: z.ZodUnion<[z.ZodString, z.ZodNullable<z.ZodOptional<z.ZodString>>]>;
7
+ }, "strip", z.ZodTypeAny, {
8
+ name?: string;
9
+ ref?: string;
10
+ }, {
11
+ name?: string;
12
+ ref?: string;
13
+ }>;
14
+ type UpdateWorkspaceRequest = z.infer<typeof UpdateWorkspaceRequestSchema>;
15
+ type UpdateWorkspaceResponse = {
16
+ ref: string;
17
+ };
18
+ declare function updateWorkspace(prisma: Prisma): (call: {
19
+ request: UpdateWorkspaceRequest;
20
+ }, callback: (error: GrpcErrorMessage, response?: UpdateWorkspaceResponse) => void) => Promise<void>;
21
+ export { updateWorkspace };
@@ -0,0 +1,76 @@
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.updateWorkspace = 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 zod_1 = require("zod");
35
+ const isWorkspaceMember_1 = require("./isWorkspaceMember");
36
+ const getTokenFromCall_1 = require("../utils/getTokenFromCall");
37
+ const getUserRefFromToken_1 = require("../utils/getUserRefFromToken");
38
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
39
+ const UpdateWorkspaceRequestSchema = zod_1.z.object({
40
+ ref: zod_1.z.string(),
41
+ name: zod_1.z.string().min(3).max(50).or(zod_1.z.string().optional().nullable())
42
+ });
43
+ function updateWorkspace(prisma) {
44
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
45
+ try {
46
+ const validatedRequest = UpdateWorkspaceRequestSchema.parse(call.request);
47
+ const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
48
+ const userRef = (0, getUserRefFromToken_1.getUserRefFromToken)(token);
49
+ const { ref, name } = validatedRequest;
50
+ logger.verbose("call to updateWorkspace", { ref, userRef });
51
+ const isMember = yield (0, isWorkspaceMember_1.isWorkspaceMember)(prisma)(ref, userRef);
52
+ if (!isMember) {
53
+ callback({
54
+ code: grpc_js_1.status.PERMISSION_DENIED,
55
+ message: "User is not a member of the workspace"
56
+ });
57
+ }
58
+ yield prisma.workspace.update({
59
+ where: {
60
+ ref
61
+ },
62
+ data: {
63
+ name
64
+ }
65
+ });
66
+ const response = {
67
+ ref
68
+ };
69
+ callback(null, response);
70
+ }
71
+ catch (error) {
72
+ (0, common_1.handleError)(error, callback);
73
+ }
74
+ });
75
+ }
76
+ exports.updateWorkspace = updateWorkspace;
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@fonoster/identity",
3
+ "version": "0.6.1-alpha.0",
4
+ "description": "Identity service for Fonoster",
5
+ "author": "Pedro Sanders <psanders@fonoster.com>",
6
+ "homepage": "https://github.com/fonoster/fonoster#readme",
7
+ "license": "MIT",
8
+ "main": "dist/index",
9
+ "types": "dist/index",
10
+ "directories": {
11
+ "src": "src",
12
+ "test": "test"
13
+ },
14
+ "scripts": {
15
+ "prebuild": "rimraf ./dist tsconfig.tsbuildinfo",
16
+ "build": "tsc -b tsconfig.json",
17
+ "clean": "rimraf ./dist node_modules tsconfig.tsbuildinfo"
18
+ },
19
+ "bin": {
20
+ "fonoster": "./dist/index.js"
21
+ },
22
+ "dependencies": {
23
+ "@fonoster/common": "^0.6.1-alpha.0",
24
+ "@fonoster/logger": "^0.6.1-alpha.0",
25
+ "@grpc/grpc-js": "^1.10.6",
26
+ "@prisma/client": "^5.14.0",
27
+ "jsonwebtoken": "^9.0.2",
28
+ "jwt-decode": "^4.0.0",
29
+ "nanoid": "^3.3.6",
30
+ "prisma-field-encryption": "^1.5.2",
31
+ "zod": "^3.23.8"
32
+ },
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/fonoster/fonoster.git"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/fonoster/fonoster/issues"
45
+ },
46
+ "devDependencies": {
47
+ "@types/jsonwebtoken": "^9.0.6"
48
+ },
49
+ "gitHead": "2cdd1508146747550fe048c35d9a010d04f6d3aa"
50
+ }