|
1 | 1 | /** |
2 | 2 | * Token Refresh Example |
3 | 3 | * |
4 | | - * Demonstrates how to use preRequestHook to automatically refresh |
5 | | - * short-lived authentication tokens (e.g., 3-minute TTL bearer tokens) |
| 4 | + * Demonstrates ATP's built-in automatic token refresh feature. |
| 5 | + * The ATP client automatically refreshes tokens before they expire, |
| 6 | + * eliminating the need for manual token management in most cases. |
| 7 | + * |
| 8 | + * Run this with the test-server example (which has short token TTL): |
| 9 | + * 1. In one terminal: cd examples/test-server && npx tsx server.ts with these following config: |
| 10 | + * `clientInit: { tokenTTL: 5000, tokenRotation: 2500 }` |
| 11 | + * 2. In another terminal: cd examples/token-refresh && npx tsx server.ts |
6 | 12 | */ |
7 | 13 |
|
8 | 14 | import { AgentToolProtocolClient } from '@mondaydotcomorg/atp-client'; |
9 | | -import type { ClientHooks } from '@mondaydotcomorg/atp-client'; |
10 | | - |
11 | | -/** |
12 | | - * Token Manager - Handles token lifecycle with caching |
13 | | - */ |
14 | | -class TokenManager { |
15 | | - private currentToken: string | null = null; |
16 | | - private tokenExpiry: number = 0; |
17 | | - private refreshPromise: Promise<void> | null = null; |
18 | | - |
19 | | - constructor( |
20 | | - private authEndpoint: string, |
21 | | - private credentials: { clientId: string; clientSecret: string } |
22 | | - ) {} |
23 | | - |
24 | | - /** |
25 | | - * Gets a valid token, refreshing if necessary |
26 | | - * Thread-safe: multiple concurrent calls will share the same refresh |
27 | | - */ |
28 | | - async getValidToken(): Promise<string> { |
29 | | - const now = Date.now(); |
30 | | - |
31 | | - // Refresh if expired or about to expire (30 second buffer) |
32 | | - if (!this.currentToken || now >= this.tokenExpiry - 30000) { |
33 | | - // Prevent multiple concurrent refreshes |
34 | | - if (!this.refreshPromise) { |
35 | | - this.refreshPromise = this.refreshToken().finally(() => { |
36 | | - this.refreshPromise = null; |
37 | | - }); |
38 | | - } |
39 | | - await this.refreshPromise; |
40 | | - } |
41 | | - |
42 | | - return this.currentToken!; |
43 | | - } |
44 | | - |
45 | | - /** |
46 | | - * Refreshes the token by calling the auth service |
47 | | - */ |
48 | | - private async refreshToken(): Promise<void> { |
49 | | - console.log('[TokenManager] Refreshing token...'); |
50 | | - |
51 | | - try { |
52 | | - const response = await fetch(this.authEndpoint, { |
53 | | - method: 'POST', |
54 | | - headers: { 'Content-Type': 'application/json' }, |
55 | | - body: JSON.stringify({ |
56 | | - grant_type: 'client_credentials', |
57 | | - client_id: this.credentials.clientId, |
58 | | - client_secret: this.credentials.clientSecret, |
59 | | - }), |
60 | | - }); |
61 | 15 |
|
62 | | - if (!response.ok) { |
63 | | - throw new Error(`Token refresh failed: ${response.status} ${response.statusText}`); |
64 | | - } |
65 | | - |
66 | | - const data: any = await response.json(); |
67 | | - this.currentToken = data.access_token; |
68 | | - |
69 | | - // Calculate expiry with buffer |
70 | | - const expiresIn = data.expires_in || 180; // Default to 3 minutes |
71 | | - this.tokenExpiry = Date.now() + expiresIn * 1000; |
72 | | - |
73 | | - console.log(`[TokenManager] Token refreshed. Expires in ${expiresIn} seconds`); |
74 | | - } catch (error) { |
75 | | - console.error('[TokenManager] Failed to refresh token:', error); |
76 | | - throw error; |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - /** |
81 | | - * Simulates getting an initial token (for demo purposes) |
82 | | - */ |
83 | | - async initialize(): Promise<void> { |
84 | | - // For demo: simulate getting initial token |
85 | | - this.currentToken = 'initial-token-' + Date.now(); |
86 | | - this.tokenExpiry = Date.now() + 180000; // 3 minutes |
87 | | - console.log('[TokenManager] Initialized with demo token'); |
88 | | - } |
89 | | -} |
| 16 | +const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); |
90 | 17 |
|
91 | 18 | /** |
92 | | - * Example: Using ATP Client with automatic token refresh |
| 19 | + * Example: ATP Client with automatic token refresh (default behavior) |
93 | 20 | */ |
94 | 21 | async function main() { |
95 | | - // Setup token manager |
96 | | - const tokenManager = new TokenManager('https://auth.example.com/oauth/token', { |
97 | | - clientId: process.env.CLIENT_ID || 'demo-client', |
98 | | - clientSecret: process.env.CLIENT_SECRET || 'demo-secret', |
99 | | - }); |
100 | | - |
101 | | - // Initialize token |
102 | | - await tokenManager.initialize(); |
103 | | - |
104 | | - // Create hooks object with token refresh |
105 | | - const hooks: ClientHooks = { |
106 | | - preRequest: async (context) => { |
107 | | - console.log(`[Hook] ${context.method} ${context.url}`); |
108 | | - |
109 | | - // Get fresh token (will refresh if needed) |
110 | | - const token = await tokenManager.getValidToken(); |
| 22 | + console.log('='.repeat(60)); |
| 23 | + console.log('ATP Automatic Token Refresh Demo'); |
| 24 | + console.log('='.repeat(60)); |
111 | 25 |
|
112 | | - // Return updated headers with fresh token |
113 | | - return { |
114 | | - headers: { |
115 | | - ...context.currentHeaders, |
116 | | - Authorization: `Bearer ${token}`, |
117 | | - 'X-Request-Time': new Date().toISOString(), |
118 | | - }, |
119 | | - }; |
120 | | - }, |
121 | | - }; |
122 | | - |
123 | | - // Create ATP client with hooks |
| 26 | + // Create ATP client - automatic token refresh is enabled by default |
124 | 27 | const client = new AgentToolProtocolClient({ |
125 | 28 | baseUrl: process.env.ATP_SERVER_URL || 'http://localhost:3333', |
126 | | - hooks, |
| 29 | + tokenRefresh: { enabled: true }, |
| 30 | + hooks: { |
| 31 | + preRequest: async (context) => { |
| 32 | + console.log('[Hook] Request to:', context.url); |
| 33 | + return { headers: context.currentHeaders }; |
| 34 | + }, |
| 35 | + }, |
127 | 36 | }); |
128 | 37 |
|
129 | 38 | console.log('\n=== Initializing ATP Client ==='); |
130 | | - await client.init({ name: 'token-refresh-example', version: '1.0.0' }); |
| 39 | + const initResult = await client.init({ name: 'token-refresh-example', version: '1.0.0' }); |
| 40 | + |
| 41 | + console.log('Current time:', new Date()); |
| 42 | + console.log('Client ID:', initResult.clientId); |
| 43 | + console.log('Token expires at:', new Date(initResult.expiresAt)); |
| 44 | + console.log('Token rotates at:', new Date(initResult.tokenRotateAt)); |
| 45 | + |
| 46 | + const tokenTTL = initResult.expiresAt - Date.now(); |
| 47 | + const rotateIn = initResult.tokenRotateAt - Date.now(); |
| 48 | + console.log(`Token TTL: ${Math.round(tokenTTL / 1000)}s, Rotate in: ${Math.round(rotateIn / 1000)}s`); |
131 | 49 |
|
132 | 50 | console.log('\n=== Connecting to Server ==='); |
133 | 51 | await client.connect(); |
134 | 52 |
|
135 | | - console.log('\n=== Executing Code ==='); |
136 | | - const result = await client.execute(` |
137 | | - // Example code that uses ATP tools |
| 53 | + // First execution - should use original token |
| 54 | + console.log('\n=== First Execution (using original token) ==='); |
| 55 | + const result1 = await client.execute(` |
| 56 | + const t = api.custom.add({ a: 2, b: 3 }); |
| 57 | + const result = { |
| 58 | + timestamp: Date.now(), |
| 59 | + message: "First call with original token" |
| 60 | + }; |
| 61 | + return result; |
| 62 | + `); |
| 63 | + console.log('Result:', JSON.stringify(result1.result, null, 2)); |
| 64 | + |
| 65 | + // Wait past the rotation time (test-server uses 2.5s rotation for 5s TTL) |
| 66 | + const waitTime = Math.max(rotateIn + 500, 30000); |
| 67 | + console.log(`\n=== Waiting ${waitTime / 1000}s to trigger token rotation ===`); |
| 68 | + await wait(waitTime); |
| 69 | + |
| 70 | + // Second execution - should automatically refresh token before calling |
| 71 | + console.log('\n=== Second Execution (token should auto-refresh) ==='); |
| 72 | + const result2 = await client.execute(` |
138 | 73 | const result = { |
139 | 74 | timestamp: Date.now(), |
140 | | - message: "Hello from ATP with auto-refreshed token!" |
| 75 | + message: "Second call - token was auto-refreshed!" |
141 | 76 | }; |
142 | 77 | return result; |
143 | 78 | `); |
| 79 | + console.log('Result:', JSON.stringify(result2.result, null, 2)); |
144 | 80 |
|
145 | | - console.log('\n=== Execution Result ==='); |
146 | | - console.log(JSON.stringify(result, null, 2)); |
| 81 | + // Third execution - should still work |
| 82 | + console.log('\n=== Third Execution (continued use) ==='); |
| 83 | + const result3 = await client.execute(` |
| 84 | + const result = { |
| 85 | + timestamp: Date.now(), |
| 86 | + message: "Third call - everything still works!" |
| 87 | + }; |
| 88 | + return result; |
| 89 | + `); |
| 90 | + console.log('Result:', JSON.stringify(result3.result, null, 2)); |
147 | 91 |
|
148 | 92 | console.log('\n=== Getting Server Info ==='); |
149 | 93 | const info = await client.getServerInfo(); |
150 | 94 | console.log('Server version:', info.version); |
151 | 95 |
|
152 | | - console.log('\n✅ All requests completed with automatic token refresh!'); |
| 96 | + console.log('\n' + '='.repeat(60)); |
| 97 | + console.log('✅ All requests completed with automatic token refresh!'); |
| 98 | + console.log('='.repeat(60)); |
| 99 | + console.log('\nKey takeaways:'); |
| 100 | + console.log('1. Token refresh happens automatically before each request'); |
| 101 | + console.log('2. No manual token management code needed'); |
| 102 | + console.log('3. Requests never fail due to expired tokens'); |
| 103 | + console.log('4. Works with short-lived tokens (even 5-second TTL)'); |
153 | 104 | } |
154 | 105 |
|
155 | | -// Run example |
156 | | -main().catch((error) => { |
157 | | - console.error('Error:', error); |
158 | | - process.exit(1); |
159 | | -}); |
| 106 | +// Run examples |
| 107 | +main() |
| 108 | + .catch((error) => { |
| 109 | + console.error('Error:', error); |
| 110 | + process.exit(1); |
| 111 | + }); |
0 commit comments