mulguard 1.0.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/README.md +368 -0
- package/dist/actions-CExpv_dD.js +1 -0
- package/dist/actions-DeCfLtHA.mjs +184 -0
- package/dist/client/hooks.d.ts +122 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.js +1 -0
- package/dist/client/index.mjs +476 -0
- package/dist/client/provider.d.ts +25 -0
- package/dist/client/server-actions-helper.d.ts +22 -0
- package/dist/components/AccountPicker.d.ts +11 -0
- package/dist/components/OAuthButton.d.ts +11 -0
- package/dist/components/PassKeyButton.d.ts +11 -0
- package/dist/components/PassKeyRegister.d.ts +10 -0
- package/dist/components/TwoFactorSetup.d.ts +8 -0
- package/dist/components/TwoFactorVerify.d.ts +9 -0
- package/dist/core/account-picker/encryption.d.ts +22 -0
- package/dist/core/account-picker/index.d.ts +22 -0
- package/dist/core/auth/index.d.ts +40 -0
- package/dist/core/auth/oauth-providers.d.ts +69 -0
- package/dist/core/auth/oauth-state-store.d.ts +44 -0
- package/dist/core/auth/oauth.d.ts +20 -0
- package/dist/core/auth/passkey.d.ts +35 -0
- package/dist/core/auth/password.d.ts +22 -0
- package/dist/core/auth/signin-unified.d.ts +33 -0
- package/dist/core/auth/two-factor.d.ts +28 -0
- package/dist/core/client/index.d.ts +132 -0
- package/dist/core/client/token-refresh-manager.d.ts +48 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/security/csrf.d.ts +46 -0
- package/dist/core/security/headers.d.ts +24 -0
- package/dist/core/security/index.d.ts +28 -0
- package/dist/core/security/rate-limit.d.ts +39 -0
- package/dist/core/security/validation.d.ts +53 -0
- package/dist/core/security/xss.d.ts +20 -0
- package/dist/core/session/index.d.ts +35 -0
- package/dist/core/types/auth.d.ts +131 -0
- package/dist/core/types/errors.d.ts +44 -0
- package/dist/core/types/index.d.ts +369 -0
- package/dist/core/utils/auth-helpers.d.ts +136 -0
- package/dist/core/utils/logger.d.ts +17 -0
- package/dist/handlers/api.d.ts +10 -0
- package/dist/handlers/route.d.ts +22 -0
- package/dist/index/index.js +1 -0
- package/dist/index/index.mjs +1633 -0
- package/dist/index.d.ts +21 -0
- package/dist/middleware/index.d.ts +28 -0
- package/dist/middleware/proxy.d.ts +53 -0
- package/dist/middleware/security.d.ts +9 -0
- package/dist/mulguard.d.ts +263 -0
- package/dist/oauth-state-CzIWQq3s.js +1 -0
- package/dist/oauth-state-LE-qeq-K.mjs +282 -0
- package/dist/server/actions.d.ts +86 -0
- package/dist/server/auth.d.ts +65 -0
- package/dist/server/cookies.d.ts +42 -0
- package/dist/server/helpers.d.ts +10 -0
- package/dist/server/index.d.ts +14 -0
- package/dist/server/index.js +1 -0
- package/dist/server/index.mjs +31 -0
- package/dist/server/middleware.d.ts +39 -0
- package/dist/server/oauth-state.d.ts +24 -0
- package/dist/server/session-helpers.d.ts +26 -0
- package/dist/server/session.d.ts +28 -0
- package/dist/server/utils.d.ts +10 -0
- package/dist/signin-unified-BS2gxaG1.mjs +30 -0
- package/dist/signin-unified-Cw41EFkc.js +1 -0
- package/package.json +73 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { EmailCredentials, RegisterData, AuthResult, ApiClient } from '../types';
|
|
2
|
+
import { OAuthMethods } from './oauth';
|
|
3
|
+
import { PassKeyMethods } from './passkey';
|
|
4
|
+
import { TwoFactorMethods } from './two-factor';
|
|
5
|
+
import { AccountPickerMethods } from '../account-picker';
|
|
6
|
+
export interface AuthMethods {
|
|
7
|
+
signIn: {
|
|
8
|
+
email(credentials: EmailCredentials): Promise<AuthResult>;
|
|
9
|
+
oauth(provider: string): Promise<{
|
|
10
|
+
url: string;
|
|
11
|
+
state: string;
|
|
12
|
+
}>;
|
|
13
|
+
passkey(options?: {
|
|
14
|
+
userId?: string;
|
|
15
|
+
}): Promise<AuthResult>;
|
|
16
|
+
};
|
|
17
|
+
signUp(data: RegisterData): Promise<AuthResult>;
|
|
18
|
+
signOut(): Promise<void>;
|
|
19
|
+
resetPassword(email: string): Promise<{
|
|
20
|
+
success: boolean;
|
|
21
|
+
error?: string;
|
|
22
|
+
}>;
|
|
23
|
+
verifyEmail(token: string): Promise<{
|
|
24
|
+
success: boolean;
|
|
25
|
+
error?: string;
|
|
26
|
+
}>;
|
|
27
|
+
oauth: OAuthMethods;
|
|
28
|
+
passkey: PassKeyMethods;
|
|
29
|
+
twoFactor: TwoFactorMethods;
|
|
30
|
+
accountPicker: AccountPickerMethods;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create authentication methods
|
|
34
|
+
*/
|
|
35
|
+
export declare function createAuthMethods(client: ApiClient, oauthProviders?: Record<string, {
|
|
36
|
+
clientId: string;
|
|
37
|
+
redirectUri: string;
|
|
38
|
+
scopes?: string[];
|
|
39
|
+
name?: string;
|
|
40
|
+
}>, accountPickerConfig?: import('../account-picker').AccountPickerConfig): AuthMethods;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in OAuth Provider Configurations
|
|
3
|
+
* Simple auth.js-like configuration for OAuth providers
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* OAuth Provider Configuration (auth.js-like)
|
|
7
|
+
*/
|
|
8
|
+
export interface OAuthProviderConfig {
|
|
9
|
+
/** OAuth client ID */
|
|
10
|
+
clientId: string;
|
|
11
|
+
/** OAuth client secret (server-side only) */
|
|
12
|
+
clientSecret?: string;
|
|
13
|
+
/** Custom redirect URI (optional, auto-generated if not provided) */
|
|
14
|
+
redirectUri?: string;
|
|
15
|
+
/** Custom scopes (optional, defaults provided) */
|
|
16
|
+
scopes?: string[];
|
|
17
|
+
/** Additional parameters */
|
|
18
|
+
params?: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* OAuth Providers Configuration
|
|
22
|
+
* Simple auth.js-like interface
|
|
23
|
+
*/
|
|
24
|
+
export interface OAuthProvidersConfig {
|
|
25
|
+
google?: OAuthProviderConfig;
|
|
26
|
+
github?: OAuthProviderConfig;
|
|
27
|
+
apple?: OAuthProviderConfig;
|
|
28
|
+
facebook?: OAuthProviderConfig;
|
|
29
|
+
[key: string]: OAuthProviderConfig | undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Provider metadata for built-in providers
|
|
33
|
+
*/
|
|
34
|
+
interface ProviderMetadata {
|
|
35
|
+
authorizationUrl: string;
|
|
36
|
+
tokenUrl: string;
|
|
37
|
+
userInfoUrl: string;
|
|
38
|
+
defaultScopes: string[];
|
|
39
|
+
defaultParams?: Record<string, string>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get provider metadata
|
|
43
|
+
*/
|
|
44
|
+
export declare function getProviderMetadata(providerId: string): ProviderMetadata | null;
|
|
45
|
+
/**
|
|
46
|
+
* Build OAuth authorization URL
|
|
47
|
+
*/
|
|
48
|
+
export declare function buildOAuthAuthorizationUrl(providerId: string, config: OAuthProviderConfig, baseUrl: string, state: string): string;
|
|
49
|
+
/**
|
|
50
|
+
* Exchange authorization code for tokens
|
|
51
|
+
*/
|
|
52
|
+
export declare function exchangeOAuthCode(providerId: string, config: OAuthProviderConfig, code: string, redirectUri: string): Promise<{
|
|
53
|
+
access_token: string;
|
|
54
|
+
refresh_token?: string;
|
|
55
|
+
expires_in?: number;
|
|
56
|
+
token_type?: string;
|
|
57
|
+
id_token?: string;
|
|
58
|
+
}>;
|
|
59
|
+
/**
|
|
60
|
+
* Get user info from OAuth provider
|
|
61
|
+
*/
|
|
62
|
+
export declare function getOAuthUserInfo(providerId: string, accessToken: string): Promise<{
|
|
63
|
+
id: string;
|
|
64
|
+
email: string;
|
|
65
|
+
name: string;
|
|
66
|
+
avatar?: string;
|
|
67
|
+
emailVerified?: boolean;
|
|
68
|
+
}>;
|
|
69
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth State Store Interface
|
|
3
|
+
* Allows pluggable state storage for OAuth CSRF protection
|
|
4
|
+
*
|
|
5
|
+
* Default implementation uses in-memory Map (for development)
|
|
6
|
+
* Production should use Redis, database, or other persistent store
|
|
7
|
+
*/
|
|
8
|
+
export interface OAuthState {
|
|
9
|
+
provider: string;
|
|
10
|
+
expiresAt: number;
|
|
11
|
+
}
|
|
12
|
+
export interface OAuthStateStore {
|
|
13
|
+
/**
|
|
14
|
+
* Store OAuth state with TTL
|
|
15
|
+
*/
|
|
16
|
+
set(state: string, data: OAuthState, ttl: number): Promise<void> | void;
|
|
17
|
+
/**
|
|
18
|
+
* Get OAuth state
|
|
19
|
+
*/
|
|
20
|
+
get(state: string): Promise<OAuthState | null> | OAuthState | null;
|
|
21
|
+
/**
|
|
22
|
+
* Delete OAuth state
|
|
23
|
+
*/
|
|
24
|
+
delete(state: string): Promise<void> | void;
|
|
25
|
+
/**
|
|
26
|
+
* Cleanup expired states (optional, for periodic cleanup)
|
|
27
|
+
*/
|
|
28
|
+
cleanup?(): Promise<void> | void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* In-memory OAuth state store (default, for development)
|
|
32
|
+
* ⚠️ Not suitable for production with multiple server instances
|
|
33
|
+
*/
|
|
34
|
+
export declare class MemoryOAuthStateStore implements OAuthStateStore {
|
|
35
|
+
private states;
|
|
36
|
+
set(state: string, data: OAuthState, _ttl: number): void;
|
|
37
|
+
get(state: string): OAuthState | null;
|
|
38
|
+
delete(state: string): void;
|
|
39
|
+
cleanup(): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Create default in-memory OAuth state store
|
|
43
|
+
*/
|
|
44
|
+
export declare function createMemoryOAuthStateStore(): OAuthStateStore;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ApiClient, AuthResult } from '../types';
|
|
2
|
+
export interface OAuthProvider {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
clientId: string;
|
|
6
|
+
redirectUri: string;
|
|
7
|
+
scopes?: string[];
|
|
8
|
+
}
|
|
9
|
+
export interface OAuthMethods {
|
|
10
|
+
initiate(provider: string): Promise<{
|
|
11
|
+
url: string;
|
|
12
|
+
state: string;
|
|
13
|
+
}>;
|
|
14
|
+
handleCallback(provider: string, code: string, state: string): Promise<AuthResult>;
|
|
15
|
+
getProviders(): Promise<OAuthProvider[]>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create OAuth methods
|
|
19
|
+
*/
|
|
20
|
+
export declare function createOAuthMethods(client: ApiClient, providers: Record<string, OAuthProvider>): OAuthMethods;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ApiClient, AuthResult } from '../types';
|
|
2
|
+
export interface PassKeyCredential {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
createdAt: Date;
|
|
6
|
+
lastUsedAt?: Date;
|
|
7
|
+
}
|
|
8
|
+
export interface PassKeyRegisterOptions {
|
|
9
|
+
name?: string;
|
|
10
|
+
userId?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface PassKeyAuthOptions {
|
|
13
|
+
userId?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface PassKeyMethods {
|
|
16
|
+
register(options?: PassKeyRegisterOptions): Promise<{
|
|
17
|
+
success: boolean;
|
|
18
|
+
passkeyId?: string;
|
|
19
|
+
error?: string;
|
|
20
|
+
}>;
|
|
21
|
+
authenticate(options?: PassKeyAuthOptions): Promise<AuthResult>;
|
|
22
|
+
list(): Promise<PassKeyCredential[]>;
|
|
23
|
+
remove(passKeyId: string): Promise<{
|
|
24
|
+
success: boolean;
|
|
25
|
+
error?: string;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Check if WebAuthn is supported
|
|
30
|
+
*/
|
|
31
|
+
export declare function isWebAuthnSupported(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Create PassKey methods
|
|
34
|
+
*/
|
|
35
|
+
export declare function createPassKeyMethods(client: ApiClient): PassKeyMethods;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Password validation and utilities
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Validate password strength
|
|
6
|
+
*/
|
|
7
|
+
export interface PasswordValidationResult {
|
|
8
|
+
valid: boolean;
|
|
9
|
+
errors: string[];
|
|
10
|
+
strength: 'weak' | 'medium' | 'strong';
|
|
11
|
+
}
|
|
12
|
+
export interface PasswordPolicy {
|
|
13
|
+
minLength?: number;
|
|
14
|
+
requireUppercase?: boolean;
|
|
15
|
+
requireLowercase?: boolean;
|
|
16
|
+
requireNumbers?: boolean;
|
|
17
|
+
requireSpecialChars?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Validate password against policy
|
|
21
|
+
*/
|
|
22
|
+
export declare function validatePassword(password: string, policy?: PasswordPolicy): PasswordValidationResult;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { MulguardInstance } from '../../mulguard';
|
|
2
|
+
import { EmailCredentials, AuthResult } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Unified sign in function - professional interface similar to auth.js
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* // OAuth providers
|
|
9
|
+
* await signIn(auth, 'google')
|
|
10
|
+
* await signIn(auth, 'github')
|
|
11
|
+
*
|
|
12
|
+
* // Credentials
|
|
13
|
+
* await signIn(auth, 'credentials', { email: 'user@example.com', password: 'password' })
|
|
14
|
+
*
|
|
15
|
+
* // OTP
|
|
16
|
+
* await signIn(auth, 'otp', { email: 'user@example.com', code: '123456' })
|
|
17
|
+
*
|
|
18
|
+
* // Passkey
|
|
19
|
+
* await signIn(auth, 'passkey', { userId: 'user-id' })
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function signIn(auth: MulguardInstance, provider: 'google' | 'github' | 'apple' | 'facebook' | string): Promise<{
|
|
23
|
+
url: string;
|
|
24
|
+
state: string;
|
|
25
|
+
}>;
|
|
26
|
+
export declare function signIn(auth: MulguardInstance, provider: 'credentials', credentials: EmailCredentials): Promise<AuthResult>;
|
|
27
|
+
export declare function signIn(auth: MulguardInstance, provider: 'otp', options: {
|
|
28
|
+
email: string;
|
|
29
|
+
code?: string;
|
|
30
|
+
}): Promise<AuthResult>;
|
|
31
|
+
export declare function signIn(auth: MulguardInstance, provider: 'passkey', options?: {
|
|
32
|
+
userId?: string;
|
|
33
|
+
}): Promise<AuthResult>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ApiClient } from '../types';
|
|
2
|
+
export interface TwoFactorMethods {
|
|
3
|
+
enable(): Promise<{
|
|
4
|
+
success: boolean;
|
|
5
|
+
qrCode?: string;
|
|
6
|
+
secret?: string;
|
|
7
|
+
error?: string;
|
|
8
|
+
}>;
|
|
9
|
+
verify(code: string): Promise<{
|
|
10
|
+
success: boolean;
|
|
11
|
+
backupCodes?: string[];
|
|
12
|
+
error?: string;
|
|
13
|
+
}>;
|
|
14
|
+
disable(): Promise<{
|
|
15
|
+
success: boolean;
|
|
16
|
+
error?: string;
|
|
17
|
+
}>;
|
|
18
|
+
generateBackupCodes(): Promise<{
|
|
19
|
+
success: boolean;
|
|
20
|
+
backupCodes?: string[];
|
|
21
|
+
error?: string;
|
|
22
|
+
}>;
|
|
23
|
+
isEnabled(): Promise<boolean>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create 2FA methods
|
|
27
|
+
*/
|
|
28
|
+
export declare function createTwoFactorMethods(client: ApiClient): TwoFactorMethods;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Client for making requests to backend API
|
|
3
|
+
* Enhanced with timeout enforcement, retry logic, and interceptors
|
|
4
|
+
*/
|
|
5
|
+
export interface HttpClientConfig {
|
|
6
|
+
baseURL: string;
|
|
7
|
+
apiPrefix: string;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
headers?: Record<string, string>;
|
|
10
|
+
retry?: RetryConfig;
|
|
11
|
+
interceptors?: InterceptorsConfig;
|
|
12
|
+
/** Token refresh manager for automatic token refresh on 401 errors */
|
|
13
|
+
tokenRefreshManager?: import('./token-refresh-manager').TokenRefreshManager;
|
|
14
|
+
}
|
|
15
|
+
export interface RetryConfig {
|
|
16
|
+
/** Maximum number of retry attempts (default: 3) */
|
|
17
|
+
maxAttempts?: number;
|
|
18
|
+
/** Delay between retries in milliseconds (default: 1000) */
|
|
19
|
+
retryDelay?: number;
|
|
20
|
+
/** Exponential backoff multiplier (default: 2) */
|
|
21
|
+
backoffMultiplier?: number;
|
|
22
|
+
/** HTTP status codes that should trigger retry (default: [408, 429, 500, 502, 503, 504]) */
|
|
23
|
+
retryableStatusCodes?: number[];
|
|
24
|
+
/** Whether to retry on network errors (default: true) */
|
|
25
|
+
retryOnNetworkError?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface InterceptorsConfig {
|
|
28
|
+
/** Request interceptor - called before sending request */
|
|
29
|
+
onRequest?: (config: RequestConfig) => Promise<RequestConfig> | RequestConfig;
|
|
30
|
+
/** Response interceptor - called after receiving response */
|
|
31
|
+
onResponse?: <T>(response: HttpResponse<T>) => Promise<HttpResponse<T>> | HttpResponse<T>;
|
|
32
|
+
/** Error interceptor - called on error */
|
|
33
|
+
onError?: (error: ApiError) => Promise<ApiError> | ApiError;
|
|
34
|
+
}
|
|
35
|
+
export interface RequestConfig {
|
|
36
|
+
url: string;
|
|
37
|
+
method: string;
|
|
38
|
+
headers: Record<string, string>;
|
|
39
|
+
body?: unknown;
|
|
40
|
+
credentials?: 'include' | 'omit' | 'same-origin';
|
|
41
|
+
}
|
|
42
|
+
interface InternalRequestOptions {
|
|
43
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
44
|
+
headers?: Record<string, string>;
|
|
45
|
+
body?: unknown;
|
|
46
|
+
credentials?: 'include' | 'omit' | 'same-origin';
|
|
47
|
+
skipRetry?: boolean;
|
|
48
|
+
skipInterceptors?: boolean;
|
|
49
|
+
skipTokenRefresh?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export interface HttpResponse<T = unknown> {
|
|
52
|
+
data: T;
|
|
53
|
+
status: number;
|
|
54
|
+
statusText: string;
|
|
55
|
+
headers: Headers;
|
|
56
|
+
}
|
|
57
|
+
export declare class HttpClient {
|
|
58
|
+
private config;
|
|
59
|
+
private pendingRequests;
|
|
60
|
+
constructor(config: HttpClientConfig);
|
|
61
|
+
/**
|
|
62
|
+
* Make HTTP request with timeout, retry, and interceptors
|
|
63
|
+
*/
|
|
64
|
+
request<T = unknown>(path: string, options?: InternalRequestOptions): Promise<HttpResponse<T>>;
|
|
65
|
+
/**
|
|
66
|
+
* Execute HTTP request with retry logic
|
|
67
|
+
*/
|
|
68
|
+
private executeRequest;
|
|
69
|
+
/**
|
|
70
|
+
* Check if request should be retried
|
|
71
|
+
*/
|
|
72
|
+
private shouldRetry;
|
|
73
|
+
/**
|
|
74
|
+
* Calculate retry delay with exponential backoff
|
|
75
|
+
*/
|
|
76
|
+
private calculateRetryDelay;
|
|
77
|
+
/**
|
|
78
|
+
* Sleep utility for retry delays
|
|
79
|
+
*/
|
|
80
|
+
private sleep;
|
|
81
|
+
/**
|
|
82
|
+
* Build RequestInit from RequestConfig
|
|
83
|
+
*/
|
|
84
|
+
private buildRequestInitFromConfig;
|
|
85
|
+
/**
|
|
86
|
+
* GET request
|
|
87
|
+
*/
|
|
88
|
+
get<T = unknown>(path: string, options?: Omit<InternalRequestOptions, 'method' | 'body'>): Promise<HttpResponse<T>>;
|
|
89
|
+
/**
|
|
90
|
+
* POST request
|
|
91
|
+
*/
|
|
92
|
+
post<T = unknown>(path: string, body?: unknown, options?: Omit<InternalRequestOptions, 'method'>): Promise<HttpResponse<T>>;
|
|
93
|
+
/**
|
|
94
|
+
* PUT request
|
|
95
|
+
*/
|
|
96
|
+
put<T = unknown>(path: string, body?: unknown, options?: Omit<InternalRequestOptions, 'method'>): Promise<HttpResponse<T>>;
|
|
97
|
+
/**
|
|
98
|
+
* DELETE request
|
|
99
|
+
*/
|
|
100
|
+
delete<T = unknown>(path: string, options?: Omit<InternalRequestOptions, 'method' | 'body'>): Promise<HttpResponse<T>>;
|
|
101
|
+
/**
|
|
102
|
+
* Build full URL
|
|
103
|
+
*/
|
|
104
|
+
private buildUrl;
|
|
105
|
+
/**
|
|
106
|
+
* Parse response
|
|
107
|
+
*/
|
|
108
|
+
private parseResponse;
|
|
109
|
+
/**
|
|
110
|
+
* Handle error response
|
|
111
|
+
*/
|
|
112
|
+
private handleErrorResponse;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* API Error class with enhanced error information
|
|
116
|
+
*/
|
|
117
|
+
export declare class ApiError extends Error {
|
|
118
|
+
code: string;
|
|
119
|
+
statusCode: number;
|
|
120
|
+
originalError?: Error;
|
|
121
|
+
retryable?: boolean;
|
|
122
|
+
constructor(message: string, code?: string, statusCode?: number, originalError?: Error, retryable?: boolean);
|
|
123
|
+
/**
|
|
124
|
+
* Check if error is retryable
|
|
125
|
+
*/
|
|
126
|
+
isRetryable(): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Get user-friendly error message
|
|
129
|
+
*/
|
|
130
|
+
getUserMessage(): string;
|
|
131
|
+
}
|
|
132
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Session } from '../types';
|
|
2
|
+
export interface TokenRefreshConfig {
|
|
3
|
+
/** Enable/disable automatic token refresh */
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
/** Time before token expiration to refresh (in seconds) */
|
|
6
|
+
refreshThreshold?: number;
|
|
7
|
+
/** Maximum number of refresh retries on failure */
|
|
8
|
+
maxRetries?: number;
|
|
9
|
+
/** Delay between retries (milliseconds) */
|
|
10
|
+
retryDelay?: number;
|
|
11
|
+
/** Maximum refresh requests per minute */
|
|
12
|
+
rateLimit?: number;
|
|
13
|
+
/** Enable/disable auto sign-out on refresh failure */
|
|
14
|
+
autoSignOutOnFailure?: boolean;
|
|
15
|
+
/** Login page URL for redirect on failure */
|
|
16
|
+
redirectToLogin?: string;
|
|
17
|
+
/** Enable/disable auto redirect on failure */
|
|
18
|
+
autoRedirectOnFailure?: boolean;
|
|
19
|
+
/** Callback on successful token refresh */
|
|
20
|
+
onTokenRefreshed?: (session: Session) => void | Promise<void>;
|
|
21
|
+
/** Callback on token refresh failure (before sign-out) */
|
|
22
|
+
onTokenRefreshFailed?: (error: Error) => void | Promise<void>;
|
|
23
|
+
/** Callback before redirect (can return false to cancel redirect) */
|
|
24
|
+
onBeforeRedirect?: (error: Error) => boolean | Promise<boolean>;
|
|
25
|
+
}
|
|
26
|
+
export interface TokenRefreshManager {
|
|
27
|
+
/** Refresh token with single refresh queue */
|
|
28
|
+
refreshToken(): Promise<Session | null>;
|
|
29
|
+
/** Check if refresh is in progress */
|
|
30
|
+
isRefreshing(): boolean;
|
|
31
|
+
/** Wait for current refresh to complete */
|
|
32
|
+
waitForRefresh(): Promise<Session | null>;
|
|
33
|
+
/** Clear state (on sign-out) */
|
|
34
|
+
clear(): void;
|
|
35
|
+
/** Handle token refresh failure */
|
|
36
|
+
handleRefreshFailure(error: Error): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
type RefreshFunction = () => Promise<Session | null>;
|
|
39
|
+
type SignOutFunction = () => Promise<{
|
|
40
|
+
success: boolean;
|
|
41
|
+
error?: string;
|
|
42
|
+
}>;
|
|
43
|
+
type ClearSessionFunction = () => Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Create Token Refresh Manager
|
|
46
|
+
*/
|
|
47
|
+
export declare function createTokenRefreshManager(refreshFn: RefreshFunction, signOutFn: SignOutFunction, clearSessionFn: ClearSessionFunction, config?: TokenRefreshConfig): TokenRefreshManager;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core authentication library - framework agnostic
|
|
3
|
+
* Part of mulguard package
|
|
4
|
+
*/
|
|
5
|
+
export type { MulguardConfig, SessionConfig, SecurityConfig, CallbacksConfig, AuthActions, Session, User, AuthResult, EmailCredentials, RegisterData, RequestContext, } from './types';
|
|
6
|
+
export * from './security';
|
|
7
|
+
export * from './utils/auth-helpers';
|
|
8
|
+
export * from './auth/signin-unified';
|
|
9
|
+
export * from './auth/oauth-providers';
|
|
10
|
+
export * from './auth/oauth-state-store';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSRF Protection utilities
|
|
3
|
+
*/
|
|
4
|
+
export interface CSRFTokenStore {
|
|
5
|
+
get(key: string): string | null;
|
|
6
|
+
set(key: string, value: string, expiresIn?: number): void;
|
|
7
|
+
delete(key: string): void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* In-memory CSRF token store (for server-side)
|
|
11
|
+
*/
|
|
12
|
+
export declare class MemoryCSRFStore implements CSRFTokenStore {
|
|
13
|
+
private tokens;
|
|
14
|
+
get(key: string): string | null;
|
|
15
|
+
set(key: string, value: string, expiresIn?: number): void;
|
|
16
|
+
delete(key: string): void;
|
|
17
|
+
clear(): void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* CSRF Protection manager
|
|
21
|
+
*/
|
|
22
|
+
export declare class CSRFProtection {
|
|
23
|
+
private store;
|
|
24
|
+
private tokenLength;
|
|
25
|
+
constructor(store?: CSRFTokenStore, tokenLength?: number);
|
|
26
|
+
/**
|
|
27
|
+
* Generate CSRF token
|
|
28
|
+
*/
|
|
29
|
+
generateToken(key: string, expiresIn?: number): string;
|
|
30
|
+
/**
|
|
31
|
+
* Validate CSRF token
|
|
32
|
+
*/
|
|
33
|
+
validateToken(key: string, token: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Get stored token without validating
|
|
36
|
+
*/
|
|
37
|
+
getToken(key: string): string | null;
|
|
38
|
+
/**
|
|
39
|
+
* Delete token
|
|
40
|
+
*/
|
|
41
|
+
deleteToken(key: string): void;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create CSRF protection instance
|
|
45
|
+
*/
|
|
46
|
+
export declare function createCSRFProtection(store?: CSRFTokenStore): CSRFProtection;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security Headers utilities
|
|
3
|
+
*/
|
|
4
|
+
export interface SecurityHeaders {
|
|
5
|
+
'X-Content-Type-Options'?: string;
|
|
6
|
+
'X-Frame-Options'?: string;
|
|
7
|
+
'X-XSS-Protection'?: string;
|
|
8
|
+
'Strict-Transport-Security'?: string;
|
|
9
|
+
'Content-Security-Policy'?: string;
|
|
10
|
+
'Referrer-Policy'?: string;
|
|
11
|
+
'Permissions-Policy'?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Default security headers
|
|
15
|
+
*/
|
|
16
|
+
export declare const DEFAULT_SECURITY_HEADERS: SecurityHeaders;
|
|
17
|
+
/**
|
|
18
|
+
* Get security headers
|
|
19
|
+
*/
|
|
20
|
+
export declare function getSecurityHeaders(custom?: Partial<SecurityHeaders>): SecurityHeaders;
|
|
21
|
+
/**
|
|
22
|
+
* Apply security headers to response
|
|
23
|
+
*/
|
|
24
|
+
export declare function applySecurityHeaders(headers: Headers, custom?: Partial<SecurityHeaders>): void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security utilities
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Generate random token
|
|
6
|
+
*/
|
|
7
|
+
export declare function generateToken(length?: number): string;
|
|
8
|
+
/**
|
|
9
|
+
* Generate CSRF token
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateCSRFToken(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Validate CSRF token (constant-time comparison)
|
|
14
|
+
*/
|
|
15
|
+
export declare function validateCSRFToken(token: string, expected: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Sanitize string input
|
|
18
|
+
*/
|
|
19
|
+
export declare function sanitizeInput(input: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Validate email format
|
|
22
|
+
*/
|
|
23
|
+
export declare function isValidEmail(email: string): boolean;
|
|
24
|
+
export * from './rate-limit';
|
|
25
|
+
export * from './headers';
|
|
26
|
+
export * from './validation';
|
|
27
|
+
export * from './csrf';
|
|
28
|
+
export * from './xss';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiting utilities
|
|
3
|
+
* Client-side rate limiting helpers (actual rate limiting should be on backend)
|
|
4
|
+
*/
|
|
5
|
+
export interface RateLimitConfig {
|
|
6
|
+
maxAttempts: number;
|
|
7
|
+
windowMs: number;
|
|
8
|
+
keyPrefix?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface RateLimitResult {
|
|
11
|
+
allowed: boolean;
|
|
12
|
+
remaining: number;
|
|
13
|
+
resetAt: Date;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Client-side rate limit tracker
|
|
17
|
+
* Note: This is just a helper. Real rate limiting must be enforced on the backend.
|
|
18
|
+
*/
|
|
19
|
+
export declare class RateLimiter {
|
|
20
|
+
private attempts;
|
|
21
|
+
private config;
|
|
22
|
+
constructor(config: RateLimitConfig);
|
|
23
|
+
/**
|
|
24
|
+
* Check if request is allowed
|
|
25
|
+
*/
|
|
26
|
+
check(key: string): RateLimitResult;
|
|
27
|
+
/**
|
|
28
|
+
* Reset rate limit for a key
|
|
29
|
+
*/
|
|
30
|
+
reset(key: string): void;
|
|
31
|
+
/**
|
|
32
|
+
* Clear all rate limits
|
|
33
|
+
*/
|
|
34
|
+
clear(): void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create rate limiter instance
|
|
38
|
+
*/
|
|
39
|
+
export declare function createRateLimiter(config: RateLimitConfig): RateLimiter;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Input validation utilities
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Validate and sanitize email
|
|
6
|
+
*/
|
|
7
|
+
export declare function validateAndSanitizeEmail(email: string): {
|
|
8
|
+
valid: boolean;
|
|
9
|
+
sanitized?: string;
|
|
10
|
+
error?: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Validate and sanitize password with enhanced security checks
|
|
14
|
+
*/
|
|
15
|
+
export declare function validateAndSanitizePassword(password: string, minLength?: number): {
|
|
16
|
+
valid: boolean;
|
|
17
|
+
error?: string;
|
|
18
|
+
strength?: 'weak' | 'medium' | 'strong';
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Validate and sanitize name
|
|
22
|
+
*/
|
|
23
|
+
export declare function validateAndSanitizeName(name: string): {
|
|
24
|
+
valid: boolean;
|
|
25
|
+
sanitized?: string;
|
|
26
|
+
error?: string;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Validate URL
|
|
30
|
+
*/
|
|
31
|
+
export declare function validateURL(url: string): {
|
|
32
|
+
valid: boolean;
|
|
33
|
+
error?: string;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Validate token format with enhanced security checks
|
|
37
|
+
*/
|
|
38
|
+
export declare function validateToken(token: string, minLength?: number): {
|
|
39
|
+
valid: boolean;
|
|
40
|
+
error?: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Validate and sanitize input with XSS prevention
|
|
44
|
+
*/
|
|
45
|
+
export declare function validateAndSanitizeInput(input: string, options?: {
|
|
46
|
+
maxLength?: number;
|
|
47
|
+
allowHtml?: boolean;
|
|
48
|
+
required?: boolean;
|
|
49
|
+
}): {
|
|
50
|
+
valid: boolean;
|
|
51
|
+
sanitized?: string;
|
|
52
|
+
error?: string;
|
|
53
|
+
};
|