@routr/simpleauth 2.0.5-alpha.9 → 2.0.6-alpha.1

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
@@ -3,6 +3,7 @@
3
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
6
+ var _a;
6
7
  Object.defineProperty(exports, "__esModule", { value: true });
7
8
  /*
8
9
  * Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
@@ -22,21 +23,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
22
23
  * See the License for the specific language governing permissions and
23
24
  * limitations under the License.
24
25
  */
25
- const logger_1 = __importDefault(require("@fonoster/logger"));
26
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
27
+ require("./tracer").init("simpleauth");
26
28
  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
- }
29
+ const common_1 = require("@routr/common");
30
+ const logger_1 = require("@fonoster/logger");
31
+ const logger = (0, logger_1.getLogger)({ service: "simpleauth", filePath: __filename });
32
+ common_1.Assertions.assertEnvsAreSet(["PATH_TO_AUTH"]);
33
+ const whiteList = process.env.WHITELIST ? process.env.WHITELIST.split(",") : [];
31
34
  try {
35
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
32
36
  const users = require(process.env.PATH_TO_AUTH);
33
- service_1.default({ bindAddr: process.env.BIND_ADDR || "0.0.0.0:51903", users });
37
+ (0, service_1.default)({
38
+ bindAddr: (_a = process.env.BIND_ADDR) !== null && _a !== void 0 ? _a : "0.0.0.0:51903",
39
+ users,
40
+ whiteList
41
+ });
34
42
  }
35
43
  catch (e) {
36
44
  if (e.code === "MODULE_NOT_FOUND") {
37
- logger_1.default.error(`auth file not found [path = ${process.env.PATH_TO_AUTH}]`);
45
+ logger.error(`auth file not found [path = ${process.env.PATH_TO_AUTH}]`);
38
46
  }
39
47
  else {
40
- logger_1.default.error(e);
48
+ logger.error(e);
41
49
  }
42
50
  }
package/dist/service.d.ts CHANGED
@@ -1,6 +1,16 @@
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[];
15
+ whiteList: string[];
6
16
  }): void;
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,30 +22,59 @@ 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
+ const api_1 = __importDefault(require("@opentelemetry/api"));
26
26
  const common_1 = require("@routr/common");
27
27
  const utils_1 = require("./utils");
28
28
  const processor_1 = __importDefault(require("@routr/processor"));
29
- function SimpleAuthProcessor(config) {
30
- const { bindAddr, users } = config;
31
- new processor_1.default({ bindAddr, name: "simpleauth" })
32
- .listen((req, res) => {
33
- logger_1.default.verbose(JSON.stringify(req, null, ' '));
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) {
41
+ const { bindAddr, users, whiteList } = config;
42
+ new processor_1.default({ bindAddr, name: "simpleauth" }).listen((req, res) => {
43
+ const tracer = api_1.default.trace.getTracer("routr-tracer");
44
+ const currentSpan = api_1.default.trace.getSpan(api_1.default.context.active());
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
+ // Q: Should we extend 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
+ }
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();
55
+ return res.send(req);
56
+ }
34
57
  // Calculate and return challenge
35
58
  if (req.message.authorization) {
36
59
  const auth = Object.assign({}, req.message.authorization);
37
60
  auth.method = req.method;
38
61
  // Calculate response and compare with the one send by the endpoint
39
- const calcRes = common_1.calculateAuthResponse(auth, utils_1.getCredentials(auth.username, users));
62
+ const calcRes = (0, common_1.calculateAuthResponse)(auth, (0, utils_1.getCredentials)(auth.username, users));
40
63
  if (calcRes !== auth.response) {
41
- 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));
42
67
  }
43
68
  }
44
69
  else {
45
- 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));
46
73
  }
47
74
  // Forward request to next middleware
75
+ span.addEvent(`user ${req.message.from.address.uri.user} authorized`);
76
+ span.end();
48
77
  res.send(req);
49
78
  });
50
79
  }
51
- 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.d.ts CHANGED
@@ -15,9 +15,5 @@ export declare const createUnauthorizedResponse: (realm: string, params?: {
15
15
  nonce: string;
16
16
  algorithm: string;
17
17
  };
18
- extensions: {
19
- name: string;
20
- value: number;
21
- }[];
22
18
  };
23
19
  };
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,20 +28,14 @@ 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
- },
39
- extensions: [
40
- {
41
- "name": "Expires",
42
- "value": 0
43
- }
44
- ]
38
+ }
45
39
  }
46
40
  };
47
41
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/simpleauth",
3
- "version": "2.0.5-alpha.9",
3
+ "version": "2.0.6-alpha.1",
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,9 +19,19 @@
19
19
  "run_simpleauth": "dist/runner.js"
20
20
  },
21
21
  "dependencies": {
22
- "@fonoster/logger": "^0.3.3",
23
- "@routr/common": "^2.0.5-alpha.9",
24
- "@routr/processor": "^2.0.5-alpha.9"
22
+ "@fonoster/logger": "0.3.18",
23
+ "@grpc/grpc-js": "^1.5.9",
24
+ "@opentelemetry/api": "^1.0.4",
25
+ "@opentelemetry/exporter-jaeger": "^1.0.4",
26
+ "@opentelemetry/instrumentation": "^0.27.0",
27
+ "@opentelemetry/instrumentation-grpc": "^0.27.0",
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.6-alpha.1",
33
+ "@routr/processor": "^2.0.6-alpha.1",
34
+ "google-protobuf": "^3.9.2"
25
35
  },
26
36
  "files": [
27
37
  "dist"
@@ -36,5 +46,5 @@
36
46
  "bugs": {
37
47
  "url": "https://github.com/fonoster/routr/issues"
38
48
  },
39
- "gitHead": "9eeafebb3f16625aca520268ed6f44896656f3a6"
49
+ "gitHead": "389513636bbac9ab7ffbe14f1b8ecdb5bb93bad4"
40
50
  }