@routr/simpleauth 2.0.5-alpha.14 → 2.0.5-alpha.16

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/runner.js CHANGED
@@ -22,22 +22,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
22
22
  * See the License for the specific language governing permissions and
23
23
  * limitations under the License.
24
24
  */
25
- const logger_1 = __importDefault(require("@fonoster/logger"));
25
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
26
+ require("./tracer").init("simpleauth");
26
27
  const service_1 = __importDefault(require("./service"));
27
- if (!process.env.PATH_TO_AUTH) {
28
- logger_1.default.error("environment variable PATH_TO_AUTH is required but was not found");
29
- process.exit(1);
30
- }
31
- const whiteList = process.env.WHITELIST ? process.env.WHITELIST.split(',') : [];
28
+ const common_1 = require("@routr/common");
29
+ const logger_1 = require("@fonoster/logger");
30
+ const logger = (0, logger_1.getLogger)({ service: "simpleauth", filePath: __filename });
31
+ common_1.Assertions.assertEnvsAreSet(["PATH_TO_AUTH"]);
32
+ const whiteList = process.env.WHITELIST ? process.env.WHITELIST.split(",") : [];
32
33
  try {
34
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
33
35
  const users = require(process.env.PATH_TO_AUTH);
34
- service_1.default({ bindAddr: process.env.BIND_ADDR || "0.0.0.0:51903", users, whiteList });
36
+ (0, service_1.default)({
37
+ bindAddr: process.env.BIND_ADDR || "0.0.0.0:51903",
38
+ users,
39
+ whiteList
40
+ });
35
41
  }
36
42
  catch (e) {
37
43
  if (e.code === "MODULE_NOT_FOUND") {
38
- logger_1.default.error(`auth file not found [path = ${process.env.PATH_TO_AUTH}]`);
44
+ logger.error(`auth file not found [path = ${process.env.PATH_TO_AUTH}]`);
39
45
  }
40
46
  else {
41
- logger_1.default.error(e);
47
+ logger.error(e);
42
48
  }
43
49
  }
package/dist/service.d.ts CHANGED
@@ -1,6 +1,15 @@
1
1
  #!/usr/bin/env node
