@vereign/core 1.4.0 → 1.5.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.
package/dist/index.js CHANGED
@@ -136,10 +136,11 @@ var CHMED16AController = class {
136
136
  constructor(logger) {
137
137
  this.process = (req, res, next) => __async(this, null, function* () {
138
138
  const { chmed, lang } = req.body;
139
+ const token = req.token;
139
140
  const start = Date.now();
140
141
  try {
141
142
  this.logger.info("Processing started");
142
- const json = yield this.parseChmedData(chmed, lang);
143
+ const json = yield this.parseChmedData(chmed, token, lang);
143
144
  yield this.generatePdfResponse(json, res);
144
145
  this.logger.info(`Total request time: ${Date.now() - start} ms`);
145
146
  } catch (error) {
@@ -150,11 +151,12 @@ var CHMED16AController = class {
150
151
  this.sds = new import_chmed_parser.SDS("https://sds-test.hin.ch", logger);
151
152
  this.parser = new import_chmed_parser.CHMEDParser(logger);
152
153
  }
153
- parseChmedData(chmed, lang) {
154
+ parseChmedData(chmed, token, lang) {
154
155
  return __async(this, null, function* () {
155
156
  try {
156
157
  const parseStart = Date.now();
157
- const json = yield this.parser.parse(chmed, this.sds, lang);
158
+ this.logger.debug(`before parsing ${chmed}, ${token}, ${lang}`);
159
+ const json = yield this.parser.parse(chmed, this.sds, token, lang);
158
160
  this.logger.info(`Parsing took ${Date.now() - parseStart} ms`);
159
161
  return json;
160
162
  } catch (error) {
@@ -265,6 +267,37 @@ var validateBody = (logger) => {
265
267
  }
266
268
  };
267
269
  };
270
+ var validateBearerToken = (logger) => {
271
+ return (req, res, next) => {
272
+ let token = null;
273
+ const headerKey = "Bearer";
274
+ if (req.headers.authorization) {
275
+ const parts = req.headers.authorization.split(" ");
276
+ if (parts.length === 2 && parts[0] === headerKey) {
277
+ token = parts[1];
278
+ }
279
+ }
280
+ logger.debug("debug token here", { token });
281
+ if (!token) {
282
+ logger.warn("Missing or invalid Authorization header", {
283
+ ip: req.ip,
284
+ userAgent: req.get("User-Agent")
285
+ });
286
+ return res.status(401).json(
287
+ new AppError(401, "Bearer token required", "UNAUTHORIZED", [
288
+ {
289
+ path: "authorization",
290
+ message: "Authorization header with Bearer token is required",
291
+ code: "missing_token"
292
+ }
293
+ ]).toJSON()
294
+ );
295
+ }
296
+ req.token = token;
297
+ logger.debug("Bearer token found and attached to request");
298
+ next();
299
+ };
300
+ };
268
301
 
269
302
  // src/chmed16/router.ts
270
303
  var CHMED16ARouter = class {
@@ -273,6 +306,7 @@ var CHMED16ARouter = class {
273
306
  const controller = new CHMED16AController(logger);
274
307
  router.post(
275
308
  "/chmed",
309
+ validateBearerToken(logger),
276
310
  validateBody(logger),
277
311
  controller.process.bind(controller)
278
312
  );
package/dist/index.mjs CHANGED
@@ -105,10 +105,11 @@ var CHMED16AController = class {
105
105
  constructor(logger) {
106
106
  this.process = (req, res, next) => __async(this, null, function* () {
107
107
  const { chmed, lang } = req.body;
108
+ const token = req.token;
108
109
  const start = Date.now();
109
110
  try {
110
111
  this.logger.info("Processing started");
111
- const json = yield this.parseChmedData(chmed, lang);
112
+ const json = yield this.parseChmedData(chmed, token, lang);
112
113
  yield this.generatePdfResponse(json, res);
113
114
  this.logger.info(`Total request time: ${Date.now() - start} ms`);
114
115
  } catch (error) {
@@ -119,11 +120,12 @@ var CHMED16AController = class {
119
120
  this.sds = new SDS("https://sds-test.hin.ch", logger);
120
121
  this.parser = new CHMEDParser(logger);
121
122
  }
122
- parseChmedData(chmed, lang) {
123
+ parseChmedData(chmed, token, lang) {
123
124
  return __async(this, null, function* () {
124
125
  try {
125
126
  const parseStart = Date.now();
126
- const json = yield this.parser.parse(chmed, this.sds, lang);
127
+ this.logger.debug(`before parsing ${chmed}, ${token}, ${lang}`);
128
+ const json = yield this.parser.parse(chmed, this.sds, token, lang);
127
129
  this.logger.info(`Parsing took ${Date.now() - parseStart} ms`);
128
130
  return json;
129
131
  } catch (error) {
@@ -234,6 +236,37 @@ var validateBody = (logger) => {
234
236
  }
235
237
  };
236
238
  };
239
+ var validateBearerToken = (logger) => {
240
+ return (req, res, next) => {
241
+ let token = null;
242
+ const headerKey = "Bearer";
243
+ if (req.headers.authorization) {
244
+ const parts = req.headers.authorization.split(" ");
245
+ if (parts.length === 2 && parts[0] === headerKey) {
246
+ token = parts[1];
247
+ }
248
+ }
249
+ logger.debug("debug token here", { token });
250
+ if (!token) {
251
+ logger.warn("Missing or invalid Authorization header", {
252
+ ip: req.ip,
253
+ userAgent: req.get("User-Agent")
254
+ });
255
+ return res.status(401).json(
256
+ new AppError(401, "Bearer token required", "UNAUTHORIZED", [
257
+ {
258
+ path: "authorization",
259
+ message: "Authorization header with Bearer token is required",
260
+ code: "missing_token"
261
+ }
262
+ ]).toJSON()
263
+ );
264
+ }
265
+ req.token = token;
266
+ logger.debug("Bearer token found and attached to request");
267
+ next();
268
+ };
269
+ };
237
270
 
238
271
  // src/chmed16/router.ts
239
272
  var CHMED16ARouter = class {
@@ -242,6 +275,7 @@ var CHMED16ARouter = class {
242
275
  const controller = new CHMED16AController(logger);
243
276
  router.post(
244
277
  "/chmed",
278
+ validateBearerToken(logger),
245
279
  validateBody(logger),
246
280
  controller.process.bind(controller)
247
281
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vereign/core",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "core api library for e prescription project",
5
5
  "repository": {
6
6
  "type": "git",
@@ -60,8 +60,8 @@
60
60
  "zod-openapi": "^5.3.1"
61
61
  },
62
62
  "dependencies": {
63
- "@vereign/chmed-parser": "^1.6.0",
64
- "@vereign/pdf-generator": "^1.4.0",
63
+ "@vereign/chmed-parser": "^1.7.3",
64
+ "@vereign/pdf-generator": "^1.5.0",
65
65
  "express": "^5.1.0",
66
66
  "pdfkit": "^0.17.1",
67
67
  "winston": "^3.17.0",