fastify-txstate 3.3.5 → 3.4.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/lib/unified-auth.d.ts +1 -0
- package/lib/unified-auth.js +8 -3
- package/package.json +1 -1
- package/lib-esm/index.d.ts +0 -155
package/lib/unified-auth.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type FastifyRequest } from 'fastify';
|
|
2
2
|
import { type FastifyTxStateAuthInfo } from '.';
|
|
3
|
+
export declare const uaCookieName: string;
|
|
3
4
|
export declare function unifiedAuthenticate(req: FastifyRequest): Promise<FastifyTxStateAuthInfo | undefined>;
|
|
4
5
|
export declare function unifiedAuthenticateAll(req: FastifyRequest): Promise<FastifyTxStateAuthInfo>;
|
package/lib/unified-auth.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.unifiedAuthenticateAll = exports.unifiedAuthenticate = void 0;
|
|
3
|
+
exports.unifiedAuthenticateAll = exports.unifiedAuthenticate = exports.uaCookieName = void 0;
|
|
4
4
|
const crypto_1 = require("crypto");
|
|
5
5
|
const jose_1 = require("jose");
|
|
6
6
|
const txstate_utils_1 = require("txstate-utils");
|
|
@@ -8,6 +8,7 @@ let hasInit = false;
|
|
|
8
8
|
const issuerKeys = new Map();
|
|
9
9
|
const issuerConfig = new Map();
|
|
10
10
|
const trustedClients = new Set();
|
|
11
|
+
exports.uaCookieName = process.env.UA_COOKIE_NAME ?? (0, crypto_1.randomBytes)(16).toString('hex');
|
|
11
12
|
const tokenCache = new txstate_utils_1.Cache(async (token, req) => {
|
|
12
13
|
const claims = (0, jose_1.decodeJwt)(token);
|
|
13
14
|
let verifyKey;
|
|
@@ -90,7 +91,11 @@ function init() {
|
|
|
90
91
|
}
|
|
91
92
|
function tokenFromReq(req) {
|
|
92
93
|
const m = req?.headers.authorization?.match(/^bearer (.*)$/i);
|
|
93
|
-
|
|
94
|
+
if (m != null)
|
|
95
|
+
return m[1];
|
|
96
|
+
const m2 = req?.headers.cookie?.match(new RegExp(`${exports.uaCookieName}=([^;]+)`));
|
|
97
|
+
if (m2 != null)
|
|
98
|
+
return m2[1];
|
|
94
99
|
}
|
|
95
100
|
async function unifiedAuthenticate(req) {
|
|
96
101
|
if (!hasInit)
|
|
@@ -113,7 +118,7 @@ async function unifiedAuthenticate(req) {
|
|
|
113
118
|
exports.unifiedAuthenticate = unifiedAuthenticate;
|
|
114
119
|
async function unifiedAuthenticateAll(req) {
|
|
115
120
|
const auth = await unifiedAuthenticate(req);
|
|
116
|
-
if (!auth?.username.length
|
|
121
|
+
if (!auth?.username.length)
|
|
117
122
|
throw new Error('All requests require authentication.');
|
|
118
123
|
return auth;
|
|
119
124
|
}
|
package/package.json
CHANGED
package/lib-esm/index.d.ts
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
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';
|
|
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
|
-
}
|
|
49
|
-
export interface FastifyTxStateOptions extends Partial<FastifyServerOptions> {
|
|
50
|
-
https?: http2.SecureServerOptions;
|
|
51
|
-
validOrigins?: string[];
|
|
52
|
-
validOriginHosts?: string[];
|
|
53
|
-
validOriginSuffixes?: string[];
|
|
54
|
-
skipOriginCheck?: boolean;
|
|
55
|
-
checkOrigin?: (req: FastifyRequest) => boolean;
|
|
56
|
-
/**
|
|
57
|
-
* Run an asynchronous function to check the health of the service.
|
|
58
|
-
*
|
|
59
|
-
* Return a non-empty error message to trigger unhealthy status.
|
|
60
|
-
*
|
|
61
|
-
* Setting a health message with setUnhealthy will override this and prevent it from being executed.
|
|
62
|
-
*/
|
|
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?: (req: FastifyRequest) => Promise<FastifyTxStateAuthInfo | undefined>;
|
|
81
|
-
}
|
|
82
|
-
declare module 'fastify' {
|
|
83
|
-
interface FastifyRequest {
|
|
84
|
-
auth?: FastifyTxStateAuthInfo;
|
|
85
|
-
}
|
|
86
|
-
interface FastifyReply {
|
|
87
|
-
extraLogInfo: any;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
export declare const devLogger: {
|
|
91
|
-
level: string;
|
|
92
|
-
info: (msg: any) => void;
|
|
93
|
-
error: {
|
|
94
|
-
(...data: any[]): void;
|
|
95
|
-
(message?: any, ...optionalParams: any[]): void;
|
|
96
|
-
};
|
|
97
|
-
debug: {
|
|
98
|
-
(...data: any[]): void;
|
|
99
|
-
(message?: any, ...optionalParams: any[]): void;
|
|
100
|
-
};
|
|
101
|
-
fatal: {
|
|
102
|
-
(...data: any[]): void;
|
|
103
|
-
(message?: any, ...optionalParams: any[]): void;
|
|
104
|
-
};
|
|
105
|
-
warn: {
|
|
106
|
-
(...data: any[]): void;
|
|
107
|
-
(message?: any, ...optionalParams: any[]): void;
|
|
108
|
-
};
|
|
109
|
-
trace: {
|
|
110
|
-
(...data: any[]): void;
|
|
111
|
-
(message?: any, ...optionalParams: any[]): void;
|
|
112
|
-
};
|
|
113
|
-
silent: (msg: any) => void;
|
|
114
|
-
child(bindings: any, options?: any): any;
|
|
115
|
-
};
|
|
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;
|
|
119
|
-
export default class Server {
|
|
120
|
-
protected config: FastifyTxStateOptions & {
|
|
121
|
-
http2?: true;
|
|
122
|
-
};
|
|
123
|
-
protected https: boolean;
|
|
124
|
-
protected errorHandlers: ErrorHandler[];
|
|
125
|
-
protected healthMessage?: string;
|
|
126
|
-
protected healthCallback?: () => Promise<string | {
|
|
127
|
-
status?: number;
|
|
128
|
-
message?: string;
|
|
129
|
-
} | undefined>;
|
|
130
|
-
protected shuttingDown: boolean;
|
|
131
|
-
protected sigHandler: (signal: any) => void;
|
|
132
|
-
protected validOrigins: Record<string, boolean>;
|
|
133
|
-
protected validOriginHosts: Record<string, boolean>;
|
|
134
|
-
protected validOriginSuffixes: Set<string>;
|
|
135
|
-
app: FastifyInstanceTyped;
|
|
136
|
-
constructor(config?: FastifyTxStateOptions & {
|
|
137
|
-
http2?: true;
|
|
138
|
-
});
|
|
139
|
-
start(port?: number): Promise<void>;
|
|
140
|
-
addErrorHandler(handler: ErrorHandler): void;
|
|
141
|
-
setUnhealthy(message: string): void;
|
|
142
|
-
setHealthy(): void;
|
|
143
|
-
setValidOrigins(origins: string[]): void;
|
|
144
|
-
setValidOriginHosts(hosts: string[]): void;
|
|
145
|
-
setValidOriginSuffixes(suffixes: string[]): void;
|
|
146
|
-
swagger(opts?: {
|
|
147
|
-
path?: string;
|
|
148
|
-
openapi?: FastifyDynamicSwaggerOptions['openapi'];
|
|
149
|
-
ui?: FastifySwaggerUiOptions;
|
|
150
|
-
}): Promise<void>;
|
|
151
|
-
close(softSeconds?: number): Promise<void>;
|
|
152
|
-
}
|
|
153
|
-
export * from './analytics';
|
|
154
|
-
export * from './error';
|
|
155
|
-
export * from './unified-auth';
|