fastify-txstate 3.2.6 → 3.2.8
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/index.js +11 -0
- package/lib/unified-auth.d.ts +2 -1
- package/lib/unified-auth.js +8 -1
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -121,6 +121,17 @@ class Server {
|
|
|
121
121
|
}
|
|
122
122
|
route.schema = newSchema;
|
|
123
123
|
});
|
|
124
|
+
/**
|
|
125
|
+
* Fastify validates response schema while serializing the response
|
|
126
|
+
*
|
|
127
|
+
* This is great, but because Ajv treats optional properties as non-nullable, optional
|
|
128
|
+
* properties that are set to `null` instead of `undefined` will fail validation (or with `coerceTypes`,
|
|
129
|
+
* be converted to empty string or 0 or false). This is silly behavior, so we're adding a hook to
|
|
130
|
+
* convert all nulls to undefined before fastify validates.
|
|
131
|
+
*/
|
|
132
|
+
this.app.addHook('preSerialization', async (req, res, payload) => {
|
|
133
|
+
return (0, txstate_utils_1.destroyNulls)(payload);
|
|
134
|
+
});
|
|
124
135
|
if (!config.skipOriginCheck && !process.env.SKIP_ORIGIN_CHECK) {
|
|
125
136
|
this.setValidOrigins([...((_d = config.validOrigins) !== null && _d !== void 0 ? _d : []), ...((_f = (_e = process.env.VALID_ORIGINS) === null || _e === void 0 ? void 0 : _e.split(',')) !== null && _f !== void 0 ? _f : [])]);
|
|
126
137
|
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 : [])]);
|
package/lib/unified-auth.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ declare class Context<AuthType extends FastifyTxStateAuthInfo = FastifyTxStateAu
|
|
|
29
29
|
}>;
|
|
30
30
|
constructor(req?: FastifyRequest);
|
|
31
31
|
waitForAuth(): Promise<AuthType | undefined>;
|
|
32
|
+
protected static hasInitialized: boolean;
|
|
32
33
|
static init(): void;
|
|
33
34
|
/**
|
|
34
35
|
* If implemented, this method will be called on startup, once per configured issuer. It receives
|
|
@@ -58,5 +59,5 @@ declare class TxStateUAuthContext extends Context {
|
|
|
58
59
|
authFromPayload(payload: JWTPayload): Promise<FastifyTxStateAuthInfo>;
|
|
59
60
|
}
|
|
60
61
|
export declare function unifiedAuthenticate(req: FastifyRequest, ContextClass?: typeof TxStateUAuthContext): Promise<FastifyTxStateAuthInfo | undefined>;
|
|
61
|
-
export declare function unifiedAuthenticateAll(req: FastifyRequest, ContextClass?: typeof TxStateUAuthContext): Promise<FastifyTxStateAuthInfo>;
|
|
62
|
+
export declare function unifiedAuthenticateAll(req: FastifyRequest, ContextClass?: typeof TxStateUAuthContext): Promise<FastifyTxStateAuthInfo | undefined>;
|
|
62
63
|
export {};
|
package/lib/unified-auth.js
CHANGED
|
@@ -28,6 +28,9 @@ class Context extends MockContext {
|
|
|
28
28
|
}
|
|
29
29
|
static init() {
|
|
30
30
|
var _b;
|
|
31
|
+
if (this.hasInitialized)
|
|
32
|
+
return;
|
|
33
|
+
this.hasInitialized = true;
|
|
31
34
|
let secret = cleanPem(process.env.JWT_SECRET_VERIFY);
|
|
32
35
|
if (secret != null) {
|
|
33
36
|
_a.jwtVerifyKey = (0, crypto_1.createPublicKey)(secret);
|
|
@@ -102,6 +105,7 @@ Context.tokenCache = new txstate_utils_1.Cache(async (token, { req, ctx }) => {
|
|
|
102
105
|
return undefined;
|
|
103
106
|
}
|
|
104
107
|
}, { freshseconds: 10 });
|
|
108
|
+
Context.hasInitialized = false;
|
|
105
109
|
class TxStateUAuthContext extends Context {
|
|
106
110
|
static processIssuerConfig(config) {
|
|
107
111
|
var _b;
|
|
@@ -132,14 +136,17 @@ class TxStateUAuthContext extends Context {
|
|
|
132
136
|
}
|
|
133
137
|
}
|
|
134
138
|
async function unifiedAuthenticate(req, ContextClass = TxStateUAuthContext) {
|
|
139
|
+
ContextClass.init();
|
|
135
140
|
const ctx = new ContextClass(req);
|
|
136
141
|
return ctx.waitForAuth();
|
|
137
142
|
}
|
|
138
143
|
exports.unifiedAuthenticate = unifiedAuthenticate;
|
|
139
144
|
async function unifiedAuthenticateAll(req, ContextClass = TxStateUAuthContext) {
|
|
145
|
+
var _b;
|
|
146
|
+
ContextClass.init();
|
|
140
147
|
const ctx = new ContextClass(req);
|
|
141
148
|
const auth = await ctx.waitForAuth();
|
|
142
|
-
if (!(auth === null || auth === void 0 ? void 0 : auth.username.length))
|
|
149
|
+
if (!(auth === null || auth === void 0 ? void 0 : auth.username.length) && !((_b = req.routeOptions.url) === null || _b === void 0 ? void 0 : _b.startsWith('/docs')))
|
|
143
150
|
throw new Error('All requests require authentication.');
|
|
144
151
|
return auth;
|
|
145
152
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify-txstate",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.8",
|
|
4
4
|
"description": "A small wrapper for fastify providing a set of common conventions & utility functions we use.",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"fastify-plugin": "^4.5.1",
|
|
30
30
|
"http-status-codes": "^2.1.4",
|
|
31
31
|
"jose": "^5.2.3",
|
|
32
|
-
"txstate-utils": "^1.
|
|
32
|
+
"txstate-utils": "^1.9.1",
|
|
33
33
|
"ua-parser-js": "^1.0.37"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|