@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/dist/errors.js ADDED
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.permissionDeniedError = exports.unauthenticatedError = 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
+ const grpc_js_1 = require("@grpc/grpc-js");
24
+ const unauthenticatedError = (call) => (0, common_1.createInterceptingCall)({
25
+ call,
26
+ code: grpc_js_1.status.UNAUTHENTICATED,
27
+ details: "Invalid or expired token"
28
+ });
29
+ exports.unauthenticatedError = unauthenticatedError;
30
+ const permissionDeniedError = (call) => (0, common_1.createInterceptingCall)({
31
+ call,
32
+ code: grpc_js_1.status.PERMISSION_DENIED,
33
+ details: "Permission denied"
34
+ });
35
+ exports.permissionDeniedError = permissionDeniedError;
@@ -0,0 +1,6 @@
1
+ declare enum TokenUseEnum {
2
+ ID = "id",
3
+ ACCESS = "access",
4
+ REFRESH = "refresh"
5
+ }
6
+ export { TokenUseEnum };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TokenUseEnum = 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 TokenUseEnum;
23
+ (function (TokenUseEnum) {
24
+ TokenUseEnum["ID"] = "id";
25
+ TokenUseEnum["ACCESS"] = "access";
26
+ TokenUseEnum["REFRESH"] = "refresh";
27
+ })(TokenUseEnum || (exports.TokenUseEnum = TokenUseEnum = {}));
@@ -0,0 +1,24 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { z } from "zod";
3
+ import { IdentityConfig } from "./types";
4
+ import { Prisma } from "../db";
5
+ declare const ExchangeApiKeysRequestSchema: z.ZodObject<{
6
+ accessKeyId: z.ZodString;
7
+ accessKeySecret: z.ZodString;
8
+ }, "strip", z.ZodTypeAny, {
9
+ accessKeyId?: string;
10
+ accessKeySecret?: string;
11
+ }, {
12
+ accessKeyId?: string;
13
+ accessKeySecret?: string;
14
+ }>;
15
+ type ExchangeApiKeysRequest = z.infer<typeof ExchangeApiKeysRequestSchema>;
16
+ type ExchangeApiKeysResponse = {
17
+ idToken: string;
18
+ accessToken: string;
19
+ refreshToken: string;
20
+ };
21
+ declare function exchangeApiKey(prisma: Prisma, identityConfig: IdentityConfig): (call: {
22
+ request: ExchangeApiKeysRequest;
23
+ }, callback: (error: GrpcErrorMessage, response?: ExchangeApiKeysResponse) => void) => Promise<void>;
24
+ export { exchangeApiKey };
@@ -0,0 +1,86 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.exchangeApiKey = void 0;
36
+ /*
37
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
38
+ * http://github.com/fonoster/fonoster
39
+ *
40
+ * This file is part of Fonoster
41
+ *
42
+ * Licensed under the MIT License (the "License");
43
+ * you may not use this file except in compliance with
44
+ * the License. You may obtain a copy of the License at
45
+ *
46
+ * https://opensource.org/licenses/MIT
47
+ *
48
+ * Unless required by applicable law or agreed to in writing, software
49
+ * distributed under the License is distributed on an "AS IS" BASIS,
50
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51
+ * See the License for the specific language governing permissions and
52
+ * limitations under the License.
53
+ */
54
+ const common_1 = require("@fonoster/common");
55
+ const logger_1 = require("@fonoster/logger");
56
+ const grpc = __importStar(require("@grpc/grpc-js"));
57
+ const zod_1 = require("zod");
58
+ const exchangeTokens_1 = require("./exchangeTokens");
59
+ const getApiKeyByAccessKeyId_1 = require("../utils/getApiKeyByAccessKeyId");
60
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
61
+ const ExchangeApiKeysRequestSchema = zod_1.z.object({
62
+ accessKeyId: zod_1.z.string(),
63
+ accessKeySecret: zod_1.z.string()
64
+ });
65
+ const invalidApiKeyError = {
66
+ code: grpc.status.PERMISSION_DENIED,
67
+ message: "Invalid credentials"
68
+ };
69
+ function exchangeApiKey(prisma, identityConfig) {
70
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
71
+ try {
72
+ const validatedRequest = ExchangeApiKeysRequestSchema.parse(call.request);
73
+ const { accessKeyId, accessKeySecret } = validatedRequest;
74
+ logger.verbose("call to exchangeApiKey", { accessKeyId });
75
+ const key = yield (0, getApiKeyByAccessKeyId_1.getApiKeyByAccessKeyId)(prisma)(accessKeyId);
76
+ if ((key === null || key === void 0 ? void 0 : key.accessKeySecret) !== (accessKeySecret === null || accessKeySecret === void 0 ? void 0 : accessKeySecret.trim())) {
77
+ return callback(invalidApiKeyError);
78
+ }
79
+ return callback(null, yield (0, exchangeTokens_1.exchangeTokens)(prisma, identityConfig)(accessKeyId));
80
+ }
81
+ catch (error) {
82
+ (0, common_1.handleError)(error, callback);
83
+ }
84
+ });
85
+ }
86
+ exports.exchangeApiKey = exchangeApiKey;
@@ -0,0 +1,24 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { z } from "zod";
3
+ import { IdentityConfig } from "./types";
4
+ import { Prisma } from "../db";
5
+ declare const ExchangeCredentialsRequestSchema: z.ZodObject<{
6
+ username: z.ZodString;
7
+ password: z.ZodString;
8
+ }, "strip", z.ZodTypeAny, {
9
+ password?: string;
10
+ username?: string;
11
+ }, {
12
+ password?: string;
13
+ username?: string;
14
+ }>;
15
+ type ExchangeCredentialsRequest = z.infer<typeof ExchangeCredentialsRequestSchema>;
16
+ type ExchangeCredentialsResponse = {
17
+ idToken: string;
18
+ accessToken: string;
19
+ refreshToken: string;
20
+ };
21
+ declare function exchangeCredentials(prisma: Prisma, identityConfig: IdentityConfig): (call: {
22
+ request: ExchangeCredentialsRequest;
23
+ }, callback: (error: GrpcErrorMessage, response?: ExchangeCredentialsResponse) => void) => Promise<void>;
24
+ export { exchangeCredentials };
@@ -0,0 +1,86 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.exchangeCredentials = void 0;
36
+ /*
37
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
38
+ * http://github.com/fonoster/fonoster
39
+ *
40
+ * This file is part of Fonoster
41
+ *
42
+ * Licensed under the MIT License (the "License");
43
+ * you may not use this file except in compliance with
44
+ * the License. You may obtain a copy of the License at
45
+ *
46
+ * https://opensource.org/licenses/MIT
47
+ *
48
+ * Unless required by applicable law or agreed to in writing, software
49
+ * distributed under the License is distributed on an "AS IS" BASIS,
50
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51
+ * See the License for the specific language governing permissions and
52
+ * limitations under the License.
53
+ */
54
+ const common_1 = require("@fonoster/common");
55
+ const logger_1 = require("@fonoster/logger");
56
+ const grpc = __importStar(require("@grpc/grpc-js"));
57
+ const zod_1 = require("zod");
58
+ const exchangeTokens_1 = require("./exchangeTokens");
59
+ const getUserByEmail_1 = require("../utils/getUserByEmail");
60
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
61
+ const ExchangeCredentialsRequestSchema = zod_1.z.object({
62
+ username: zod_1.z.string(),
63
+ password: zod_1.z.string()
64
+ });
65
+ const invalidCredentialsError = {
66
+ code: grpc.status.PERMISSION_DENIED,
67
+ message: "Invalid credentials"
68
+ };
69
+ function exchangeCredentials(prisma, identityConfig) {
70
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
71
+ try {
72
+ const validatedRequest = ExchangeCredentialsRequestSchema.parse(call.request);
73
+ const { username, password } = validatedRequest;
74
+ logger.verbose("call to exchangeCredentials", { username });
75
+ const user = yield (0, getUserByEmail_1.getUserByEmail)(prisma)(username);
76
+ if (!user || user.password !== (password === null || password === void 0 ? void 0 : password.trim())) {
77
+ return callback(invalidCredentialsError);
78
+ }
79
+ return callback(null, yield (0, exchangeTokens_1.exchangeTokens)(prisma, identityConfig)(user.accessKeyId));
80
+ }
81
+ catch (error) {
82
+ (0, common_1.handleError)(error, callback);
83
+ }
84
+ });
85
+ }
86
+ exports.exchangeCredentials = exchangeCredentials;
@@ -0,0 +1,21 @@
1
+ import { GrpcErrorMessage } from "@fonoster/common";
2
+ import { z } from "zod";
3
+ import { IdentityConfig } from "./types";
4
+ import { Prisma } from "../db";
5
+ declare const ExchangeRefreshTokenRequestSchema: z.ZodObject<{
6
+ refreshToken: z.ZodString;
7
+ }, "strip", z.ZodTypeAny, {
8
+ refreshToken?: string;
9
+ }, {
10
+ refreshToken?: string;
11
+ }>;
12
+ type ExchangeRefreshTokenRequest = z.infer<typeof ExchangeRefreshTokenRequestSchema>;
13
+ type ExchangeCredentialsResponse = {
14
+ idToken: string;
15
+ accessToken: string;
16
+ refreshToken: string;
17
+ };
18
+ declare function exchangeRefreshToken(prisma: Prisma, identityConfig: IdentityConfig): (call: {
19
+ request: ExchangeRefreshTokenRequest;
20
+ }, callback: (error: GrpcErrorMessage, response?: ExchangeCredentialsResponse) => void) => Promise<void>;
21
+ export { exchangeRefreshToken };
@@ -0,0 +1,62 @@
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.exchangeRefreshToken = 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 common_1 = require("@fonoster/common");
35
+ const logger_1 = require("@fonoster/logger");
36
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
37
+ const zod_1 = require("zod");
38
+ const exchangeTokens_1 = require("./exchangeTokens");
39
+ const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
40
+ const ExchangeRefreshTokenRequestSchema = zod_1.z.object({
41
+ refreshToken: zod_1.z.string()
42
+ });
43
+ const SIGN_ALGORITHM = "RS256";
44
+ function exchangeRefreshToken(prisma, identityConfig) {
45
+ return (call, callback) => __awaiter(this, void 0, void 0, function* () {
46
+ try {
47
+ const validatedRequest = ExchangeRefreshTokenRequestSchema.parse(call.request);
48
+ const { refreshToken: oldRefreshToken } = validatedRequest;
49
+ const { privateKey } = identityConfig;
50
+ const oldRefreshTokenDecoded = jsonwebtoken_1.default.verify(oldRefreshToken, privateKey, {
51
+ algorithms: [SIGN_ALGORITHM]
52
+ });
53
+ const { accessKeyId } = oldRefreshTokenDecoded;
54
+ logger.verbose("call to exchangeRefreshToken", { accessKeyId });
55
+ return callback(null, yield (0, exchangeTokens_1.exchangeTokens)(prisma, identityConfig)(accessKeyId));
56
+ }
57
+ catch (error) {
58
+ (0, common_1.handleError)(error, callback);
59
+ }
60
+ });
61
+ }
62
+ exports.exchangeRefreshToken = exchangeRefreshToken;
@@ -0,0 +1,8 @@
1
+ import { IdentityConfig } from "./types";
2
+ import { Prisma } from "../db";
3
+ declare function exchangeTokens(prisma: Prisma, identityConfig: IdentityConfig): (accessKeyId: string) => Promise<{
4
+ idToken: any;
5
+ accessToken: any;
6
+ refreshToken: any;
7
+ }>;
8
+ export { exchangeTokens };
@@ -0,0 +1,92 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.exchangeTokens = void 0;
39
+ /*
40
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
41
+ * http://github.com/fonoster/fonoster
42
+ *
43
+ * This file is part of Fonoster
44
+ *
45
+ * Licensed under the MIT License (the "License");
46
+ * you may not use this file except in compliance with
47
+ * the License. You may obtain a copy of the License at
48
+ *
49
+ * https://opensource.org/licenses/MIT
50
+ *
51
+ * Unless required by applicable law or agreed to in writing, software
52
+ * distributed under the License is distributed on an "AS IS" BASIS,
53
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
54
+ * See the License for the specific language governing permissions and
55
+ * limitations under the License.
56
+ */
57
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
58
+ const AK = __importStar(require("./payloads/apikeys"));
59
+ const US = __importStar(require("./payloads/users"));
60
+ const SIGN_ALGORITHM = "RS256";
61
+ // prettier-ignore
62
+ function exchangeTokens(prisma, identityConfig) {
63
+ return (accessKeyId) => __awaiter(this, void 0, void 0, function* () {
64
+ const { privateKey, idTokenExpiresIn, accessTokenExpiresIn, refreshTokenExpiresIn } = identityConfig;
65
+ const idTokenSignOptions = { algorithm: SIGN_ALGORITHM, expiresIn: idTokenExpiresIn };
66
+ const accessTokenSignOptions = { algorithm: SIGN_ALGORITHM, expiresIn: accessTokenExpiresIn };
67
+ const refreshTokenSignOptions = { algorithm: SIGN_ALGORITHM, expiresIn: refreshTokenExpiresIn };
68
+ let idToken = null;
69
+ let accessToken = null;
70
+ let refreshToken = null;
71
+ if (accessKeyId.startsWith("US")) {
72
+ const idTokenPayload = yield US.getIdTokenPayload(prisma, identityConfig)(accessKeyId);
73
+ const accessTokenPayload = yield US.getAccessTokenPayload(prisma, identityConfig)(accessKeyId);
74
+ const refreshTokenPayload = yield US.getRefreshTokenPayload(prisma, identityConfig)(accessKeyId);
75
+ idToken = jsonwebtoken_1.default.sign(idTokenPayload, privateKey, idTokenSignOptions);
76
+ accessToken = jsonwebtoken_1.default.sign(accessTokenPayload, privateKey, accessTokenSignOptions);
77
+ refreshToken = jsonwebtoken_1.default.sign(refreshTokenPayload, privateKey, refreshTokenSignOptions);
78
+ }
79
+ else {
80
+ const accessTokenPayload = yield AK.getAccessTokenPayload(prisma, identityConfig)(accessKeyId);
81
+ const refreshTokenPayload = yield AK.getRefreshTokenPayload(prisma, identityConfig)(accessKeyId);
82
+ accessToken = jsonwebtoken_1.default.sign(accessTokenPayload, privateKey, accessTokenSignOptions);
83
+ refreshToken = jsonwebtoken_1.default.sign(refreshTokenPayload, privateKey, refreshTokenSignOptions);
84
+ }
85
+ return {
86
+ idToken,
87
+ accessToken,
88
+ refreshToken
89
+ };
90
+ });
91
+ }
92
+ exports.exchangeTokens = exchangeTokens;
@@ -0,0 +1,5 @@
1
+ export * from "./exchangeApiKey";
2
+ export * from "./exchangeCredentials";
3
+ export * from "./exchangeRefreshToken";
4
+ export * from "./TokenUseEnum";
5
+ export * from "./types";
@@ -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("./exchangeApiKey"), exports);
36
+ __exportStar(require("./exchangeCredentials"), exports);
37
+ __exportStar(require("./exchangeRefreshToken"), exports);
38
+ __exportStar(require("./TokenUseEnum"), exports);
39
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,4 @@
1
+ import { Prisma } from "../../../db";
2
+ import { AccessToken, IdentityConfig } from "../../types";
3
+ declare function getAccessTokenPayload(prisma: Prisma, identityConfig: IdentityConfig): (accessKeyId: string) => Promise<AccessToken>;
4
+ export { getAccessTokenPayload };
@@ -0,0 +1,45 @@
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.getAccessTokenPayload = void 0;
13
+ const TokenUseEnum_1 = require("../../TokenUseEnum");
14
+ function getAccessTokenPayload(prisma, identityConfig) {
15
+ return (accessKeyId) => __awaiter(this, void 0, void 0, function* () {
16
+ const apiKey = yield prisma.apiKey.findFirst({
17
+ where: {
18
+ accessKeyId
19
+ },
20
+ include: {
21
+ workspace: true
22
+ }
23
+ });
24
+ if (!apiKey) {
25
+ return null;
26
+ }
27
+ const { issuer, audience } = identityConfig;
28
+ const { ref, workspace } = apiKey;
29
+ const access = [
30
+ {
31
+ accessKeyId: workspace.accessKeyId,
32
+ role: apiKey.role
33
+ }
34
+ ];
35
+ return {
36
+ iss: issuer,
37
+ sub: ref,
38
+ aud: audience,
39
+ tokenUse: TokenUseEnum_1.TokenUseEnum.ACCESS,
40
+ accessKeyId,
41
+ access
42
+ };
43
+ });
44
+ }
45
+ exports.getAccessTokenPayload = getAccessTokenPayload;
@@ -0,0 +1,4 @@
1
+ import { Prisma } from "../../../db";
2
+ import { IdentityConfig, RefreshToken } from "../../types";
3
+ declare function getRefreshTokenPayload(prisma: Prisma, identityConfig: IdentityConfig): (accessKeyId: string) => Promise<RefreshToken>;
4
+ export { getRefreshTokenPayload };
@@ -0,0 +1,32 @@
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.getRefreshTokenPayload = void 0;
13
+ const buildRefreshTokenPayload_1 = require("../buildRefreshTokenPayload");
14
+ function getRefreshTokenPayload(prisma, identityConfig) {
15
+ return (accessKeyId) => __awaiter(this, void 0, void 0, function* () {
16
+ const apiKey = yield prisma.apiKey.findFirst({
17
+ where: {
18
+ accessKeyId
19
+ }
20
+ });
21
+ if (!apiKey) {
22
+ return null;
23
+ }
24
+ const { ref: identityRef } = apiKey;
25
+ return (0, buildRefreshTokenPayload_1.buildRefreshTokenPayload)({
26
+ identityConfig,
27
+ accessKeyId,
28
+ identityRef
29
+ });
30
+ });
31
+ }
32
+ exports.getRefreshTokenPayload = getRefreshTokenPayload;
@@ -0,0 +1,2 @@
1
+ export * from "./getAccessTokenPayload";
2
+ export * from "./getRefreshTokenPayload";
@@ -0,0 +1,36 @@
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("./getAccessTokenPayload"), exports);
36
+ __exportStar(require("./getRefreshTokenPayload"), exports);