fastify-txstate 3.1.8 → 3.2.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.
@@ -0,0 +1,33 @@
1
+ import { type InteractionEvent } from '@txstate-mws/fastify-shared';
2
+ import type { FastifyBaseLogger, FastifyInstance } from 'fastify';
3
+ import { type IBrowser, type IDevice, type IOS } from 'ua-parser-js';
4
+ import { type FastifyTxStateAuthInfo } from '.';
5
+ export interface StoredInteractionEvent extends InteractionEvent {
6
+ '@timestamp': string;
7
+ appName: string;
8
+ environment: string;
9
+ browser: IBrowser;
10
+ device: IDevice;
11
+ os: IOS;
12
+ user: {
13
+ remoteip: string;
14
+ ga?: string;
15
+ } & FastifyTxStateAuthInfo;
16
+ }
17
+ export declare class AnalyticsClient {
18
+ push(events: StoredInteractionEvent[]): Promise<void>;
19
+ }
20
+ export declare class LoggingAnalyticsClient extends AnalyticsClient {
21
+ protected logger: FastifyBaseLogger;
22
+ constructor(logger: FastifyBaseLogger);
23
+ push(events: StoredInteractionEvent[]): Promise<void>;
24
+ }
25
+ export declare class ElasticAnalyticsClient extends AnalyticsClient {
26
+ private readonly elasticClient;
27
+ constructor();
28
+ push(events: StoredInteractionEvent[]): Promise<void>;
29
+ }
30
+ export declare function analyticsPlugin(fastify: FastifyInstance, opts: {
31
+ appName: string;
32
+ analyticsClient?: AnalyticsClient;
33
+ }, done: (err?: Error) => void): void;
@@ -0,0 +1,115 @@
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.analyticsPlugin = exports.ElasticAnalyticsClient = exports.LoggingAnalyticsClient = exports.AnalyticsClient = void 0;
7
+ const elasticsearch_1 = __importDefault(require("@elastic/elasticsearch"));
8
+ const fastify_shared_1 = require("@txstate-mws/fastify-shared");
9
+ const txstate_utils_1 = require("txstate-utils");
10
+ const ua_parser_js_1 = require("ua-parser-js");
11
+ class AnalyticsClient {
12
+ async push(events) {
13
+ for (const event of events)
14
+ console.info('analytics event:', JSON.stringify((0, txstate_utils_1.pick)(event, 'eventType', 'screen', 'action', 'target')));
15
+ }
16
+ }
17
+ exports.AnalyticsClient = AnalyticsClient;
18
+ class LoggingAnalyticsClient extends AnalyticsClient {
19
+ constructor(logger) {
20
+ super();
21
+ this.logger = logger;
22
+ }
23
+ async push(events) {
24
+ for (const event of events)
25
+ this.logger.info({ analyticsEvent: event });
26
+ }
27
+ }
28
+ exports.LoggingAnalyticsClient = LoggingAnalyticsClient;
29
+ class ElasticAnalyticsClient extends AnalyticsClient {
30
+ constructor() {
31
+ var _a, _b;
32
+ super();
33
+ this.elasticClient = new elasticsearch_1.default.Client({
34
+ node: process.env.ELASTICSEARCH_URL,
35
+ auth: {
36
+ username: (_a = process.env.ELASTICSEARCH_USER) !== null && _a !== void 0 ? _a : 'elastic',
37
+ password: (_b = process.env.ELASTICSEARCH_PASS) !== null && _b !== void 0 ? _b : 'not_provided'
38
+ }
39
+ });
40
+ }
41
+ async push(events) {
42
+ if (events.length)
43
+ await this.elasticClient.bulk({ body: events.reduce((acc, event) => { var _a; acc.push({ index: { _index: (_a = process.env.ELASTICSEARCH_USEREVENTS_INDEX) !== null && _a !== void 0 ? _a : 'interaction-analytics' } }, event); return acc; }, []) });
44
+ }
45
+ }
46
+ exports.ElasticAnalyticsClient = ElasticAnalyticsClient;
47
+ function analyticsPlugin(fastify, opts, done) {
48
+ var _a;
49
+ const environment = process.env.NODE_ENV;
50
+ if ((0, txstate_utils_1.isBlank)(environment))
51
+ throw new Error('Must set NODE_ENV when reporting analytics.');
52
+ const eventQueue = [];
53
+ const analyticsClient = (_a = opts.analyticsClient) !== null && _a !== void 0 ? _a : ((0, txstate_utils_1.isBlank)(process.env.ELASTICSEARCH_URL)
54
+ ? environment === 'development'
55
+ ? new AnalyticsClient()
56
+ : new LoggingAnalyticsClient(fastify.log)
57
+ : new ElasticAnalyticsClient());
58
+ const UACache = new txstate_utils_1.Cache(async (ua) => {
59
+ const parser = new ua_parser_js_1.UAParser(ua);
60
+ return parser.getResult();
61
+ }, { freshseconds: 86400, staleseconds: 864000 });
62
+ async function flushQueue() {
63
+ const eventQueueSlice = [...eventQueue];
64
+ try {
65
+ eventQueue.length = 0;
66
+ const eventsToStore = [];
67
+ for (const queueItem of eventQueueSlice) {
68
+ const uaInfo = await UACache.get(queueItem.ua);
69
+ eventsToStore.push({
70
+ ...queueItem.event,
71
+ '@timestamp': queueItem.time,
72
+ appName: opts.appName,
73
+ environment,
74
+ ...(0, txstate_utils_1.pick)(uaInfo, 'browser', 'device', 'os'),
75
+ user: {
76
+ remoteip: queueItem.remoteIp,
77
+ ga: queueItem.gaCookie,
78
+ ...queueItem.auth
79
+ }
80
+ });
81
+ }
82
+ if (eventsToStore.length)
83
+ await analyticsClient.push(eventsToStore);
84
+ }
85
+ catch (e) {
86
+ eventQueue.push(...eventQueueSlice);
87
+ console.error(e);
88
+ }
89
+ finally {
90
+ setTimeout(() => { void flushQueue(); }, 5000);
91
+ }
92
+ }
93
+ setTimeout(() => { void flushQueue(); }, 5000);
94
+ function queueEvents(auth, headers, remoteIp, events) {
95
+ var _a, _b, _c;
96
+ for (const event of events) {
97
+ eventQueue.push({
98
+ event,
99
+ remoteIp,
100
+ ua: (_a = headers['user-agent']) !== null && _a !== void 0 ? _a : '',
101
+ time: new Date().toISOString(),
102
+ gaCookie: (_c = (_b = headers.cookie) === null || _b === void 0 ? void 0 : _b.replace(/^.*?(?:_ga=([^;]+))?.*$/, '$1')) !== null && _c !== void 0 ? _c : '',
103
+ auth
104
+ });
105
+ }
106
+ }
107
+ fastify.post('/analytics', { schema: { body: { type: 'array', items: fastify_shared_1.interactionEvent }, response: { 202: { type: 'string', enum: ['OK'] } } } }, async (req, res) => {
108
+ const { auth } = req;
109
+ queueEvents(auth !== null && auth !== void 0 ? auth : { username: 'unauthenticated' }, req.headers, req.ip, req.body);
110
+ res.statusCode = 202;
111
+ return 'OK';
112
+ });
113
+ done();
114
+ }
115
+ exports.analyticsPlugin = analyticsPlugin;
package/lib/error.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export declare class HttpError extends Error {
2
+ statusCode: number;
3
+ constructor(statusCode: number, message?: string);
4
+ }
5
+ type ValidationErrors = Record<string, string[]>;
6
+ export declare class FailedValidationError extends HttpError {
7
+ errors: ValidationErrors;
8
+ constructor(errors: ValidationErrors);
9
+ }
10
+ export {};
package/lib/error.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FailedValidationError = exports.HttpError = void 0;
4
+ const http_status_codes_1 = require("http-status-codes");
5
+ class HttpError extends Error {
6
+ constructor(statusCode, message) {
7
+ if (!message) {
8
+ if (statusCode === 401)
9
+ message = 'Authentication is required.';
10
+ else if (statusCode === 403)
11
+ message = 'You are not authorized for that.';
12
+ else
13
+ message = (0, http_status_codes_1.getReasonPhrase)(statusCode);
14
+ }
15
+ super(message);
16
+ this.statusCode = statusCode;
17
+ }
18
+ }
19
+ exports.HttpError = HttpError;
20
+ class FailedValidationError extends HttpError {
21
+ constructor(errors) {
22
+ super(422, 'Validation failure.');
23
+ this.errors = errors;
24
+ }
25
+ }
26
+ exports.FailedValidationError = FailedValidationError;
package/lib/index.js CHANGED
@@ -1,13 +1,32 @@
1
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
+ };
2
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
18
  };
