prostgles-server 4.2.153 → 4.2.155

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.
@@ -1,204 +0,0 @@
1
- import { Express, NextFunction, Request, Response } from "express";
2
- import { AnyObject, FieldFilter } from "prostgles-types";
3
- import { LocalParams, PRGLIOSocket } from "./DboBuilder/DboBuilder";
4
- import { DBOFullyTyped } from "./DBSchemaBuilder";
5
- import { DB, DBHandlerServer, Prostgles } from "./Prostgles";
6
- type Awaitable<T> = T | Promise<T>;
7
- type AuthSocketSchema = {
8
- user?: AnyObject;
9
- register?: boolean;
10
- login?: boolean;
11
- logout?: boolean;
12
- pathGuard?: boolean;
13
- };
14
- export type ExpressReq = Request;
15
- export type ExpressRes = Response;
16
- export type LoginClientInfo = {
17
- ip_address: string;
18
- ip_address_remote: string | undefined;
19
- x_real_ip: string | undefined;
20
- user_agent: string | undefined;
21
- };
22
- export type BasicSession = {
23
- /** Must be hard to bruteforce */
24
- sid: string;
25
- /** UNIX millisecond timestamp */
26
- expires: number;
27
- /** On expired */
28
- onExpiration: "redirect" | "show_error";
29
- };
30
- export type AuthClientRequest = {
31
- socket: PRGLIOSocket;
32
- } | {
33
- httpReq: ExpressReq;
34
- };
35
- export type UserLike = {
36
- id: string;
37
- type: string;
38
- [key: string]: any;
39
- };
40
- export type SessionUser<ServerUser extends UserLike = UserLike, ClientUser extends AnyObject = AnyObject> = {
41
- /**
42
- * This user will be available in all serverside prostgles options
43
- * id and type values will be available in the prostgles.user session variable in postgres
44
- * */
45
- user: ServerUser;
46
- /**
47
- * Controls which fields from user are available in postgres session variable
48
- */
49
- sessionFields?: FieldFilter<ServerUser>;
50
- /**
51
- * User data sent to the authenticated client
52
- */
53
- clientUser: ClientUser;
54
- };
55
- export type AuthResult<SU = SessionUser> = SU & {
56
- sid: string;
57
- } | {
58
- user?: undefined;
59
- clientUser?: undefined;
60
- sid?: string;
61
- } | undefined;
62
- export declare const HTTPCODES: {
63
- AUTH_ERROR: number;
64
- NOT_FOUND: number;
65
- BAD_REQUEST: number;
66
- INTERNAL_SERVER_ERROR: number;
67
- };
68
- export declare const getLoginClientInfo: (req: AuthClientRequest) => AuthClientRequest & LoginClientInfo;
69
- export type AuthRequestParams<S, SUser extends SessionUser> = {
70
- db: DB;
71
- dbo: DBOFullyTyped<S>;
72
- getUser: () => Promise<AuthResult<SUser>>;
73
- };
74
- export type Auth<S = void, SUser extends SessionUser = SessionUser> = {
75
- /**
76
- * Name of the cookie or socket hadnshake query param that represents the session id.
77
- * Defaults to "session_id"
78
- */
79
- sidKeyName?: string;
80
- /**
81
- * Response time rounding in milliseconds to prevent timing attacks on login. Login response time should always be a multiple of this value. Defaults to 500 milliseconds
82
- */
83
- responseThrottle?: number;
84
- expressConfig?: {
85
- /**
86
- * Express app instance. If provided Prostgles will attempt to set sidKeyName to user cookie
87
- */
88
- app: Express;
89
- /**
90
- * Used in allowing logging in through express. Defaults to /login
91
- */
92
- loginRoute?: string;
93
- /**
94
- * Used in allowing logging out through express. Defaults to /logout
95
- */
96
- logoutGetPath?: string;
97
- /**
98
- * Options used in setting the cookie after a successful login
99
- */
100
- cookieOptions?: AnyObject;
101
- /**
102
- * False by default. If false and userRoutes are provided then the socket will request window.location.reload if the current url is on a user route.
103
- */
104
- disableSocketAuthGuard?: boolean;
105
- /**
106
- * If provided, any client requests to NOT these routes (or their subroutes) will be redirected to loginRoute (if logged in) and then redirected back to the initial route after logging in
107
- * If logged in the user is allowed to access these routes
108
- */
109
- publicRoutes?: string[];
110
- /**
111
- * Will attach a app.use listener and will expose getUser
112
- * Used for blocking access
113
- */
114
- use?: (args: {
115
- req: ExpressReq;
116
- res: ExpressRes;
117
- next: NextFunction;
118
- } & AuthRequestParams<S, SUser>) => void | Promise<void>;
119
- /**
120
- * Will be called after a GET request is authorised
121
- * This means that
122
- */
123
- onGetRequestOK?: (req: ExpressReq, res: ExpressRes, params: AuthRequestParams<S, SUser>) => any;
124
- /**
125
- * Name of get url parameter used in redirecting user after successful login.
126
- * Defaults to "returnURL"
127
- */
128
- returnUrlParamName?: string;
129
- magicLinks?: {
130
- /**
131
- * Will default to /magic-link
132
- */
133
- route?: string;
134
- /**
135
- * Used in creating a session/logging in using a magic link
136
- */
137
- check: (magicId: string, dbo: DBOFullyTyped<S>, db: DB, client: LoginClientInfo) => Awaitable<BasicSession | undefined>;
138
- };
139
- };
140
- /**
141
- * undefined sid is allowed to enable public users
142
- */
143
- getUser: (sid: string | undefined, dbo: DBOFullyTyped<S>, db: DB, client: AuthClientRequest & LoginClientInfo) => Awaitable<AuthResult<SUser>>;
144
- register?: (params: AnyObject, dbo: DBOFullyTyped<S>, db: DB) => Awaitable<BasicSession> | BasicSession;
145
- login?: (params: AnyObject, dbo: DBOFullyTyped<S>, db: DB, client: LoginClientInfo) => Awaitable<BasicSession> | BasicSession;
146
- logout?: (sid: string | undefined, dbo: DBOFullyTyped<S>, db: DB) => Awaitable<any>;
147
- /**
148
- * If provided then session info will be saved on socket.__prglCache and reused from there
149
- */
150
- cacheSession?: {
151
- getSession: (sid: string | undefined, dbo: DBOFullyTyped<S>, db: DB) => Awaitable<BasicSession>;
152
- };
153
- };
154
- export declare class AuthHandler {
155
- protected prostgles: Prostgles;
156
- protected opts?: Auth;
157
- dbo: DBHandlerServer;
158
- db: DB;
159
- sidKeyName?: string;
160
- routes: {
161
- login?: string;
162
- returnUrlParamName?: string;
163
- logoutGetPath?: string;
164
- magicLinks?: {
165
- route: string;
166
- expressRoute: string;
167
- };
168
- readonly catchAll: '*';
169
- };
170
- constructor(prostgles: Prostgles);
171
- validateSid: (sid: string | undefined) => string | undefined;
172
- matchesRoute: (route: string | undefined, clientFullRoute: string) => boolean | "" | undefined;
173
- isUserRoute: (pathname: string) => boolean;
174
- private setCookieAndGoToReturnURLIFSet;
175
- getUser: (clientReq: {
176
- httpReq: ExpressReq;
177
- }) => Promise<AuthResult>;
178
- init(): Promise<void>;
179
- getReturnUrl: (req: ExpressReq) => string | null | undefined;
180
- destroy: () => void;
181
- throttledFunc: <T>(func: () => Promise<T>, throttle?: number) => Promise<T>;
182
- loginThrottled: (params: AnyObject, client: LoginClientInfo) => Promise<BasicSession>;
183
- /**
184
- * Will return first sid value found in:
185
- * Bearer header
186
- * http cookie
187
- * query params
188
- * Based on sid names in auth
189
- */
190
- getSID(localParams: LocalParams): string | undefined;
191
- /**
192
- * Used for logging
193
- */
194
- getSIDNoError: (localParams: LocalParams | undefined) => string | undefined;
195
- getClientInfo(localParams: Pick<LocalParams, "socket" | "httpReq">): Promise<AuthResult>;
196
- isValidSocketSession: (socket: PRGLIOSocket, session: BasicSession) => boolean;
197
- makeSocketAuth: (socket: PRGLIOSocket) => Promise<Record<string, never> | {
198
- auth: AuthSocketSchema;
199
- userData: AuthResult;
200
- }>;
201
- }
202
- export declare const getSafeReturnURL: (returnURL: string, returnUrlParamName: string, quiet?: boolean) => string | undefined;
203
- export {};
204
- //# sourceMappingURL=AuthHandler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AuthHandler.d.ts","sourceRoot":"","sources":["../lib/AuthHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnE,OAAO,EAAE,SAAS,EAA0D,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACjH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,EAAE,EAAE,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7D,KAAK,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AACnC,KAAK,gBAAgB,GAAG;IACtB,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EA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