mulguard 1.1.2 → 1.1.4
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/core/auth/oauth-providers.d.ts +175 -49
- package/dist/core/auth/oauth-state-store-redis.d.ts +25 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/mulguard/auth-handlers.d.ts +100 -0
- package/dist/core/mulguard/defaults.d.ts +58 -0
- package/dist/core/mulguard/index.d.ts +9 -0
- package/dist/core/mulguard/oauth-handler.d.ts +93 -0
- package/dist/core/mulguard/session-manager.d.ts +94 -0
- package/dist/core/security/index.d.ts +113 -9
- package/dist/core/security/validation.d.ts +231 -33
- package/dist/core/types/auth.d.ts +234 -75
- package/dist/core/types/errors.d.ts +174 -18
- package/dist/core/types/index.d.ts +397 -333
- package/dist/core/utils/logger.d.ts +112 -8
- package/dist/handlers/route.d.ts +59 -5
- package/dist/index/index.js +1 -1
- package/dist/index/index.mjs +1579 -1213
- package/dist/mulguard.d.ts +76 -1
- package/dist/server/helpers.d.ts +3 -3
- package/dist/server/utils.d.ts +3 -3
- package/package.json +1 -1
|
@@ -1,71 +1,197 @@
|
|
|
1
|
+
import { OAuthProviderConfig } from '../types';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Simple auth.js-like configuration for OAuth providers
|
|
3
|
+
* OAuth provider identifier.
|
|
4
4
|
*/
|
|
5
|
+
export type OAuthProviderId = 'google' | 'github' | 'apple' | 'facebook' | string;
|
|
5
6
|
/**
|
|
6
|
-
* OAuth
|
|
7
|
+
* Provider metadata for OAuth endpoints and configuration.
|
|
7
8
|
*/
|
|
8
|
-
export interface
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
redirectUri?: string;
|
|
15
|
-
/** Custom scopes (optional, defaults provided) */
|
|
16
|
-
scopes?: string[];
|
|
17
|
-
/** Additional parameters */
|
|
18
|
-
params?: Record<string, string>;
|
|
9
|
+
export interface ProviderMetadata {
|
|
10
|
+
readonly authorizationUrl: string;
|
|
11
|
+
readonly tokenUrl: string;
|
|
12
|
+
readonly userInfoUrl: string;
|
|
13
|
+
readonly defaultScopes: readonly string[];
|
|
14
|
+
readonly defaultParams?: Readonly<Record<string, string>>;
|
|
19
15
|
}
|
|
20
16
|
/**
|
|
21
|
-
* OAuth
|
|
22
|
-
* Simple auth.js-like interface
|
|
17
|
+
* Normalized OAuth user profile.
|
|
23
18
|
*/
|
|
24
|
-
export interface
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
export interface OAuthUserProfile {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly email: string;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
readonly avatar?: string;
|
|
24
|
+
readonly emailVerified?: boolean;
|
|
25
|
+
readonly rawProfile: Readonly<Record<string, unknown>>;
|
|
30
26
|
}
|
|
31
27
|
/**
|
|
32
|
-
*
|
|
28
|
+
* OAuth token exchange result.
|
|
33
29
|
*/
|
|
34
|
-
interface
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
export interface TokenExchangeResult {
|
|
31
|
+
readonly access_token: string;
|
|
32
|
+
readonly refresh_token?: string;
|
|
33
|
+
readonly expires_in?: number;
|
|
34
|
+
readonly token_type?: string;
|
|
35
|
+
readonly id_token?: string;
|
|
36
|
+
readonly scope?: string;
|
|
40
37
|
}
|
|
41
38
|
/**
|
|
42
|
-
*
|
|
39
|
+
* OAuth error response.
|
|
40
|
+
*/
|
|
41
|
+
export interface OAuthErrorResponse {
|
|
42
|
+
readonly error: string;
|
|
43
|
+
readonly error_description?: string;
|
|
44
|
+
readonly error_uri?: string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Built-in OAuth provider metadata.
|
|
48
|
+
*
|
|
49
|
+
* Contains endpoint URLs, default scopes, and provider-specific parameters.
|
|
50
|
+
*/
|
|
51
|
+
declare const PROVIDER_METADATA: Readonly<Record<string, ProviderMetadata>>;
|
|
52
|
+
/**
|
|
53
|
+
* Gets provider metadata for a given provider ID.
|
|
54
|
+
*
|
|
55
|
+
* @param providerId - OAuth provider identifier
|
|
56
|
+
* @returns Provider metadata or null if not found
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const metadata = getProviderMetadata('google')
|
|
61
|
+
* if (metadata) {
|
|
62
|
+
* console.log(metadata.authorizationUrl)
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
43
65
|
*/
|
|
44
66
|
export declare function getProviderMetadata(providerId: string): ProviderMetadata | null;
|
|
45
67
|
/**
|
|
46
|
-
*
|
|
68
|
+
* Type predicate to check if provider metadata exists.
|
|
69
|
+
*
|
|
70
|
+
* @param providerId - Provider ID to check
|
|
71
|
+
* @returns True if provider is supported
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* if (isSupportedProvider('google')) {
|
|
76
|
+
* // Provider is supported
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function isSupportedProvider(providerId: string): providerId is keyof typeof PROVIDER_METADATA;
|
|
81
|
+
/**
|
|
82
|
+
* Builds OAuth authorization URL with proper parameters.
|
|
83
|
+
*
|
|
84
|
+
* @param providerId - OAuth provider identifier
|
|
85
|
+
* @param config - Provider configuration
|
|
86
|
+
* @param baseUrl - Base URL for redirect URI
|
|
87
|
+
* @param state - CSRF state token
|
|
88
|
+
* @returns Authorization URL
|
|
89
|
+
* @throws {Error} If provider is not supported
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const url = buildOAuthAuthorizationUrl(
|
|
94
|
+
* 'google',
|
|
95
|
+
* { clientId: '...' },
|
|
96
|
+
* 'https://example.com',
|
|
97
|
+
* 'state123'
|
|
98
|
+
* )
|
|
99
|
+
* ```
|
|
47
100
|
*/
|
|
48
101
|
export declare function buildOAuthAuthorizationUrl(providerId: string, config: OAuthProviderConfig, baseUrl: string, state: string): string;
|
|
49
102
|
/**
|
|
50
|
-
*
|
|
103
|
+
* Exchanges authorization code for access tokens.
|
|
104
|
+
*
|
|
105
|
+
* @param providerId - OAuth provider identifier
|
|
106
|
+
* @param config - Provider configuration
|
|
107
|
+
* @param code - Authorization code from OAuth callback
|
|
108
|
+
* @param redirectUri - Redirect URI used in authorization request
|
|
109
|
+
* @returns Token exchange result
|
|
110
|
+
* @throws {Error} If exchange fails or provider is not supported
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const tokens = await exchangeOAuthCode(
|
|
115
|
+
* 'google',
|
|
116
|
+
* { clientId: '...', clientSecret: '...' },
|
|
117
|
+
* 'code123',
|
|
118
|
+
* 'https://example.com/callback'
|
|
119
|
+
* )
|
|
120
|
+
* console.log(tokens.access_token)
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export declare function exchangeOAuthCode(providerId: string, config: OAuthProviderConfig, code: string, redirectUri: string): Promise<TokenExchangeResult>;
|
|
124
|
+
/**
|
|
125
|
+
* Retrieves user information from OAuth provider.
|
|
126
|
+
*
|
|
127
|
+
* @param providerId - OAuth provider identifier
|
|
128
|
+
* @param accessToken - OAuth access token
|
|
129
|
+
* @returns Normalized user profile with raw provider data
|
|
130
|
+
* @throws {Error} If retrieval fails or provider is not supported
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const profile = await getOAuthUserInfo('google', 'access_token_123')
|
|
135
|
+
* console.log(profile.email, profile.name)
|
|
136
|
+
* ```
|
|
51
137
|
*/
|
|
52
|
-
export declare function
|
|
53
|
-
access_token: string;
|
|
54
|
-
refresh_token?: string;
|
|
55
|
-
expires_in?: number;
|
|
56
|
-
token_type?: string;
|
|
57
|
-
id_token?: string;
|
|
58
|
-
}>;
|
|
138
|
+
export declare function getOAuthUserInfo(providerId: string, accessToken: string): Promise<OAuthUserProfile>;
|
|
59
139
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
140
|
+
* Type predicate to check if a value is a valid OAuth provider config.
|
|
141
|
+
*
|
|
142
|
+
* @param value - Value to check
|
|
143
|
+
* @returns True if value is a valid OAuth provider config
|
|
62
144
|
*/
|
|
63
|
-
export declare function
|
|
64
|
-
id: string;
|
|
65
|
-
email: string;
|
|
66
|
-
name: string;
|
|
67
|
-
avatar?: string;
|
|
68
|
-
emailVerified?: boolean;
|
|
69
|
-
rawProfile: Record<string, unknown>;
|
|
70
|
-
}>;
|
|
145
|
+
export declare function isOAuthProviderConfig(value: unknown): value is OAuthProviderConfig;
|
|
71
146
|
export {};
|
|
147
|
+
/**
|
|
148
|
+
* TODO: Performance
|
|
149
|
+
* - [ ] Add token exchange result caching (with TTL)
|
|
150
|
+
* - [ ] Implement request retry logic with exponential backoff
|
|
151
|
+
* - [ ] Add connection pooling for OAuth API calls
|
|
152
|
+
* - [ ] Cache provider metadata lookups
|
|
153
|
+
*
|
|
154
|
+
* TODO: Features
|
|
155
|
+
* - [ ] Add support for PKCE (Proof Key for Code Exchange)
|
|
156
|
+
* - [ ] Implement token refresh flow
|
|
157
|
+
* - [ ] Add support for custom OAuth providers
|
|
158
|
+
* - [ ] Create provider plugin system
|
|
159
|
+
* - [ ] Add OAuth 2.1 compliance
|
|
160
|
+
* - [ ] Support for OpenID Connect
|
|
161
|
+
*
|
|
162
|
+
* TODO: Type Safety
|
|
163
|
+
* - [ ] Add branded types for provider IDs
|
|
164
|
+
* - [ ] Create type-safe provider configuration
|
|
165
|
+
* - [ ] Add compile-time provider validation
|
|
166
|
+
* - [ ] Implement type-level endpoint validation
|
|
167
|
+
*
|
|
168
|
+
* TODO: Security
|
|
169
|
+
* - [ ] Add token validation before use
|
|
170
|
+
* - [ ] Implement token encryption at rest
|
|
171
|
+
* - [ ] Add request signing for OAuth calls
|
|
172
|
+
* - [ ] Create security audit logging
|
|
173
|
+
* - [ ] Add rate limiting for OAuth operations
|
|
174
|
+
*
|
|
175
|
+
* TODO: Error Handling
|
|
176
|
+
* - [ ] Add structured error types for OAuth errors
|
|
177
|
+
* - [ ] Implement error recovery strategies
|
|
178
|
+
* - [ ] Add error retry logic
|
|
179
|
+
* - [ ] Create error reporting
|
|
180
|
+
*
|
|
181
|
+
* TODO: Testing
|
|
182
|
+
* - [ ] Add comprehensive unit tests
|
|
183
|
+
* - [ ] Test all provider normalizations
|
|
184
|
+
* - [ ] Test error handling scenarios
|
|
185
|
+
* - [ ] Add integration tests with mock OAuth servers
|
|
186
|
+
*
|
|
187
|
+
* TODO: Documentation
|
|
188
|
+
* - [ ] Add provider-specific configuration guides
|
|
189
|
+
* - [ ] Document error handling best practices
|
|
190
|
+
* - [ ] Create troubleshooting guide
|
|
191
|
+
*
|
|
192
|
+
* TODO: Limitations
|
|
193
|
+
* - [ ] GitHub email retrieval may fail silently
|
|
194
|
+
* - [ ] Apple profile name only available on first sign-in
|
|
195
|
+
* - [ ] Facebook API version is hardcoded (v18.0)
|
|
196
|
+
* - [ ] No support for OAuth 1.0 providers
|
|
197
|
+
*/
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { OAuthStateStore } from './oauth-state-store';
|
|
2
|
+
/**
|
|
3
|
+
* Redis client interface (compatible with ioredis and node-redis)
|
|
4
|
+
*/
|
|
5
|
+
export interface RedisClient {
|
|
6
|
+
set(key: string, value: string, mode?: string, duration?: number): Promise<string | null>;
|
|
7
|
+
get(key: string): Promise<string | null>;
|
|
8
|
+
del(key: string): Promise<number>;
|
|
9
|
+
keys(pattern: string): Promise<string[]>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Redis-based OAuth state store.
|
|
13
|
+
*
|
|
14
|
+
* @param redis - Redis client instance (ioredis or node-redis)
|
|
15
|
+
* @param keyPrefix - Key prefix for state keys (default: 'mulguard:oauth:state:')
|
|
16
|
+
* @returns OAuth state store implementation
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import Redis from 'ioredis'
|
|
21
|
+
* const redis = new Redis(process.env.REDIS_URL)
|
|
22
|
+
* const stateStore = createRedisOAuthStateStore(redis)
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function createRedisOAuthStateStore(redis: RedisClient, keyPrefix?: string): OAuthStateStore;
|
package/dist/core/index.d.ts
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { AuthResult, EmailCredentials, RegisterData, User, Session, AuthActions, CallbacksConfig } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Authentication handlers configuration.
|
|
4
|
+
*/
|
|
5
|
+
export interface AuthHandlersConfig<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>> {
|
|
6
|
+
readonly actions: AuthActions<TUser, TSession>;
|
|
7
|
+
readonly callbacks: CallbacksConfig;
|
|
8
|
+
readonly saveSessionAfterAuth: (result: AuthResult<TUser, TSession>) => Promise<{
|
|
9
|
+
success: boolean;
|
|
10
|
+
error?: string;
|
|
11
|
+
warning?: string;
|
|
12
|
+
}>;
|
|
13
|
+
readonly onError?: (error: Error, context: string) => Promise<void> | void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Sign-in handlers result.
|
|
17
|
+
*/
|
|
18
|
+
export interface SignInHandlers<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>> {
|
|
19
|
+
readonly email: (credentials: EmailCredentials) => Promise<AuthResult<TUser, TSession>>;
|
|
20
|
+
readonly oauth?: (provider: string) => Promise<{
|
|
21
|
+
url: string;
|
|
22
|
+
state: string;
|
|
23
|
+
}>;
|
|
24
|
+
readonly passkey?: (options?: {
|
|
25
|
+
userId?: string;
|
|
26
|
+
}) => Promise<AuthResult<TUser, TSession>>;
|
|
27
|
+
readonly otp?: (email: string, code?: string) => Promise<AuthResult<TUser, TSession>>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates unified sign-in handler that supports both provider-based and direct method calls.
|
|
31
|
+
*
|
|
32
|
+
* @template TUser - User type
|
|
33
|
+
* @template TSession - Session type
|
|
34
|
+
* @param config - Handler configuration
|
|
35
|
+
* @param storeOAuthState - Function to store OAuth state
|
|
36
|
+
* @returns Unified sign-in handler
|
|
37
|
+
*/
|
|
38
|
+
export declare function createUnifiedSignInHandler<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>>(config: AuthHandlersConfig<TUser, TSession>, storeOAuthState: (state: string, provider: string) => Promise<void>): SignInHandlers<TUser, TSession> & {
|
|
39
|
+
(provider: string): Promise<{
|
|
40
|
+
url: string;
|
|
41
|
+
state: string;
|
|
42
|
+
}>;
|
|
43
|
+
(provider: 'credentials', credentials: EmailCredentials): Promise<AuthResult<TUser, TSession>>;
|
|
44
|
+
(provider: 'otp', options: {
|
|
45
|
+
email: string;
|
|
46
|
+
code?: string;
|
|
47
|
+
}): Promise<AuthResult<TUser, TSession>>;
|
|
48
|
+
(provider: 'passkey', options?: {
|
|
49
|
+
userId?: string;
|
|
50
|
+
}): Promise<AuthResult<TUser, TSession>>;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Creates sign-up handler.
|
|
54
|
+
*
|
|
55
|
+
* @template TUser - User type
|
|
56
|
+
* @template TSession - Session type
|
|
57
|
+
* @param config - Handler configuration
|
|
58
|
+
* @returns Sign-up handler
|
|
59
|
+
*/
|
|
60
|
+
export declare function createSignUpHandler<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>>(config: AuthHandlersConfig<TUser, TSession>): (data: RegisterData) => Promise<AuthResult<TUser, TSession>>;
|
|
61
|
+
/**
|
|
62
|
+
* TODO: Performance
|
|
63
|
+
* - [ ] Add request deduplication for concurrent sign-in attempts
|
|
64
|
+
* - [ ] Implement rate limiting per email/IP
|
|
65
|
+
* - [ ] Add sign-in attempt caching
|
|
66
|
+
* - [ ] Optimize validation logic
|
|
67
|
+
*
|
|
68
|
+
* TODO: Features
|
|
69
|
+
* - [ ] Add social login account linking
|
|
70
|
+
* - [ ] Implement sign-in with magic link
|
|
71
|
+
* - [ ] Add biometric authentication support
|
|
72
|
+
* - [ ] Create sign-in attempt tracking
|
|
73
|
+
*
|
|
74
|
+
* TODO: Type Safety
|
|
75
|
+
* - [ ] Add runtime validation for all inputs
|
|
76
|
+
* - [ ] Create type-safe credential builders
|
|
77
|
+
* - [ ] Implement compile-time provider validation
|
|
78
|
+
*
|
|
79
|
+
* TODO: Security
|
|
80
|
+
* - [ ] Add CAPTCHA support for repeated failures
|
|
81
|
+
* - [ ] Implement account lockout after failed attempts
|
|
82
|
+
* - [ ] Add device fingerprinting
|
|
83
|
+
* - [ ] Create security event logging
|
|
84
|
+
*
|
|
85
|
+
* TODO: Testing
|
|
86
|
+
* - [ ] Add comprehensive handler tests
|
|
87
|
+
* - [ ] Test all validation scenarios
|
|
88
|
+
* - [ ] Test error handling
|
|
89
|
+
* - [ ] Add integration tests
|
|
90
|
+
*
|
|
91
|
+
* TODO: Documentation
|
|
92
|
+
* - [ ] Document all sign-in methods
|
|
93
|
+
* - [ ] Add usage examples
|
|
94
|
+
* - [ ] Create troubleshooting guide
|
|
95
|
+
*
|
|
96
|
+
* TODO: Limitations
|
|
97
|
+
* - [ ] Input validation is basic (consider schema validation)
|
|
98
|
+
* - [ ] No support for custom validation rules
|
|
99
|
+
* - [ ] Error messages may be too generic
|
|
100
|
+
*/
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { SessionConfig } from '../types';
|
|
2
|
+
import { TokenRefreshConfig } from '../client/token-refresh-manager';
|
|
3
|
+
/**
|
|
4
|
+
* Default session configuration.
|
|
5
|
+
*
|
|
6
|
+
* Auto-detects environment and applies secure defaults.
|
|
7
|
+
*
|
|
8
|
+
* @returns Default session configuration
|
|
9
|
+
*/
|
|
10
|
+
export declare function getDefaultSessionConfig(): SessionConfig;
|
|
11
|
+
/**
|
|
12
|
+
* Default token refresh configuration.
|
|
13
|
+
*
|
|
14
|
+
* Optimized to prevent infinite loops and improve performance.
|
|
15
|
+
*
|
|
16
|
+
* @returns Default token refresh configuration
|
|
17
|
+
*/
|
|
18
|
+
export declare function getDefaultTokenRefreshConfig(): TokenRefreshConfig;
|
|
19
|
+
/**
|
|
20
|
+
* Gets the base URL for OAuth redirects.
|
|
21
|
+
*
|
|
22
|
+
* Detects the base URL from environment variables or defaults to localhost.
|
|
23
|
+
*
|
|
24
|
+
* @returns Base URL string
|
|
25
|
+
*/
|
|
26
|
+
export declare function getBaseUrl(): string;
|
|
27
|
+
/**
|
|
28
|
+
* TODO: Performance
|
|
29
|
+
* - [ ] Cache base URL detection result
|
|
30
|
+
* - [ ] Add configuration validation at startup
|
|
31
|
+
* - [ ] Implement configuration hot-reload support
|
|
32
|
+
*
|
|
33
|
+
* TODO: Features
|
|
34
|
+
* - [ ] Add environment-specific configuration presets
|
|
35
|
+
* - [ ] Create configuration builder pattern
|
|
36
|
+
* - [ ] Add configuration schema validation
|
|
37
|
+
* - [ ] Implement configuration inheritance
|
|
38
|
+
*
|
|
39
|
+
* TODO: Type Safety
|
|
40
|
+
* - [ ] Add type-level validation for configuration values
|
|
41
|
+
* - [ ] Create branded types for configuration
|
|
42
|
+
* - [ ] Implement compile-time configuration checking
|
|
43
|
+
*
|
|
44
|
+
* TODO: Testing
|
|
45
|
+
* - [ ] Add tests for default configurations
|
|
46
|
+
* - [ ] Test environment detection
|
|
47
|
+
* - [ ] Test configuration merging
|
|
48
|
+
*
|
|
49
|
+
* TODO: Documentation
|
|
50
|
+
* - [ ] Document all default values
|
|
51
|
+
* - [ ] Add configuration examples
|
|
52
|
+
* - [ ] Create configuration reference guide
|
|
53
|
+
*
|
|
54
|
+
* TODO: Limitations
|
|
55
|
+
* - [ ] Base URL detection is basic (consider more sophisticated detection)
|
|
56
|
+
* - [ ] No configuration validation at runtime
|
|
57
|
+
* - [ ] Defaults are fixed (consider configurable defaults)
|
|
58
|
+
*/
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { AuthResult, OAuthUserInfo, User, Session, OAuthProviderConfig, OAuthProvidersConfig, CallbacksConfig } from '../types';
|
|
2
|
+
import { exchangeOAuthCode } from '../auth/oauth-providers';
|
|
3
|
+
/**
|
|
4
|
+
* OAuth handler configuration.
|
|
5
|
+
*/
|
|
6
|
+
export interface OAuthHandlerConfig<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>> {
|
|
7
|
+
readonly oauthProviders: OAuthProvidersConfig;
|
|
8
|
+
readonly baseUrl: string;
|
|
9
|
+
readonly callbacks: CallbacksConfig;
|
|
10
|
+
readonly createSession: (user: TUser, userInfo: OAuthUserInfo, tokens: Awaited<ReturnType<typeof exchangeOAuthCode>>) => TSession;
|
|
11
|
+
readonly saveSession: (session: TSession) => Promise<void>;
|
|
12
|
+
readonly onError?: (error: Error, context: string) => Promise<void> | void;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* OAuth state validation function.
|
|
16
|
+
*/
|
|
17
|
+
export type ValidateOAuthState = (state: string, provider: string) => Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Creates OAuth callback handler.
|
|
20
|
+
*
|
|
21
|
+
* Automatically handles:
|
|
22
|
+
* 1. Code exchange for tokens
|
|
23
|
+
* 2. User profile retrieval
|
|
24
|
+
* 3. User creation/lookup via callback
|
|
25
|
+
* 4. Session creation and storage
|
|
26
|
+
*
|
|
27
|
+
* @template TUser - User type
|
|
28
|
+
* @template TSession - Session type
|
|
29
|
+
* @param config - OAuth handler configuration
|
|
30
|
+
* @param validateState - Function to validate OAuth state
|
|
31
|
+
* @returns OAuth callback handler
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const handler = createOAuthCallbackHandler(config, validateState)
|
|
36
|
+
* const result = await handler('google', 'code123', 'state456')
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function createOAuthCallbackHandler<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>>(config: OAuthHandlerConfig<TUser, TSession>, _validateState: ValidateOAuthState): (provider: string, code: string, _state: string) => Promise<AuthResult<TUser, TSession>>;
|
|
40
|
+
/**
|
|
41
|
+
* Creates OAuth initiation action if providers are configured.
|
|
42
|
+
*
|
|
43
|
+
* @param oauthProviders - OAuth providers configuration
|
|
44
|
+
* @param baseUrl - Base URL for redirects
|
|
45
|
+
* @param generateState - Function to generate CSRF state
|
|
46
|
+
* @param buildAuthUrl - Function to build authorization URL
|
|
47
|
+
* @returns OAuth initiation action or undefined
|
|
48
|
+
*/
|
|
49
|
+
export declare function createOAuthInitiationAction(oauthProviders: OAuthProvidersConfig, baseUrl: string, generateState: () => string, buildAuthUrl: (provider: string, config: OAuthProviderConfig, baseUrl: string, state: string) => string): ((provider: string) => Promise<{
|
|
50
|
+
url: string;
|
|
51
|
+
state: string;
|
|
52
|
+
}>) | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* TODO: Performance
|
|
55
|
+
* - [ ] Add token exchange result caching
|
|
56
|
+
* - [ ] Implement request retry logic
|
|
57
|
+
* - [ ] Add connection pooling for OAuth API calls
|
|
58
|
+
* - [ ] Cache user profile data
|
|
59
|
+
*
|
|
60
|
+
* TODO: Features
|
|
61
|
+
* - [ ] Add PKCE support
|
|
62
|
+
* - [ ] Implement token refresh flow
|
|
63
|
+
* - [ ] Add OAuth 2.1 compliance
|
|
64
|
+
* - [ ] Support for OpenID Connect
|
|
65
|
+
* - [ ] Add account linking
|
|
66
|
+
*
|
|
67
|
+
* TODO: Type Safety
|
|
68
|
+
* - [ ] Add type-safe provider configuration
|
|
69
|
+
* - [ ] Create type-level endpoint validation
|
|
70
|
+
* - [ ] Implement compile-time provider validation
|
|
71
|
+
*
|
|
72
|
+
* TODO: Security
|
|
73
|
+
* - [ ] Add token validation before use
|
|
74
|
+
* - [ ] Implement token encryption
|
|
75
|
+
* - [ ] Add request signing
|
|
76
|
+
* - [ ] Create security audit logging
|
|
77
|
+
*
|
|
78
|
+
* TODO: Testing
|
|
79
|
+
* - [ ] Add comprehensive OAuth handler tests
|
|
80
|
+
* - [ ] Test all provider flows
|
|
81
|
+
* - [ ] Test error handling
|
|
82
|
+
* - [ ] Add integration tests
|
|
83
|
+
*
|
|
84
|
+
* TODO: Documentation
|
|
85
|
+
* - [ ] Document OAuth flow
|
|
86
|
+
* - [ ] Add provider setup guides
|
|
87
|
+
* - [ ] Create troubleshooting guide
|
|
88
|
+
*
|
|
89
|
+
* TODO: Limitations
|
|
90
|
+
* - [ ] No support for OAuth 1.0
|
|
91
|
+
* - [ ] Token refresh not implemented
|
|
92
|
+
* - [ ] No PKCE support yet
|
|
93
|
+
*/
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Session, SessionConfig } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Session cache entry.
|
|
4
|
+
*/
|
|
5
|
+
interface SessionCacheEntry {
|
|
6
|
+
readonly session: Session | null;
|
|
7
|
+
readonly timestamp: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Session manager configuration.
|
|
11
|
+
*/
|
|
12
|
+
export interface SessionManagerConfig {
|
|
13
|
+
readonly sessionConfig: SessionConfig;
|
|
14
|
+
readonly cacheTtl: number;
|
|
15
|
+
readonly getSessionAction?: () => Promise<Session | null>;
|
|
16
|
+
readonly onSessionExpired?: (session: Session) => Promise<void> | void;
|
|
17
|
+
readonly onError?: (error: Error, context: string) => Promise<void> | void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Session operation result.
|
|
21
|
+
*/
|
|
22
|
+
export interface SessionResult {
|
|
23
|
+
readonly success: boolean;
|
|
24
|
+
readonly error?: string;
|
|
25
|
+
readonly warning?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Creates a session manager instance.
|
|
29
|
+
*
|
|
30
|
+
* @param config - Session manager configuration
|
|
31
|
+
* @returns Session manager functions
|
|
32
|
+
*/
|
|
33
|
+
export declare function createSessionManager(config: SessionManagerConfig): {
|
|
34
|
+
getSession: () => Promise<Session | null>;
|
|
35
|
+
setSession: (session: Session) => Promise<SessionResult>;
|
|
36
|
+
clearSessionCookie: () => Promise<void>;
|
|
37
|
+
getAccessToken: () => Promise<string | null>;
|
|
38
|
+
getRefreshToken: () => Promise<string | null>;
|
|
39
|
+
hasValidTokens: () => Promise<boolean>;
|
|
40
|
+
clearCache: () => void;
|
|
41
|
+
getSessionConfig: () => {
|
|
42
|
+
cookieName: string;
|
|
43
|
+
config: SessionConfig;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Type predicate to check if a value is a valid session cache entry.
|
|
48
|
+
*
|
|
49
|
+
* @param value - Value to check
|
|
50
|
+
* @returns True if value is a valid cache entry
|
|
51
|
+
*/
|
|
52
|
+
export declare function isSessionCacheEntry(value: unknown): value is SessionCacheEntry;
|
|
53
|
+
export {};
|
|
54
|
+
/**
|
|
55
|
+
* TODO: Performance
|
|
56
|
+
* - [ ] Add session compression for large sessions
|
|
57
|
+
* - [ ] Implement session chunking for very large sessions
|
|
58
|
+
* - [ ] Add session cache invalidation strategies
|
|
59
|
+
* - [ ] Consider using WeakMap for session references
|
|
60
|
+
*
|
|
61
|
+
* TODO: Features
|
|
62
|
+
* - [ ] Add session encryption at rest
|
|
63
|
+
* - [ ] Implement session rotation
|
|
64
|
+
* - [ ] Add session fingerprinting
|
|
65
|
+
* - [ ] Create session analytics
|
|
66
|
+
* - [ ] Add session migration support
|
|
67
|
+
*
|
|
68
|
+
* TODO: Type Safety
|
|
69
|
+
* - [ ] Add type-level session validation
|
|
70
|
+
* - [ ] Create type-safe session builders
|
|
71
|
+
* - [ ] Implement session schema validation
|
|
72
|
+
*
|
|
73
|
+
* TODO: Security
|
|
74
|
+
* - [ ] Add session hijacking detection
|
|
75
|
+
* - [ ] Implement session timeout warnings
|
|
76
|
+
* - [ ] Add session audit logging
|
|
77
|
+
* - [ ] Create session security monitoring
|
|
78
|
+
*
|
|
79
|
+
* TODO: Testing
|
|
80
|
+
* - [ ] Add comprehensive session manager tests
|
|
81
|
+
* - [ ] Test cache invalidation
|
|
82
|
+
* - [ ] Test session expiration
|
|
83
|
+
* - [ ] Test error handling
|
|
84
|
+
*
|
|
85
|
+
* TODO: Documentation
|
|
86
|
+
* - [ ] Document session lifecycle
|
|
87
|
+
* - [ ] Add session best practices guide
|
|
88
|
+
* - [ ] Create troubleshooting guide
|
|
89
|
+
*
|
|
90
|
+
* TODO: Limitations
|
|
91
|
+
* - [ ] Session cache is in-memory (not shared across instances)
|
|
92
|
+
* - [ ] No session persistence (consider database-backed sessions)
|
|
93
|
+
* - [ ] Session validation is basic (consider schema validation)
|
|
94
|
+
*/
|