@fonoster/common 0.8.24 → 0.8.26
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.
- package/dist/errors/handleError.js +2 -2
- package/dist/identity/createAuthInterceptor.d.ts +15 -0
- package/dist/identity/createAuthInterceptor.js +79 -0
- package/dist/identity/decodeToken.d.ts +3 -0
- package/dist/identity/decodeToken.js +25 -0
- package/dist/identity/errors.d.ts +4 -0
- package/dist/identity/errors.js +35 -0
- package/dist/identity/getAccessKeyIdFromCall.d.ts +3 -0
- package/dist/identity/getAccessKeyIdFromCall.js +8 -0
- package/dist/identity/getPublicKey.d.ts +5 -0
- package/dist/identity/getPublicKey.js +77 -0
- package/dist/identity/getTokenFromCall.d.ts +3 -0
- package/dist/identity/getTokenFromCall.js +8 -0
- package/dist/identity/hasAccess.d.ts +3 -0
- package/dist/identity/hasAccess.js +9 -0
- package/dist/identity/index.d.ts +9 -0
- package/dist/identity/index.js +43 -0
- package/dist/identity/isValidToken.d.ts +2 -0
- package/dist/identity/isValidToken.js +51 -0
- package/dist/identity/roles.d.ts +5 -0
- package/dist/identity/roles.js +129 -0
- package/dist/identity/tokenHasAccessKeyId.d.ts +2 -0
- package/dist/identity/tokenHasAccessKeyId.js +28 -0
- package/dist/identity/types.d.ts +44 -0
- package/dist/identity/types.js +14 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils/createFetchSingleCallByCallId.d.ts +4 -0
- package/dist/utils/{makeFetchSingleCallByCallId.js → createFetchSingleCallByCallId.js} +9 -7
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/package.json +3 -3
- package/dist/utils/makeFetchSingleCallByCallId.d.ts +0 -4
|
@@ -97,7 +97,7 @@ function handleError(error, callback) {
|
|
|
97
97
|
logAndCallback(grpc_js_1.status.INVALID_ARGUMENT, message !== null && message !== void 0 ? message : "Your request has invalid arguments", "invalid argument error");
|
|
98
98
|
break;
|
|
99
99
|
default:
|
|
100
|
-
logger.error("
|
|
101
|
-
callback({ code: grpc_js_1.status.
|
|
100
|
+
logger.error("internal server error:", error);
|
|
101
|
+
callback({ code: grpc_js_1.status.INTERNAL, message: "Internal server error" });
|
|
102
102
|
}
|
|
103
103
|
}
|
|
@@ -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,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAuthInterceptor = createAuthInterceptor;
|
|
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 roles_1 = require("./roles");
|
|
24
|
+
const getAccessKeyIdFromCall_1 = require("./getAccessKeyIdFromCall");
|
|
25
|
+
const getTokenFromCall_1 = require("./getTokenFromCall");
|
|
26
|
+
const isValidToken_1 = require("./isValidToken");
|
|
27
|
+
const decodeToken_1 = require("./decodeToken");
|
|
28
|
+
const hasAccess_1 = require("./hasAccess");
|
|
29
|
+
const tokenHasAccessKeyId_1 = require("./tokenHasAccessKeyId");
|
|
30
|
+
const errors_1 = require("./errors");
|
|
31
|
+
const logger = (0, logger_1.getLogger)({ service: "common", filePath: __filename });
|
|
32
|
+
/**
|
|
33
|
+
* This function is a gRPC interceptor that checks if the request is valid
|
|
34
|
+
* and if the user has the right permissions to access the resource. When
|
|
35
|
+
* validating the request, the function will check if the request is in the
|
|
36
|
+
* skip list, if the token is valid and if the role is allowed by the RBAC.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} identityPublicKey - The public key to validate the token
|
|
39
|
+
* @param {string[]} publicPath - The list of public paths
|
|
40
|
+
* @return {Function} - The gRPC interceptor
|
|
41
|
+
*/
|
|
42
|
+
function createAuthInterceptor(identityPublicKey, publicPath) {
|
|
43
|
+
/**
|
|
44
|
+
* Inner function that will be called by the gRPC server.
|
|
45
|
+
*
|
|
46
|
+
* @param {object} methodDefinition - The method definition
|
|
47
|
+
* @param {string} methodDefinition.path - The path of the gRPC method
|
|
48
|
+
* @param {ServerInterceptingCall} call - The call object
|
|
49
|
+
* @return {ServerInterceptingCall} - The modified call object
|
|
50
|
+
*/
|
|
51
|
+
return (methodDefinition, call) => {
|
|
52
|
+
const { path } = methodDefinition;
|
|
53
|
+
const accessKeyId = (0, getAccessKeyIdFromCall_1.getAccessKeyIdFromCall)(call);
|
|
54
|
+
logger.verbose("intercepting api call to path", { accessKeyId, path });
|
|
55
|
+
if (publicPath.includes(methodDefinition.path)) {
|
|
56
|
+
logger.verbose("passing auth control to edge function", { path });
|
|
57
|
+
return call;
|
|
58
|
+
}
|
|
59
|
+
const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
|
|
60
|
+
logger.verbose("validating token", { accessKeyId, path });
|
|
61
|
+
if (!(0, isValidToken_1.isValidToken)(token, identityPublicKey)) {
|
|
62
|
+
return (0, errors_1.unauthenticatedError)(call);
|
|
63
|
+
}
|
|
64
|
+
const decodedToken = (0, decodeToken_1.decodeToken)(token);
|
|
65
|
+
logger.verbose("checking access for accessKeyId", {
|
|
66
|
+
accessKeyId,
|
|
67
|
+
path,
|
|
68
|
+
hasAccess: (0, hasAccess_1.hasAccess)(decodedToken.access, path),
|
|
69
|
+
pathIsWorkspacePath: roles_1.workspaceAccess.includes(path),
|
|
70
|
+
tokenHasAccessKeyId: (0, tokenHasAccessKeyId_1.tokenHasAccessKeyId)(token, accessKeyId)
|
|
71
|
+
});
|
|
72
|
+
if (!(0, hasAccess_1.hasAccess)(decodedToken.access, path) ||
|
|
73
|
+
(roles_1.workspaceAccess.includes(path) &&
|
|
74
|
+
!(0, tokenHasAccessKeyId_1.tokenHasAccessKeyId)(token, accessKeyId))) {
|
|
75
|
+
return (0, errors_1.permissionDeniedError)(call);
|
|
76
|
+
}
|
|
77
|
+
return call;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeToken = decodeToken;
|
|
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
|
+
}
|
|
@@ -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 { permissionDeniedError, unauthenticatedError };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unauthenticatedError = exports.permissionDeniedError = 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 grpc_js_1 = require("@grpc/grpc-js");
|
|
23
|
+
const utils_1 = require("../utils");
|
|
24
|
+
const unauthenticatedError = (call) => (0, utils_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, utils_1.createInterceptingCall)({
|
|
31
|
+
call,
|
|
32
|
+
code: grpc_js_1.status.PERMISSION_DENIED,
|
|
33
|
+
details: "Permission denied"
|
|
34
|
+
});
|
|
35
|
+
exports.permissionDeniedError = permissionDeniedError;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAccessKeyIdFromCall = getAccessKeyIdFromCall;
|
|
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
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getPublicKey = getPublicKey;
|
|
37
|
+
/*
|
|
38
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
39
|
+
* http://github.com/fonoster/fonoster
|
|
40
|
+
*
|
|
41
|
+
* This file is part of Fonoster
|
|
42
|
+
*
|
|
43
|
+
* Licensed under the MIT License (the "License");
|
|
44
|
+
* you may not use this file except in compliance with
|
|
45
|
+
* the License. You may obtain a copy of the License at
|
|
46
|
+
*
|
|
47
|
+
* https://opensource.org/licenses/MIT
|
|
48
|
+
*
|
|
49
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
50
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
51
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
52
|
+
* See the License for the specific language governing permissions and
|
|
53
|
+
* limitations under the License.
|
|
54
|
+
*/
|
|
55
|
+
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
56
|
+
const utils_1 = require("../utils");
|
|
57
|
+
const IdentityServiceClient = grpc.makeGenericClientConstructor((0, utils_1.createServiceDefinition)({
|
|
58
|
+
serviceName: "Identity",
|
|
59
|
+
pckg: "identity",
|
|
60
|
+
proto: "identity.proto",
|
|
61
|
+
version: "v1beta2"
|
|
62
|
+
}), "", {});
|
|
63
|
+
function getPublicKey(endpoint, allowInsecure = false) {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
const client = new IdentityServiceClient(endpoint, allowInsecure
|
|
66
|
+
? grpc.credentials.createInsecure()
|
|
67
|
+
: grpc.credentials.createSsl());
|
|
68
|
+
client.getPublicKey({}, (error, response) => {
|
|
69
|
+
if (error) {
|
|
70
|
+
reject(error);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
resolve(response);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTokenFromCall = getTokenFromCall;
|
|
4
|
+
function getTokenFromCall(call) {
|
|
5
|
+
var _a;
|
|
6
|
+
const metadata = call.metadata.getMap();
|
|
7
|
+
return (_a = metadata["token"]) === null || _a === void 0 ? void 0 : _a.toString();
|
|
8
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hasAccess = hasAccess;
|
|
4
|
+
const roles_1 = require("./roles");
|
|
5
|
+
// This function only checks if the role has access to the grpc method
|
|
6
|
+
function hasAccess(access, method) {
|
|
7
|
+
const roleList = access.map((a) => a.role);
|
|
8
|
+
return roleList.some((r) => roles_1.roles.find((role) => role.name === r && role.access.includes(method)));
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./createAuthInterceptor";
|
|
2
|
+
export * from "./decodeToken";
|
|
3
|
+
export * from "./getPublicKey";
|
|
4
|
+
export * from "./getAccessKeyIdFromCall";
|
|
5
|
+
export * from "./getTokenFromCall";
|
|
6
|
+
export * from "./hasAccess";
|
|
7
|
+
export * from "./isValidToken";
|
|
8
|
+
export * from "./types";
|
|
9
|
+
export * from "./roles";
|
|
@@ -0,0 +1,43 @@
|
|
|
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("./createAuthInterceptor"), exports);
|
|
36
|
+
__exportStar(require("./decodeToken"), exports);
|
|
37
|
+
__exportStar(require("./getPublicKey"), exports);
|
|
38
|
+
__exportStar(require("./getAccessKeyIdFromCall"), exports);
|
|
39
|
+
__exportStar(require("./getTokenFromCall"), exports);
|
|
40
|
+
__exportStar(require("./hasAccess"), exports);
|
|
41
|
+
__exportStar(require("./isValidToken"), exports);
|
|
42
|
+
__exportStar(require("./types"), exports);
|
|
43
|
+
__exportStar(require("./roles"), exports);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isValidToken = isValidToken;
|
|
7
|
+
/*
|
|
8
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
9
|
+
* http://github.com/fonoster/fonoster
|
|
10
|
+
*
|
|
11
|
+
* This file is part of Fonoster
|
|
12
|
+
*
|
|
13
|
+
* Licensed under the MIT License (the "License");
|
|
14
|
+
* you may not use this file except in compliance with
|
|
15
|
+
* the License. You may obtain a copy of the License at
|
|
16
|
+
*
|
|
17
|
+
* https://opensource.org/licenses/MIT
|
|
18
|
+
*
|
|
19
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
* See the License for the specific language governing permissions and
|
|
23
|
+
* limitations under the License.
|
|
24
|
+
*/
|
|
25
|
+
const logger_1 = require("@fonoster/logger");
|
|
26
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
27
|
+
const types_1 = require("./types");
|
|
28
|
+
const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
|
|
29
|
+
function isValidToken(token, secret) {
|
|
30
|
+
try {
|
|
31
|
+
const decoded = jsonwebtoken_1.default.verify(token, secret);
|
|
32
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
|
33
|
+
if (decoded.exp <= currentTime) {
|
|
34
|
+
logger.verbose("token expired", { exp: decoded.exp, currentTime });
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error.name === types_1.JsonWebErrorEnum.JsonWebTokenError) {
|
|
41
|
+
logger.verbose("invalid JWT token", { token });
|
|
42
|
+
}
|
|
43
|
+
else if (error.name === types_1.JsonWebErrorEnum.TokenExpiredError) {
|
|
44
|
+
logger.verbose("token expired", { token });
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
logger.verbose("unexpected JWT error:", error);
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.workspaceAccess = exports.roles = exports.VOICE_SERVICE_ROLE = void 0;
|
|
4
|
+
/* eslint-disable sonarjs/no-duplicate-string */
|
|
5
|
+
/*
|
|
6
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
7
|
+
* http://github.com/fonoster/fonoster
|
|
8
|
+
*
|
|
9
|
+
* This file is part of Fonoster
|
|
10
|
+
*
|
|
11
|
+
* Licensed under the MIT License (the "License");
|
|
12
|
+
* you may not use this file except in compliance with
|
|
13
|
+
* the License. You may obtain a copy of the License at
|
|
14
|
+
*
|
|
15
|
+
* https://opensource.org/licenses/MIT
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
* See the License for the specific language governing permissions and
|
|
21
|
+
* limitations under the License.
|
|
22
|
+
*/
|
|
23
|
+
const types_1 = require("@fonoster/types");
|
|
24
|
+
const VOICE_SERVICE_ROLE = "VOICE_SERVICE";
|
|
25
|
+
exports.VOICE_SERVICE_ROLE = VOICE_SERVICE_ROLE;
|
|
26
|
+
const workspaceAccess = [
|
|
27
|
+
"/fonoster.applications.v1beta2.Applications/CreateApplication",
|
|
28
|
+
"/fonoster.applications.v1beta2.Applications/UpdateApplication",
|
|
29
|
+
"/fonoster.applications.v1beta2.Applications/GetApplication",
|
|
30
|
+
"/fonoster.applications.v1beta2.Applications/DeleteApplication",
|
|
31
|
+
"/fonoster.applications.v1beta2.Applications/ListApplications",
|
|
32
|
+
"/fonoster.agents.v1beta2.Agents/CreateAgent",
|
|
33
|
+
"/fonoster.agents.v1beta2.Agents/UpdateAgent",
|
|
34
|
+
"/fonoster.agents.v1beta2.Agents/GetAgent",
|
|
35
|
+
"/fonoster.agents.v1beta2.Agents/DeleteAgent",
|
|
36
|
+
"/fonoster.agents.v1beta2.Agents/ListAgents",
|
|
37
|
+
"/fonoster.acls.v1beta2.Acls/CreateAcl",
|
|
38
|
+
"/fonoster.acls.v1beta2.Acls/UpdateAcl",
|
|
39
|
+
"/fonoster.acls.v1beta2.Acls/ListAcls",
|
|
40
|
+
"/fonoster.acls.v1beta2.Acls/GetAcl",
|
|
41
|
+
"/fonoster.acls.v1beta2.Acls/DeleteAcl",
|
|
42
|
+
"/fonoster.credentials.v1beta2.CredentialsService/CreateCredentials",
|
|
43
|
+
"/fonoster.credentials.v1beta2.CredentialsService/UpdateCredentials",
|
|
44
|
+
"/fonoster.credentials.v1beta2.CredentialsService/GetCredentials",
|
|
45
|
+
"/fonoster.credentials.v1beta2.CredentialsService/DeleteCredentials",
|
|
46
|
+
"/fonoster.credentials.v1beta2.CredentialsService/ListCredentials",
|
|
47
|
+
"/fonoster.domains.v1beta2.Domains/CreateDomain",
|
|
48
|
+
"/fonoster.domains.v1beta2.Domains/UpdateDomain",
|
|
49
|
+
"/fonoster.domains.v1beta2.Domains/GetDomain",
|
|
50
|
+
"/fonoster.domains.v1beta2.Domains/DeleteDomain",
|
|
51
|
+
"/fonoster.domains.v1beta2.Domains/ListDomains",
|
|
52
|
+
"/fonoster.trunks.v1beta2.Trunks/CreateTrunk",
|
|
53
|
+
"/fonoster.trunks.v1beta2.Trunks/UpdateTrunk",
|
|
54
|
+
"/fonoster.trunks.v1beta2.Trunks/GetTrunk",
|
|
55
|
+
"/fonoster.trunks.v1beta2.Trunks/DeleteTrunk",
|
|
56
|
+
"/fonoster.trunks.v1beta2.Trunks/ListTrunks",
|
|
57
|
+
"/fonoster.numbers.v1beta2.Numbers/CreateNumber",
|
|
58
|
+
"/fonoster.numbers.v1beta2.Numbers/UpdateNumber",
|
|
59
|
+
"/fonoster.numbers.v1beta2.Numbers/GetNumber",
|
|
60
|
+
"/fonoster.numbers.v1beta2.Numbers/DeleteNumber",
|
|
61
|
+
"/fonoster.numbers.v1beta2.Numbers/ListNumbers",
|
|
62
|
+
"/fonoster.secrets.v1beta2.Secrets/CreateSecret",
|
|
63
|
+
"/fonoster.secrets.v1beta2.Secrets/UpdateSecret",
|
|
64
|
+
"/fonoster.secrets.v1beta2.Secrets/GetSecret",
|
|
65
|
+
"/fonoster.secrets.v1beta2.Secrets/DeleteSecret",
|
|
66
|
+
"/fonoster.secrets.v1beta2.Secrets/ListSecrets",
|
|
67
|
+
"/fonoster.calls.v1beta2.Calls/CreateCall",
|
|
68
|
+
"/fonoster.calls.v1beta2.Calls/ListCalls",
|
|
69
|
+
"/fonoster.calls.v1beta2.Calls/GetCall",
|
|
70
|
+
"/fonoster.calls.v1beta2.Calls/TrackCall",
|
|
71
|
+
"/fonoster.voice.v1beta2.Voice/CreateSession"
|
|
72
|
+
];
|
|
73
|
+
exports.workspaceAccess = workspaceAccess;
|
|
74
|
+
const fullIdentityAccess = [
|
|
75
|
+
"/fonoster.identity.v1beta2.Identity/GetUser",
|
|
76
|
+
"/fonoster.identity.v1beta2.Identity/UpdateUser",
|
|
77
|
+
"/fonoster.identity.v1beta2.Identity/DeleteUser",
|
|
78
|
+
"/fonoster.identity.v1beta2.Identity/CreateWorkspace",
|
|
79
|
+
"/fonoster.identity.v1beta2.Identity/GetWorkspace",
|
|
80
|
+
"/fonoster.identity.v1beta2.Identity/UpdateWorkspace",
|
|
81
|
+
"/fonoster.identity.v1beta2.Identity/ListWorkspaces",
|
|
82
|
+
"/fonoster.identity.v1beta2.Identity/DeleteWorkspace",
|
|
83
|
+
"/fonoster.identity.v1beta2.Identity/InviteUserToWorkspace",
|
|
84
|
+
"/fonoster.identity.v1beta2.Identity/RemoveUserFromWorkspace",
|
|
85
|
+
"/fonoster.identity.v1beta2.Identity/ResendWorkspaceMembershipInvitation",
|
|
86
|
+
"/fonoster.identity.v1beta2.Identity/RefreshToken",
|
|
87
|
+
"/fonoster.identity.v1beta2.Identity/CreateApiKey",
|
|
88
|
+
"/fonoster.identity.v1beta2.Identity/DeleteApiKey",
|
|
89
|
+
"/fonoster.identity.v1beta2.Identity/ListApiKeys",
|
|
90
|
+
"/fonoster.identity.v1beta2.Identity/RegenerateApiKey"
|
|
91
|
+
];
|
|
92
|
+
const roles = [
|
|
93
|
+
{
|
|
94
|
+
name: types_1.WorkspaceRoleEnum.OWNER,
|
|
95
|
+
description: "Access to all endpoints",
|
|
96
|
+
access: [...fullIdentityAccess, ...workspaceAccess]
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: types_1.WorkspaceRoleEnum.ADMIN,
|
|
100
|
+
description: "Access to all endpoints",
|
|
101
|
+
access: [...fullIdentityAccess, ...workspaceAccess]
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: types_1.WorkspaceRoleEnum.USER,
|
|
105
|
+
description: "Access to User and Workspace endpoints",
|
|
106
|
+
access: [
|
|
107
|
+
"/fonoster.identity.v1beta2.Identity/GetUser",
|
|
108
|
+
"/fonoster.identity.v1beta2.Identity/UpdateUser",
|
|
109
|
+
"/fonoster.identity.v1beta2.Identity/DeleteUser",
|
|
110
|
+
"/fonoster.identity.v1beta2.Identity/CreateWorkspace",
|
|
111
|
+
"/fonoster.identity.v1beta2.Identity/GetWorkspace",
|
|
112
|
+
"/fonoster.identity.v1beta2.Identity/UpdateWorkspace",
|
|
113
|
+
"/fonoster.identity.v1beta2.Identity/ListWorkspaces",
|
|
114
|
+
"/fonoster.identity.v1beta2.Identity/RefreshToken",
|
|
115
|
+
...workspaceAccess
|
|
116
|
+
]
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: types_1.ApiRoleEnum.WORKSPACE_ADMIN,
|
|
120
|
+
description: "Access to all endpoints",
|
|
121
|
+
access: [...fullIdentityAccess, ...workspaceAccess]
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: VOICE_SERVICE_ROLE,
|
|
125
|
+
description: "Role with access only to the Voice service endpoint",
|
|
126
|
+
access: ["/fonoster.voice.v1beta2.Voice/CreateSession"]
|
|
127
|
+
}
|
|
128
|
+
];
|
|
129
|
+
exports.roles = roles;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tokenHasAccessKeyId = tokenHasAccessKeyId;
|
|
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 _1 = require(".");
|
|
23
|
+
function tokenHasAccessKeyId(token, accessKeyId) {
|
|
24
|
+
var _a;
|
|
25
|
+
const decodedToken = (0, _1.decodeToken)(token);
|
|
26
|
+
const accessKeyIds = (_a = decodedToken.access) === null || _a === void 0 ? void 0 : _a.map((a) => a.accessKeyId);
|
|
27
|
+
return accessKeyIds.includes(accessKeyId);
|
|
28
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { WorkspaceRoleEnum } from "@fonoster/types";
|
|
2
|
+
declare enum TokenUseEnum {
|
|
3
|
+
ID = "id",
|
|
4
|
+
ACCESS = "access",
|
|
5
|
+
REFRESH = "refresh"
|
|
6
|
+
}
|
|
7
|
+
declare enum JsonWebErrorEnum {
|
|
8
|
+
JsonWebTokenError = "JsonWebTokenError",
|
|
9
|
+
TokenExpiredError = "TokenExpiredError"
|
|
10
|
+
}
|
|
11
|
+
type Role = {
|
|
12
|
+
name: string;
|
|
13
|
+
description: string;
|
|
14
|
+
access: string[];
|
|
15
|
+
};
|
|
16
|
+
type Access = {
|
|
17
|
+
accessKeyId: string;
|
|
18
|
+
role: WorkspaceRoleEnum;
|
|
19
|
+
};
|
|
20
|
+
type BaseToken = {
|
|
21
|
+
iss: string;
|
|
22
|
+
sub: string;
|
|
23
|
+
aud: string;
|
|
24
|
+
exp: number;
|
|
25
|
+
iat: number;
|
|
26
|
+
tokenUse: TokenUseEnum;
|
|
27
|
+
accessKeyId: string;
|
|
28
|
+
};
|
|
29
|
+
type IdToken = BaseToken & {
|
|
30
|
+
emailVerified: boolean;
|
|
31
|
+
phoneNumberVerified: boolean;
|
|
32
|
+
phoneNumber: string;
|
|
33
|
+
email: string;
|
|
34
|
+
tokenUse: TokenUseEnum.ID;
|
|
35
|
+
};
|
|
36
|
+
type AccessToken = BaseToken & {
|
|
37
|
+
access: Access[];
|
|
38
|
+
tokenUse: TokenUseEnum.ACCESS;
|
|
39
|
+
};
|
|
40
|
+
type RefreshToken = BaseToken & {
|
|
41
|
+
tokenUse: TokenUseEnum.REFRESH;
|
|
42
|
+
};
|
|
43
|
+
type DecodedToken<T extends TokenUseEnum> = T extends TokenUseEnum.ID ? IdToken : T extends TokenUseEnum.ACCESS ? AccessToken : T extends TokenUseEnum.REFRESH ? TokenUseEnum : never;
|
|
44
|
+
export { Access, AccessToken, DecodedToken, IdToken, RefreshToken, Role, TokenUseEnum, JsonWebErrorEnum };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonWebErrorEnum = exports.TokenUseEnum = void 0;
|
|
4
|
+
var TokenUseEnum;
|
|
5
|
+
(function (TokenUseEnum) {
|
|
6
|
+
TokenUseEnum["ID"] = "id";
|
|
7
|
+
TokenUseEnum["ACCESS"] = "access";
|
|
8
|
+
TokenUseEnum["REFRESH"] = "refresh";
|
|
9
|
+
})(TokenUseEnum || (exports.TokenUseEnum = TokenUseEnum = {}));
|
|
10
|
+
var JsonWebErrorEnum;
|
|
11
|
+
(function (JsonWebErrorEnum) {
|
|
12
|
+
JsonWebErrorEnum["JsonWebTokenError"] = "JsonWebTokenError";
|
|
13
|
+
JsonWebErrorEnum["TokenExpiredError"] = "TokenExpiredError";
|
|
14
|
+
})(JsonWebErrorEnum || (exports.JsonWebErrorEnum = JsonWebErrorEnum = {}));
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.createFetchSingleCallByCallId = createFetchSingleCallByCallId;
|
|
13
13
|
/*
|
|
14
14
|
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
15
15
|
* http://github.com/fonoster/fonoster
|
|
@@ -30,9 +30,10 @@ exports.makeFetchSingleCallByCallId = makeFetchSingleCallByCallId;
|
|
|
30
30
|
*/
|
|
31
31
|
const influxdb_client_1 = require("@influxdata/influxdb-client");
|
|
32
32
|
const constants_1 = require("../constants");
|
|
33
|
-
function
|
|
34
|
-
return
|
|
35
|
-
|
|
33
|
+
function createFetchSingleCallByCallId(influxdb) {
|
|
34
|
+
return function fetchSingleCallByCallId(callId) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const query = (0, influxdb_client_1.flux) `from(bucket: "${constants_1.INFLUXDB_CALLS_BUCKET}")
|
|
36
37
|
|> range(start: -365d)
|
|
37
38
|
|> pivot(rowKey: ["callId"], columnKey: ["_field"], valueColumn: "_value")
|
|
38
39
|
|> map(fn: (r) => ({
|
|
@@ -43,7 +44,8 @@ function makeFetchSingleCallByCallId(influxdb) {
|
|
|
43
44
|
|> filter(fn: (r) => r.callId == ${callId})
|
|
44
45
|
|> sort(columns: ["_time"], desc: true)
|
|
45
46
|
|> limit(n: 1)`;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const items = (yield influxdb.collectRows(query));
|
|
48
|
+
return items.length > 0 ? items[0] : null;
|
|
49
|
+
});
|
|
50
|
+
};
|
|
49
51
|
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -44,4 +44,4 @@ __exportStar(require("./withErrorHandling"), exports);
|
|
|
44
44
|
__exportStar(require("./withErrorHandlingAndValidation"), exports);
|
|
45
45
|
__exportStar(require("./withValidation"), exports);
|
|
46
46
|
__exportStar(require("./types"), exports);
|
|
47
|
-
__exportStar(require("./
|
|
47
|
+
__exportStar(require("./createFetchSingleCallByCallId"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fonoster/common",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.26",
|
|
4
4
|
"description": "Common library for Fonoster projects",
|
|
5
5
|
"author": "Pedro Sanders <psanders@fonoster.com>",
|
|
6
6
|
"homepage": "https://github.com/fonoster/fonoster#readme",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"clean": "rimraf ./dist node_modules tsconfig.tsbuildinfo"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@fonoster/logger": "^0.8.
|
|
21
|
+
"@fonoster/logger": "^0.8.26",
|
|
22
22
|
"@grpc/grpc-js": "~1.10.6",
|
|
23
23
|
"@grpc/proto-loader": "^0.7.12",
|
|
24
24
|
"@influxdata/influxdb-client": "^1.35.0",
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/nodemailer": "^6.4.14"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "f01e634eca9a94b3a276369e998c6e75f8b75284"
|
|
50
50
|
}
|