@mcp-z/oauth-google 1.0.0
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/LICENSE +21 -0
- package/README.md +93 -0
- package/dist/cjs/index.d.cts +16 -0
- package/dist/cjs/index.d.ts +16 -0
- package/dist/cjs/index.js +112 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/lib/dcr-router.d.cts +44 -0
- package/dist/cjs/lib/dcr-router.d.ts +44 -0
- package/dist/cjs/lib/dcr-router.js +1189 -0
- package/dist/cjs/lib/dcr-router.js.map +1 -0
- package/dist/cjs/lib/dcr-utils.d.cts +160 -0
- package/dist/cjs/lib/dcr-utils.d.ts +160 -0
- package/dist/cjs/lib/dcr-utils.js +860 -0
- package/dist/cjs/lib/dcr-utils.js.map +1 -0
- package/dist/cjs/lib/dcr-verify.d.cts +53 -0
- package/dist/cjs/lib/dcr-verify.d.ts +53 -0
- package/dist/cjs/lib/dcr-verify.js +193 -0
- package/dist/cjs/lib/dcr-verify.js.map +1 -0
- package/dist/cjs/lib/fetch-with-timeout.d.cts +14 -0
- package/dist/cjs/lib/fetch-with-timeout.d.ts +14 -0
- package/dist/cjs/lib/fetch-with-timeout.js +257 -0
- package/dist/cjs/lib/fetch-with-timeout.js.map +1 -0
- package/dist/cjs/lib/token-verifier.d.cts +44 -0
- package/dist/cjs/lib/token-verifier.d.ts +44 -0
- package/dist/cjs/lib/token-verifier.js +253 -0
- package/dist/cjs/lib/token-verifier.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/providers/dcr.d.cts +107 -0
- package/dist/cjs/providers/dcr.d.ts +107 -0
- package/dist/cjs/providers/dcr.js +584 -0
- package/dist/cjs/providers/dcr.js.map +1 -0
- package/dist/cjs/providers/loopback-oauth.d.cts +119 -0
- package/dist/cjs/providers/loopback-oauth.d.ts +119 -0
- package/dist/cjs/providers/loopback-oauth.js +1334 -0
- package/dist/cjs/providers/loopback-oauth.js.map +1 -0
- package/dist/cjs/providers/service-account.d.cts +131 -0
- package/dist/cjs/providers/service-account.d.ts +131 -0
- package/dist/cjs/providers/service-account.js +800 -0
- package/dist/cjs/providers/service-account.js.map +1 -0
- package/dist/cjs/schemas/index.d.cts +20 -0
- package/dist/cjs/schemas/index.d.ts +20 -0
- package/dist/cjs/schemas/index.js +37 -0
- package/dist/cjs/schemas/index.js.map +1 -0
- package/dist/cjs/setup/config.d.cts +112 -0
- package/dist/cjs/setup/config.d.ts +112 -0
- package/dist/cjs/setup/config.js +236 -0
- package/dist/cjs/setup/config.js.map +1 -0
- package/dist/cjs/types.d.cts +173 -0
- package/dist/cjs/types.d.ts +173 -0
- package/dist/cjs/types.js +16 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lib/dcr-router.d.ts +44 -0
- package/dist/esm/lib/dcr-router.js +515 -0
- package/dist/esm/lib/dcr-router.js.map +1 -0
- package/dist/esm/lib/dcr-utils.d.ts +160 -0
- package/dist/esm/lib/dcr-utils.js +270 -0
- package/dist/esm/lib/dcr-utils.js.map +1 -0
- package/dist/esm/lib/dcr-verify.d.ts +53 -0
- package/dist/esm/lib/dcr-verify.js +53 -0
- package/dist/esm/lib/dcr-verify.js.map +1 -0
- package/dist/esm/lib/fetch-with-timeout.d.ts +14 -0
- package/dist/esm/lib/fetch-with-timeout.js +30 -0
- package/dist/esm/lib/fetch-with-timeout.js.map +1 -0
- package/dist/esm/lib/token-verifier.d.ts +44 -0
- package/dist/esm/lib/token-verifier.js +53 -0
- package/dist/esm/lib/token-verifier.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/providers/dcr.d.ts +107 -0
- package/dist/esm/providers/dcr.js +242 -0
- package/dist/esm/providers/dcr.js.map +1 -0
- package/dist/esm/providers/loopback-oauth.d.ts +119 -0
- package/dist/esm/providers/loopback-oauth.js +639 -0
- package/dist/esm/providers/loopback-oauth.js.map +1 -0
- package/dist/esm/providers/service-account.d.ts +131 -0
- package/dist/esm/providers/service-account.js +353 -0
- package/dist/esm/providers/service-account.js.map +1 -0
- package/dist/esm/schemas/index.d.ts +20 -0
- package/dist/esm/schemas/index.js +18 -0
- package/dist/esm/schemas/index.js.map +1 -0
- package/dist/esm/setup/config.d.ts +112 -0
- package/dist/esm/setup/config.js +258 -0
- package/dist/esm/setup/config.js.map +1 -0
- package/dist/esm/types.d.ts +173 -0
- package/dist/esm/types.js +6 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +89 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loopback OAuth Implementation (RFC 8252)
|
|
3
|
+
*
|
|
4
|
+
* Implements OAuth 2.0 Authorization Code Flow with PKCE using loopback interface redirection.
|
|
5
|
+
* Uses ephemeral local server with OS-assigned port (RFC 8252 Section 8.3).
|
|
6
|
+
*/
|
|
7
|
+
import { type OAuth2TokenStorageProvider } from '@mcp-z/oauth';
|
|
8
|
+
import { OAuth2Client } from 'google-auth-library';
|
|
9
|
+
import { type LoopbackOAuthConfig } from '../types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Loopback OAuth Client (RFC 8252 Section 7.3)
|
|
12
|
+
*
|
|
13
|
+
* Implements OAuth 2.0 Authorization Code Flow with PKCE for native applications
|
|
14
|
+
* using loopback interface redirection. Manages ephemeral OAuth flows and token persistence
|
|
15
|
+
* with Keyv for key-based token storage using compound keys.
|
|
16
|
+
*
|
|
17
|
+
* Token key format: {accountId}:{service}:token (e.g., "user@example.com:gmail:token")
|
|
18
|
+
*/
|
|
19
|
+
export declare class LoopbackOAuthProvider implements OAuth2TokenStorageProvider {
|
|
20
|
+
private config;
|
|
21
|
+
constructor(config: LoopbackOAuthConfig);
|
|
22
|
+
/**
|
|
23
|
+
* Get access token from Keyv using compound key
|
|
24
|
+
*
|
|
25
|
+
* @param accountId - Account identifier (email address). Required for loopback OAuth.
|
|
26
|
+
* @returns Access token for API requests
|
|
27
|
+
*/
|
|
28
|
+
getAccessToken(accountId?: string): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Convert to googleapis-compatible OAuth2Client
|
|
31
|
+
*
|
|
32
|
+
* @param accountId - Account identifier for multi-account support (e.g., 'user@example.com')
|
|
33
|
+
* @returns OAuth2Client configured for the specified account
|
|
34
|
+
*/
|
|
35
|
+
toAuth(accountId?: string): OAuth2Client;
|
|
36
|
+
/**
|
|
37
|
+
* Authenticate new account with OAuth flow
|
|
38
|
+
* Triggers account selection, stores token, registers account
|
|
39
|
+
*
|
|
40
|
+
* @returns Email address of newly authenticated account
|
|
41
|
+
* @throws Error in headless mode (cannot open browser for OAuth)
|
|
42
|
+
*/
|
|
43
|
+
authenticateNewAccount(): Promise<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Get user email from Google's userinfo endpoint (pure query)
|
|
46
|
+
* Used to query email for existing authenticated account
|
|
47
|
+
*
|
|
48
|
+
* @param accountId - Account identifier to get email for
|
|
49
|
+
* @returns User's email address
|
|
50
|
+
*/
|
|
51
|
+
getUserEmail(accountId?: string): Promise<string>;
|
|
52
|
+
/**
|
|
53
|
+
* Check for existing accounts in token storage (incremental OAuth detection)
|
|
54
|
+
*
|
|
55
|
+
* Uses key-utils helper for forward compatibility with key format changes.
|
|
56
|
+
*
|
|
57
|
+
* @returns Array of account IDs that have tokens for this service
|
|
58
|
+
*/
|
|
59
|
+
private getExistingAccounts;
|
|
60
|
+
private isTokenValid;
|
|
61
|
+
/**
|
|
62
|
+
* Fetch user email from Google OAuth2 userinfo endpoint
|
|
63
|
+
* Called during OAuth flow to get email for accountId
|
|
64
|
+
*
|
|
65
|
+
* @param accessToken - Fresh access token from OAuth exchange
|
|
66
|
+
* @returns User's email address
|
|
67
|
+
*/
|
|
68
|
+
private fetchUserEmailFromToken;
|
|
69
|
+
private performEphemeralOAuthFlow;
|
|
70
|
+
private exchangeCodeForToken;
|
|
71
|
+
private refreshAccessToken;
|
|
72
|
+
/**
|
|
73
|
+
* Create authentication middleware for MCP tools, resources, and prompts
|
|
74
|
+
*
|
|
75
|
+
* Returns position-aware middleware wrappers that enrich handlers with authentication context.
|
|
76
|
+
* The middleware handles token retrieval, refresh, and AuthRequiredError automatically.
|
|
77
|
+
*
|
|
78
|
+
* Single-user middleware for desktop/CLI apps where ONE user runs the entire process:
|
|
79
|
+
* - Desktop applications (Claude Desktop)
|
|
80
|
+
* - CLI tools (Gmail CLI)
|
|
81
|
+
* - Personal automation scripts
|
|
82
|
+
*
|
|
83
|
+
* All requests use token lookups based on the active account or account override.
|
|
84
|
+
*
|
|
85
|
+
* @returns Object with withToolAuth, withResourceAuth, withPromptAuth methods
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const loopback = new LoopbackOAuthProvider({ service: 'gmail', ... });
|
|
90
|
+
* const authMiddleware = loopback.authMiddleware();
|
|
91
|
+
* const tools = toolFactories.map(f => f()).map(authMiddleware.withToolAuth);
|
|
92
|
+
* const resources = resourceFactories.map(f => f()).map(authMiddleware.withResourceAuth);
|
|
93
|
+
* const prompts = promptFactories.map(f => f()).map(authMiddleware.withPromptAuth);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
authMiddleware(): {
|
|
97
|
+
withToolAuth: <T extends {
|
|
98
|
+
name: string;
|
|
99
|
+
config: unknown;
|
|
100
|
+
handler: unknown;
|
|
101
|
+
}>(module: T) => T;
|
|
102
|
+
withResourceAuth: <T extends {
|
|
103
|
+
name: string;
|
|
104
|
+
template?: unknown;
|
|
105
|
+
config?: unknown;
|
|
106
|
+
handler: unknown;
|
|
107
|
+
}>(module: T) => T;
|
|
108
|
+
withPromptAuth: <T extends {
|
|
109
|
+
name: string;
|
|
110
|
+
config: unknown;
|
|
111
|
+
handler: unknown;
|
|
112
|
+
}>(module: T) => T;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Create a loopback OAuth client for Google services
|
|
117
|
+
* Works for both stdio and HTTP transports
|
|
118
|
+
*/
|
|
119
|
+
export declare function createGoogleFileAuth(config: LoopbackOAuthConfig): OAuth2TokenStorageProvider;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loopback OAuth Implementation (RFC 8252)
|
|
3
|
+
*
|
|
4
|
+
* Implements OAuth 2.0 Authorization Code Flow with PKCE using loopback interface redirection.
|
|
5
|
+
* Uses ephemeral local server with OS-assigned port (RFC 8252 Section 8.3).
|
|
6
|
+
*/
|
|
7
|
+
import { type OAuth2TokenStorageProvider } from '@mcp-z/oauth';
|
|
8
|
+
import { OAuth2Client } from 'google-auth-library';
|
|
9
|
+
import { type LoopbackOAuthConfig } from '../types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Loopback OAuth Client (RFC 8252 Section 7.3)
|
|
12
|
+
*
|
|
13
|
+
* Implements OAuth 2.0 Authorization Code Flow with PKCE for native applications
|
|
14
|
+
* using loopback interface redirection. Manages ephemeral OAuth flows and token persistence
|
|
15
|
+
* with Keyv for key-based token storage using compound keys.
|
|
16
|
+
*
|
|
17
|
+
* Token key format: {accountId}:{service}:token (e.g., "user@example.com:gmail:token")
|
|
18
|
+
*/
|
|
19
|
+
export declare class LoopbackOAuthProvider implements OAuth2TokenStorageProvider {
|
|
20
|
+
private config;
|
|
21
|
+
constructor(config: LoopbackOAuthConfig);
|
|
22
|
+
/**
|
|
23
|
+
* Get access token from Keyv using compound key
|
|
24
|
+
*
|
|
25
|
+
* @param accountId - Account identifier (email address). Required for loopback OAuth.
|
|
26
|
+
* @returns Access token for API requests
|
|
27
|
+
*/
|
|
28
|
+
getAccessToken(accountId?: string): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Convert to googleapis-compatible OAuth2Client
|
|
31
|
+
*
|
|
32
|
+
* @param accountId - Account identifier for multi-account support (e.g., 'user@example.com')
|
|
33
|
+
* @returns OAuth2Client configured for the specified account
|
|
34
|
+
*/
|
|
35
|
+
toAuth(accountId?: string): OAuth2Client;
|
|
36
|
+
/**
|
|
37
|
+
* Authenticate new account with OAuth flow
|
|
38
|
+
* Triggers account selection, stores token, registers account
|
|
39
|
+
*
|
|
40
|
+
* @returns Email address of newly authenticated account
|
|
41
|
+
* @throws Error in headless mode (cannot open browser for OAuth)
|
|
42
|
+
*/
|
|
43
|
+
authenticateNewAccount(): Promise<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Get user email from Google's userinfo endpoint (pure query)
|
|
46
|
+
* Used to query email for existing authenticated account
|
|
47
|
+
*
|
|
48
|
+
* @param accountId - Account identifier to get email for
|
|
49
|
+
* @returns User's email address
|
|
50
|
+
*/
|
|
51
|
+
getUserEmail(accountId?: string): Promise<string>;
|
|
52
|
+
/**
|
|
53
|
+
* Check for existing accounts in token storage (incremental OAuth detection)
|
|
54
|
+
*
|
|
55
|
+
* Uses key-utils helper for forward compatibility with key format changes.
|
|
56
|
+
*
|
|
57
|
+
* @returns Array of account IDs that have tokens for this service
|
|
58
|
+
*/
|
|
59
|
+
private getExistingAccounts;
|
|
60
|
+
private isTokenValid;
|
|
61
|
+
/**
|
|
62
|
+
* Fetch user email from Google OAuth2 userinfo endpoint
|
|
63
|
+
* Called during OAuth flow to get email for accountId
|
|
64
|
+
*
|
|
65
|
+
* @param accessToken - Fresh access token from OAuth exchange
|
|
66
|
+
* @returns User's email address
|
|
67
|
+
*/
|
|
68
|
+
private fetchUserEmailFromToken;
|
|
69
|
+
private performEphemeralOAuthFlow;
|
|
70
|
+
private exchangeCodeForToken;
|
|
71
|
+
private refreshAccessToken;
|
|
72
|
+
/**
|
|
73
|
+
* Create authentication middleware for MCP tools, resources, and prompts
|
|
74
|
+
*
|
|
75
|
+
* Returns position-aware middleware wrappers that enrich handlers with authentication context.
|
|
76
|
+
* The middleware handles token retrieval, refresh, and AuthRequiredError automatically.
|
|
77
|
+
*
|
|
78
|
+
* Single-user middleware for desktop/CLI apps where ONE user runs the entire process:
|
|
79
|
+
* - Desktop applications (Claude Desktop)
|
|
80
|
+
* - CLI tools (Gmail CLI)
|
|
81
|
+
* - Personal automation scripts
|
|
82
|
+
*
|
|
83
|
+
* All requests use token lookups based on the active account or account override.
|
|
84
|
+
*
|
|
85
|
+
* @returns Object with withToolAuth, withResourceAuth, withPromptAuth methods
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const loopback = new LoopbackOAuthProvider({ service: 'gmail', ... });
|
|
90
|
+
* const authMiddleware = loopback.authMiddleware();
|
|
91
|
+
* const tools = toolFactories.map(f => f()).map(authMiddleware.withToolAuth);
|
|
92
|
+
* const resources = resourceFactories.map(f => f()).map(authMiddleware.withResourceAuth);
|
|
93
|
+
* const prompts = promptFactories.map(f => f()).map(authMiddleware.withPromptAuth);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
authMiddleware(): {
|
|
97
|
+
withToolAuth: <T extends {
|
|
98
|
+
name: string;
|
|
99
|
+
config: unknown;
|
|
100
|
+
handler: unknown;
|
|
101
|
+
}>(module: T) => T;
|
|
102
|
+
withResourceAuth: <T extends {
|
|
103
|
+
name: string;
|
|
104
|
+
template?: unknown;
|
|
105
|
+
config?: unknown;
|
|
106
|
+
handler: unknown;
|
|
107
|
+
}>(module: T) => T;
|
|
108
|
+
withPromptAuth: <T extends {
|
|
109
|
+
name: string;
|
|
110
|
+
config: unknown;
|
|
111
|
+
handler: unknown;
|
|
112
|
+
}>(module: T) => T;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Create a loopback OAuth client for Google services
|
|
117
|
+
* Works for both stdio and HTTP transports
|
|
118
|
+
*/
|
|
119
|
+
export declare function createGoogleFileAuth(config: LoopbackOAuthConfig): OAuth2TokenStorageProvider;
|