@devmoods/express-extras 0.52.1 → 0.52.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.
@@ -95,17 +95,18 @@ export function extractAccessTokenFromRequest(req) {
95
95
  * If the provided token is `null` the cookie will be cleared.
96
96
  */
97
97
  export function setAccessTokenCookie(res, token) {
98
+ const sameSite = (config.value.AUTH_COOKIE_SAME_SITE || 'strict');
98
99
  if (token == null) {
99
100
  res.cookie(ACCESS_TOKEN_COOKIE_NAME, v4(), {
100
101
  expires: new Date(1970, 0, 1),
101
- sameSite: config.value.AUTH_COOKIE_SAME_SITE,
102
+ sameSite,
102
103
  secure: process.env.NODE_ENV === 'production',
103
104
  });
104
105
  }
105
106
  else {
106
107
  res.cookie(ACCESS_TOKEN_COOKIE_NAME, token, {
107
108
  httpOnly: true,
108
- sameSite: config.value.AUTH_COOKIE_SAME_SITE,
109
+ sameSite,
109
110
  secure: process.env.NODE_ENV === 'production',
110
111
  });
111
112
  }
package/dist/auth.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type RequestHandler, Router } from 'express';
1
+ import { type RequestHandler, type Response, type Request, Router } from 'express';
2
2
  import { TokenStorage, type AnyUser, type AuthMiddlewareOptions, type AuthOptions, type DecodedTemporaryToken } from './auth/types.js';
3
3
  import { type RedisClient } from './redis.js';
4
4
  export * from './auth/types.js';
@@ -14,6 +14,7 @@ export interface CreateAuth<UserModel extends AnyUser> {
14
14
  createTemporaryToken(user: UserModel): Promise<string>;
15
15
  verifyTemporaryToken(token: string, user: UserModel): Promise<void>;
16
16
  decodeTemporaryToken(token: string): DecodedTemporaryToken;
17
+ logout: (req: Request, res: Response) => Promise<void>;
17
18
  }
18
19
  export declare class RedisTokenStorage extends TokenStorage {
19
20
  private readonly redis;
package/dist/auth.js CHANGED
@@ -52,9 +52,12 @@ export function createAuth({ getUserById, filterUser, tokenStorage, providers, l
52
52
  }
53
53
  return accessToken;
54
54
  };
55
- const logout = async (res, sessionId) => {
56
- await tokenStorage.invalidate(sessionId);
57
- setAccessTokenCookie(res, null);
55
+ const logout = async (req, res) => {
56
+ const token = extractAccessTokenFromRequest(req);
57
+ if (token) {
58
+ await tokenStorage.invalidate(sha256(token));
59
+ setAccessTokenCookie(res, null);
60
+ }
58
61
  };
59
62
  // Make timing attacks harder
60
63
  const jitter = (duration = 0) => new Promise((resolve) => void setTimeout(resolve, Math.max((0.5 - duration) * 1000, 200)));
@@ -80,10 +83,7 @@ export function createAuth({ getUserById, filterUser, tokenStorage, providers, l
80
83
  return filterUser(user);
81
84
  }));
82
85
  router.get('/logout', route(async (req, res) => {
83
- const token = extractAccessTokenFromRequest(req);
84
- if (token) {
85
- await logout(res, sha256(token));
86
- }
86
+ await logout(req, res);
87
87
  res.redirect(loginUrl);
88
88
  }));
89
89
  /**
@@ -139,12 +139,12 @@ export function createAuth({ getUserById, filterUser, tokenStorage, providers, l
139
139
  logger.warn('request with invalid access token');
140
140
  return next();
141
141
  }
142
- await logout(res, sessionId);
142
+ await logout(req, res);
143
143
  throw new InvalidToken('session token expired');
144
144
  }
145
145
  const decoded = decodeAccessToken(token);
146
146
  if (decoded.v !== sessionTokenVersion) {
147
- await logout(res, token);
147
+ await logout(req, res);
148
148
  throw new InvalidToken('Invalid token version');
149
149
  }
150
150
  let user;
@@ -152,7 +152,7 @@ export function createAuth({ getUserById, filterUser, tokenStorage, providers, l
152
152
  user = await getUserById(decoded.uid);
153
153
  }
154
154
  catch {
155
- await logout(res, token);
155
+ await logout(req, res);
156
156
  throw new InvalidToken('User not found');
157
157
  }
158
158
  const tokenLifetimeMs = decoded.exp * 1000 - decoded.iat * 1000;
@@ -241,5 +241,6 @@ export function createAuth({ getUserById, filterUser, tokenStorage, providers, l
241
241
  createTemporaryToken,
242
242
  verifyTemporaryToken,
243
243
  decodeTemporaryToken,
244
+ logout,
244
245
  };
245
246
  }
package/dist/csrf.d.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import { type RequestHandler, type Request, type Response } from 'express';
2
2
  export declare const getCsrfSecret: () => string;
3
3
  export interface CsrfMiddlewareOptions {
4
- excempt?: boolean;
4
+ exempt?: boolean;
5
5
  cookieName?: string;
6
6
  sessionIdentifier?: (req: Request) => string;
7
7
  }
8
8
  interface CsrfOptions {
9
- excempt: boolean;
9
+ exempt: boolean;
10
10
  cookieName: string;
11
11
  sessionIdentifier: string;
12
12
  }
13
- export declare function csrf({ excempt, sessionIdentifier, cookieName, }?: CsrfMiddlewareOptions): RequestHandler;
13
+ export declare function csrf({ exempt, sessionIdentifier, cookieName, }?: CsrfMiddlewareOptions): RequestHandler;
14
14
  export declare function setCsrfOptions(options: CsrfOptions): void;
15
15
  interface TokenOptions {
16
16
  overwrite?: boolean;
package/dist/csrf.js CHANGED
@@ -16,7 +16,7 @@ export const getCsrfSecret = () => {
16
16
  }
17
17
  return secret;
18
18
  };
19
- export function csrf({ excempt = false, sessionIdentifier, cookieName, } = {}) {
19
+ export function csrf({ exempt = false, sessionIdentifier, cookieName, } = {}) {
20
20
  return (req, res, next) => {
21
21
  const existingOptions = useCsrfOptions();
22
22
  setCsrfOptions({
@@ -24,7 +24,7 @@ export function csrf({ excempt = false, sessionIdentifier, cookieName, } = {}) {
24
24
  sessionIdentifier: sessionIdentifier
25
25
  ? sessionIdentifier(req)
26
26
  : existingOptions.sessionIdentifier,
27
- excempt,
27
+ exempt,
28
28
  });
29
29
  next();
30
30
  };
@@ -35,7 +35,7 @@ export function setCsrfOptions(options) {
35
35
  }
36
36
  function useCsrfOptions() {
37
37
  return (useContext(CSRF_CONTEXT_KEY) || {
38
- excempt: true,
38
+ exempt: true,
39
39
  cookieName: defaultCookieName(),
40
40
  sessionIdentifier: '',
41
41
  });
@@ -109,8 +109,8 @@ const validateRequest = (req) => {
109
109
  };
110
110
  export function doubleCsrfProtection(req) {
111
111
  return async () => {
112
- const { excempt } = useCsrfOptions();
113
- if (excempt) {
112
+ const { exempt } = useCsrfOptions();
113
+ if (exempt) {
114
114
  return;
115
115
  }
116
116
  if (ignoredMethods.has(req.method)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devmoods/express-extras",
3
- "version": "0.52.1",
3
+ "version": "0.52.3",
4
4
  "description": "Kubernetes-ready utilities for rapid Node.js application development",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -46,27 +46,27 @@
46
46
  "devDependencies": {
47
47
  "@devmoods/eslint-config": "^2.6.0",
48
48
  "@types/koa-compose": "^3.2.8",
49
- "@types/node": "22.13.0",
49
+ "@types/node": "22.13.4",
50
50
  "@types/supertest": "^6.0.2",
51
51
  "@vitejs/plugin-react": "^4.3.4",
52
- "@vitest/coverage-v8": "^3.0.4",
52
+ "@vitest/coverage-v8": "^3.0.5",
53
53
  "arktype": "^2.0.4",
54
54
  "cookiejar": "^2.1.4",
55
- "eslint": "^9.19.0",
55
+ "eslint": "^9.20.1",
56
56
  "faktory-worker": "^4.6.0",
57
- "prettier": "3.4.2",
57
+ "prettier": "3.5.1",
58
58
  "supertest": "7.0.0",
59
59
  "ts-node": "10.9.2",
60
60
  "tsx": "^4.19.2",
61
61
  "typescript": "5.7.3",
62
- "vite": "^6.0.11",
63
- "vitest": "^3.0.4",
62
+ "vite": "^6.1.0",
63
+ "vitest": "^3.0.5",
64
64
  "vitest-fail-on-console": "^0.7.1"
65
65
  },
66
66
  "dependencies": {
67
67
  "@devmoods/fetch": "^4.0.1",
68
- "@sentry/node": "^8.53.0",
69
- "@sentry/profiling-node": "^8.53.0",
68
+ "@sentry/node": "^9.1.0",
69
+ "@sentry/profiling-node": "^9.1.0",
70
70
  "@types/bcrypt": "^5.0.2",
71
71
  "@types/cookie-parser": "^1.4.8",
72
72
  "@types/express": "^5.0.0",
@@ -87,7 +87,7 @@
87
87
  "helmet": "^8.0.0",
88
88
  "jsonwebtoken": "^9.0.2",
89
89
  "nodemailer": "^6.10.0",
90
- "pg": "^8.13.1",
90
+ "pg": "^8.13.3",
91
91
  "redis": "^4.7.0",
92
92
  "sonda": "^0.7.1",
93
93
  "sql-formatter": "^15.4.10",