@leanmcp/auth 0.4.2 → 0.4.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.
@@ -0,0 +1,181 @@
1
+ import { T as TokenStorage, O as OAuthTokens, C as ClientRegistration, S as StoredSession } from '../types-DMpGN530.js';
2
+ export { i as isTokenExpired, w as withExpiresAt } from '../types-DMpGN530.js';
3
+
4
+ /**
5
+ * In-memory token storage
6
+ *
7
+ * Fast, simple storage for development and short-lived sessions.
8
+ * Tokens are lost when the process exits.
9
+ */
10
+
11
+ /**
12
+ * In-memory token storage implementation
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const storage = new MemoryStorage();
17
+ * await storage.setTokens('https://mcp.example.com', tokens);
18
+ * ```
19
+ */
20
+ declare class MemoryStorage implements TokenStorage {
21
+ private tokens;
22
+ private clients;
23
+ /**
24
+ * Normalize server URL for consistent key lookup
25
+ */
26
+ private normalizeUrl;
27
+ /**
28
+ * Check if an entry is expired
29
+ */
30
+ private isExpired;
31
+ getTokens(serverUrl: string): Promise<OAuthTokens | null>;
32
+ setTokens(serverUrl: string, tokens: OAuthTokens): Promise<void>;
33
+ clearTokens(serverUrl: string): Promise<void>;
34
+ getClientInfo(serverUrl: string): Promise<ClientRegistration | null>;
35
+ setClientInfo(serverUrl: string, info: ClientRegistration): Promise<void>;
36
+ clearClientInfo(serverUrl: string): Promise<void>;
37
+ clearAll(): Promise<void>;
38
+ getAllSessions(): Promise<StoredSession[]>;
39
+ }
40
+
41
+ /**
42
+ * File-based token storage
43
+ *
44
+ * Persists tokens to a JSON file for survival across restarts.
45
+ * Optionally encrypts tokens for security.
46
+ */
47
+
48
+ interface FileStorageOptions {
49
+ /** Path to the storage file */
50
+ filePath: string;
51
+ /** Optional encryption key (if omitted, data stored in plaintext) */
52
+ encryptionKey?: string;
53
+ /** Whether to pretty-print JSON (default: false) */
54
+ prettyPrint?: boolean;
55
+ }
56
+ /**
57
+ * File-based token storage with optional encryption
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * // Plaintext storage
62
+ * const storage = new FileStorage({ filePath: '~/.leanmcp/tokens.json' });
63
+ *
64
+ * // Encrypted storage
65
+ * const storage = new FileStorage({
66
+ * filePath: '~/.leanmcp/tokens.enc',
67
+ * encryptionKey: process.env.TOKEN_ENCRYPTION_KEY
68
+ * });
69
+ * ```
70
+ */
71
+ declare class FileStorage implements TokenStorage {
72
+ private filePath;
73
+ private encryptionKey?;
74
+ private prettyPrint;
75
+ private cache;
76
+ private writePromise;
77
+ constructor(options: FileStorageOptions | string);
78
+ /**
79
+ * Expand ~ to home directory
80
+ */
81
+ private expandPath;
82
+ /**
83
+ * Normalize server URL for consistent key lookup
84
+ */
85
+ private normalizeUrl;
86
+ /**
87
+ * Encrypt data
88
+ */
89
+ private encrypt;
90
+ /**
91
+ * Decrypt data
92
+ */
93
+ private decrypt;
94
+ /**
95
+ * Read data from file
96
+ */
97
+ private readFile;
98
+ /**
99
+ * Write data to file (coalesced to avoid race conditions)
100
+ */
101
+ private writeFile;
102
+ getTokens(serverUrl: string): Promise<OAuthTokens | null>;
103
+ setTokens(serverUrl: string, tokens: OAuthTokens): Promise<void>;
104
+ clearTokens(serverUrl: string): Promise<void>;
105
+ getClientInfo(serverUrl: string): Promise<ClientRegistration | null>;
106
+ setClientInfo(serverUrl: string, info: ClientRegistration): Promise<void>;
107
+ clearClientInfo(serverUrl: string): Promise<void>;
108
+ clearAll(): Promise<void>;
109
+ getAllSessions(): Promise<StoredSession[]>;
110
+ }
111
+
112
+ /**
113
+ * OS Keychain Token Storage
114
+ *
115
+ * Secure storage using the operating system's credential manager:
116
+ * - macOS: Keychain
117
+ * - Windows: Credential Vault
118
+ * - Linux: libsecret (GNOME Keyring, KWallet, etc.)
119
+ *
120
+ * Requires the optional 'keytar' peer dependency.
121
+ */
122
+
123
+ /**
124
+ * Keychain storage options
125
+ */
126
+ interface KeychainStorageOptions {
127
+ /** Custom service name (default: 'leanmcp-auth') */
128
+ serviceName?: string;
129
+ }
130
+ /**
131
+ * OS Keychain-based token storage
132
+ *
133
+ * Uses the operating system's secure credential storage for maximum security.
134
+ * Tokens are encrypted at rest by the OS.
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * import { KeychainStorage } from '@leanmcp/auth/storage';
139
+ *
140
+ * // Requires 'keytar' to be installed
141
+ * const storage = new KeychainStorage();
142
+ *
143
+ * await storage.setTokens('https://mcp.example.com', tokens);
144
+ * ```
145
+ */
146
+ declare class KeychainStorage implements TokenStorage {
147
+ private serviceName;
148
+ private keytar;
149
+ private initPromise;
150
+ constructor(options?: KeychainStorageOptions);
151
+ /**
152
+ * Initialize keytar (lazy load)
153
+ */
154
+ private init;
155
+ /**
156
+ * Normalize server URL for consistent key lookup
157
+ */
158
+ private normalizeUrl;
159
+ /**
160
+ * Get account key for tokens
161
+ */
162
+ private getTokensAccount;
163
+ /**
164
+ * Get account key for client info
165
+ */
166
+ private getClientAccount;
167
+ getTokens(serverUrl: string): Promise<OAuthTokens | null>;
168
+ setTokens(serverUrl: string, tokens: OAuthTokens): Promise<void>;
169
+ clearTokens(serverUrl: string): Promise<void>;
170
+ getClientInfo(serverUrl: string): Promise<ClientRegistration | null>;
171
+ setClientInfo(serverUrl: string, info: ClientRegistration): Promise<void>;
172
+ clearClientInfo(serverUrl: string): Promise<void>;
173
+ clearAll(): Promise<void>;
174
+ getAllSessions(): Promise<StoredSession[]>;
175
+ }
176
+ /**
177
+ * Check if keychain storage is available
178
+ */
179
+ declare function isKeychainAvailable(): Promise<boolean>;
180
+
181
+ export { ClientRegistration, FileStorage, KeychainStorage, type KeychainStorageOptions, MemoryStorage, OAuthTokens, StoredSession, TokenStorage, isKeychainAvailable };
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Token Storage Types
3
+ *
4
+ * Defines interfaces for storing OAuth tokens across different backends
5
+ * (memory, file, keychain, browser localStorage, etc.)
6
+ */
7
+ /**
8
+ * OAuth 2.0/2.1 token response
9
+ */
10
+ interface OAuthTokens {
11
+ /** The access token issued by the authorization server */
12
+ access_token: string;
13
+ /** Token type (usually "Bearer") */
14
+ token_type: string;
15
+ /** Lifetime in seconds of the access token */
16
+ expires_in?: number;
17
+ /** Refresh token for obtaining new access tokens */
18
+ refresh_token?: string;
19
+ /** ID token (OpenID Connect) */
20
+ id_token?: string;
21
+ /** Scope granted by the authorization server */
22
+ scope?: string;
23
+ /** Computed: Unix timestamp when token expires */
24
+ expires_at?: number;
25
+ }
26
+ /**
27
+ * OAuth client registration information
28
+ * Used for Dynamic Client Registration (RFC 7591)
29
+ */
30
+ interface ClientRegistration {
31
+ /** OAuth client identifier */
32
+ client_id: string;
33
+ /** OAuth client secret (for confidential clients) */
34
+ client_secret?: string;
35
+ /** Unix timestamp when client secret expires */
36
+ client_secret_expires_at?: number;
37
+ /** Token for accessing registration endpoint */
38
+ registration_access_token?: string;
39
+ /** Client metadata from registration */
40
+ metadata?: Record<string, unknown>;
41
+ }
42
+ /**
43
+ * Stored session combining tokens and client info
44
+ */
45
+ interface StoredSession {
46
+ /** Server URL this session is for */
47
+ serverUrl: string;
48
+ /** OAuth tokens */
49
+ tokens: OAuthTokens;
50
+ /** Client registration info (if dynamic registration used) */
51
+ clientInfo?: ClientRegistration;
52
+ /** Unix timestamp when session was created */
53
+ createdAt: number;
54
+ /** Unix timestamp when session was last updated */
55
+ updatedAt: number;
56
+ }
57
+ /**
58
+ * Token storage interface
59
+ *
60
+ * Implement this interface to create custom storage backends.
61
+ * All operations should be async to support various backends.
62
+ */
63
+ interface TokenStorage {
64
+ /**
65
+ * Get stored tokens for a server
66
+ * @param serverUrl - The MCP server URL
67
+ * @returns Tokens if found, null otherwise
68
+ */
69
+ getTokens(serverUrl: string): Promise<OAuthTokens | null>;
70
+ /**
71
+ * Store tokens for a server
72
+ * @param serverUrl - The MCP server URL
73
+ * @param tokens - OAuth tokens to store
74
+ */
75
+ setTokens(serverUrl: string, tokens: OAuthTokens): Promise<void>;
76
+ /**
77
+ * Clear tokens for a server
78
+ * @param serverUrl - The MCP server URL
79
+ */
80
+ clearTokens(serverUrl: string): Promise<void>;
81
+ /**
82
+ * Get stored client registration for a server
83
+ * @param serverUrl - The MCP server URL
84
+ * @returns Client info if found, null otherwise
85
+ */
86
+ getClientInfo(serverUrl: string): Promise<ClientRegistration | null>;
87
+ /**
88
+ * Store client registration for a server
89
+ * @param serverUrl - The MCP server URL
90
+ * @param info - Client registration info
91
+ */
92
+ setClientInfo(serverUrl: string, info: ClientRegistration): Promise<void>;
93
+ /**
94
+ * Clear client registration for a server
95
+ * @param serverUrl - The MCP server URL
96
+ */
97
+ clearClientInfo(serverUrl: string): Promise<void>;
98
+ /**
99
+ * Clear all stored data
100
+ */
101
+ clearAll(): Promise<void>;
102
+ /**
103
+ * Get all stored sessions (optional)
104
+ * @returns Array of stored sessions
105
+ */
106
+ getAllSessions?(): Promise<StoredSession[]>;
107
+ }
108
+ /**
109
+ * Check if tokens are expired or about to expire
110
+ * @param tokens - OAuth tokens to check
111
+ * @param bufferSeconds - Seconds before expiry to consider expired (default: 60)
112
+ * @returns True if tokens are expired or will expire within buffer
113
+ */
114
+ declare function isTokenExpired(tokens: OAuthTokens, bufferSeconds?: number): boolean;
115
+ /**
116
+ * Compute expires_at from expires_in if not present
117
+ * @param tokens - OAuth tokens to enhance
118
+ * @returns Tokens with expires_at computed
119
+ */
120
+ declare function withExpiresAt(tokens: OAuthTokens): OAuthTokens;
121
+
122
+ export { type ClientRegistration as C, type OAuthTokens as O, type StoredSession as S, type TokenStorage as T, isTokenExpired as i, withExpiresAt as w };
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Token Storage Types
3
+ *
4
+ * Defines interfaces for storing OAuth tokens across different backends
5
+ * (memory, file, keychain, browser localStorage, etc.)
6
+ */
7
+ /**
8
+ * OAuth 2.0/2.1 token response
9
+ */
10
+ interface OAuthTokens {
11
+ /** The access token issued by the authorization server */
12
+ access_token: string;
13
+ /** Token type (usually "Bearer") */
14
+ token_type: string;
15
+ /** Lifetime in seconds of the access token */
16
+ expires_in?: number;
17
+ /** Refresh token for obtaining new access tokens */
18
+ refresh_token?: string;
19
+ /** ID token (OpenID Connect) */
20
+ id_token?: string;
21
+ /** Scope granted by the authorization server */
22
+ scope?: string;
23
+ /** Computed: Unix timestamp when token expires */
24
+ expires_at?: number;
25
+ }
26
+ /**
27
+ * OAuth client registration information
28
+ * Used for Dynamic Client Registration (RFC 7591)
29
+ */
30
+ interface ClientRegistration {
31
+ /** OAuth client identifier */
32
+ client_id: string;
33
+ /** OAuth client secret (for confidential clients) */
34
+ client_secret?: string;
35
+ /** Unix timestamp when client secret expires */
36
+ client_secret_expires_at?: number;
37
+ /** Token for accessing registration endpoint */
38
+ registration_access_token?: string;
39
+ /** Client metadata from registration */
40
+ metadata?: Record<string, unknown>;
41
+ }
42
+ /**
43
+ * Stored session combining tokens and client info
44
+ */
45
+ interface StoredSession {
46
+ /** Server URL this session is for */
47
+ serverUrl: string;
48
+ /** OAuth tokens */
49
+ tokens: OAuthTokens;
50
+ /** Client registration info (if dynamic registration used) */
51
+ clientInfo?: ClientRegistration;
52
+ /** Unix timestamp when session was created */
53
+ createdAt: number;
54
+ /** Unix timestamp when session was last updated */
55
+ updatedAt: number;
56
+ }
57
+ /**
58
+ * Token storage interface
59
+ *
60
+ * Implement this interface to create custom storage backends.
61
+ * All operations should be async to support various backends.
62
+ */
63
+ interface TokenStorage {
64
+ /**
65
+ * Get stored tokens for a server
66
+ * @param serverUrl - The MCP server URL
67
+ * @returns Tokens if found, null otherwise
68
+ */
69
+ getTokens(serverUrl: string): Promise<OAuthTokens | null>;
70
+ /**
71
+ * Store tokens for a server
72
+ * @param serverUrl - The MCP server URL
73
+ * @param tokens - OAuth tokens to store
74
+ */
75
+ setTokens(serverUrl: string, tokens: OAuthTokens): Promise<void>;
76
+ /**
77
+ * Clear tokens for a server
78
+ * @param serverUrl - The MCP server URL
79
+ */
80
+ clearTokens(serverUrl: string): Promise<void>;
81
+ /**
82
+ * Get stored client registration for a server
83
+ * @param serverUrl - The MCP server URL
84
+ * @returns Client info if found, null otherwise
85
+ */
86
+ getClientInfo(serverUrl: string): Promise<ClientRegistration | null>;
87
+ /**
88
+ * Store client registration for a server
89
+ * @param serverUrl - The MCP server URL
90
+ * @param info - Client registration info
91
+ */
92
+ setClientInfo(serverUrl: string, info: ClientRegistration): Promise<void>;
93
+ /**
94
+ * Clear client registration for a server
95
+ * @param serverUrl - The MCP server URL
96
+ */
97
+ clearClientInfo(serverUrl: string): Promise<void>;
98
+ /**
99
+ * Clear all stored data
100
+ */
101
+ clearAll(): Promise<void>;
102
+ /**
103
+ * Get all stored sessions (optional)
104
+ * @returns Array of stored sessions
105
+ */
106
+ getAllSessions?(): Promise<StoredSession[]>;
107
+ }
108
+ /**
109
+ * Check if tokens are expired or about to expire
110
+ * @param tokens - OAuth tokens to check
111
+ * @param bufferSeconds - Seconds before expiry to consider expired (default: 60)
112
+ * @returns True if tokens are expired or will expire within buffer
113
+ */
114
+ declare function isTokenExpired(tokens: OAuthTokens, bufferSeconds?: number): boolean;
115
+ /**
116
+ * Compute expires_at from expires_in if not present
117
+ * @param tokens - OAuth tokens to enhance
118
+ * @returns Tokens with expires_at computed
119
+ */
120
+ declare function withExpiresAt(tokens: OAuthTokens): OAuthTokens;
121
+
122
+ export { type ClientRegistration as C, type OAuthTokens as O, type StoredSession as S, type TokenStorage as T, isTokenExpired as i, withExpiresAt as w };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leanmcp/auth",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Authentication and identity module with OAuth 2.1 client, token storage, and multiple providers",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -48,6 +48,7 @@
48
48
  "reflect-metadata": "^0.2.1"
49
49
  },
50
50
  "devDependencies": {
51
+ "@leanmcp/env-injection": "^0.1.0",
51
52
  "@types/jest": "^29.5.0",
52
53
  "@types/jsonwebtoken": "^9.0.10",
53
54
  "@types/jwk-to-pem": "^2.0.3",