mulguard 1.1.6 → 1.1.7
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/README.md +210 -706
- package/dist/actions-CMtg7FGv.js +1 -0
- package/dist/{actions-DeCfLtHA.mjs → actions-CjQUKaXF.mjs} +54 -38
- package/dist/client/index.js +1 -1
- package/dist/client/index.mjs +84 -78
- package/dist/core/auth/email-password.d.ts +145 -0
- package/dist/core/auth/oauth/index.d.ts +14 -0
- package/dist/core/auth/oauth/oauth-handler.d.ts +172 -0
- package/dist/core/auth/oauth/pkce.d.ts +168 -0
- package/dist/core/auth/{oauth-providers.d.ts → oauth/providers.d.ts} +8 -7
- package/dist/core/auth/{oauth-state-store-cookie.d.ts → oauth/state-store-cookie.d.ts} +4 -4
- package/dist/core/auth/{oauth-state-store-redis.d.ts → oauth/state-store-redis.d.ts} +1 -1
- package/dist/core/auth/{oauth-state-store.d.ts → oauth/state-store.d.ts} +4 -1
- package/dist/core/auth/otp.d.ts +184 -0
- package/dist/core/errors/index.d.ts +269 -0
- package/dist/core/index.d.ts +1 -3
- package/dist/core/logger/index.d.ts +147 -0
- package/dist/core/mulguard/integration.d.ts +104 -0
- package/dist/core/mulguard/oauth-handler.d.ts +1 -1
- package/dist/core/security/security-manager.d.ts +236 -0
- package/dist/core/session/session-manager.d.ts +235 -0
- package/dist/core/types/index.d.ts +27 -5
- package/dist/index/index.js +1 -1
- package/dist/index/index.mjs +1388 -881
- package/dist/index.d.ts +3 -6
- package/dist/{client → nextjs/client}/hooks.d.ts +2 -2
- package/dist/nextjs/client/index.d.ts +13 -0
- package/dist/{client → nextjs/client}/provider.d.ts +1 -1
- package/dist/{client → nextjs/client}/server-actions-helper.d.ts +2 -2
- package/dist/{handlers → nextjs/handlers}/api.d.ts +1 -1
- package/dist/nextjs/handlers/index.d.ts +9 -0
- package/dist/{handlers → nextjs/handlers}/route.d.ts +1 -1
- package/dist/nextjs/index.d.ts +15 -0
- package/dist/nextjs/proxy/index.d.ts +149 -0
- package/dist/nextjs/server/actions.d.ts +30 -0
- package/dist/{server → nextjs/server}/auth.d.ts +6 -6
- package/dist/{server → nextjs/server}/cookies.d.ts +5 -6
- package/dist/nextjs/server/index.d.ts +18 -0
- package/dist/{server → nextjs/server}/oauth-state.d.ts +5 -3
- package/dist/{server → nextjs/server}/session-helpers.d.ts +1 -3
- package/dist/nextjs/server/session.d.ts +144 -0
- package/dist/oauth-state-Drwz6fES.js +1 -0
- package/dist/oauth-state-pdypStuS.mjs +210 -0
- package/dist/server/index.js +1 -1
- package/dist/server/index.mjs +27 -29
- package/package.json +64 -11
- package/dist/actions-CExpv_dD.js +0 -1
- package/dist/client/index.d.ts +0 -5
- package/dist/core/auth/index.d.ts +0 -40
- package/dist/core/auth/oauth.d.ts +0 -20
- package/dist/middleware/index.d.ts +0 -28
- package/dist/middleware/proxy.d.ts +0 -53
- package/dist/oauth-state-DKle8eCr.mjs +0 -289
- package/dist/oauth-state-DlvrCV11.js +0 -1
- package/dist/server/actions.d.ts +0 -86
- package/dist/server/helpers.d.ts +0 -10
- package/dist/server/index.d.ts +0 -14
- package/dist/server/middleware.d.ts +0 -39
- package/dist/server/session.d.ts +0 -28
- package/dist/server/utils.d.ts +0 -10
- /package/dist/{middleware → nextjs/proxy}/security.d.ts +0 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { MulguardConfig, User, Session, EmailCredentials } from '../types';
|
|
2
|
+
import { SecurityManager } from '../security/security-manager';
|
|
3
|
+
import { SessionManager as CoreSessionManager } from '../session/session-manager';
|
|
4
|
+
import { EmailPasswordAuth } from '../auth/email-password';
|
|
5
|
+
import { OTPAuth } from '../auth/otp';
|
|
6
|
+
import { OAuthHandler } from '../auth/oauth/oauth-handler';
|
|
7
|
+
import { Logger } from '../logger';
|
|
8
|
+
/**
|
|
9
|
+
* Core modules integration configuration.
|
|
10
|
+
*/
|
|
11
|
+
export interface CoreModulesIntegrationConfig<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>> {
|
|
12
|
+
readonly config: MulguardConfig<TUser, TSession>;
|
|
13
|
+
readonly baseUrl: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Integrated core modules instance.
|
|
17
|
+
*/
|
|
18
|
+
export interface IntegratedCoreModules {
|
|
19
|
+
readonly security: SecurityManager;
|
|
20
|
+
readonly session: CoreSessionManager;
|
|
21
|
+
readonly emailPassword: EmailPasswordAuth;
|
|
22
|
+
readonly otp: OTPAuth;
|
|
23
|
+
readonly oauth: OAuthHandler | null;
|
|
24
|
+
readonly logger: Logger;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates integrated core modules for Mulguard instance.
|
|
28
|
+
*
|
|
29
|
+
* Initializes and configures all core modules with proper dependencies.
|
|
30
|
+
*
|
|
31
|
+
* @template TUser - User type
|
|
32
|
+
* @template TSession - Session type
|
|
33
|
+
* @param integrationConfig - Integration configuration
|
|
34
|
+
* @returns Integrated core modules
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const modules = createCoreModulesIntegration({
|
|
39
|
+
* config: mulguardConfig,
|
|
40
|
+
* baseUrl: 'https://example.com',
|
|
41
|
+
* })
|
|
42
|
+
*
|
|
43
|
+
* // Use modules.security, modules.session, etc.
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function createCoreModulesIntegration<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>>(integrationConfig: CoreModulesIntegrationConfig<TUser, TSession>): IntegratedCoreModules;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a wrapper for custom email/password action that uses EmailPasswordAuth.
|
|
49
|
+
*
|
|
50
|
+
* @template TUser - User type
|
|
51
|
+
* @template TSession - Session type
|
|
52
|
+
* @param emailPassword - EmailPasswordAuth instance
|
|
53
|
+
* @param customAction - Custom action from config
|
|
54
|
+
* @returns Wrapped action that uses EmailPasswordAuth for validation
|
|
55
|
+
*/
|
|
56
|
+
export declare function wrapEmailPasswordAction<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>>(emailPassword: EmailPasswordAuth, customAction: (credentials: EmailCredentials) => Promise<import('../types').AuthResult<TUser, TSession>>): (credentials: EmailCredentials) => Promise<import('../types').AuthResult<TUser, TSession>>;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a wrapper for custom OTP action that uses OTPAuth.
|
|
59
|
+
*
|
|
60
|
+
* @template TUser - User type
|
|
61
|
+
* @template TSession - Session type
|
|
62
|
+
* @param otp - OTPAuth instance
|
|
63
|
+
* @param customAction - Custom action from config
|
|
64
|
+
* @returns Wrapped action that uses OTPAuth for validation
|
|
65
|
+
*/
|
|
66
|
+
export declare function wrapOTPAction<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>>(otp: OTPAuth, customAction: (email: string, code?: string) => Promise<import('../types').AuthResult<TUser, TSession>>): (email: string, code?: string) => Promise<import('../types').AuthResult<TUser, TSession>>;
|
|
67
|
+
export type { SecurityManager, SecurityConfig } from '../security/security-manager';
|
|
68
|
+
export type { SessionManager, SessionManagerConfig } from '../session/session-manager';
|
|
69
|
+
export type { EmailPasswordAuth, EmailPasswordConfig } from '../auth/email-password';
|
|
70
|
+
export type { OTPAuth, OTPConfig } from '../auth/otp';
|
|
71
|
+
export type { OAuthHandler, OAuthHandlerConfig } from '../auth/oauth/oauth-handler';
|
|
72
|
+
/**
|
|
73
|
+
* TODO: Performance
|
|
74
|
+
* - [ ] Add module initialization caching
|
|
75
|
+
* - [ ] Optimize module dependency resolution
|
|
76
|
+
* - [ ] Add lazy module loading
|
|
77
|
+
* - [ ] Implement module pooling
|
|
78
|
+
*
|
|
79
|
+
* TODO: Features
|
|
80
|
+
* - [ ] Add module health checks
|
|
81
|
+
* - [ ] Implement module reloading
|
|
82
|
+
* - [ ] Create module dependency graph
|
|
83
|
+
* - [ ] Add module metrics collection
|
|
84
|
+
*
|
|
85
|
+
* TODO: Type Safety
|
|
86
|
+
* - [ ] Add type-safe module configuration
|
|
87
|
+
* - [ ] Create compile-time module validation
|
|
88
|
+
* - [ ] Implement type-level dependency checking
|
|
89
|
+
*
|
|
90
|
+
* TODO: Testing
|
|
91
|
+
* - [ ] Add integration tests for module initialization
|
|
92
|
+
* - [ ] Test module dependency resolution
|
|
93
|
+
* - [ ] Test module error handling
|
|
94
|
+
*
|
|
95
|
+
* TODO: Documentation
|
|
96
|
+
* - [ ] Document module integration flow
|
|
97
|
+
* - [ ] Add module configuration guide
|
|
98
|
+
* - [ ] Create module troubleshooting guide
|
|
99
|
+
*
|
|
100
|
+
* TODO: Limitations
|
|
101
|
+
* - [ ] Module initialization is synchronous (consider async initialization)
|
|
102
|
+
* - [ ] No module dependency validation at runtime
|
|
103
|
+
* - [ ] Module configuration is fixed (consider dynamic configuration)
|
|
104
|
+
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AuthResult, OAuthUserInfo, User, Session, OAuthProviderConfig, OAuthProvidersConfig, CallbacksConfig } from '../types';
|
|
2
|
-
import { exchangeOAuthCode } from '../auth/oauth
|
|
2
|
+
import { exchangeOAuthCode } from '../auth/oauth/providers';
|
|
3
3
|
/**
|
|
4
4
|
* OAuth handler configuration.
|
|
5
5
|
*/
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { CSRFTokenStore } from './csrf';
|
|
2
|
+
import { ValidationResult } from './validation';
|
|
3
|
+
import { Logger } from '../logger';
|
|
4
|
+
/**
|
|
5
|
+
* Comprehensive security configuration.
|
|
6
|
+
*/
|
|
7
|
+
export interface SecurityConfig {
|
|
8
|
+
readonly csrfProtection?: {
|
|
9
|
+
readonly enabled: boolean;
|
|
10
|
+
readonly tokenLength?: number;
|
|
11
|
+
readonly expiresIn?: number;
|
|
12
|
+
readonly store?: CSRFTokenStore;
|
|
13
|
+
};
|
|
14
|
+
readonly rateLimiting?: {
|
|
15
|
+
readonly enabled: boolean;
|
|
16
|
+
readonly maxAttempts?: number;
|
|
17
|
+
readonly windowMs?: number;
|
|
18
|
+
readonly strategy?: 'ip' | 'user' | 'email';
|
|
19
|
+
};
|
|
20
|
+
readonly validation?: {
|
|
21
|
+
readonly strictEmail?: boolean;
|
|
22
|
+
readonly minPasswordLength?: number;
|
|
23
|
+
readonly requireStrongPassword?: boolean;
|
|
24
|
+
};
|
|
25
|
+
readonly xssProtection?: {
|
|
26
|
+
readonly enabled: boolean;
|
|
27
|
+
readonly sanitizeHtml?: boolean;
|
|
28
|
+
};
|
|
29
|
+
readonly logger?: Logger;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Comprehensive security manager for authentication operations.
|
|
33
|
+
*
|
|
34
|
+
* Provides unified access to all security utilities with centralized configuration.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const security = new SecurityManager({
|
|
39
|
+
* csrfProtection: { enabled: true },
|
|
40
|
+
* rateLimiting: { enabled: true, maxAttempts: 5 },
|
|
41
|
+
* })
|
|
42
|
+
*
|
|
43
|
+
* // Validate email
|
|
44
|
+
* const emailResult = security.validateEmail('user@example.com')
|
|
45
|
+
*
|
|
46
|
+
* // Check rate limit
|
|
47
|
+
* const rateLimitResult = security.checkRateLimit('user@example.com')
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare class SecurityManager {
|
|
51
|
+
private readonly config;
|
|
52
|
+
private readonly csrfProtection;
|
|
53
|
+
private readonly rateLimiter;
|
|
54
|
+
constructor(config?: SecurityConfig);
|
|
55
|
+
/**
|
|
56
|
+
* Validates and sanitizes an email address.
|
|
57
|
+
*
|
|
58
|
+
* @param email - Email address to validate
|
|
59
|
+
* @returns Validation result with sanitized email if valid
|
|
60
|
+
* @throws ValidationError if validation fails and strict mode is enabled
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const result = security.validateEmail('user@example.com')
|
|
65
|
+
* if (result.valid) {
|
|
66
|
+
* console.log(result.sanitized) // 'user@example.com'
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
validateEmail(email: unknown): ValidationResult<string>;
|
|
71
|
+
/**
|
|
72
|
+
* Validates and sanitizes a password with strength assessment.
|
|
73
|
+
*
|
|
74
|
+
* @param password - Password to validate
|
|
75
|
+
* @returns Validation result with strength indicator if valid
|
|
76
|
+
* @throws ValidationError if validation fails and strict mode is enabled
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const result = security.validatePassword('MyP@ssw0rd!')
|
|
81
|
+
* if (result.valid) {
|
|
82
|
+
* console.log(result.strength) // 'strong'
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
validatePassword(password: unknown): ValidationResult<string>;
|
|
87
|
+
/**
|
|
88
|
+
* Validates and sanitizes a name.
|
|
89
|
+
*
|
|
90
|
+
* @param name - Name to validate
|
|
91
|
+
* @returns Validation result with sanitized name if valid
|
|
92
|
+
*/
|
|
93
|
+
validateName(name: unknown): ValidationResult<string>;
|
|
94
|
+
/**
|
|
95
|
+
* Validates a token format.
|
|
96
|
+
*
|
|
97
|
+
* @param token - Token to validate
|
|
98
|
+
* @returns Validation result
|
|
99
|
+
*/
|
|
100
|
+
validateToken(token: unknown): ValidationResult<string>;
|
|
101
|
+
/**
|
|
102
|
+
* Validates a URL.
|
|
103
|
+
*
|
|
104
|
+
* @param url - URL to validate
|
|
105
|
+
* @returns Validation result
|
|
106
|
+
*/
|
|
107
|
+
validateURL(url: unknown): ValidationResult<string>;
|
|
108
|
+
/**
|
|
109
|
+
* Validates and sanitizes generic input with XSS prevention.
|
|
110
|
+
*
|
|
111
|
+
* @param input - Input to validate and sanitize
|
|
112
|
+
* @param options - Sanitization options
|
|
113
|
+
* @returns Validation result with sanitized input if valid
|
|
114
|
+
*/
|
|
115
|
+
sanitizeInput(input: unknown, options?: {
|
|
116
|
+
maxLength?: number;
|
|
117
|
+
allowHtml?: boolean;
|
|
118
|
+
required?: boolean;
|
|
119
|
+
}): ValidationResult<string>;
|
|
120
|
+
/**
|
|
121
|
+
* Escapes HTML to prevent XSS.
|
|
122
|
+
*
|
|
123
|
+
* @param str - String to escape
|
|
124
|
+
* @returns Escaped string
|
|
125
|
+
*/
|
|
126
|
+
escapeHTML(str: string): string;
|
|
127
|
+
/**
|
|
128
|
+
* Sanitizes user input with XSS prevention.
|
|
129
|
+
*
|
|
130
|
+
* @param input - Input to sanitize
|
|
131
|
+
* @returns Sanitized string
|
|
132
|
+
*/
|
|
133
|
+
sanitizeUserInput(input: unknown): string;
|
|
134
|
+
/**
|
|
135
|
+
* Generates a CSRF token.
|
|
136
|
+
*
|
|
137
|
+
* @param key - Token key (e.g., session ID)
|
|
138
|
+
* @param expiresIn - Expiration time in milliseconds (optional)
|
|
139
|
+
* @returns CSRF token
|
|
140
|
+
* @throws Error if CSRF protection is disabled
|
|
141
|
+
*/
|
|
142
|
+
generateCSRFToken(key: string, expiresIn?: number): string;
|
|
143
|
+
/**
|
|
144
|
+
* Validates a CSRF token.
|
|
145
|
+
*
|
|
146
|
+
* @param key - Token key (e.g., session ID)
|
|
147
|
+
* @param token - Token to validate
|
|
148
|
+
* @returns True if token is valid
|
|
149
|
+
*/
|
|
150
|
+
validateCSRFToken(key: string, token: string): boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Checks rate limit for a given key.
|
|
153
|
+
*
|
|
154
|
+
* @param key - Rate limit key (IP, user ID, email, etc.)
|
|
155
|
+
* @returns Rate limit result
|
|
156
|
+
* @throws RateLimitError if rate limit is exceeded
|
|
157
|
+
*/
|
|
158
|
+
checkRateLimit(key: string): {
|
|
159
|
+
allowed: boolean;
|
|
160
|
+
remaining: number;
|
|
161
|
+
resetAt: Date;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Resets rate limit for a given key.
|
|
165
|
+
*
|
|
166
|
+
* @param key - Rate limit key
|
|
167
|
+
*/
|
|
168
|
+
resetRateLimit(key: string): void;
|
|
169
|
+
/**
|
|
170
|
+
* Gets current security configuration.
|
|
171
|
+
*
|
|
172
|
+
* @returns Security configuration
|
|
173
|
+
*/
|
|
174
|
+
getConfig(): Readonly<SecurityConfig>;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Creates a security manager instance.
|
|
178
|
+
*
|
|
179
|
+
* @param config - Security configuration
|
|
180
|
+
* @returns Security manager instance
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* const security = createSecurityManager({
|
|
185
|
+
* csrfProtection: { enabled: true },
|
|
186
|
+
* rateLimiting: { enabled: true, maxAttempts: 5 },
|
|
187
|
+
* })
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
export declare function createSecurityManager(config?: SecurityConfig): SecurityManager;
|
|
191
|
+
export * from './validation';
|
|
192
|
+
export * from './csrf';
|
|
193
|
+
export * from './rate-limit';
|
|
194
|
+
export * from './xss';
|
|
195
|
+
/**
|
|
196
|
+
* TODO: Performance
|
|
197
|
+
* - [ ] Add validation result caching (with TTL)
|
|
198
|
+
* - [ ] Optimize rate limiting with sliding window algorithm
|
|
199
|
+
* - [ ] Add batch validation support
|
|
200
|
+
* - [ ] Implement async validation for heavy operations
|
|
201
|
+
*
|
|
202
|
+
* TODO: Features
|
|
203
|
+
* - [ ] Add Redis-based rate limiting support
|
|
204
|
+
* - [ ] Implement distributed CSRF token storage
|
|
205
|
+
* - [ ] Add security event logging
|
|
206
|
+
* - [ ] Create security metrics collection
|
|
207
|
+
* - [ ] Add IP whitelist/blacklist support
|
|
208
|
+
* - [ ] Implement CAPTCHA integration
|
|
209
|
+
*
|
|
210
|
+
* TODO: Type Safety
|
|
211
|
+
* - [ ] Add branded types for validated inputs
|
|
212
|
+
* - [ ] Create type-safe security configuration
|
|
213
|
+
* - [ ] Implement compile-time validation rules
|
|
214
|
+
*
|
|
215
|
+
* TODO: Security
|
|
216
|
+
* - [ ] Add security headers management
|
|
217
|
+
* - [ ] Implement content security policy (CSP) helpers
|
|
218
|
+
* - [ ] Add security audit logging
|
|
219
|
+
* - [ ] Create security incident reporting
|
|
220
|
+
*
|
|
221
|
+
* TODO: Testing
|
|
222
|
+
* - [ ] Add comprehensive unit tests
|
|
223
|
+
* - [ ] Test rate limiting edge cases
|
|
224
|
+
* - [ ] Test CSRF token expiration
|
|
225
|
+
* - [ ] Add security fuzzing tests
|
|
226
|
+
*
|
|
227
|
+
* TODO: Documentation
|
|
228
|
+
* - [ ] Document security best practices
|
|
229
|
+
* - [ ] Add security configuration guide
|
|
230
|
+
* - [ ] Create security troubleshooting guide
|
|
231
|
+
*
|
|
232
|
+
* TODO: Limitations
|
|
233
|
+
* - [ ] Rate limiting is in-memory (consider Redis for distributed systems)
|
|
234
|
+
* - [ ] CSRF tokens are in-memory (consider persistent storage)
|
|
235
|
+
* - [ ] HTML sanitization is basic (consider DOMPurify for complex cases)
|
|
236
|
+
*/
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { Session, User, SessionConfig } from '../types';
|
|
2
|
+
import { Logger } from '../logger';
|
|
3
|
+
/**
|
|
4
|
+
* Session storage adapter interface.
|
|
5
|
+
*/
|
|
6
|
+
export interface SessionStorageAdapter {
|
|
7
|
+
/**
|
|
8
|
+
* Gets a session by key.
|
|
9
|
+
*
|
|
10
|
+
* @param key - Session key
|
|
11
|
+
* @returns Session data or null if not found
|
|
12
|
+
*/
|
|
13
|
+
get(key: string): Promise<string | null>;
|
|
14
|
+
/**
|
|
15
|
+
* Sets a session.
|
|
16
|
+
*
|
|
17
|
+
* @param key - Session key
|
|
18
|
+
* @param value - Session data
|
|
19
|
+
* @param expiresIn - Expiration time in milliseconds
|
|
20
|
+
*/
|
|
21
|
+
set(key: string, value: string, expiresIn: number): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Deletes a session.
|
|
24
|
+
*
|
|
25
|
+
* @param key - Session key
|
|
26
|
+
*/
|
|
27
|
+
delete(key: string): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Clears all sessions (optional).
|
|
30
|
+
*/
|
|
31
|
+
clear?(): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Cookie-based session storage adapter.
|
|
35
|
+
*
|
|
36
|
+
* Uses browser/document.cookie for client-side or Next.js cookies() for server-side.
|
|
37
|
+
*/
|
|
38
|
+
export declare class CookieSessionStorage implements SessionStorageAdapter {
|
|
39
|
+
private readonly cookieName;
|
|
40
|
+
private readonly cookieOptions;
|
|
41
|
+
constructor(cookieName: string, options?: {
|
|
42
|
+
readonly httpOnly?: boolean;
|
|
43
|
+
readonly secure?: boolean;
|
|
44
|
+
readonly sameSite?: 'strict' | 'lax' | 'none';
|
|
45
|
+
readonly path?: string;
|
|
46
|
+
readonly domain?: string;
|
|
47
|
+
});
|
|
48
|
+
get(_key: string): Promise<string | null>;
|
|
49
|
+
set(_key: string, value: string, expiresIn: number): Promise<void>;
|
|
50
|
+
delete(_key: string): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* In-memory session storage adapter (for testing or development).
|
|
54
|
+
*/
|
|
55
|
+
export declare class MemorySessionStorage implements SessionStorageAdapter {
|
|
56
|
+
private readonly storage;
|
|
57
|
+
get(key: string): Promise<string | null>;
|
|
58
|
+
set(key: string, value: string, expiresIn: number): Promise<void>;
|
|
59
|
+
delete(key: string): Promise<void>;
|
|
60
|
+
clear(): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Session manager configuration.
|
|
64
|
+
*/
|
|
65
|
+
export interface SessionManagerConfig extends SessionConfig {
|
|
66
|
+
readonly storage?: SessionStorageAdapter;
|
|
67
|
+
readonly logger?: Logger;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Comprehensive session manager for authentication operations.
|
|
71
|
+
*
|
|
72
|
+
* Handles session creation, validation, storage, and expiration with
|
|
73
|
+
* support for multiple storage backends and caching.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const sessionManager = new SessionManager({
|
|
78
|
+
* cookieName: '__mulguard_session',
|
|
79
|
+
* expiresIn: 60 * 60 * 24 * 7, // 7 days
|
|
80
|
+
* httpOnly: true,
|
|
81
|
+
* secure: true,
|
|
82
|
+
* })
|
|
83
|
+
*
|
|
84
|
+
* // Create session
|
|
85
|
+
* const session = await sessionManager.createSession(user)
|
|
86
|
+
*
|
|
87
|
+
* // Get session
|
|
88
|
+
* const currentSession = await sessionManager.getSession()
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare class SessionManager {
|
|
92
|
+
private readonly config;
|
|
93
|
+
private readonly storage;
|
|
94
|
+
private readonly cache;
|
|
95
|
+
private readonly cacheTtl;
|
|
96
|
+
constructor(config: SessionManagerConfig);
|
|
97
|
+
/**
|
|
98
|
+
* Creates a new session for a user.
|
|
99
|
+
*
|
|
100
|
+
* @param user - User object
|
|
101
|
+
* @param additionalData - Additional session data (optional)
|
|
102
|
+
* @returns Created session
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const session = await sessionManager.createSession(user, {
|
|
107
|
+
* accessToken: 'token123',
|
|
108
|
+
* })
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
createSession<TUser extends User = User>(user: TUser, additionalData?: Partial<Omit<Session<TUser>, 'user' | 'expiresAt'>>): Promise<Session<TUser>>;
|
|
112
|
+
/**
|
|
113
|
+
* Gets the current session.
|
|
114
|
+
*
|
|
115
|
+
* Checks cache first, then storage, and validates expiration.
|
|
116
|
+
*
|
|
117
|
+
* @returns Current session or null if not found/expired
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const session = await sessionManager.getSession()
|
|
122
|
+
* if (session) {
|
|
123
|
+
* console.log('User:', session.user.email)
|
|
124
|
+
* }
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
getSession<TUser extends User = User>(): Promise<Session<TUser> | null>;
|
|
128
|
+
/**
|
|
129
|
+
* Saves a session to storage.
|
|
130
|
+
*
|
|
131
|
+
* @param session - Session to save
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* await sessionManager.saveSession(session)
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
saveSession<TUser extends User = User>(session: Session<TUser>): Promise<void>;
|
|
139
|
+
/**
|
|
140
|
+
* Deletes the current session.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* await sessionManager.deleteSession()
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
deleteSession(): Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* Validates a session.
|
|
150
|
+
*
|
|
151
|
+
* @param session - Session to validate
|
|
152
|
+
* @returns True if session is valid
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* if (sessionManager.validateSession(session)) {
|
|
157
|
+
* // Session is valid
|
|
158
|
+
* }
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
validateSession<TUser extends User = User>(session: Session<TUser>): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Clears the session cache.
|
|
164
|
+
*/
|
|
165
|
+
clearCache(): void;
|
|
166
|
+
/**
|
|
167
|
+
* Cleans up expired cache entries.
|
|
168
|
+
*/
|
|
169
|
+
private cleanupCache;
|
|
170
|
+
/**
|
|
171
|
+
* Gets session configuration.
|
|
172
|
+
*
|
|
173
|
+
* @returns Session configuration
|
|
174
|
+
*/
|
|
175
|
+
getConfig(): Readonly<SessionManagerConfig>;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Creates a session manager instance.
|
|
179
|
+
*
|
|
180
|
+
* @param config - Session manager configuration
|
|
181
|
+
* @returns Session manager instance
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* const sessionManager = createSessionManager({
|
|
186
|
+
* cookieName: '__mulguard_session',
|
|
187
|
+
* expiresIn: 60 * 60 * 24 * 7,
|
|
188
|
+
* })
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
export declare function createSessionManager(config: SessionManagerConfig): SessionManager;
|
|
192
|
+
export * from './index';
|
|
193
|
+
/**
|
|
194
|
+
* TODO: Performance
|
|
195
|
+
* - [ ] Add session compression for large sessions
|
|
196
|
+
* - [ ] Implement session chunking for very large sessions
|
|
197
|
+
* - [ ] Add session indexing for faster lookups
|
|
198
|
+
* - [ ] Optimize cache eviction strategy
|
|
199
|
+
*
|
|
200
|
+
* TODO: Features
|
|
201
|
+
* - [ ] Add Redis-based session storage adapter
|
|
202
|
+
* - [ ] Implement database session storage adapter
|
|
203
|
+
* - [ ] Add session refresh mechanism
|
|
204
|
+
* - [ ] Create session migration support
|
|
205
|
+
* - [ ] Add session analytics
|
|
206
|
+
* - [ ] Implement session sharing across subdomains
|
|
207
|
+
*
|
|
208
|
+
* TODO: Type Safety
|
|
209
|
+
* - [ ] Add branded types for session IDs
|
|
210
|
+
* - [ ] Create type-safe session data validation
|
|
211
|
+
* - [ ] Implement compile-time session schema validation
|
|
212
|
+
*
|
|
213
|
+
* TODO: Security
|
|
214
|
+
* - [ ] Add session encryption support
|
|
215
|
+
* - [ ] Implement session signing
|
|
216
|
+
* - [ ] Add session fingerprinting
|
|
217
|
+
* - [ ] Create session audit logging
|
|
218
|
+
* - [ ] Add session hijacking detection
|
|
219
|
+
*
|
|
220
|
+
* TODO: Testing
|
|
221
|
+
* - [ ] Add comprehensive unit tests
|
|
222
|
+
* - [ ] Test session expiration handling
|
|
223
|
+
* - [ ] Test cache invalidation
|
|
224
|
+
* - [ ] Add storage adapter tests
|
|
225
|
+
*
|
|
226
|
+
* TODO: Documentation
|
|
227
|
+
* - [ ] Document session lifecycle
|
|
228
|
+
* - [ ] Add storage adapter guide
|
|
229
|
+
* - [ ] Create session best practices guide
|
|
230
|
+
*
|
|
231
|
+
* TODO: Limitations
|
|
232
|
+
* - [ ] Cookie storage has size limits (~4KB)
|
|
233
|
+
* - [ ] Memory storage is not persistent
|
|
234
|
+
* - [ ] Cache cleanup runs on interval (consider event-based)
|
|
235
|
+
*/
|
|
@@ -182,14 +182,36 @@ export interface SessionConfig {
|
|
|
182
182
|
/**
|
|
183
183
|
* Security configuration options.
|
|
184
184
|
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
185
|
+
* Enhanced with comprehensive security settings from SecurityManager.
|
|
186
|
+
*
|
|
187
|
+
* @property csrfProtection - CSRF protection configuration
|
|
188
|
+
* @property rateLimiting - Rate limiting configuration
|
|
189
|
+
* @property validation - Input validation configuration
|
|
190
|
+
* @property xssProtection - XSS protection configuration
|
|
187
191
|
* @property requireHttps - Require HTTPS (default: true in production)
|
|
188
192
|
* @property allowedOrigins - Allowed origins for CORS
|
|
189
193
|
*/
|
|
190
194
|
export interface SecurityConfig {
|
|
191
|
-
readonly csrfProtection?:
|
|
192
|
-
|
|
195
|
+
readonly csrfProtection?: {
|
|
196
|
+
readonly enabled?: boolean;
|
|
197
|
+
readonly tokenLength?: number;
|
|
198
|
+
readonly expiresIn?: number;
|
|
199
|
+
};
|
|
200
|
+
readonly rateLimiting?: {
|
|
201
|
+
readonly enabled?: boolean;
|
|
202
|
+
readonly maxAttempts?: number;
|
|
203
|
+
readonly windowMs?: number;
|
|
204
|
+
readonly strategy?: 'ip' | 'user' | 'email';
|
|
205
|
+
};
|
|
206
|
+
readonly validation?: {
|
|
207
|
+
readonly strictEmail?: boolean;
|
|
208
|
+
readonly minPasswordLength?: number;
|
|
209
|
+
readonly requireStrongPassword?: boolean;
|
|
210
|
+
};
|
|
211
|
+
readonly xssProtection?: {
|
|
212
|
+
readonly enabled?: boolean;
|
|
213
|
+
readonly sanitizeHtml?: boolean;
|
|
214
|
+
};
|
|
193
215
|
readonly requireHttps?: boolean;
|
|
194
216
|
readonly allowedOrigins?: readonly string[];
|
|
195
217
|
}
|
|
@@ -281,7 +303,7 @@ export interface MulguardConfig<TUser extends User = User, TSession extends Sess
|
|
|
281
303
|
readonly providers?: {
|
|
282
304
|
readonly oauth?: OAuthProvidersConfig;
|
|
283
305
|
};
|
|
284
|
-
readonly oauthStateStore?: import('../auth/oauth
|
|
306
|
+
readonly oauthStateStore?: import('../auth/oauth/state-store').OAuthStateStore;
|
|
285
307
|
readonly sessionCacheTtl?: number;
|
|
286
308
|
}
|
|
287
309
|
export type { User, Session, AuthResult, SuccessfulAuthResult, FailedAuthResult, TwoFactorAuthResult, EmailCredentials, RegisterData, Verify2FAData, };
|