@mcp-z/oauth-microsoft 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 +98 -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 +1227 -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 +110 -0
  29. package/dist/cjs/providers/dcr.d.ts +110 -0
  30. package/dist/cjs/providers/dcr.js +600 -0
  31. package/dist/cjs/providers/dcr.js.map +1 -0
  32. package/dist/cjs/providers/device-code.d.cts +179 -0
  33. package/dist/cjs/providers/device-code.d.ts +179 -0
  34. package/dist/cjs/providers/device-code.js +896 -0
  35. package/dist/cjs/providers/device-code.js.map +1 -0
  36. package/dist/cjs/providers/loopback-oauth.d.cts +125 -0
  37. package/dist/cjs/providers/loopback-oauth.d.ts +125 -0
  38. package/dist/cjs/providers/loopback-oauth.js +1325 -0
  39. package/dist/cjs/providers/loopback-oauth.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 +113 -0
  45. package/dist/cjs/setup/config.d.ts +113 -0
  46. package/dist/cjs/setup/config.js +246 -0
  47. package/dist/cjs/setup/config.js.map +1 -0
  48. package/dist/cjs/types.d.cts +188 -0
  49. package/dist/cjs/types.d.ts +188 -0
  50. package/dist/cjs/types.js +18 -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 +556 -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 +110 -0
  72. package/dist/esm/providers/dcr.js +235 -0
  73. package/dist/esm/providers/dcr.js.map +1 -0
  74. package/dist/esm/providers/device-code.d.ts +179 -0
  75. package/dist/esm/providers/device-code.js +417 -0
  76. package/dist/esm/providers/device-code.js.map +1 -0
  77. package/dist/esm/providers/loopback-oauth.d.ts +125 -0
  78. package/dist/esm/providers/loopback-oauth.js +643 -0
  79. package/dist/esm/providers/loopback-oauth.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 +113 -0
  84. package/dist/esm/setup/config.js +268 -0
  85. package/dist/esm/setup/config.js.map +1 -0
  86. package/dist/esm/types.d.ts +188 -0
  87. package/dist/esm/types.js +8 -0
  88. package/dist/esm/types.js.map +1 -0
  89. package/package.json +87 -0
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Loopback OAuth Implementation for Microsoft (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
+ * Eliminates port conflicts by using port 0, allowing unlimited concurrent servers.
7
+ *
8
+ * Flow:
9
+ * 1. Check token cache (memory + storage)
10
+ * 2. If cache miss: Start ephemeral server on port 0
11
+ * 3. Generate auth URL with dynamic redirect (localhost:{assigned-port})
12
+ * 4. Open browser or print URL (headless flag controls behavior)
13
+ * 5. Handle callback, exchange code for token
14
+ * 6. Cache token to storage
15
+ * 7. Close ephemeral server
16
+ */
17
+ import { type OAuth2TokenStorageProvider } from '@mcp-z/oauth';
18
+ import { type LoopbackOAuthConfig } from '../types.js';
19
+ /**
20
+ * Loopback OAuth Client (RFC 8252 Section 7.3)
21
+ *
22
+ * Implements OAuth 2.0 Authorization Code Flow with PKCE for native applications
23
+ * using loopback interface redirection. Manages ephemeral OAuth flows and token persistence
24
+ * with Keyv for key-based token storage using compound keys.
25
+ *
26
+ * Token key format: {accountId}:{service}:token (e.g., "user@example.com:outlook:token")
27
+ */
28
+ export declare class LoopbackOAuthProvider implements OAuth2TokenStorageProvider {
29
+ private config;
30
+ constructor(config: LoopbackOAuthConfig);
31
+ /**
32
+ * Get access token from Keyv using compound key
33
+ *
34
+ * @param accountId - Account identifier (email address). Required for loopback OAuth.
35
+ * @returns Access token for API requests
36
+ */
37
+ getAccessToken(accountId?: string): Promise<string>;
38
+ /**
39
+ * Convert to Microsoft Graph-compatible auth provider
40
+ *
41
+ * @param accountId - Account identifier for multi-account support (e.g., 'user@example.com')
42
+ * @returns Auth provider configured for the specified account
43
+ */
44
+ toAuthProvider(accountId?: string): {
45
+ getAccessToken: () => Promise<string>;
46
+ };
47
+ /**
48
+ * Authenticate new account with OAuth flow
49
+ * Triggers account selection, stores token, registers account
50
+ *
51
+ * @returns Email address of newly authenticated account
52
+ * @throws Error in headless mode (cannot open browser for OAuth)
53
+ */
54
+ authenticateNewAccount(): Promise<string>;
55
+ /**
56
+ * Get user email from Microsoft Graph API (pure query)
57
+ * Used to query email for existing authenticated account
58
+ *
59
+ * @param accountId - Account identifier to get email for
60
+ * @returns User's email address
61
+ */
62
+ getUserEmail(accountId?: string): Promise<string>;
63
+ /**
64
+ * Check for existing accounts in token storage (incremental OAuth detection)
65
+ *
66
+ * Uses key-utils helper for forward compatibility with key format changes.
67
+ *
68
+ * @returns Array of account IDs that have tokens for this service
69
+ */
70
+ private getExistingAccounts;
71
+ private isTokenValid;
72
+ /**
73
+ * Fetch user email from Microsoft Graph using access token
74
+ * Called during OAuth flow to get email for accountId
75
+ *
76
+ * @param accessToken - Fresh access token from OAuth exchange
77
+ * @returns User's email address (mail field or userPrincipalName fallback)
78
+ */
79
+ private fetchUserEmailFromToken;
80
+ private performEphemeralOAuthFlow;
81
+ private exchangeCodeForToken;
82
+ private refreshAccessToken;
83
+ /**
84
+ * Create auth middleware for single-user context (single active account per service)
85
+ *
86
+ * Single-user mode:
87
+ * - Maintains per-service active accounts in storage
88
+ * - Supports backchannel account override via extra._meta.accountId
89
+ * - Automatically enhances output schemas with auth_required branch
90
+ *
91
+ * Example:
92
+ * ```typescript
93
+ * const loopback = new LoopbackOAuthProvider({ service: 'outlook', ... });
94
+ * const middleware = loopback.authMiddleware();
95
+ * const tools = toolFactories.map(f => f()).map(middleware.withToolAuth);
96
+ * const resources = resourceFactories.map(f => f()).map(middleware.withResourceAuth);
97
+ * const prompts = promptFactories.map(f => f()).map(middleware.withPromptAuth);
98
+ * ```
99
+ *
100
+ * @returns Object with withToolAuth, withResourceAuth, withPromptAuth methods
101
+ */
102
+ authMiddleware(): {
103
+ withToolAuth: <T extends {
104
+ name: string;
105
+ config: unknown;
106
+ handler: unknown;
107
+ }>(module: T) => T;
108
+ withResourceAuth: <T extends {
109
+ name: string;
110
+ template?: unknown;
111
+ config?: unknown;
112
+ handler: unknown;
113
+ }>(module: T) => T;
114
+ withPromptAuth: <T extends {
115
+ name: string;
116
+ config: unknown;
117
+ handler: unknown;
118
+ }>(module: T) => T;
119
+ };
120
+ }
121
+ /**
122
+ * Create a loopback OAuth client for Microsoft services
123
+ * Works for both stdio and HTTP transports
124
+ */
125
+ export declare function createMicrosoftFileAuth(config: LoopbackOAuthConfig): OAuth2TokenStorageProvider;