@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.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +93 -0
  3. package/dist/cjs/index.d.cts +16 -0
  4. package/dist/cjs/index.d.ts +16 -0
  5. package/dist/cjs/index.js +112 -0
  6. package/dist/cjs/index.js.map +1 -0
  7. package/dist/cjs/lib/dcr-router.d.cts +44 -0
  8. package/dist/cjs/lib/dcr-router.d.ts +44 -0
  9. package/dist/cjs/lib/dcr-router.js +1189 -0
  10. package/dist/cjs/lib/dcr-router.js.map +1 -0
  11. package/dist/cjs/lib/dcr-utils.d.cts +160 -0
  12. package/dist/cjs/lib/dcr-utils.d.ts +160 -0
  13. package/dist/cjs/lib/dcr-utils.js +860 -0
  14. package/dist/cjs/lib/dcr-utils.js.map +1 -0
  15. package/dist/cjs/lib/dcr-verify.d.cts +53 -0
  16. package/dist/cjs/lib/dcr-verify.d.ts +53 -0
  17. package/dist/cjs/lib/dcr-verify.js +193 -0
  18. package/dist/cjs/lib/dcr-verify.js.map +1 -0
  19. package/dist/cjs/lib/fetch-with-timeout.d.cts +14 -0
  20. package/dist/cjs/lib/fetch-with-timeout.d.ts +14 -0
  21. package/dist/cjs/lib/fetch-with-timeout.js +257 -0
  22. package/dist/cjs/lib/fetch-with-timeout.js.map +1 -0
  23. package/dist/cjs/lib/token-verifier.d.cts +44 -0
  24. package/dist/cjs/lib/token-verifier.d.ts +44 -0
  25. package/dist/cjs/lib/token-verifier.js +253 -0
  26. package/dist/cjs/lib/token-verifier.js.map +1 -0
  27. package/dist/cjs/package.json +1 -0
  28. package/dist/cjs/providers/dcr.d.cts +107 -0
  29. package/dist/cjs/providers/dcr.d.ts +107 -0
  30. package/dist/cjs/providers/dcr.js +584 -0
  31. package/dist/cjs/providers/dcr.js.map +1 -0
  32. package/dist/cjs/providers/loopback-oauth.d.cts +119 -0
  33. package/dist/cjs/providers/loopback-oauth.d.ts +119 -0
  34. package/dist/cjs/providers/loopback-oauth.js +1334 -0
  35. package/dist/cjs/providers/loopback-oauth.js.map +1 -0
  36. package/dist/cjs/providers/service-account.d.cts +131 -0
  37. package/dist/cjs/providers/service-account.d.ts +131 -0
  38. package/dist/cjs/providers/service-account.js +800 -0
  39. package/dist/cjs/providers/service-account.js.map +1 -0
  40. package/dist/cjs/schemas/index.d.cts +20 -0
  41. package/dist/cjs/schemas/index.d.ts +20 -0
  42. package/dist/cjs/schemas/index.js +37 -0
  43. package/dist/cjs/schemas/index.js.map +1 -0
  44. package/dist/cjs/setup/config.d.cts +112 -0
  45. package/dist/cjs/setup/config.d.ts +112 -0
  46. package/dist/cjs/setup/config.js +236 -0
  47. package/dist/cjs/setup/config.js.map +1 -0
  48. package/dist/cjs/types.d.cts +173 -0
  49. package/dist/cjs/types.d.ts +173 -0
  50. package/dist/cjs/types.js +16 -0
  51. package/dist/cjs/types.js.map +1 -0
  52. package/dist/esm/index.d.ts +16 -0
  53. package/dist/esm/index.js +16 -0
  54. package/dist/esm/index.js.map +1 -0
  55. package/dist/esm/lib/dcr-router.d.ts +44 -0
  56. package/dist/esm/lib/dcr-router.js +515 -0
  57. package/dist/esm/lib/dcr-router.js.map +1 -0
  58. package/dist/esm/lib/dcr-utils.d.ts +160 -0
  59. package/dist/esm/lib/dcr-utils.js +270 -0
  60. package/dist/esm/lib/dcr-utils.js.map +1 -0
  61. package/dist/esm/lib/dcr-verify.d.ts +53 -0
  62. package/dist/esm/lib/dcr-verify.js +53 -0
  63. package/dist/esm/lib/dcr-verify.js.map +1 -0
  64. package/dist/esm/lib/fetch-with-timeout.d.ts +14 -0
  65. package/dist/esm/lib/fetch-with-timeout.js +30 -0
  66. package/dist/esm/lib/fetch-with-timeout.js.map +1 -0
  67. package/dist/esm/lib/token-verifier.d.ts +44 -0
  68. package/dist/esm/lib/token-verifier.js +53 -0
  69. package/dist/esm/lib/token-verifier.js.map +1 -0
  70. package/dist/esm/package.json +1 -0
  71. package/dist/esm/providers/dcr.d.ts +107 -0
  72. package/dist/esm/providers/dcr.js +242 -0
  73. package/dist/esm/providers/dcr.js.map +1 -0
  74. package/dist/esm/providers/loopback-oauth.d.ts +119 -0
  75. package/dist/esm/providers/loopback-oauth.js +639 -0
  76. package/dist/esm/providers/loopback-oauth.js.map +1 -0
  77. package/dist/esm/providers/service-account.d.ts +131 -0
  78. package/dist/esm/providers/service-account.js +353 -0
  79. package/dist/esm/providers/service-account.js.map +1 -0
  80. package/dist/esm/schemas/index.d.ts +20 -0
  81. package/dist/esm/schemas/index.js +18 -0
  82. package/dist/esm/schemas/index.js.map +1 -0
  83. package/dist/esm/setup/config.d.ts +112 -0
  84. package/dist/esm/setup/config.js +258 -0
  85. package/dist/esm/setup/config.js.map +1 -0
  86. package/dist/esm/types.d.ts +173 -0
  87. package/dist/esm/types.js +6 -0
  88. package/dist/esm/types.js.map +1 -0
  89. 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;