2
- import { User } from './types';
3
- export default function SimpleAuthProcessor(config: {
2
+ import { User } from "./types";
3
+ /**
4
+ * A simple authentication middleware that authenticates users based on a list of users
5
+ * or whitelisted endpoints paths.
6
+ *
7
+ * @param {MiddlewareConfig} config - configuration for the middleware
8
+ * @param {string} config.bindAddr - address to bind to
9
+ * @param {User[]} config.users - list of users
10
+ * @param {string[]} config.whiteList - list of path that required no authentication
11
+ */
12
+ export default function simpleAuthMiddleware(config: {
4
13
  bindAddr: string;
5
14
  users: User[];
6
15
  whiteList: string[];
package/dist/service.js CHANGED
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  *
11
11
  * This file is part of Routr
12
12
  *
13
- * Licensed under the MIT License (the "License");
13
+ * Licensed under the MIT License (the "License")
14
14
  * you may not use this file except in compliance with
15
15
  * the License. You may obtain a copy of the License at
16
16
  *
@@ -22,28 +22,36 @@ Object.defineProperty(exports, "__esModule", { value: true });
22
22
  * See the License for the specific language governing permissions and
23
23
  * limitations under the License.
24
24
  */
25
- const common_1 = require("@routr/common");
26
- const tracer = common_1.Tracer.init("simpleauth");
27
25
  const api_1 = __importDefault(require("@opentelemetry/api"));
28
- const logger_1 = __importDefault(require("@fonoster/logger"));
29
- const common_2 = require("@routr/common");
26
+ const common_1 = require("@routr/common");
30
27
  const utils_1 = require("./utils");
31
28
  const processor_1 = __importDefault(require("@routr/processor"));
32
- function SimpleAuthProcessor(config) {
29
+ const logger_1 = require("@fonoster/logger");
30
+ const logger = (0, logger_1.getLogger)({ service: "simpledata", filePath: __filename });
31
+ /**
32
+ * A simple authentication middleware that authenticates users based on a list of users
33
+ * or whitelisted endpoints paths.
34
+ *
35
+ * @param {MiddlewareConfig} config - configuration for the middleware
36
+ * @param {string} config.bindAddr - address to bind to
37
+ * @param {User[]} config.users - list of users
38
+ * @param {string[]} config.whiteList - list of path that required no authentication
39
+ */
40
+ function simpleAuthMiddleware(config) {
33
41
  const { bindAddr, users, whiteList } = config;
34
- new processor_1.default({ bindAddr, name: "simpleauth" })
35
- .listen((req, res) => {
36
- logger_1.default.verbose(`authenticating ${req.message.from.address.uri.user} endpoint with simpleauth`);
37
- logger_1.default.silly(JSON.stringify(req, null, ' '));
42
+ new processor_1.default({ bindAddr, name: "simpleauth" }).listen((req, res) => {
43
+ const tracer = api_1.default.trace.getTracer("routr-tracer");
38
44
  const currentSpan = api_1.default.trace.getSpan(api_1.default.context.active());
39
- // display traceid in the terminal
40
- logger_1.default.verbose(`traceid: ${currentSpan.spanContext().traceId}`);
41
- const span = tracer.startSpan('server.js:sayHello()', {
42
- kind: 1,
43
- attributes: { key: 'value' },
44
- });
45
- span.addEvent(`invoking sayHello() to...`);
45
+ logger.silly(`authenticating ${req.message.from.address.uri.user} endpoint with simpleauth`, { traceId: currentSpan === null || currentSpan === void 0 ? void 0 : currentSpan.spanContext().traceId });
46
+ logger.silly(JSON.stringify(req, null, " "));
47
+ const span = tracer.startSpan("server.js:sayHello()", { kind: 1 });
48
+ // Consider extending the list to other message types
49
+ if (![common_1.CommonTypes.Method.INVITE, common_1.CommonTypes.Method.MESSAGE, common_1.CommonTypes.Method.REGISTER].includes(req.method)) {
50
+ return res.send(req);
51
+ }
46
52
  if (whiteList.includes(req.message.from.address.uri.user)) {
53
+ span.addEvent(`authenticated ${req.message.from.address.uri.user} from whitelist`);
54
+ span.end();
47
55
  return res.send(req);
48
56
  }
49
57
  // Calculate and return challenge
@@ -51,16 +59,22 @@ function SimpleAuthProcessor(config) {
51
59
  const auth = Object.assign({}, req.message.authorization);
52
60
  auth.method = req.method;
53
61
  // Calculate response and compare with the one send by the endpoint
54
- const calcRes = common_2.calculateAuthResponse(auth, utils_1.getCredentials(auth.username, users));
62
+ const calcRes = (0, common_1.calculateAuthResponse)(auth, (0, utils_1.getCredentials)(auth.username, users));
55
63
  if (calcRes !== auth.response) {
56
- return res.send(utils_1.createUnauthorizedResponse(auth.realm));
64
+ span.addEvent(`user ${req.message.from.address.uri.user} unauthorized to complete request`);
65
+ span.end();
66
+ return res.send((0, utils_1.createUnauthorizedResponse)(auth.realm));
57
67
  }
58
68
  }
59
69
  else {
60
- return res.send(utils_1.createUnauthorizedResponse(req.message.requestUri.host));
70
+ span.addEvent(`authorization header not found for user ${req.message.from.address.uri.user}`);
71
+ span.end();
72
+ return res.send((0, utils_1.createUnauthorizedResponse)(req.message.requestUri.host));
61
73
  }
62
74
  // Forward request to next middleware
75
+ span.addEvent(`user ${req.message.from.address.uri.user} authorized`);
76
+ span.end();
63
77
  res.send(req);
64
78
  });
65
79
  }
66
- exports.default = SimpleAuthProcessor;
80
+ exports.default = simpleAuthMiddleware;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * This function registers the instrumentations for the service.
3
+ *
4
+ * @param {string} serviceName - The name of the service.
5
+ * @return {Tracer} The tracer object.
6
+ */
7
+ export declare function init(serviceName: string): import("@opentelemetry/api").Tracer;
package/dist/tracer.js ADDED
@@ -0,0 +1,53 @@
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.init = void 0;
7
+ /*
8
+ * Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
9
+ * http://github.com/fonoster/routr
10
+ *
11
+ * This file is part of Routr
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 api_1 = __importDefault(require("@opentelemetry/api"));
26
+ const instrumentation_1 = require("@opentelemetry/instrumentation");
27
+ const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
28
+ const resources_1 = require("@opentelemetry/resources");
29
+ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
30
+ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
31
+ const exporter_jaeger_1 = require("@opentelemetry/exporter-jaeger");
32
+ const instrumentation_grpc_1 = require("@opentelemetry/instrumentation-grpc");
33
+ /**
34
+ * This function registers the instrumentations for the service.
35
+ *
36
+ * @param {string} serviceName - The name of the service.
37
+ * @return {Tracer} The tracer object.
38
+ */
39
+ function init(serviceName) {
40
+ const provider = new sdk_trace_node_1.NodeTracerProvider({
41
+ resource: new resources_1.Resource({
42
+ [semantic_conventions_1.SemanticResourceAttributes.SERVICE_NAME]: serviceName
43
+ })
44
+ });
45
+ const exporter = new exporter_jaeger_1.JaegerExporter();
46
+ provider.addSpanProcessor(new sdk_trace_base_1.SimpleSpanProcessor(exporter));
47
+ provider.register();
48
+ (0, instrumentation_1.registerInstrumentations)({
49
+ instrumentations: [new instrumentation_grpc_1.GrpcInstrumentation()]
50
+ });
51
+ return api_1.default.trace.getTracer("routr-tracer");
52
+ }
53
+ exports.init = init;
package/dist/utils.js CHANGED
@@ -20,7 +20,7 @@ exports.createUnauthorizedResponse = exports.getCredentials = void 0;
20
20
  * limitations under the License.
21
21
  */
22
22
  const common_1 = require("@routr/common");
23
- const getCredentials = (username, users) => users.find(user => user.username === username);
23
+ const getCredentials = (username, users) => users.find((user) => user.username === username);
24
24
  exports.getCredentials = getCredentials;
25
25
  const createUnauthorizedResponse = (realm, params = { qop: "auth", algorithm: "MD5" }) => {
26
26
  return {
@@ -28,18 +28,18 @@ const createUnauthorizedResponse = (realm, params = { qop: "auth", algorithm: "M
28
28
  // UNAUTHORIZED CODE
29
29
  responseType: 17,
30
30
  wwwAuthenticate: {
31
- scheme: 'Digest',
31
+ scheme: "Digest",
32
32
  realm: realm,
33
33
  qop: params.qop,
34
- opaque: '',
34
+ opaque: "",
35
35
  stale: false,
36
- nonce: common_1.generateNonce(),
36
+ nonce: (0, common_1.generateNonce)(),
37
37
  algorithm: params.algorithm
38
38
  },
39
39
  extensions: [
40
40
  {
41
- "name": "Expires",
42
- "value": 0
41
+ name: "Expires",
42
+ value: 0
43
43
  }
44
44
  ]
45
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/simpleauth",
3
- "version": "2.0.5-alpha.14",
3
+ "version": "2.0.5-alpha.16",
4
4
  "description": "This middleware delegates authentication to an external service",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -19,19 +19,18 @@
19
19
  "run_simpleauth": "dist/runner.js"
20
20
  },
21
21
  "dependencies": {
22
- "@fonoster/logger": "^0.3.3",
23
- "@grpc/grpc-js": "^1.3.7",
24
- "@opentelemetry/api": "^1.0.2",
25
- "@opentelemetry/exporter-jaeger": "^1.0.1",
26
- "@opentelemetry/exporter-zipkin": "^1.0.1",
22
+ "@fonoster/logger": "0.3.17-alpha.2",
23
+ "@grpc/grpc-js": "^1.5.9",
24
+ "@opentelemetry/api": "^1.0.4",
25
+ "@opentelemetry/exporter-jaeger": "^1.0.4",
27
26
  "@opentelemetry/instrumentation": "^0.27.0",
28
27
  "@opentelemetry/instrumentation-grpc": "^0.27.0",
29
- "@opentelemetry/resources": "^1.0.1",
30
- "@opentelemetry/sdk-trace-base": "^1.0.1",
31
- "@opentelemetry/sdk-trace-node": "^1.0.1",
32
- "@opentelemetry/semantic-conventions": "^1.0.1",
33
- "@routr/common": "^2.0.5-alpha.14",
34
- "@routr/processor": "^2.0.5-alpha.14",
28
+ "@opentelemetry/resources": "^1.0.4",
29
+ "@opentelemetry/sdk-trace-base": "^1.0.4",
30
+ "@opentelemetry/sdk-trace-node": "^1.0.4",
31
+ "@opentelemetry/semantic-conventions": "^1.0.4",
32
+ "@routr/common": "^2.0.5-alpha.16",
33
+ "@routr/processor": "^2.0.5-alpha.16",
35
34
  "google-protobuf": "^3.9.2"
36
35
  },
37
36
  "files": [
@@ -47,5 +46,5 @@
47
46
  "bugs": {
48
47
  "url": "https://github.com/fonoster/routr/issues"
49
48
  },
50
- "gitHead": "6dd8fe8ce94c72a1d669c98466bead23400875ce"
49
+ "gitHead": "b40323ac18cc3872b168bf4e8273f5bdb3f7510a"
51
50
  }