fastify-txstate 3.2.1 → 3.2.3
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/lib/analytics.d.ts +2 -1
- package/lib/analytics.js +3 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.js +18 -5
- package/lib/unified-auth.d.ts +1 -0
- package/lib/unified-auth.js +8 -1
- package/lib-esm/index.js +1 -0
- package/package.json +1 -1
package/lib/analytics.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type InteractionEvent } from '@txstate-mws/fastify-shared';
|
|
2
|
-
import type { FastifyBaseLogger, FastifyInstance } from 'fastify';
|
|
2
|
+
import type { FastifyBaseLogger, FastifyInstance, FastifyRequest } from 'fastify';
|
|
3
3
|
import { type IBrowser, type IDevice, type IOS } from 'ua-parser-js';
|
|
4
4
|
import { type FastifyTxStateAuthInfo } from '.';
|
|
5
5
|
export interface StoredInteractionEvent extends InteractionEvent {
|
|
@@ -30,4 +30,5 @@ export declare class ElasticAnalyticsClient extends AnalyticsClient {
|
|
|
30
30
|
export declare function analyticsPlugin(fastify: FastifyInstance, opts: {
|
|
31
31
|
appName: string;
|
|
32
32
|
analyticsClient?: AnalyticsClient;
|
|
33
|
+
authorize?: (req: FastifyRequest) => boolean;
|
|
33
34
|
}, done: (err?: Error) => void): void;
|
package/lib/analytics.js
CHANGED
|
@@ -8,6 +8,7 @@ const elasticsearch_1 = __importDefault(require("@elastic/elasticsearch"));
|
|
|
8
8
|
const fastify_shared_1 = require("@txstate-mws/fastify-shared");
|
|
9
9
|
const txstate_utils_1 = require("txstate-utils");
|
|
10
10
|
const ua_parser_js_1 = require("ua-parser-js");
|
|
11
|
+
const _1 = require(".");
|
|
11
12
|
class AnalyticsClient {
|
|
12
13
|
async push(events) {
|
|
13
14
|
for (const event of events)
|
|
@@ -106,6 +107,8 @@ function analyticsPlugin(fastify, opts, done) {
|
|
|
106
107
|
}
|
|
107
108
|
fastify.post('/analytics', { schema: { body: { type: 'array', items: fastify_shared_1.interactionEvent }, response: { 202: { type: 'string', enum: ['OK'] } } } }, async (req, res) => {
|
|
108
109
|
const { auth } = req;
|
|
110
|
+
if (opts.authorize && !opts.authorize(req))
|
|
111
|
+
throw new _1.HttpError(401);
|
|
109
112
|
queueEvents(auth !== null && auth !== void 0 ? auth : { username: 'unauthenticated' }, req.headers, req.ip, req.body);
|
|
110
113
|
res.statusCode = 202;
|
|
111
114
|
return 'OK';
|
package/lib/index.d.ts
CHANGED
|
@@ -114,6 +114,8 @@ export declare const devLogger: {
|
|
|
114
114
|
child(bindings: any, options?: any): any;
|
|
115
115
|
};
|
|
116
116
|
export declare const prodLogger: FastifyLoggerOptions;
|
|
117
|
+
export type FastifyInstanceTyped = FastifyInstance<RawServerDefault, http.IncomingMessage, http.ServerResponse<http.IncomingMessage>, FastifyBaseLogger, JsonSchemaToTsProvider>;
|
|
118
|
+
export type TxServer = Server;
|
|
117
119
|
export default class Server {
|
|
118
120
|
protected config: FastifyTxStateOptions & {
|
|
119
121
|
http2?: true;
|
|
@@ -130,7 +132,7 @@ export default class Server {
|
|
|
130
132
|
protected validOrigins: Record<string, boolean>;
|
|
131
133
|
protected validOriginHosts: Record<string, boolean>;
|
|
132
134
|
protected validOriginSuffixes: Set<string>;
|
|
133
|
-
app:
|
|
135
|
+
app: FastifyInstanceTyped;
|
|
134
136
|
constructor(config?: FastifyTxStateOptions & {
|
|
135
137
|
http2?: true;
|
|
136
138
|
});
|
package/lib/index.js
CHANGED
|
@@ -140,7 +140,7 @@ class Server {
|
|
|
140
140
|
this.setValidOriginHosts([...((_g = config.validOriginHosts) !== null && _g !== void 0 ? _g : []), ...((_j = (_h = process.env.VALID_ORIGIN_HOSTS) === null || _h === void 0 ? void 0 : _h.split(',')) !== null && _j !== void 0 ? _j : [])]);
|
|
141
141
|
this.setValidOriginSuffixes([...((_k = config.validOriginSuffixes) !== null && _k !== void 0 ? _k : []), ...((_m = (_l = process.env.VALID_ORIGIN_SUFFIXES) === null || _l === void 0 ? void 0 : _l.split(',')) !== null && _m !== void 0 ? _m : [])]);
|
|
142
142
|
this.app.addHook('preHandler', async (req, res) => {
|
|
143
|
-
var _a
|
|
143
|
+
var _a;
|
|
144
144
|
res.extraLogInfo = {};
|
|
145
145
|
if (!req.headers.origin)
|
|
146
146
|
return;
|
|
@@ -174,17 +174,30 @@ class Server {
|
|
|
174
174
|
if (req.headers['access-control-request-headers'])
|
|
175
175
|
void res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
|
|
176
176
|
}
|
|
177
|
+
});
|
|
178
|
+
this.app.options('*', async (req, res) => {
|
|
179
|
+
await res.send();
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (config.authenticate) {
|
|
183
|
+
const authenticatedMethods = {
|
|
184
|
+
GET: true,
|
|
185
|
+
POST: true,
|
|
186
|
+
PUT: true,
|
|
187
|
+
PATCH: true,
|
|
188
|
+
DELETE: true
|
|
189
|
+
};
|
|
190
|
+
this.app.addHook('preHandler', async (req, res) => {
|
|
191
|
+
if (!authenticatedMethods[req.method])
|
|
192
|
+
return;
|
|
177
193
|
try {
|
|
178
|
-
req.auth = await
|
|
194
|
+
req.auth = await config.authenticate(req);
|
|
179
195
|
}
|
|
180
196
|
catch (e) {
|
|
181
197
|
await res.status(401).send('Failed to authenticate.');
|
|
182
198
|
return res;
|
|
183
199
|
}
|
|
184
200
|
});
|
|
185
|
-
this.app.options('*', async (req, res) => {
|
|
186
|
-
await res.send();
|
|
187
|
-
});
|
|
188
201
|
}
|
|
189
202
|
this.app.addHook('onSend', this.https && process.env.NODE_ENV !== 'development'
|
|
190
203
|
? async (_, resp) => {
|
package/lib/unified-auth.d.ts
CHANGED
|
@@ -58,4 +58,5 @@ declare class TxStateUAuthContext extends Context {
|
|
|
58
58
|
authFromPayload(payload: JWTPayload): Promise<FastifyTxStateAuthInfo>;
|
|
59
59
|
}
|
|
60
60
|
export declare function unifiedAuthenticate(req: FastifyRequest, ContextClass?: typeof TxStateUAuthContext): Promise<FastifyTxStateAuthInfo | undefined>;
|
|
61
|
+
export declare function unifiedAuthenticateAll(req: FastifyRequest, ContextClass?: typeof TxStateUAuthContext): Promise<void>;
|
|
61
62
|
export {};
|
package/lib/unified-auth.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.unifiedAuthenticate = void 0;
|
|
4
|
+
exports.unifiedAuthenticateAll = exports.unifiedAuthenticate = void 0;
|
|
5
5
|
const crypto_1 = require("crypto");
|
|
6
6
|
const jose_1 = require("jose");
|
|
7
7
|
const txstate_utils_1 = require("txstate-utils");
|
|
@@ -136,3 +136,10 @@ async function unifiedAuthenticate(req, ContextClass = TxStateUAuthContext) {
|
|
|
136
136
|
return ctx.waitForAuth();
|
|
137
137
|
}
|
|
138
138
|
exports.unifiedAuthenticate = unifiedAuthenticate;
|
|
139
|
+
async function unifiedAuthenticateAll(req, ContextClass = TxStateUAuthContext) {
|
|
140
|
+
const ctx = new ContextClass(req);
|
|
141
|
+
const auth = await ctx.waitForAuth();
|
|
142
|
+
if (!(auth === null || auth === void 0 ? void 0 : auth.username.length))
|
|
143
|
+
throw new Error('All requests require authentication.');
|
|
144
|
+
}
|
|
145
|
+
exports.unifiedAuthenticateAll = unifiedAuthenticateAll;
|
package/lib-esm/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export const prodLogger = ftxst.prodLogger
|
|
|
5
5
|
export const HttpError = ftxst.HttpError
|
|
6
6
|
export const FailedValidationError = ftxst.FailedValidationError
|
|
7
7
|
export const unifiedAuthenticate = ftxst.unifiedAuthenticate
|
|
8
|
+
export const unifiedAuthenticateAll = ftxst.unifiedAuthenticateAll
|
|
8
9
|
export const analyticsPlugin = ftxst.analyticsPlugin
|
|
9
10
|
export const AnalyticsClient = ftxst.AnalyticsClient
|
|
10
11
|
export const LoggingAnalyticsClient = ftxst.LoggingAnalyticsClient
|