5
19
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.FailedValidationError = exports.HttpError = exports.prodLogger = exports.devLogger = void 0;
20
+ exports.prodLogger = exports.devLogger = void 0;
21
+ const swagger_1 = __importDefault(require("@fastify/swagger"));
22
+ const swagger_ui_1 = __importDefault(require("@fastify/swagger-ui"));
23
+ const ajv_errors_1 = __importDefault(require("ajv-errors"));
7
24
  const fastify_1 = require("fastify");
8
- const fs_1 = __importDefault(require("fs"));
9
- const http_1 = __importDefault(require("http"));
10
- const http_status_codes_1 = require("http-status-codes");
25
+ const node_fs_1 = __importDefault(require("node:fs"));
26
+ const node_http_1 = __importDefault(require("node:http"));
27
+ const txstate_utils_1 = require("txstate-utils");
28
+ const error_1 = require("./error");
29
+ const unified_auth_1 = require("./unified-auth");
11
30
  exports.devLogger = {
12
31
  level: 'info',
13
32
  info: (msg) => { console.info(msg.req ? `${msg.req.method} ${msg.req.url}` : msg.res ? `${msg.res.statusCode} - ${msg.responseTime}` : msg); },
@@ -25,25 +44,27 @@ exports.prodLogger = {
25
44
  req(req) {
26
45
  return {
27
46
  method: req.method,
28
- url: req.url.replace(/token=[\w.]+/, 'token=redacted'),
47
+ url: req.url.replace(/(token|unifiedJwt)=[\w.]+/i, '$1=redacted'),
29
48
  remoteAddress: req.ip,
30
49
  traceparent: req.headers.traceparent
31
50
  };
32
51
  },
33
52
  res(res) {
34
- var _a, _b;
53
+ var _a, _b, _c, _d;
35
54
  return {
36
55
  statusCode: res.statusCode,
37
- url: (_a = res.request) === null || _a === void 0 ? void 0 : _a.url.replace(/token=[\w.]+/, 'token=redacted'),
38
- length: (_b = res.getHeader) === null || _b === void 0 ? void 0 : _b.call(res, 'content-length'),
39
- ...res.extraLogInfo
56
+ url: (_a = res.request) === null || _a === void 0 ? void 0 : _a.url.replace(/(token|unifiedJwt)=[\w.]+/i, '$1=redacted'),
57
+ length: Number((0, txstate_utils_1.toArray)((_b = res.getHeader) === null || _b === void 0 ? void 0 : _b.call(res, 'content-length'))[0]),
58
+ ...res.extraLogInfo,
59
+ auth: (_d = (_c = res.request) === null || _c === void 0 ? void 0 : _c.auth) !== null && _d !== void 0 ? _d : res.extraLogInfo.auth
40
60
  };
41
61
  }
42
62
  }
43
63
  };
