win-portal-auth-sdk 1.4.1 → 1.5.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/client/api/auth.api.d.ts +14 -3
- package/dist/client/api/auth.api.d.ts.map +1 -1
- package/dist/client/api/auth.api.js +12 -4
- package/dist/client/api/index.d.ts +1 -0
- package/dist/client/api/index.d.ts.map +1 -1
- package/dist/client/api/index.js +3 -1
- package/dist/client/api/todo.api.d.ts +31 -0
- package/dist/client/api/todo.api.d.ts.map +1 -0
- package/dist/client/api/todo.api.js +36 -0
- package/dist/client/auth-client.d.ts +18 -6
- package/dist/client/auth-client.d.ts.map +1 -1
- package/dist/client/auth-client.js +71 -44
- package/dist/middleware/express.middleware.d.ts +1 -1
- package/dist/middleware/express.middleware.d.ts.map +1 -1
- package/dist/middleware/express.middleware.js +19 -92
- package/dist/middleware/nestjs.guard.d.ts.map +1 -1
- package/dist/middleware/nestjs.guard.js +24 -114
- package/dist/middleware/shared/auth-service.d.ts +36 -0
- package/dist/middleware/shared/auth-service.d.ts.map +1 -0
- package/dist/middleware/shared/auth-service.js +98 -0
- package/dist/middleware/shared/index.d.ts +7 -0
- package/dist/middleware/shared/index.d.ts.map +1 -0
- package/dist/middleware/shared/index.js +22 -0
- package/dist/middleware/shared/token-extractor.d.ts +15 -0
- package/dist/middleware/shared/token-extractor.d.ts.map +1 -0
- package/dist/middleware/shared/token-extractor.js +47 -0
- package/dist/middleware/shared/user-cache.d.ts +51 -0
- package/dist/middleware/shared/user-cache.d.ts.map +1 -0
- package/dist/middleware/shared/user-cache.js +81 -0
- package/dist/types/event-log.types.d.ts +103 -1
- package/dist/types/event-log.types.d.ts.map +1 -1
- package/dist/types/event-log.types.js +5 -1
- package/dist/types/index.d.ts +31 -29
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +9 -3
- package/dist/types/system-config.types.d.ts +2 -5
- package/dist/types/system-config.types.d.ts.map +1 -1
- package/dist/types/todo.types.d.ts +162 -0
- package/dist/types/todo.types.d.ts.map +1 -0
- package/dist/types/todo.types.js +18 -0
- package/package.json +1 -1
- package/dist/types/event-log.interfaces.d.ts +0 -108
- package/dist/types/event-log.interfaces.d.ts.map +0 -1
- package/dist/types/event-log.interfaces.js +0 -7
|
@@ -6,12 +6,13 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.clearAuthCache = exports.requireAuth = exports.getAuth = exports.authMiddleware = void 0;
|
|
9
|
-
const
|
|
9
|
+
const auth_service_1 = require("./shared/auth-service");
|
|
10
|
+
const token_extractor_1 = require("./shared/token-extractor");
|
|
11
|
+
const user_cache_1 = require("./shared/user-cache");
|
|
10
12
|
// Import Express type augmentation
|
|
11
13
|
require("./express.types");
|
|
12
|
-
const userCache = new Map();
|
|
13
14
|
/**
|
|
14
|
-
* Create Express
|
|
15
|
+
* Create Express middleware
|
|
15
16
|
*
|
|
16
17
|
* @example
|
|
17
18
|
* ```typescript
|
|
@@ -31,103 +32,29 @@ const userCache = new Map();
|
|
|
31
32
|
* ```
|
|
32
33
|
*/
|
|
33
34
|
function authMiddleware(config) {
|
|
34
|
-
const
|
|
35
|
-
baseURL: config.baseURL,
|
|
36
|
-
apiKey: config.apiKey,
|
|
37
|
-
apiKeyHeader: config.apiKeyHeader,
|
|
38
|
-
});
|
|
39
|
-
const cacheTimeout = (config.cacheTimeout || 300) * 1000; // Convert to ms
|
|
40
|
-
const tokenStrategy = config.tokenStrategy || 'bearer';
|
|
41
|
-
const cookieName = config.cookieName || 'access_token';
|
|
42
|
-
const excludePaths = config.excludePaths || [];
|
|
35
|
+
const authService = new auth_service_1.AuthService(config);
|
|
43
36
|
return async (req, res, next) => {
|
|
44
37
|
// No type assertion needed - req already has user/token from Express.Request augmentation
|
|
45
38
|
// Check if path is excluded
|
|
46
|
-
|
|
47
|
-
if (typeof pattern === 'string') {
|
|
48
|
-
return req.path === pattern;
|
|
49
|
-
}
|
|
50
|
-
return pattern.test(req.path);
|
|
51
|
-
});
|
|
52
|
-
if (shouldSkip) {
|
|
39
|
+
if ((0, token_extractor_1.shouldExcludePath)(req.path, config.excludePaths)) {
|
|
53
40
|
req.user = null;
|
|
54
41
|
req.token = null;
|
|
55
42
|
return next();
|
|
56
43
|
}
|
|
57
|
-
//
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (authHeader && authHeader.startsWith('Bearer ')) {
|
|
65
|
-
token = authHeader.substring(7);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
else if (tokenStrategy === 'cookie') {
|
|
69
|
-
token = req.cookies?.[cookieName] || null;
|
|
70
|
-
}
|
|
71
|
-
// No token found
|
|
72
|
-
if (!token) {
|
|
73
|
-
if (config.optional) {
|
|
74
|
-
req.user = null;
|
|
75
|
-
req.token = null;
|
|
76
|
-
return next();
|
|
77
|
-
}
|
|
78
|
-
return res.status(401).json({
|
|
79
|
-
statusCode: 401,
|
|
80
|
-
message: 'Authentication required',
|
|
81
|
-
error: 'Unauthorized',
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
try {
|
|
85
|
-
// Check cache first
|
|
86
|
-
const cached = userCache.get(token);
|
|
87
|
-
if (cached && Date.now() - cached.timestamp < cacheTimeout) {
|
|
88
|
-
req.user = cached.user;
|
|
89
|
-
req.token = token;
|
|
90
|
-
return next();
|
|
91
|
-
}
|
|
92
|
-
// Fetch user profile from API
|
|
93
|
-
client.setToken(token);
|
|
94
|
-
const user = await client.auth.profile();
|
|
95
|
-
// Update cache
|
|
96
|
-
userCache.set(token, {
|
|
97
|
-
user,
|
|
98
|
-
timestamp: Date.now(),
|
|
99
|
-
});
|
|
100
|
-
// Clean up old cache entries (simple cleanup)
|
|
101
|
-
if (userCache.size > 1000) {
|
|
102
|
-
const now = Date.now();
|
|
103
|
-
const entries = Array.from(userCache.entries());
|
|
104
|
-
for (const [key, entry] of entries) {
|
|
105
|
-
if (now - entry.timestamp > cacheTimeout) {
|
|
106
|
-
userCache.delete(key);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
// Attach user to request
|
|
111
|
-
req.user = user;
|
|
112
|
-
req.token = token;
|
|
113
|
-
next();
|
|
114
|
-
}
|
|
115
|
-
catch (error) {
|
|
116
|
-
// Clear cache on error
|
|
117
|
-
userCache.delete(token);
|
|
118
|
-
if (config.optional) {
|
|
119
|
-
req.user = null;
|
|
120
|
-
req.token = null;
|
|
121
|
-
return next();
|
|
122
|
-
}
|
|
123
|
-
const status = error.response?.status || 401;
|
|
124
|
-
const message = error.response?.data?.message || 'Authentication failed';
|
|
125
|
-
return res.status(status).json({
|
|
126
|
-
statusCode: status,
|
|
127
|
-
message,
|
|
44
|
+
// Authenticate request
|
|
45
|
+
const result = await authService.authenticate(req);
|
|
46
|
+
// Handle error
|
|
47
|
+
if (result.error) {
|
|
48
|
+
return res.status(result.error.status).json({
|
|
49
|
+
statusCode: result.error.status,
|
|
50
|
+
message: result.error.message,
|
|
128
51
|
error: 'Unauthorized',
|
|
129
52
|
});
|
|
130
53
|
}
|
|
54
|
+
// Attach user and token to request
|
|
55
|
+
req.user = result.user;
|
|
56
|
+
req.token = result.token;
|
|
57
|
+
next();
|
|
131
58
|
};
|
|
132
59
|
}
|
|
133
60
|
exports.authMiddleware = authMiddleware;
|
|
@@ -176,10 +103,10 @@ exports.requireAuth = requireAuth;
|
|
|
176
103
|
*/
|
|
177
104
|
function clearAuthCache(token) {
|
|
178
105
|
if (token) {
|
|
179
|
-
userCache.delete(token);
|
|
106
|
+
user_cache_1.userCache.delete(token);
|
|
180
107
|
}
|
|
181
108
|
else {
|
|
182
|
-
userCache.clear();
|
|
109
|
+
user_cache_1.userCache.clear();
|
|
183
110
|
}
|
|
184
111
|
}
|
|
185
112
|
exports.clearAuthCache = clearAuthCache;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nestjs.guard.d.ts","sourceRoot":"","sources":["../../src/middleware/nestjs.guard.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"nestjs.guard.d.ts","sourceRoot":"","sources":["../../src/middleware/nestjs.guard.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAI3C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,IAAI,CAAC,gBAAgB,EAAE,UAAU,CAAC;;6BAO3C,GAAG,GAAG,QAAQ,OAAO,CAAC;;EAoBpD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,IAAI,CAAC,gBAAgB,EAAE,UAAU,CAAC;;6BAOnD,GAAG,GAAG,QAAQ,OAAO,CAAC;;EAepD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,CAAC,EAAE,MAAM,QAMhD"}
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.clearNestAuthCache = exports.createOptionalAuthGuard = exports.createAuthGuard = void 0;
|
|
9
|
-
const
|
|
10
|
-
const
|
|
9
|
+
const auth_service_1 = require("./shared/auth-service");
|
|
10
|
+
const user_cache_1 = require("./shared/user-cache");
|
|
11
11
|
/**
|
|
12
12
|
* NestJS Auth Guard
|
|
13
13
|
*
|
|
@@ -30,69 +30,23 @@ const userCache = new Map();
|
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
32
|
function createAuthGuard(config) {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
apiKeyHeader: config.apiKeyHeader,
|
|
33
|
+
const authService = new auth_service_1.AuthService({
|
|
34
|
+
...config,
|
|
35
|
+
optional: false,
|
|
37
36
|
});
|
|
38
|
-
const cacheTimeout = (config.cacheTimeout || 300) * 1000;
|
|
39
|
-
const tokenStrategy = config.tokenStrategy || 'bearer';
|
|
40
|
-
const cookieName = config.cookieName || 'access_token';
|
|
41
37
|
class AuthGuard {
|
|
42
38
|
async canActivate(context) {
|
|
43
39
|
const request = context.switchToHttp().getRequest();
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
if
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
else if (tokenStrategy === 'bearer') {
|
|
50
|
-
const authHeader = request.headers.authorization;
|
|
51
|
-
if (authHeader && authHeader.startsWith('Bearer ')) {
|
|
52
|
-
token = authHeader.substring(7);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
else if (tokenStrategy === 'cookie') {
|
|
56
|
-
token = request.cookies?.[cookieName] || null;
|
|
57
|
-
}
|
|
58
|
-
if (!token) {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
try {
|
|
62
|
-
// Check cache first
|
|
63
|
-
const cached = userCache.get(token);
|
|
64
|
-
if (cached && Date.now() - cached.timestamp < cacheTimeout) {
|
|
65
|
-
request.user = cached.user;
|
|
66
|
-
request.token = token;
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
// Fetch user profile
|
|
70
|
-
client.setToken(token);
|
|
71
|
-
const user = await client.auth.profile();
|
|
72
|
-
// Update cache
|
|
73
|
-
userCache.set(token, {
|
|
74
|
-
user,
|
|
75
|
-
timestamp: Date.now(),
|
|
76
|
-
});
|
|
77
|
-
// Clean up old cache entries
|
|
78
|
-
if (userCache.size > 1000) {
|
|
79
|
-
const now = Date.now();
|
|
80
|
-
const entries = Array.from(userCache.entries());
|
|
81
|
-
for (const [key, entry] of entries) {
|
|
82
|
-
if (now - entry.timestamp > cacheTimeout) {
|
|
83
|
-
userCache.delete(key);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
// Attach user to request
|
|
88
|
-
request.user = user;
|
|
89
|
-
request.token = token;
|
|
90
|
-
return true;
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
userCache.delete(token);
|
|
40
|
+
// Authenticate request
|
|
41
|
+
const result = await authService.authenticate(request);
|
|
42
|
+
// Return false if authentication failed
|
|
43
|
+
if (result.error || !result.user || !result.token) {
|
|
94
44
|
return false;
|
|
95
45
|
}
|
|
46
|
+
// Attach user and token to request
|
|
47
|
+
request.user = result.user;
|
|
48
|
+
request.token = result.token;
|
|
49
|
+
return true;
|
|
96
50
|
}
|
|
97
51
|
}
|
|
98
52
|
return AuthGuard;
|
|
@@ -112,63 +66,19 @@ exports.createAuthGuard = createAuthGuard;
|
|
|
112
66
|
* ```
|
|
113
67
|
*/
|
|
114
68
|
function createOptionalAuthGuard(config) {
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
apiKeyHeader: config.apiKeyHeader,
|
|
69
|
+
const authService = new auth_service_1.AuthService({
|
|
70
|
+
...config,
|
|
71
|
+
optional: true,
|
|
119
72
|
});
|
|
120
|
-
const cacheTimeout = (config.cacheTimeout || 300) * 1000;
|
|
121
|
-
const tokenStrategy = config.tokenStrategy || 'bearer';
|
|
122
|
-
const cookieName = config.cookieName || 'access_token';
|
|
123
73
|
class OptionalAuthGuard {
|
|
124
74
|
async canActivate(context) {
|
|
125
75
|
const request = context.switchToHttp().getRequest();
|
|
126
|
-
//
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const authHeader = request.headers.authorization;
|
|
133
|
-
if (authHeader && authHeader.startsWith('Bearer ')) {
|
|
134
|
-
token = authHeader.substring(7);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else if (tokenStrategy === 'cookie') {
|
|
138
|
-
token = request.cookies?.[cookieName] || null;
|
|
139
|
-
}
|
|
140
|
-
if (!token) {
|
|
141
|
-
request.user = null;
|
|
142
|
-
request.token = null;
|
|
143
|
-
return true; // Allow access even without token
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
// Check cache first
|
|
147
|
-
const cached = userCache.get(token);
|
|
148
|
-
if (cached && Date.now() - cached.timestamp < cacheTimeout) {
|
|
149
|
-
request.user = cached.user;
|
|
150
|
-
request.token = token;
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
// Fetch user profile
|
|
154
|
-
client.setToken(token);
|
|
155
|
-
const user = await client.auth.profile();
|
|
156
|
-
// Update cache
|
|
157
|
-
userCache.set(token, {
|
|
158
|
-
user,
|
|
159
|
-
timestamp: Date.now(),
|
|
160
|
-
});
|
|
161
|
-
// Attach user to request
|
|
162
|
-
request.user = user;
|
|
163
|
-
request.token = token;
|
|
164
|
-
return true;
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
userCache.delete(token);
|
|
168
|
-
request.user = null;
|
|
169
|
-
request.token = null;
|
|
170
|
-
return true; // Allow access even on error
|
|
171
|
-
}
|
|
76
|
+
// Authenticate request (optional mode - always allows access)
|
|
77
|
+
const result = await authService.authenticate(request);
|
|
78
|
+
// Attach user and token to request (may be null)
|
|
79
|
+
request.user = result.user;
|
|
80
|
+
request.token = result.token;
|
|
81
|
+
return true; // Always allow access
|
|
172
82
|
}
|
|
173
83
|
}
|
|
174
84
|
return OptionalAuthGuard;
|
|
@@ -179,10 +89,10 @@ exports.createOptionalAuthGuard = createOptionalAuthGuard;
|
|
|
179
89
|
*/
|
|
180
90
|
function clearNestAuthCache(token) {
|
|
181
91
|
if (token) {
|
|
182
|
-
userCache.delete(token);
|
|
92
|
+
user_cache_1.userCache.delete(token);
|
|
183
93
|
}
|
|
184
94
|
else {
|
|
185
|
-
userCache.clear();
|
|
95
|
+
user_cache_1.userCache.clear();
|
|
186
96
|
}
|
|
187
97
|
}
|
|
188
98
|
exports.clearNestAuthCache = clearNestAuthCache;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth Service - Shared authentication logic for Express and NestJS
|
|
3
|
+
*
|
|
4
|
+
* Centralized authentication service to avoid code duplication
|
|
5
|
+
*/
|
|
6
|
+
import { AuthClient } from '../../client';
|
|
7
|
+
import { User } from '../../types';
|
|
8
|
+
import { MiddlewareConfig } from '../types';
|
|
9
|
+
/**
|
|
10
|
+
* Authentication result
|
|
11
|
+
*/
|
|
12
|
+
export interface AuthResult {
|
|
13
|
+
user: User | null;
|
|
14
|
+
token: string | null;
|
|
15
|
+
error?: {
|
|
16
|
+
status: number;
|
|
17
|
+
message: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Authentication service
|
|
22
|
+
*/
|
|
23
|
+
export declare class AuthService {
|
|
24
|
+
private client;
|
|
25
|
+
private config;
|
|
26
|
+
constructor(config: MiddlewareConfig);
|
|
27
|
+
/**
|
|
28
|
+
* Authenticate request
|
|
29
|
+
*/
|
|
30
|
+
authenticate(req: any): Promise<AuthResult>;
|
|
31
|
+
/**
|
|
32
|
+
* Get client instance (for advanced usage)
|
|
33
|
+
*/
|
|
34
|
+
getClient(): AuthClient;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=auth-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-service.d.ts","sourceRoot":"","sources":["../../../src/middleware/shared/auth-service.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAI5C;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAClB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE;QACN,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;CACH;AAED;;GAEG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,MAAM,CAAmB;gBAErB,MAAM,EAAE,gBAAgB;IASpC;;OAEG;IACG,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC;IAyEjD;;OAEG;IACH,SAAS,IAAI,UAAU;CAGxB"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Auth Service - Shared authentication logic for Express and NestJS
|
|
4
|
+
*
|
|
5
|
+
* Centralized authentication service to avoid code duplication
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.AuthService = void 0;
|
|
9
|
+
const client_1 = require("../../client");
|
|
10
|
+
const user_cache_1 = require("./user-cache");
|
|
11
|
+
const token_extractor_1 = require("./token-extractor");
|
|
12
|
+
/**
|
|
13
|
+
* Authentication service
|
|
14
|
+
*/
|
|
15
|
+
class AuthService {
|
|
16
|
+
constructor(config) {
|
|
17
|
+
this.config = config;
|
|
18
|
+
this.client = new client_1.AuthClient({
|
|
19
|
+
baseURL: config.baseURL,
|
|
20
|
+
apiKey: config.apiKey,
|
|
21
|
+
apiKeyHeader: config.apiKeyHeader,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Authenticate request
|
|
26
|
+
*/
|
|
27
|
+
async authenticate(req) {
|
|
28
|
+
// Extract token
|
|
29
|
+
const token = (0, token_extractor_1.extractToken)(req, this.config);
|
|
30
|
+
// No token found
|
|
31
|
+
if (!token) {
|
|
32
|
+
if (this.config.optional) {
|
|
33
|
+
return {
|
|
34
|
+
user: null,
|
|
35
|
+
token: null,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
user: null,
|
|
40
|
+
token: null,
|
|
41
|
+
error: {
|
|
42
|
+
status: 401,
|
|
43
|
+
message: 'Authentication required',
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const cacheTimeout = (this.config.cacheTimeout || 300) * 1000;
|
|
49
|
+
// Check cache first
|
|
50
|
+
const cachedUser = user_cache_1.userCache.get(token, cacheTimeout);
|
|
51
|
+
if (cachedUser) {
|
|
52
|
+
// Cleanup cache if needed
|
|
53
|
+
user_cache_1.userCache.cleanupIfNeeded(1000, cacheTimeout);
|
|
54
|
+
return {
|
|
55
|
+
user: cachedUser,
|
|
56
|
+
token,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// Fetch user profile from API
|
|
60
|
+
this.client.setToken(token);
|
|
61
|
+
const user = await this.client.auth.profile();
|
|
62
|
+
// Update cache
|
|
63
|
+
user_cache_1.userCache.set(token, user);
|
|
64
|
+
user_cache_1.userCache.cleanupIfNeeded(1000, cacheTimeout);
|
|
65
|
+
return {
|
|
66
|
+
user,
|
|
67
|
+
token,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
// Clear cache on error
|
|
72
|
+
user_cache_1.userCache.delete(token);
|
|
73
|
+
if (this.config.optional) {
|
|
74
|
+
return {
|
|
75
|
+
user: null,
|
|
76
|
+
token: null,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const status = error.response?.status || 401;
|
|
80
|
+
const message = error.response?.data?.message || 'Authentication failed';
|
|
81
|
+
return {
|
|
82
|
+
user: null,
|
|
83
|
+
token: null,
|
|
84
|
+
error: {
|
|
85
|
+
status,
|
|
86
|
+
message,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get client instance (for advanced usage)
|
|
93
|
+
*/
|
|
94
|
+
getClient() {
|
|
95
|
+
return this.client;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.AuthService = AuthService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/middleware/shared/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Shared utilities exports
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
__exportStar(require("./user-cache"), exports);
|
|
21
|
+
__exportStar(require("./token-extractor"), exports);
|
|
22
|
+
__exportStar(require("./auth-service"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token Extractor - Shared token extraction logic for Express and NestJS
|
|
3
|
+
*
|
|
4
|
+
* Centralized token extraction to avoid code duplication
|
|
5
|
+
*/
|
|
6
|
+
import { MiddlewareConfig } from '../types';
|
|
7
|
+
/**
|
|
8
|
+
* Extract token from request
|
|
9
|
+
*/
|
|
10
|
+
export declare function extractToken(req: any, config: MiddlewareConfig): string | null;
|
|
11
|
+
/**
|
|
12
|
+
* Check if path should be excluded
|
|
13
|
+
*/
|
|
14
|
+
export declare function shouldExcludePath(path: string, excludePaths?: (string | RegExp)[]): boolean;
|
|
15
|
+
//# sourceMappingURL=token-extractor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-extractor.d.ts","sourceRoot":"","sources":["../../../src/middleware/shared/token-extractor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C;;GAEG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,gBAAgB,GAAG,MAAM,GAAG,IAAI,CAqB9E;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,OAAO,CAW3F"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Token Extractor - Shared token extraction logic for Express and NestJS
|
|
4
|
+
*
|
|
5
|
+
* Centralized token extraction to avoid code duplication
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.shouldExcludePath = exports.extractToken = void 0;
|
|
9
|
+
/**
|
|
10
|
+
* Extract token from request
|
|
11
|
+
*/
|
|
12
|
+
function extractToken(req, config) {
|
|
13
|
+
// Custom extractor takes priority
|
|
14
|
+
if (config.tokenExtractor) {
|
|
15
|
+
return config.tokenExtractor(req);
|
|
16
|
+
}
|
|
17
|
+
const tokenStrategy = config.tokenStrategy || 'bearer';
|
|
18
|
+
const cookieName = config.cookieName || 'access_token';
|
|
19
|
+
if (tokenStrategy === 'bearer') {
|
|
20
|
+
const authHeader = req.headers?.authorization || req.headers?.Authorization;
|
|
21
|
+
if (authHeader && typeof authHeader === 'string' && authHeader.startsWith('Bearer ')) {
|
|
22
|
+
return authHeader.substring(7);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else if (tokenStrategy === 'cookie') {
|
|
26
|
+
// Support both Express (req.cookies) and NestJS (req.cookies)
|
|
27
|
+
const cookies = req.cookies || {};
|
|
28
|
+
return cookies[cookieName] || null;
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
exports.extractToken = extractToken;
|
|
33
|
+
/**
|
|
34
|
+
* Check if path should be excluded
|
|
35
|
+
*/
|
|
36
|
+
function shouldExcludePath(path, excludePaths) {
|
|
37
|
+
if (!excludePaths || excludePaths.length === 0) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return excludePaths.some((pattern) => {
|
|
41
|
+
if (typeof pattern === 'string') {
|
|
42
|
+
return path === pattern;
|
|
43
|
+
}
|
|
44
|
+
return pattern.test(path);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
exports.shouldExcludePath = shouldExcludePath;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User Cache - Shared cache management for Express and NestJS
|
|
3
|
+
*
|
|
4
|
+
* Centralized cache implementation to avoid code duplication
|
|
5
|
+
*/
|
|
6
|
+
import { User } from '../../types';
|
|
7
|
+
/**
|
|
8
|
+
* Cache entry structure
|
|
9
|
+
*/
|
|
10
|
+
export interface CacheEntry {
|
|
11
|
+
user: User;
|
|
12
|
+
timestamp: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* User cache manager
|
|
16
|
+
*/
|
|
17
|
+
declare class UserCacheManager {
|
|
18
|
+
private cache;
|
|
19
|
+
private defaultTimeout;
|
|
20
|
+
/**
|
|
21
|
+
* Get cached user
|
|
22
|
+
*/
|
|
23
|
+
get(token: string, timeout?: number): User | null;
|
|
24
|
+
/**
|
|
25
|
+
* Set cached user
|
|
26
|
+
*/
|
|
27
|
+
set(token: string, user: User): void;
|
|
28
|
+
/**
|
|
29
|
+
* Delete cached user
|
|
30
|
+
*/
|
|
31
|
+
delete(token: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Clear all cache
|
|
34
|
+
*/
|
|
35
|
+
clear(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Clean up old entries
|
|
38
|
+
*/
|
|
39
|
+
cleanup(timeout?: number): void;
|
|
40
|
+
/**
|
|
41
|
+
* Cleanup when cache size exceeds limit
|
|
42
|
+
*/
|
|
43
|
+
cleanupIfNeeded(maxSize?: number, timeout?: number): void;
|
|
44
|
+
/**
|
|
45
|
+
* Get cache size
|
|
46
|
+
*/
|
|
47
|
+
size(): number;
|
|
48
|
+
}
|
|
49
|
+
export declare const userCache: UserCacheManager;
|
|
50
|
+
export {};
|
|
51
|
+
//# sourceMappingURL=user-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-cache.d.ts","sourceRoot":"","sources":["../../../src/middleware/shared/user-cache.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEnC;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,IAAI,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,cAAM,gBAAgB;IACpB,OAAO,CAAC,KAAK,CAAiC;IAC9C,OAAO,CAAC,cAAc,CAAsB;IAE5C;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,MAA4B,GAAG,IAAI,GAAG,IAAI;IAetE;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI;IAOpC;;OAEG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAI3B;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,OAAO,CAAC,OAAO,GAAE,MAA4B,GAAG,IAAI;IAWpD;;OAEG;IACH,eAAe,CAAC,OAAO,GAAE,MAAa,EAAE,OAAO,GAAE,MAA4B,GAAG,IAAI;IAMpF;;OAEG;IACH,IAAI,IAAI,MAAM;CAGf;AAGD,eAAO,MAAM,SAAS,kBAAyB,CAAC"}
|