@devmoods/express-extras 0.51.0 → 0.52.1
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/dist/auth/passwordless.d.ts +1 -1
- package/dist/auth/passwordless.js +19 -2
- package/dist/auth/token.js +2 -2
- package/dist/cache.d.ts +4 -1
- package/dist/cache.js +26 -2
- package/dist/config.d.ts +2 -0
- package/dist/config.js +1 -0
- package/dist/cookie-consent.d.ts +3 -0
- package/dist/cookie-consent.js +33 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +9 -9
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AnyUser, type AuthProvider } from './types.js';
|
|
2
2
|
export interface PasswordlessAuthProviderOptions<UserModel = AnyUser> {
|
|
3
|
-
getUserByEmail(email: string): Promise<UserModel>;
|
|
3
|
+
getUserByEmail(email: string): Promise<UserModel | null>;
|
|
4
4
|
onLoginRequest(user: UserModel, temporaryToken: string): Promise<void>;
|
|
5
5
|
}
|
|
6
6
|
export declare function PasswordlessAuthProvider<UserModel extends AnyUser>(options: PasswordlessAuthProviderOptions<UserModel>): AuthProvider<UserModel>;
|
|
@@ -3,9 +3,21 @@ import {} from './types.js';
|
|
|
3
3
|
import { Unauthorized } from '../errors.js';
|
|
4
4
|
import { getLogger } from '../logger.js';
|
|
5
5
|
import { route } from '../middleware.js';
|
|
6
|
+
import { DoesNotExistError } from '../postgres.js';
|
|
6
7
|
import { validateBody } from '../validate.js';
|
|
7
8
|
const logger = getLogger('auth');
|
|
8
9
|
export function PasswordlessAuthProvider(options) {
|
|
10
|
+
const getUserOrNull = async (email) => {
|
|
11
|
+
try {
|
|
12
|
+
return await options.getUserByEmail(email);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error instanceof DoesNotExistError) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
9
21
|
return {
|
|
10
22
|
setupRoutes(router, context) {
|
|
11
23
|
router.post('/passwordless/login/:token', route(async (req, res) => {
|
|
@@ -16,10 +28,11 @@ export function PasswordlessAuthProvider(options) {
|
|
|
16
28
|
await context.verifyTemporaryToken(temporaryToken, user);
|
|
17
29
|
}
|
|
18
30
|
catch {
|
|
31
|
+
logger.warn('passwordless: invalid temp token', { uid: user.id });
|
|
19
32
|
throw Unauthorized('Invalid token');
|
|
20
33
|
}
|
|
21
34
|
const accessToken = await context.login(req, res, user);
|
|
22
|
-
logger.info('passwordless login');
|
|
35
|
+
logger.info('passwordless login', { uid: user.id });
|
|
23
36
|
if (req.accepts('html')) {
|
|
24
37
|
return res.redirect('/');
|
|
25
38
|
}
|
|
@@ -36,7 +49,11 @@ export function PasswordlessAuthProvider(options) {
|
|
|
36
49
|
required: ['email'],
|
|
37
50
|
}), route(async (req) => {
|
|
38
51
|
const { email } = req.body;
|
|
39
|
-
const user = await
|
|
52
|
+
const user = await getUserOrNull(email);
|
|
53
|
+
if (!user) {
|
|
54
|
+
logger.warn('passwordless: user not found', { email });
|
|
55
|
+
return 202;
|
|
56
|
+
}
|
|
40
57
|
const token = await context.createTemporaryToken(user);
|
|
41
58
|
await options.onLoginRequest(user, token);
|
|
42
59
|
logger.info('passwordless login requested', { email });
|
package/dist/auth/token.js
CHANGED
|
@@ -98,14 +98,14 @@ export function setAccessTokenCookie(res, token) {
|
|
|
98
98
|
if (token == null) {
|
|
99
99
|
res.cookie(ACCESS_TOKEN_COOKIE_NAME, v4(), {
|
|
100
100
|
expires: new Date(1970, 0, 1),
|
|
101
|
-
sameSite:
|
|
101
|
+
sameSite: config.value.AUTH_COOKIE_SAME_SITE,
|
|
102
102
|
secure: process.env.NODE_ENV === 'production',
|
|
103
103
|
});
|
|
104
104
|
}
|
|
105
105
|
else {
|
|
106
106
|
res.cookie(ACCESS_TOKEN_COOKIE_NAME, token, {
|
|
107
107
|
httpOnly: true,
|
|
108
|
-
sameSite:
|
|
108
|
+
sameSite: config.value.AUTH_COOKIE_SAME_SITE,
|
|
109
109
|
secure: process.env.NODE_ENV === 'production',
|
|
110
110
|
});
|
|
111
111
|
}
|
package/dist/cache.d.ts
CHANGED
|
@@ -19,5 +19,8 @@ export declare function createCache(adapter: CacheAdapter): {
|
|
|
19
19
|
get: (key: string) => Promise<any>;
|
|
20
20
|
clear: (key?: string) => Promise<void>;
|
|
21
21
|
wrap: <T>(key: string, options: CacheOptions | number | null, fn: () => Promise<T>) => Promise<T>;
|
|
22
|
-
decorate: <T, Arguments extends any[]>(options: CacheOptions | number | null, fn: (...args: Arguments) => T | Promise<T>) =>
|
|
22
|
+
decorate: <T, Arguments extends any[]>(options: CacheOptions | number | null, fn: (...args: Arguments) => T | Promise<T>) => {
|
|
23
|
+
(...args: Arguments): Promise<T>;
|
|
24
|
+
clear(...args: Arguments): Promise<void>;
|
|
25
|
+
};
|
|
23
26
|
};
|
package/dist/cache.js
CHANGED
|
@@ -69,11 +69,14 @@ export function createCache(adapter) {
|
|
|
69
69
|
return result;
|
|
70
70
|
}
|
|
71
71
|
function cachedFn(options, fn) {
|
|
72
|
-
|
|
72
|
+
const getCacheKey = (...args) => {
|
|
73
73
|
const baseKey = typeof options === 'object' && options != null
|
|
74
74
|
? options.key
|
|
75
75
|
: undefined;
|
|
76
|
-
|
|
76
|
+
return `${baseKey ?? Buffer.from(fn.toString()).toString('hex')}-${hashKey(args)}`;
|
|
77
|
+
};
|
|
78
|
+
const wrapped = async (...args) => {
|
|
79
|
+
const cacheKey = getCacheKey(...args);
|
|
77
80
|
const cachedValue = await adapter.get(cacheKey);
|
|
78
81
|
if (cachedValue != null) {
|
|
79
82
|
return cachedValue;
|
|
@@ -85,6 +88,8 @@ export function createCache(adapter) {
|
|
|
85
88
|
await adapter.set(cacheKey, result, expiresMs);
|
|
86
89
|
return result;
|
|
87
90
|
};
|
|
91
|
+
wrapped.clear = (...args) => adapter.clear(getCacheKey(...args));
|
|
92
|
+
return wrapped;
|
|
88
93
|
}
|
|
89
94
|
return {
|
|
90
95
|
set: adapter.set.bind(adapter),
|
|
@@ -94,3 +99,22 @@ export function createCache(adapter) {
|
|
|
94
99
|
decorate: cachedFn,
|
|
95
100
|
};
|
|
96
101
|
}
|
|
102
|
+
function hashKey(obj) {
|
|
103
|
+
return JSON.stringify(obj, (key, value) => {
|
|
104
|
+
if (value === null || value === undefined) {
|
|
105
|
+
return String(value);
|
|
106
|
+
}
|
|
107
|
+
if (typeof value === 'object') {
|
|
108
|
+
if (Array.isArray(value)) {
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
return Object.keys(value)
|
|
112
|
+
.sort()
|
|
113
|
+
.reduce((result, item) => {
|
|
114
|
+
result[item] = value[item];
|
|
115
|
+
return result;
|
|
116
|
+
}, {});
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
});
|
|
120
|
+
}
|
package/dist/config.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare const config: {
|
|
|
6
6
|
AUTH_TOKEN_EXPIRES_IN: number;
|
|
7
7
|
AUTH_TEMP_TOKEN_EXPIRES_IN: number;
|
|
8
8
|
AUTH_SECRET: string;
|
|
9
|
+
AUTH_COOKIE_SAME_SITE: string;
|
|
9
10
|
REDIS_URL: string;
|
|
10
11
|
REDIS_KEY_PREFIX: string;
|
|
11
12
|
POSTGRES_URL: string;
|
|
@@ -37,6 +38,7 @@ export declare function extendConfig<T extends Record<string, unknown>>(appConfi
|
|
|
37
38
|
AUTH_TOKEN_EXPIRES_IN: number;
|
|
38
39
|
AUTH_TEMP_TOKEN_EXPIRES_IN: number;
|
|
39
40
|
AUTH_SECRET: string;
|
|
41
|
+
AUTH_COOKIE_SAME_SITE: string;
|
|
40
42
|
REDIS_URL: string;
|
|
41
43
|
REDIS_KEY_PREFIX: string;
|
|
42
44
|
POSTGRES_URL: string;
|
package/dist/config.js
CHANGED
|
@@ -9,6 +9,7 @@ const baseConfig = {
|
|
|
9
9
|
AUTH_TOKEN_EXPIRES_IN: readEnv.number('AUTH_TOKEN_EXPIRES', 3600 * 24 * 30), // 30 days
|
|
10
10
|
AUTH_TEMP_TOKEN_EXPIRES_IN: readEnv.number('AUTH_TEMP_TOKEN_EXPIRES', 3600),
|
|
11
11
|
AUTH_SECRET: readEnv('AUTH_JWT_SECRET', ''),
|
|
12
|
+
AUTH_COOKIE_SAME_SITE: readEnv('AUTH_COOKIE_SAME_SITE', 'strict'),
|
|
12
13
|
REDIS_URL: readEnv('REDIS_URL', ''),
|
|
13
14
|
REDIS_KEY_PREFIX: readEnv('REDIS_KEY_PREFIX', ''),
|
|
14
15
|
POSTGRES_URL: readEnv('POSTGRES_URL', ''),
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { getLogger } from './logger.js';
|
|
3
|
+
import { route } from './middleware.js';
|
|
4
|
+
import { validateBody } from './validate.js';
|
|
5
|
+
const logger = getLogger();
|
|
6
|
+
export function cookieConsent({ onConsent, }) {
|
|
7
|
+
const router = Router();
|
|
8
|
+
router.get('/', route(() => {
|
|
9
|
+
return {
|
|
10
|
+
consents: [],
|
|
11
|
+
};
|
|
12
|
+
}));
|
|
13
|
+
router.post('/', validateBody({
|
|
14
|
+
type: 'object',
|
|
15
|
+
properties: {
|
|
16
|
+
consents: {
|
|
17
|
+
type: 'array',
|
|
18
|
+
items: { type: 'string' },
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
}), route(async (req, res) => {
|
|
22
|
+
const { consents } = req.body;
|
|
23
|
+
res.cookie('cookie_consent', consents.join(',') || '1', {
|
|
24
|
+
expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000),
|
|
25
|
+
sameSite: 'strict',
|
|
26
|
+
secure: process.env.NODE_ENV === 'production',
|
|
27
|
+
});
|
|
28
|
+
await onConsent?.(consents);
|
|
29
|
+
logger.info('cookie consent');
|
|
30
|
+
return { consents };
|
|
31
|
+
}));
|
|
32
|
+
return router;
|
|
33
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devmoods/express-extras",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.1",
|
|
4
4
|
"description": "Kubernetes-ready utilities for rapid Node.js application development",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -44,15 +44,15 @@
|
|
|
44
44
|
],
|
|
45
45
|
"license": "ISC",
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"arktype": "^2.0.2",
|
|
48
47
|
"@devmoods/eslint-config": "^2.6.0",
|
|
49
48
|
"@types/koa-compose": "^3.2.8",
|
|
50
|
-
"@types/node": "22.
|
|
49
|
+
"@types/node": "22.13.0",
|
|
51
50
|
"@types/supertest": "^6.0.2",
|
|
52
51
|
"@vitejs/plugin-react": "^4.3.4",
|
|
53
52
|
"@vitest/coverage-v8": "^3.0.4",
|
|
53
|
+
"arktype": "^2.0.4",
|
|
54
54
|
"cookiejar": "^2.1.4",
|
|
55
|
-
"eslint": "^9.
|
|
55
|
+
"eslint": "^9.19.0",
|
|
56
56
|
"faktory-worker": "^4.6.0",
|
|
57
57
|
"prettier": "3.4.2",
|
|
58
58
|
"supertest": "7.0.0",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
67
|
"@devmoods/fetch": "^4.0.1",
|
|
68
|
-
"@sentry/node": "^8.
|
|
69
|
-
"@sentry/profiling-node": "^8.
|
|
68
|
+
"@sentry/node": "^8.53.0",
|
|
69
|
+
"@sentry/profiling-node": "^8.53.0",
|
|
70
70
|
"@types/bcrypt": "^5.0.2",
|
|
71
71
|
"@types/cookie-parser": "^1.4.8",
|
|
72
72
|
"@types/express": "^5.0.0",
|
|
@@ -90,7 +90,7 @@
|
|
|
90
90
|
"pg": "^8.13.1",
|
|
91
91
|
"redis": "^4.7.0",
|
|
92
92
|
"sonda": "^0.7.1",
|
|
93
|
-
"sql-formatter": "^15.4.
|
|
93
|
+
"sql-formatter": "^15.4.10",
|
|
94
94
|
"uuid": "^11.0.5"
|
|
95
95
|
},
|
|
96
96
|
"prettier": {
|
|
@@ -98,9 +98,9 @@
|
|
|
98
98
|
},
|
|
99
99
|
"peerDependencies": {
|
|
100
100
|
"@vitejs/plugin-react": ">=4.3.4",
|
|
101
|
+
"arktype": ">=2.0.0",
|
|
101
102
|
"faktory-worker": ">=4.5.1",
|
|
102
|
-
"vite": ">=6.0.5"
|
|
103
|
-
"arktype": ">=2.0.2"
|
|
103
|
+
"vite": ">=6.0.5"
|
|
104
104
|
},
|
|
105
105
|
"engines": {
|
|
106
106
|
"node": ">=20 <24"
|