44
64
  class Server {
45
65
  constructor(config = {}) {
46
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
66
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
67
+ this.config = config;
47
68
  this.https = false;
48
69
  this.errorHandlers = [];
49
70
  this.shuttingDown = false;
@@ -51,8 +72,8 @@ class Server {
51
72
  this.validOriginHosts = {};
52
73
  this.validOriginSuffixes = new Set();
53
74
  try {
54
- const key = fs_1.default.readFileSync('/securekeys/private.key');
55
- const cert = fs_1.default.readFileSync('/securekeys/cert.pem');
75
+ const key = node_fs_1.default.readFileSync('/securekeys/private.key');
76
+ const cert = node_fs_1.default.readFileSync('/securekeys/cert.pem');
56
77
  config.https = {
57
78
  ...config.https,
58
79
  allowHTTP1: true,
@@ -78,14 +99,47 @@ class Server {
78
99
  else
79
100
  config.trustProxy = process.env.TRUST_PROXY;
80
101
  }
102
+ config.ajv = { ...config.ajv, plugins: [...((_c = (_b = (_a = config.ajv) === null || _a === void 0 ? void 0 : _a.plugins) === null || _b === void 0 ? void 0 : _b.filter(p => p !== ajv_errors_1.default)) !== null && _c !== void 0 ? _c : []), ajv_errors_1.default], customOptions: { ...(_d = config.ajv) === null || _d === void 0 ? void 0 : _d.customOptions, allErrors: true, strictSchema: false } };
81
103
  this.healthCallback = config.checkHealth;
82
104
  this.app = (0, fastify_1.fastify)(config);
105
+ this.app.addHook('onRoute', route => {
106
+ var _a, _b, _c;
107
+ if (!((_a = route.schema) === null || _a === void 0 ? void 0 : _a.body))
108
+ return;
109
+ const missingResponse = ((_b = route.schema) === null || _b === void 0 ? void 0 : _b.response) == null;
110
+ const newSchema = (0, txstate_utils_1.set)((_c = route.schema) !== null && _c !== void 0 ? _c : {}, 'response.422', {
111
+ description: 'Validation failure.',
112
+ type: 'object',
113
+ properties: {
114
+ errors: {
115
+ type: 'array',
116
+ items: {
117
+ type: 'object',
118
+ properties: {
119
+ type: { type: 'string', enum: ['error', 'warning', 'success', 'system'], example: 'error' },
120
+ message: { type: 'string', example: 'must be an integer' },
121
+ path: { type: 'string', example: 'cart.item.0.quantity', description: 'Dot-separated path to the field in the request body that caused the validation error.' }
122
+ },
123
+ required: ['type', 'message'],
124
+ additionalProperties: false
125
+ }
126
+ }
127
+ }
128
+ });
129
+ if (missingResponse) {
130
+ newSchema.response['200'] = {
131
+ description: 'Success. Return type has not been specified.',
132
+ type: 'null'
133
+ };
134
+ }
135
+ route.schema = newSchema;
136
+ });
83
137
  if (!config.skipOriginCheck && !process.env.SKIP_ORIGIN_CHECK) {
84
- this.setValidOrigins([...((_a = config.validOrigins) !== null && _a !== void 0 ? _a : []), ...((_c = (_b = process.env.VALID_ORIGINS) === null || _b === void 0 ? void 0 : _b.split(',')) !== null && _c !== void 0 ? _c : [])]);
85
- this.setValidOriginHosts([...((_d = config.validOriginHosts) !== null && _d !== void 0 ? _d : []), ...((_f = (_e = process.env.VALID_ORIGIN_HOSTS) === null || _e === void 0 ? void 0 : _e.split(',')) !== null && _f !== void 0 ? _f : [])]);
86
- this.setValidOriginSuffixes([...((_g = config.validOriginSuffixes) !== null && _g !== void 0 ? _g : []), ...((_j = (_h = process.env.VALID_ORIGIN_SUFFIXES) === null || _h === void 0 ? void 0 : _h.split(',')) !== null && _j !== void 0 ? _j : [])]);
138
+ this.setValidOrigins([...((_e = config.validOrigins) !== null && _e !== void 0 ? _e : []), ...((_g = (_f = process.env.VALID_ORIGINS) === null || _f === void 0 ? void 0 : _f.split(',')) !== null && _g !== void 0 ? _g : [])]);
139
+ this.setValidOriginHosts([...((_h = config.validOriginHosts) !== null && _h !== void 0 ? _h : []), ...((_k = (_j = process.env.VALID_ORIGIN_HOSTS) === null || _j === void 0 ? void 0 : _j.split(',')) !== null && _k !== void 0 ? _k : [])]);
140
+ this.setValidOriginSuffixes([...((_l = config.validOriginSuffixes) !== null && _l !== void 0 ? _l : []), ...((_o = (_m = process.env.VALID_ORIGIN_SUFFIXES) === null || _m === void 0 ? void 0 : _m.split(',')) !== null && _o !== void 0 ? _o : [])]);
87
141
  this.app.addHook('preHandler', async (req, res) => {
88
- var _a;
142
+ var _a, _b;
89
143
  res.extraLogInfo = {};
90
144
  if (!req.headers.origin)
91
145
  return;
@@ -119,6 +173,13 @@ class Server {
119
173
  if (req.headers['access-control-request-headers'])
120
174
  void res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
121
175
  }
176
+ try {
177
+ req.auth = await ((_b = config.authenticate) === null || _b === void 0 ? void 0 : _b.call(config, req));
178
+ }
179
+ catch (e) {
180
+ await res.status(401).send('Failed to authenticate.');
181
+ return res;
182
+ }
122
183
  });
123
184
  this.app.options('*', async (req, res) => {
124
185
  await res.send();
@@ -138,20 +199,24 @@ class Server {
138
199
  });
139
200
  this.app.setNotFoundHandler((req, res) => { void res.status(404).send('Not Found.'); });
140
201
  this.app.setErrorHandler(async (err, req, res) => {
202
+ var _a, _b;
141
203
  req.log.warn(err);
142
204
  for (const errorHandler of this.errorHandlers) {
143
205
  if (!res.sent)
144
206
  await errorHandler(err, req, res);
145
207
  }
146
208
  if (!res.sent) {
147
- if (err instanceof FailedValidationError) {
209
+ if (err instanceof error_1.FailedValidationError) {
148
210
  await res.status(err.statusCode).send(err.errors);
149
211
  }
150
- else if (err instanceof HttpError) {
212
+ else if (err instanceof error_1.HttpError) {
151
213
  await res.status(err.statusCode).send(err.message);
152
214
  }
215
+ else if (err.code === 'FST_ERR_VALIDATION') {
216
+ await res.status(422).send({ errors: (_b = (_a = err.validation) === null || _a === void 0 ? void 0 : _a.map(err => ({ message: err.message, path: err.instancePath.substring(1).replace(/\//g, '.'), type: 'error' }))) !== null && _b !== void 0 ? _b : [] });
217
+ }
153
218
  else if (err.statusCode) {
154
- await res.status(err.statusCode).send(new HttpError(err.statusCode).message);
219
+ await res.status(err.statusCode).send(new error_1.HttpError(err.statusCode).message);
155
220
  }
156
221
  else {
157
222
  await res.status(500).send('Internal Server Error.');
@@ -189,14 +254,16 @@ class Server {
189
254
  process.on('SIGINT', this.sigHandler);
190
255
  }
191
256
  async start(port) {
192
- var _a;
257
+ var _a, _b, _c;
193
258
  const customPort = port !== null && port !== void 0 ? port : parseInt((_a = process.env.PORT) !== null && _a !== void 0 ? _a : '0');
259
+ await this.app.ready();
260
+ (_c = (_b = this.app).swagger) === null || _c === void 0 ? void 0 : _c.call(_b);
194
261
  if (customPort) {
195
262
  await this.app.listen({ port: customPort, host: '0.0.0.0' });
196
263
  }
197
264
  else if (this.https) {
198
265
  // redirect 80 to 443
199
- http_1.default.createServer((req, res) => {
266
+ node_http_1.default.createServer((req, res) => {
200
267
  var _a, _b, _c, _d;
201
268
  res.writeHead(301, { Location: 'https://' + ((_c = (_b = (_a = req === null || req === void 0 ? void 0 : req.headers) === null || _a === void 0 ? void 0 : _a.host) === null || _b === void 0 ? void 0 : _b.replace(/:\d+$/, '')) !== null && _c !== void 0 ? _c : '') + ((_d = req.url) !== null && _d !== void 0 ? _d : '') });
202
269
  res.end();
@@ -227,6 +294,25 @@ class Server {
227
294
  for (const s of suffixes)
228
295
  this.validOriginSuffixes.add(s);
229
296
  }
297
+ async swagger(opts) {
298
+ var _a, _b, _c, _d;
299
+ let openapi = (_a = opts === null || opts === void 0 ? void 0 : opts.openapi) !== null && _a !== void 0 ? _a : {};
300
+ if (this.config.authenticate === unified_auth_1.unifiedAuthenticate) {
301
+ openapi = (0, txstate_utils_1.set)(openapi, 'components.securitySchemes', {
302
+ unifiedAuth: {
303
+ type: 'http',
304
+ scheme: 'bearer',
305
+ bearerFormat: 'JWT',
306
+ description: `Enter a token obtained from the TxState Unified Authentication service. An easy way to do
307
+ this is log into this application and use dev tools to pull your token from the Authorization header.`
308
+ }
309
+ });
310
+ // Apply the security globally to all operations
311
+ openapi.security = [{ unifiedAuth: [] }];
312
+ }
313
+ await this.app.register(swagger_1.default, { openapi });
314
+ await this.app.register(swagger_ui_1.default, { ...opts === null || opts === void 0 ? void 0 : opts.ui, routePrefix: (_d = (_b = opts === null || opts === void 0 ? void 0 : opts.path) !== null && _b !== void 0 ? _b : (_c = opts === null || opts === void 0 ? void 0 : opts.ui) === null || _c === void 0 ? void 0 : _c.routePrefix) !== null && _d !== void 0 ? _d : '/docs' });
315
+ }
230
316
  async close(softSeconds) {
231
317
  var _a;
232
318
  if (typeof softSeconds === 'undefined')
@@ -235,31 +321,12 @@ class Server {
235
321
  process.removeListener('SIGINT', this.sigHandler);
236
322
  if (softSeconds) {
237
323
  this.shuttingDown = true;
238
- await new Promise(resolve => setTimeout(resolve, softSeconds * 1000));
324
+ await (0, txstate_utils_1.sleep)(softSeconds);
239
325
  }
240
326
  await this.app.close();
241
327
  }
242
328
  }
243
329
  exports.default = Server;
244
- class HttpError extends Error {
245
- constructor(statusCode, message) {
246
- if (!message) {
247
- if (statusCode === 401)
248
- message = 'Authentication is required.';
249
- else if (statusCode === 403)
250
- message = 'You are not authorized for that.';
251
- else
252
- message = (0, http_status_codes_1.getReasonPhrase)(statusCode);
253
- }
254
- super(message);
255
- this.statusCode = statusCode;
256
- }
257
- }
258
- exports.HttpError = HttpError;
259
- class FailedValidationError extends HttpError {
260
- constructor(errors) {
261
- super(422, 'Validation failure.');
262
- this.errors = errors;
263
- }
264
- }
265
- exports.FailedValidationError = FailedValidationError;
330
+ __exportStar(require("./analytics"), exports);
331
+ __exportStar(require("./error"), exports);
332
+ __exportStar(require("./unified-auth"), exports);
@@ -0,0 +1,61 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { type KeyObject } from 'crypto';
4
+ import { type FastifyRequest } from 'fastify';
5
+ import { type JWTPayload, type JWTVerifyGetKey, type KeyLike } from 'jose';
6
+ import { Cache } from 'txstate-utils';
7
+ import { type FastifyTxStateAuthInfo } from '.';
8
+ declare class MockContext<AuthType = any> {
9
+ auth?: AuthType;
10
+ constructor(auth: any);
11
+ waitForAuth(): Promise<AuthType | undefined>;
12
+ static init(): void;
13
+ }
14
+ interface IssuerConfig {
15
+ iss: string;
16
+ url?: string;
17
+ publicKey?: string;
18
+ secret?: string;
19
+ validateUrl: URL;
20
+ }
21
+ declare class Context<AuthType extends FastifyTxStateAuthInfo = FastifyTxStateAuthInfo> extends MockContext<AuthType> {
22
+ private readonly authPromise;
23
+ protected static jwtVerifyKey: KeyObject | undefined;
24
+ protected static issuerKeys: Map<string, KeyLike | JWTVerifyGetKey>;
25
+ protected static issuerConfig: Map<string, any>;
26
+ protected static tokenCache: Cache<string, any, {
27
+ req?: FastifyRequest<import("fastify").RouteGenericInterface, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, import("fastify").RouteGenericInterface>> | undefined;
28
+ ctx: Context;
29
+ }>;
30
+ constructor(req?: FastifyRequest);
31
+ waitForAuth(): Promise<AuthType | undefined>;
32
+ static init(): void;
33
+ /**
34
+ * If implemented, this method will be called on startup, once per configured issuer. It receives
35
+ * the issuer configuration from the JWT_TRUSTED_ISSUERS environment variable and allows you to manipulate
36
+ * the configuration before storing it.
37
+ *
38
+ * Once stored, whatever you create may be used in your custom validateToken method. For example,
39
+ * you might want to create an in-memory URL object with an issuer's URL so that it can be manipulated
40
+ * easily to send validation checks to the issuer.
41
+ */
42
+ static processIssuerConfig: undefined | ((config: any) => any);
43
+ /**
44
+ * If implemented, this method is called after a token's signature is checked and passes. You would
45
+ * typically implement this method to check whether the user has manually signed out, or the token has
46
+ * been otherwise deauthorized before its expiration date.
47
+ *
48
+ * If the token is not valid, this method should throw an error with an appropriate message.
49
+ */
50
+ static validateToken: undefined | ((token: string, issuerConfig: any, claims: JWTPayload) => void | Promise<void>);
51
+ tokenFromReq(req?: FastifyRequest): string | undefined;
52
+ authFromReq(req?: FastifyRequest): Promise<AuthType | undefined>;
53
+ authFromPayload(payload: JWTPayload): Promise<AuthType>;
54
+ }
55
+ declare class TxStateUAuthContext extends Context {
56
+ static processIssuerConfig(config: IssuerConfig): IssuerConfig;
57
+ static validateToken(token: string, issuerConfig: IssuerConfig, claims: any): Promise<void>;
58
+ authFromPayload(payload: JWTPayload): Promise<FastifyTxStateAuthInfo>;
59
+ }
60
+ export declare function unifiedAuthenticate(req: FastifyRequest, ContextClass?: typeof TxStateUAuthContext): Promise<FastifyTxStateAuthInfo | undefined>;
61
+ export {};
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.unifiedAuthenticate = void 0;
5
+ const crypto_1 = require("crypto");
6
+ const jose_1 = require("jose");
7
+ const txstate_utils_1 = require("txstate-utils");
8
+ function cleanPem(secretOrPem) {
9
+ return secretOrPem === null || secretOrPem === void 0 ? void 0 : secretOrPem.replace(/(-+BEGIN [\w\s]+ KEY-+)\s*(.*?)\s*(-+END [\w\s]+ KEY-+)/, '$1\n$2\n$3');
10
+ }
11
+ class MockContext {
12
+ constructor(auth) {
13
+ this.auth = auth;
14
+ }
15
+ async waitForAuth() {
16
+ return this.auth;
17
+ }
18
+ static init() { }
19
+ }
20
+ class Context extends MockContext {
21
+ constructor(req) {
22
+ super(undefined);
23
+ this.authPromise = this.authFromReq(req);
24
+ }
25
+ async waitForAuth() {
26
+ this.auth = await this.authPromise;
27
+ return this.auth;
28
+ }
29
+ static init() {
30
+ var _b;
31
+ let secret = cleanPem(process.env.JWT_SECRET_VERIFY);
32
+ if (secret != null) {
33
+ _a.jwtVerifyKey = (0, crypto_1.createPublicKey)(secret);
34
+ }
35
+ else {
36
+ secret = cleanPem(process.env.JWT_SECRET);
37
+ if (secret != null) {
38
+ try {
39
+ _a.jwtVerifyKey = (0, crypto_1.createPublicKey)(secret);
40
+ }
41
+ catch (e) {
42
+ console.info('JWT_SECRET was not a private key, treating it as symmetric.');
43
+ _a.jwtVerifyKey = (0, crypto_1.createSecretKey)(Buffer.from(secret, 'ascii'));
44
+ }
45
+ }
46
+ }
47
+ if (process.env.JWT_TRUSTED_ISSUERS) {
48
+ const issuers = (0, txstate_utils_1.toArray)(JSON.parse(process.env.JWT_TRUSTED_ISSUERS));
49
+ for (const issuer of issuers) {
50
+ this.issuerConfig.set(issuer.iss, (_b = this.processIssuerConfig) === null || _b === void 0 ? void 0 : _b.call(this, (0, txstate_utils_1.omit)(issuer, 'publicKey', 'secret')));
51
+ if (issuer.url)
52
+ this.issuerKeys.set(issuer.iss, (0, jose_1.createRemoteJWKSet)(new URL(issuer.url)));
53
+ else if (issuer.publicKey)
54
+ this.issuerKeys.set(issuer.iss, (0, crypto_1.createPublicKey)(issuer.publicKey));
55
+ else if (issuer.secret)
56
+ this.issuerKeys.set(issuer.iss, (0, crypto_1.createSecretKey)(Buffer.from(issuer.secret, 'ascii')));
57
+ }
58
+ }
59
+ }
60
+ tokenFromReq(req) {
61
+ var _b;
62
+ const m = (_b = req === null || req === void 0 ? void 0 : req.headers.authorization) === null || _b === void 0 ? void 0 : _b.match(/^bearer (.*)$/i);
63
+ return m === null || m === void 0 ? void 0 : m[1];
64
+ }
65
+ async authFromReq(req) {
66
+ const token = this.tokenFromReq(req);
67
+ if (!token)
68
+ return undefined;
69
+ return this.constructor.tokenCache.get(token, { req, ctx: this });
70
+ }
71
+ async authFromPayload(payload) {
72
+ return payload;
73
+ }
74
+ }
75
+ _a = Context;
76
+ Context.issuerKeys = new Map();
77
+ Context.issuerConfig = new Map();
78
+ Context.tokenCache = new txstate_utils_1.Cache(async (token, { req, ctx }) => {
79
+ var _b, _c;
80
+ // `this` is always the Context class, even if we are making instances of a subclass of Context
81
+ // we need to get the instance's constructor instead in case it has overridden one of our
82
+ // static methods/variables
83
+ const ctxStatic = ctx.constructor;
84
+ const logger = (_b = req === null || req === void 0 ? void 0 : req.log) !== null && _b !== void 0 ? _b : console;
85
+ let verifyKey = _a.jwtVerifyKey;
86
+ try {
87
+ const claims = (0, jose_1.decodeJwt)(token);
88
+ if (claims.iss && ctxStatic.issuerKeys.has(claims.iss))
89
+ verifyKey = ctxStatic.issuerKeys.get(claims.iss);
90
+ if (!verifyKey) {
91
+ logger.info(`Received token with issuer: ${claims.iss} but JWT secret could not be found. The server may be misconfigured or the user may have presented a JWT from an untrusted issuer.`);
92
+ return undefined;
93
+ }
94
+ await ((_c = ctxStatic.validateToken) === null || _c === void 0 ? void 0 : _c.call(ctxStatic, token, ctxStatic.issuerConfig.get(claims.iss), claims));
95
+ const { payload } = await (0, jose_1.jwtVerify)(token, verifyKey);
96
+ return await ctx.authFromPayload(payload);
97
+ }
98
+ catch (e) {
99
+ // squelch errors about bad tokens, we can already see the 401 in the log
100
+ if (e.code !== 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED')
101
+ logger.error(e);
102
+ return undefined;
103
+ }
104
+ }, { freshseconds: 10 });
105
+ class TxStateUAuthContext extends Context {
106
+ static processIssuerConfig(config) {
107
+ var _b;
108
+ if (config.iss === 'unified-auth') {
109
+ config.validateUrl = new URL((_b = config.url) !== null && _b !== void 0 ? _b : '');
110
+ config.validateUrl.pathname = '/validateToken';
111
+ }
112
+ return config;
113
+ }
114
+ static async validateToken(token, issuerConfig, claims) {
115
+ var _b;
116
+ if (claims.iss === 'unified-auth') {
117
+ const validateUrl = new URL(issuerConfig.validateUrl);
118
+ validateUrl.searchParams.set('unifiedJwt', token);
119
+ const resp = await fetch(validateUrl);
120
+ const validate = await resp.json();
121
+ if (!validate.valid)
122
+ throw new Error((_b = validate.reason) !== null && _b !== void 0 ? _b : 'Your session has been ended on another device or in another browser tab/window. It\'s also possible your NetID is no longer active.');
123
+ }
124
+ }
125
+ async authFromPayload(payload) {
126
+ return {
127
+ username: payload.sub,
128
+ sessionId: payload.sub + '-' + payload.iat,
129
+ clientId: payload.client_id,
130
+ impersonatedBy: payload.impersonatedBy
131
+ };
132
+ }
133
+ }
134
+ async function unifiedAuthenticate(req, ContextClass = TxStateUAuthContext) {
135
+ const ctx = new ContextClass(req);
136
+ return ctx.waitForAuth();
137
+ }
138
+ exports.unifiedAuthenticate = unifiedAuthenticate;
@@ -1,7 +1,51 @@
1
1
  /// <reference types="node" />
2
- import { type FastifyInstance, type FastifyRequest, type FastifyReply, type FastifyServerOptions, type FastifyLoggerOptions } from 'fastify';
3
- import type http2 from 'http2';
2
+ /// <reference types="node" />
3
+ import { type FastifyDynamicSwaggerOptions } from '@fastify/swagger';
4
+ import { type FastifySwaggerUiOptions } from '@fastify/swagger-ui';
5
+ import type { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts';
6
+ import { type FastifyInstance, type FastifyRequest, type FastifyReply, type FastifyServerOptions, type FastifyLoggerOptions, type FastifyBaseLogger, type RawServerDefault } from 'fastify';
7
+ import http from 'node:http';
8
+ import type http2 from 'node:http2';
4
9
  type ErrorHandler = (error: Error, req: FastifyRequest, res: FastifyReply) => Promise<void>;
10
+ export interface FastifyTxStateAuthInfo {
11
+ /**
12
+ * The primary identifier for the user that is making the request, after processing
13
+ * their session token / JWT.
14
+ */
15
+ username: string;
16
+ /**
17
+ * This should be an identifier for the particular session, so that the same user
18
+ * on different devices/browsers/tabs can be distinguished from one another.
19
+ *
20
+ * It should NOT be usable as a cookie or bearer token, as it will appear in logs. If you
21
+ * use JSON Web Tokens, an easy thing is to combine the username with the `iat` issued
22
+ * date to create something unique but not useful to attackers.
23
+ *
24
+ * For lookup tokens, you can do the same `${username}-${createdAt}` after looking up
25
+ * the session in your database.
26
+ *
27
+ * If all else fails, you can sha256 the session token with a salt.
28
+ */
29
+ sessionId: string;
30
+ /**
31
+ * Some authentication systems allow administrators to impersonate regular users, so that
32
+ * they can see what that user sees and troubleshoot issues. We still want to log the administrator
33
+ * with any actions they take while impersonating someone, for auditing purposes, so you should
34
+ * fill this field when applicable.
35
+ *
36
+ * This will also be available at `req.auth.impersonatedBy`, so it is possible for your API
37
+ * to implement complicated authorization rules based on whether a user is being impersonated.
38
+ * It sort of defeats the purpose of impersonation, but used sparingly it could prevent administrators
39
+ * from making mistakes.
40
+ */
41
+ impersonatedBy?: string;
42
+ /**
43
+ * If your API may be accessed by a different client application, such that the user is actually logged
44
+ * into that application instead of yours, but you accept that application's session tokens, filling
45
+ * this field can help log requests that are authenticated with the other application's token.
46
+ */
47
+ clientId?: string;
48
+ }
5
49
  export interface FastifyTxStateOptions extends Partial<FastifyServerOptions> {
6
50
  https?: http2.SecureServerOptions;
7
51
  validOrigins?: string[];
@@ -16,9 +60,29 @@ export interface FastifyTxStateOptions extends Partial<FastifyServerOptions> {
16
60
  *
17
61
  * Setting a health message with setUnhealthy will override this and prevent it from being executed.
18
62
  */
19
- checkHealth?: () => Promise<string | undefined>;
63
+ checkHealth?: () => Promise<string | {
64
+ status?: number;
65
+ message?: string;
66
+ } | undefined>;
67
+ /**
68
+ * Run an async function to get authentication information out of the request
69
+ * object. Should return an object with at least a username and sessionid (see FastifyTxStateAuthInfo
70
+ * for further detail).
71
+ *
72
+ * The return object will be added to the request object as `req.auth` for later
73
+ * use in your route handlers. It will also be added to the logs in production.
74
+ *
75
+ * IMPORTANT: It is not advisable to return excessive amounts of data here, nor anything
76
+ * particularly sensitive, since it will all be included in every log entry.
77
+ *
78
+ * If this function throws, the client will receive a 401 response.
79
+ */
80
+ authenticate?: <T extends FastifyTxStateAuthInfo>(req: FastifyRequest) => Promise<T | undefined>;
20
81
  }
21
82
  declare module 'fastify' {
83
+ interface FastifyRequest {
84
+ auth?: FastifyTxStateAuthInfo;
85
+ }
22
86
  interface FastifyReply {
23
87
  extraLogInfo: any;
24
88
  }
@@ -51,6 +115,9 @@ export declare const devLogger: {
51
115
  };
52
116
  export declare const prodLogger: FastifyLoggerOptions;
53
117
  export default class Server {
118
+ protected config: FastifyTxStateOptions & {
119
+ http2?: true;
120
+ };
54
121
  protected https: boolean;
55
122
  protected errorHandlers: ErrorHandler[];
56
123
  protected healthMessage?: string;
@@ -63,7 +130,7 @@ export default class Server {
63
130
  protected validOrigins: Record<string, boolean>;
64
131
  protected validOriginHosts: Record<string, boolean>;
65
132
  protected validOriginSuffixes: Set<string>;
66
- app: FastifyInstance;
133
+ app: FastifyInstance<RawServerDefault, http.IncomingMessage, http.ServerResponse<http.IncomingMessage>, FastifyBaseLogger, JsonSchemaToTsProvider>;
67
134
  constructor(config?: FastifyTxStateOptions & {
68
135
  http2?: true;
69
136
  });
@@ -74,15 +141,13 @@ export default class Server {
74
141
  setValidOrigins(origins: string[]): void;
75
142
  setValidOriginHosts(hosts: string[]): void;
76
143
  setValidOriginSuffixes(suffixes: string[]): void;
144
+ swagger(opts?: {
145
+ path?: string;
146
+ openapi?: FastifyDynamicSwaggerOptions['openapi'];
147
+ ui?: FastifySwaggerUiOptions;
148
+ }): Promise<void>;
77
149
  close(softSeconds?: number): Promise<void>;
78
150
  }
79
- export declare class HttpError extends Error {
80
- statusCode: number;
81
- constructor(statusCode: number, message?: string);
82
- }
83
- type ValidationErrors = Record<string, string[]>;
84
- export declare class FailedValidationError extends HttpError {
85
- errors: ValidationErrors;
86
- constructor(errors: ValidationErrors);
87
- }
88
- export {};
151
+ export * from './analytics';
152
+ export * from './error';
153
+ export * from './unified-auth';
package/lib-esm/index.js CHANGED
@@ -4,4 +4,9 @@ export const devLogger = ftxst.devLogger
4
4
  export const prodLogger = ftxst.prodLogger
5
5
  export const HttpError = ftxst.HttpError
6
6
  export const FailedValidationError = ftxst.FailedValidationError
7
+ export const unifiedAuthenticate = ftxst.unifiedAuthenticate
8
+ export const analyticsPlugin = ftxst.analyticsPlugin
9
+ export const AnalyticsClient = ftxst.AnalyticsClient
10
+ export const LoggingAnalyticsClient = ftxst.LoggingAnalyticsClient
11
+ export const ElasticAnalyticsClient = ftxst.ElasticAnalyticsClient
7
12
  export default ftxst.default
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-txstate",
3
- "version": "3.1.8",
3
+ "version": "3.2.0",
4
4
  "description": "A small wrapper for fastify providing a set of common conventions & utility functions we use.",
5
5
  "exports": {
6
6
  "types": "./lib-esm/index.d.ts",
@@ -13,19 +13,31 @@
13
13
  "build": "rm -rf lib && tsc && mv lib/index.d.ts lib-esm/index.d.ts",
14
14
  "test": "./test.sh",
15
15
  "mocha": "TS_NODE_PROJECT=test/tsconfig.json mocha -r ts-node/register test/**/*.ts",
16
- "testserver": "ts-node-script testserver/index.ts"
16
+ "testserver": "node -r ts-node/register --no-warnings testserver/index.ts"
17
17
  },
18
18
  "dependencies": {
19
+ "@elastic/elasticsearch": "^8.12.2",
20
+ "@fastify/swagger": "^8.14.0",
21
+ "@fastify/swagger-ui": "^3.0.0",
22
+ "@fastify/type-provider-json-schema-to-ts": "^3.0.0",
23
+ "@txstate-mws/fastify-shared": "^1.0.4",
24
+ "ajv-errors": "^3.0.0",
19
25
  "fastify": "^4.9.2",
20
- "http-status-codes": "^2.1.4"
26
+ "fastify-plugin": "^4.5.1",
27
+ "http-status-codes": "^2.1.4",
28
+ "jose": "^5.2.3",
29
+ "txstate-utils": "^1.8.15",
30
+ "ua-parser-js": "^1.0.37"
21
31
  },
22
32
  "devDependencies": {
23
33
  "@types/chai": "^4.2.14",
24
34
  "@types/mocha": "^10.0.0",
25
- "@types/node": "^16.7.2",
26
- "axios": ">=0.20.0",
35
+ "@types/node": "^20.0.0",
36
+ "@types/ua-parser-js": ">=0.7.39",
37
+ "axios": "^1.6.8",
27
38
  "chai": "^4.2.0",
28
- "eslint-config-standard-with-typescript": "^34.0.0",
39
+ "eslint-config-standard-with-typescript": "^43.0.0",
40
+ "json-schema-to-ts": "^3.0.1",
29
41
  "mocha": "^10.0.0",
30
42
  "ts-node": "^10.2.1",
31
43
  "typescript": "^5.0.4"