@tiledesk/tiledesk-server 3.3.5 → 3.5.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/CHANGELOG.md +17 -0
- package/app.js +7 -0
- package/docs/activities-client.md +27 -0
- package/docs/mcp-oauth2-it.md +279 -0
- package/docs/mcp-oauth2.md +279 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server-it.md +155 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server.md +155 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot-it.md +63 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot.md +63 -0
- package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design-it.md +207 -0
- package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design.md +207 -0
- package/jobs.js +15 -0
- package/models/mcpOAuthConnection.js +131 -0
- package/package.json +1 -1
- package/pubmodules/activities/activityMessageUtil.js +137 -0
- package/pubmodules/activities/models/activity.js +2 -0
- package/pubmodules/activities/routes/activity.js +30 -1
- package/pubmodules/activities/test/execution-filter.test.js +133 -0
- package/pubmodules/activities/test/execution-messages.test.js +158 -0
- package/pubmodules/executions-publisher/index.js +457 -0
- package/pubmodules/executions-publisher/test/audit-consumer.test.js +384 -0
- package/pubmodules/executions-publisher/test/executions-publisher.test.js +204 -0
- package/pubmodules/pubModulesManager.js +12 -0
- package/routes/executions-refire.js +148 -0
- package/routes/mcp.js +11 -8
- package/routes/mcpOAuth.js +133 -0
- package/routes/mcpOAuthCallback.js +75 -0
- package/routes/request.js +5 -0
- package/routes/webhook.js +120 -54
- package/services/mcpOAuthDiscovery.js +248 -0
- package/services/mcpOAuthGoogleScopes.js +55 -0
- package/services/mcpOAuthService.js +732 -0
- package/services/mcpService.js +111 -15
- package/services/requestService.js +4 -2
- package/services/webhookService.js +4 -2
- package/test/executions-refire.test.js +362 -0
- package/test/mcpOAuthDiscovery.test.js +331 -0
- package/test/mcpOAuthGoogleScopes.test.js +25 -0
- package/test/mcpOAuthRoute.test.js +455 -0
- package/test/mcpOAuthService.test.js +693 -0
- package/test/mcpOAuthUtils.test.js +348 -0
- package/test/mcpServiceOAuth.test.js +267 -0
- package/test/requestRoute.js +57 -0
- package/test/webhookConversationRoute.js +51 -0
- package/utils/logRedactor.js +4 -2
- package/utils/mcpOAuthCrypto.js +69 -0
- package/utils/mcpOAuthPkce.js +20 -0
- package/utils/outboundUrlGuard.js +131 -0
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
|
|
3
|
+
const { expect } = require('chai');
|
|
4
|
+
const crypto = require('crypto');
|
|
5
|
+
const dns = require('dns');
|
|
6
|
+
const nock = require('nock');
|
|
7
|
+
const mongoose = require('mongoose');
|
|
8
|
+
|
|
9
|
+
const config = require('../config/database');
|
|
10
|
+
const { TdCache } = require('../utils/TdCache');
|
|
11
|
+
const McpOAuthConnection = require('../models/mcpOAuthConnection');
|
|
12
|
+
const mcpOAuthCrypto = require('../utils/mcpOAuthCrypto');
|
|
13
|
+
const pkce = require('../utils/mcpOAuthPkce');
|
|
14
|
+
const { McpOAuthService } = require('../services/mcpOAuthService');
|
|
15
|
+
|
|
16
|
+
const PROJECT = 'mcp-oauth-test-p1';
|
|
17
|
+
const OTHER_PROJECT = 'mcp-oauth-test-p2';
|
|
18
|
+
const MCP_ORIGIN = 'https://mcp.example.com';
|
|
19
|
+
const AUTH_ORIGIN = 'https://auth.example.com';
|
|
20
|
+
const SERVER_URL = MCP_ORIGIN + '/mcp';
|
|
21
|
+
const REDIRECT_URI = 'https://api.tiledesk.test/oauth2/mcp/callback';
|
|
22
|
+
const RETURN_URL = 'https://panel.tiledesk.test/#/project/p1/integrations';
|
|
23
|
+
|
|
24
|
+
const ENV = {
|
|
25
|
+
MCP_OAUTH_ENCRYPTION_KEY: crypto.randomBytes(32).toString('base64'),
|
|
26
|
+
MCP_OAUTH_REDIRECT_BASE_URL: 'https://api.tiledesk.test/',
|
|
27
|
+
MCP_OAUTH_RETURN_ORIGINS: 'https://panel.tiledesk.test, https://studio.tiledesk.test',
|
|
28
|
+
MCP_OAUTH_GOOGLE_CLIENT_ID: 'google-client-id',
|
|
29
|
+
MCP_OAUTH_GOOGLE_CLIENT_SECRET: 'google-client-secret',
|
|
30
|
+
MCP_OAUTH_ALLOW_INSECURE_URLS: undefined
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function mockDiscovery() {
|
|
34
|
+
nock(MCP_ORIGIN)
|
|
35
|
+
.post('/mcp').reply(401, {}, { 'WWW-Authenticate': 'Bearer resource_metadata="' + MCP_ORIGIN + '/.well-known/oauth-protected-resource"' })
|
|
36
|
+
.get('/.well-known/oauth-protected-resource')
|
|
37
|
+
.reply(200, { resource: MCP_ORIGIN + '/', authorization_servers: [AUTH_ORIGIN], scopes_supported: ['mcp:tools'] });
|
|
38
|
+
nock(AUTH_ORIGIN).get('/.well-known/oauth-authorization-server').reply(200, {
|
|
39
|
+
issuer: AUTH_ORIGIN,
|
|
40
|
+
authorization_endpoint: AUTH_ORIGIN + '/authorize',
|
|
41
|
+
token_endpoint: AUTH_ORIGIN + '/token',
|
|
42
|
+
registration_endpoint: AUTH_ORIGIN + '/register',
|
|
43
|
+
revocation_endpoint: AUTH_ORIGIN + '/revoke',
|
|
44
|
+
code_challenge_methods_supported: ['S256'],
|
|
45
|
+
token_endpoint_auth_methods_supported: ['none']
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function mockRegister() {
|
|
50
|
+
return nock(AUTH_ORIGIN).post('/register').reply(201, { client_id: 'dcr-client-1', token_endpoint_auth_method: 'none' });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function mockToken(reply, onBody) {
|
|
54
|
+
return nock(AUTH_ORIGIN).post('/token', (body) => {
|
|
55
|
+
if (onBody) {
|
|
56
|
+
onBody(body);
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}).reply(reply.status || 200, reply.body);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function seedActive(overrides = {}) {
|
|
63
|
+
return McpOAuthConnection.create({
|
|
64
|
+
id_project: PROJECT,
|
|
65
|
+
mode: 'manual',
|
|
66
|
+
server_url: SERVER_URL,
|
|
67
|
+
name: 'Example',
|
|
68
|
+
status: 'active',
|
|
69
|
+
client_id: 'manual-client',
|
|
70
|
+
token_endpoint_auth_method: 'none',
|
|
71
|
+
authorization_endpoint: AUTH_ORIGIN + '/authorize',
|
|
72
|
+
token_endpoint: AUTH_ORIGIN + '/token',
|
|
73
|
+
revocation_endpoint: AUTH_ORIGIN + '/revoke',
|
|
74
|
+
access_token_enc: mcpOAuthCrypto.encrypt('at-old'),
|
|
75
|
+
refresh_token_enc: mcpOAuthCrypto.encrypt('rt-old'),
|
|
76
|
+
token_type: 'Bearer',
|
|
77
|
+
expires_at: new Date(Date.now() + 60 * 1000),
|
|
78
|
+
last_refresh_at: new Date(Date.now() - 3600 * 1000),
|
|
79
|
+
...overrides
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function rejection(promise) {
|
|
84
|
+
try {
|
|
85
|
+
await promise;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return error;
|
|
88
|
+
}
|
|
89
|
+
throw new Error('expected a rejection');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
describe('mcpOAuthService', function () {
|
|
93
|
+
this.timeout(20000);
|
|
94
|
+
|
|
95
|
+
const savedEnv = {};
|
|
96
|
+
let originalLookup;
|
|
97
|
+
let tdCache;
|
|
98
|
+
let service;
|
|
99
|
+
|
|
100
|
+
before(async () => {
|
|
101
|
+
Object.keys(ENV).forEach((key) => {
|
|
102
|
+
savedEnv[key] = process.env[key];
|
|
103
|
+
if (ENV[key] === undefined) {
|
|
104
|
+
delete process.env[key];
|
|
105
|
+
} else {
|
|
106
|
+
process.env[key] = ENV[key];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
if (mongoose.connection.readyState === 0) {
|
|
110
|
+
await mongoose.connect(config.databasetest, { useNewUrlParser: true, useUnifiedTopology: true });
|
|
111
|
+
}
|
|
112
|
+
tdCache = new TdCache({ host: process.env.CACHE_REDIS_HOST || 'localhost', port: process.env.CACHE_REDIS_PORT || 6379 });
|
|
113
|
+
await tdCache.connect();
|
|
114
|
+
nock.disableNetConnect();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
after(async () => {
|
|
118
|
+
nock.enableNetConnect();
|
|
119
|
+
await McpOAuthConnection.deleteMany({ id_project: { $in: [PROJECT, OTHER_PROJECT] } });
|
|
120
|
+
tdCache.getClient().quit();
|
|
121
|
+
Object.keys(savedEnv).forEach((key) => {
|
|
122
|
+
if (savedEnv[key] === undefined) {
|
|
123
|
+
delete process.env[key];
|
|
124
|
+
} else {
|
|
125
|
+
process.env[key] = savedEnv[key];
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
beforeEach(async () => {
|
|
131
|
+
originalLookup = dns.promises.lookup;
|
|
132
|
+
dns.promises.lookup = async () => [{ address: '93.184.216.34', family: 4 }];
|
|
133
|
+
await McpOAuthConnection.deleteMany({ id_project: { $in: [PROJECT, OTHER_PROJECT] } });
|
|
134
|
+
service = new McpOAuthService();
|
|
135
|
+
service.setTdCache(tdCache);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
afterEach(() => {
|
|
139
|
+
dns.promises.lookup = originalLookup;
|
|
140
|
+
const pending = nock.pendingMocks();
|
|
141
|
+
nock.cleanAll();
|
|
142
|
+
expect(pending, 'unused nock interceptors').to.deep.equal([]);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe('mcp mode', () => {
|
|
146
|
+
|
|
147
|
+
it('discovers, registers, and builds the PKCE authorization URL', async () => {
|
|
148
|
+
mockDiscovery();
|
|
149
|
+
mockRegister();
|
|
150
|
+
|
|
151
|
+
const result = await service.startAuthorization(PROJECT, 'user-1', { mode: 'mcp', server_url: SERVER_URL, return_url: RETURN_URL });
|
|
152
|
+
|
|
153
|
+
const url = new URL(result.authorization_url);
|
|
154
|
+
expect(url.origin + url.pathname).to.equal(AUTH_ORIGIN + '/authorize');
|
|
155
|
+
expect(url.searchParams.get('response_type')).to.equal('code');
|
|
156
|
+
expect(url.searchParams.get('client_id')).to.equal('dcr-client-1');
|
|
157
|
+
expect(url.searchParams.get('redirect_uri')).to.equal(REDIRECT_URI);
|
|
158
|
+
expect(url.searchParams.get('code_challenge_method')).to.equal('S256');
|
|
159
|
+
expect(url.searchParams.get('code_challenge')).to.have.lengthOf(43);
|
|
160
|
+
expect(url.searchParams.get('resource')).to.equal(MCP_ORIGIN + '/');
|
|
161
|
+
expect(url.searchParams.get('scope')).to.equal('mcp:tools');
|
|
162
|
+
|
|
163
|
+
const state = url.searchParams.get('state');
|
|
164
|
+
const saved = await tdCache.getJSON('mcp_oauth:state:' + state);
|
|
165
|
+
expect(saved).to.include({ id_project: PROJECT, connection_id: result.connection_id, return_url: RETURN_URL, user_id: 'user-1' });
|
|
166
|
+
expect(pkce.challengeOf(saved.code_verifier)).to.equal(url.searchParams.get('code_challenge'));
|
|
167
|
+
|
|
168
|
+
const connection = await McpOAuthConnection.findById(result.connection_id);
|
|
169
|
+
expect(connection.status).to.equal('pending');
|
|
170
|
+
expect(connection.client_id).to.equal('dcr-client-1');
|
|
171
|
+
expect(connection.resource).to.equal(MCP_ORIGIN + '/');
|
|
172
|
+
expect(connection.token_endpoint).to.equal(AUTH_ORIGIN + '/token');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('exchanges the code and stores the tokens encrypted', async () => {
|
|
176
|
+
mockDiscovery();
|
|
177
|
+
mockRegister();
|
|
178
|
+
const { authorization_url, connection_id } = await service.startAuthorization(PROJECT, 'user-1', { mode: 'mcp', server_url: SERVER_URL, return_url: RETURN_URL });
|
|
179
|
+
const url = new URL(authorization_url);
|
|
180
|
+
const state = url.searchParams.get('state');
|
|
181
|
+
|
|
182
|
+
let tokenBody;
|
|
183
|
+
mockToken({ body: { access_token: 'at-1', refresh_token: 'rt-1', token_type: 'bearer', expires_in: 3600, scope: 'mcp:tools' } }, (body) => { tokenBody = body; });
|
|
184
|
+
|
|
185
|
+
const result = await service.completeAuthorization({ code: 'code-1', state });
|
|
186
|
+
|
|
187
|
+
expect(result.error).to.equal(undefined);
|
|
188
|
+
expect(result.return_url).to.equal(RETURN_URL);
|
|
189
|
+
expect(tokenBody).to.include({ grant_type: 'authorization_code', code: 'code-1', redirect_uri: REDIRECT_URI, client_id: 'dcr-client-1', resource: MCP_ORIGIN + '/' });
|
|
190
|
+
expect(pkce.challengeOf(tokenBody.code_verifier)).to.equal(url.searchParams.get('code_challenge'));
|
|
191
|
+
expect(tokenBody).to.not.have.property('client_secret');
|
|
192
|
+
|
|
193
|
+
const connection = await McpOAuthConnection.findById(connection_id);
|
|
194
|
+
expect(connection.status).to.equal('active');
|
|
195
|
+
expect(connection.access_token_enc).to.not.contain('at-1');
|
|
196
|
+
expect(mcpOAuthCrypto.decrypt(connection.access_token_enc)).to.equal('at-1');
|
|
197
|
+
expect(mcpOAuthCrypto.decrypt(connection.refresh_token_enc)).to.equal('rt-1');
|
|
198
|
+
expect(connection.token_type).to.equal('Bearer');
|
|
199
|
+
expect(connection.granted_scopes).to.deep.equal(['mcp:tools']);
|
|
200
|
+
expect(connection.expires_at.getTime()).to.be.closeTo(Date.now() + 3600 * 1000, 5000);
|
|
201
|
+
expect(await tdCache.get('mcp_oauth:state:' + state)).to.equal(null);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('reuses the registered client on the next authorization', async () => {
|
|
205
|
+
mockDiscovery();
|
|
206
|
+
mockRegister();
|
|
207
|
+
await service.startAuthorization(PROJECT, 'user-1', { mode: 'mcp', server_url: SERVER_URL });
|
|
208
|
+
mockDiscovery();
|
|
209
|
+
|
|
210
|
+
const second = await service.startAuthorization(PROJECT, 'user-1', { mode: 'mcp', server_url: SERVER_URL });
|
|
211
|
+
|
|
212
|
+
expect(new URL(second.authorization_url).searchParams.get('client_id')).to.equal('dcr-client-1');
|
|
213
|
+
expect(await McpOAuthConnection.countDocuments({ id_project: PROJECT })).to.equal(1);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('accepts each state once', async () => {
|
|
217
|
+
mockDiscovery();
|
|
218
|
+
mockRegister();
|
|
219
|
+
const { authorization_url } = await service.startAuthorization(PROJECT, 'user-1', { mode: 'mcp', server_url: SERVER_URL });
|
|
220
|
+
const state = new URL(authorization_url).searchParams.get('state');
|
|
221
|
+
mockToken({ body: { access_token: 'at-1', expires_in: 3600 } });
|
|
222
|
+
|
|
223
|
+
await service.completeAuthorization({ code: 'code-1', state });
|
|
224
|
+
const error = await rejection(service.completeAuthorization({ code: 'code-1', state }));
|
|
225
|
+
|
|
226
|
+
expect(error.status).to.equal(400);
|
|
227
|
+
expect(error.message).to.equal('Invalid or expired state');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('accepts each state once under concurrent callbacks', async () => {
|
|
231
|
+
mockDiscovery();
|
|
232
|
+
mockRegister();
|
|
233
|
+
const { authorization_url } = await service.startAuthorization(PROJECT, 'user-1', { mode: 'mcp', server_url: SERVER_URL });
|
|
234
|
+
const state = new URL(authorization_url).searchParams.get('state');
|
|
235
|
+
mockToken({ body: { access_token: 'at-1', expires_in: 3600 } });
|
|
236
|
+
|
|
237
|
+
const results = await Promise.allSettled([
|
|
238
|
+
service.completeAuthorization({ code: 'code-1', state }),
|
|
239
|
+
service.completeAuthorization({ code: 'code-1', state })
|
|
240
|
+
]);
|
|
241
|
+
|
|
242
|
+
expect(results.filter((r) => r.status === 'fulfilled')).to.have.lengthOf(1);
|
|
243
|
+
expect(results.filter((r) => r.status === 'rejected')[0].reason.status).to.equal(400);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('rejects an unknown state', async () => {
|
|
247
|
+
const error = await rejection(service.completeAuthorization({ code: 'x', state: 'unknown' }));
|
|
248
|
+
expect(error.status).to.equal(400);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('records a provider error from the callback', async () => {
|
|
252
|
+
mockDiscovery();
|
|
253
|
+
mockRegister();
|
|
254
|
+
const { authorization_url, connection_id } = await service.startAuthorization(PROJECT, 'user-1', { mode: 'mcp', server_url: SERVER_URL, return_url: RETURN_URL });
|
|
255
|
+
const state = new URL(authorization_url).searchParams.get('state');
|
|
256
|
+
|
|
257
|
+
const result = await service.completeAuthorization({ state, error: 'access_denied', error_description: 'User denied' });
|
|
258
|
+
|
|
259
|
+
expect(result.error).to.equal('access_denied');
|
|
260
|
+
expect(result.return_url).to.equal(RETURN_URL);
|
|
261
|
+
const connection = await McpOAuthConnection.findById(connection_id);
|
|
262
|
+
expect(connection.status).to.equal('pending');
|
|
263
|
+
expect(connection.last_error.code).to.equal('access_denied');
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('records a failed code exchange and drops a rejected client', async () => {
|
|
267
|
+
mockDiscovery();
|
|
268
|
+
mockRegister();
|
|
269
|
+
const { authorization_url, connection_id } = await service.startAuthorization(PROJECT, 'user-1', { mode: 'mcp', server_url: SERVER_URL });
|
|
270
|
+
const state = new URL(authorization_url).searchParams.get('state');
|
|
271
|
+
mockToken({ status: 401, body: { error: 'invalid_client' } });
|
|
272
|
+
|
|
273
|
+
const result = await service.completeAuthorization({ code: 'code-1', state });
|
|
274
|
+
|
|
275
|
+
expect(result.error).to.equal('invalid_client');
|
|
276
|
+
const connection = await McpOAuthConnection.findById(connection_id);
|
|
277
|
+
expect(connection.client_id).to.equal(undefined);
|
|
278
|
+
expect(connection.last_error.code).to.equal('invalid_client');
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe('google mode', () => {
|
|
283
|
+
|
|
284
|
+
it('builds a Google authorization URL with offline access', async () => {
|
|
285
|
+
const result = await service.startAuthorization(PROJECT, 'user-1', {
|
|
286
|
+
mode: 'google',
|
|
287
|
+
server_url: SERVER_URL,
|
|
288
|
+
scopes: ['https://www.googleapis.com/auth/calendar.readonly']
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
const url = new URL(result.authorization_url);
|
|
292
|
+
expect(url.host).to.equal('accounts.google.com');
|
|
293
|
+
expect(url.searchParams.get('client_id')).to.equal('google-client-id');
|
|
294
|
+
expect(url.searchParams.get('access_type')).to.equal('offline');
|
|
295
|
+
expect(url.searchParams.get('prompt')).to.equal('consent');
|
|
296
|
+
expect(url.searchParams.get('include_granted_scopes')).to.equal('true');
|
|
297
|
+
expect(url.searchParams.get('scope')).to.equal('https://www.googleapis.com/auth/calendar.readonly');
|
|
298
|
+
expect(url.searchParams.has('resource')).to.equal(false);
|
|
299
|
+
const connection = await McpOAuthConnection.findById(result.connection_id);
|
|
300
|
+
expect(connection.client_id).to.equal(undefined);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('requires scopes for a Google MCP server without defaults', async () => {
|
|
304
|
+
const error = await rejection(service.startAuthorization(PROJECT, 'user-1', { mode: 'google', server_url: SERVER_URL }));
|
|
305
|
+
expect(error.status).to.equal(400);
|
|
306
|
+
expect(error.message).to.match(/scopes/);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('uses the documented scopes of a known Google MCP server when none are sent', async () => {
|
|
310
|
+
const result = await service.startAuthorization(PROJECT, 'user-1', {
|
|
311
|
+
mode: 'google',
|
|
312
|
+
server_url: 'https://calendarmcp.googleapis.com/mcp/v1'
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
const expected = [
|
|
316
|
+
'https://www.googleapis.com/auth/calendar.calendarlist.readonly',
|
|
317
|
+
'https://www.googleapis.com/auth/calendar.events.freebusy',
|
|
318
|
+
'https://www.googleapis.com/auth/calendar.events.readonly'
|
|
319
|
+
];
|
|
320
|
+
expect(new URL(result.authorization_url).searchParams.get('scope')).to.equal(expected.join(' '));
|
|
321
|
+
const connection = await McpOAuthConnection.findById(result.connection_id);
|
|
322
|
+
expect(connection.scopes).to.deep.equal(expected);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it('prefers the scopes sent over the defaults', async () => {
|
|
326
|
+
const result = await service.startAuthorization(PROJECT, 'user-1', {
|
|
327
|
+
mode: 'google',
|
|
328
|
+
server_url: 'https://calendarmcp.googleapis.com/mcp/v1',
|
|
329
|
+
scopes: ['https://www.googleapis.com/auth/calendar.events']
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
expect(new URL(result.authorization_url).searchParams.get('scope')).to.equal('https://www.googleapis.com/auth/calendar.events');
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('answers 503 when the Google client is not configured', async () => {
|
|
336
|
+
delete process.env.MCP_OAUTH_GOOGLE_CLIENT_SECRET;
|
|
337
|
+
try {
|
|
338
|
+
const error = await rejection(service.startAuthorization(PROJECT, 'user-1', { mode: 'google', server_url: SERVER_URL, scopes: ['openid'] }));
|
|
339
|
+
expect(error.status).to.equal(503);
|
|
340
|
+
} finally {
|
|
341
|
+
process.env.MCP_OAUTH_GOOGLE_CLIENT_SECRET = ENV.MCP_OAUTH_GOOGLE_CLIENT_SECRET;
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('keeps the refresh token when Google does not rotate it', async () => {
|
|
346
|
+
const connection = await seedActive({
|
|
347
|
+
mode: 'google',
|
|
348
|
+
client_id: undefined,
|
|
349
|
+
token_endpoint_auth_method: undefined,
|
|
350
|
+
token_endpoint: 'https://oauth2.googleapis.com/token',
|
|
351
|
+
revocation_endpoint: 'https://oauth2.googleapis.com/revoke'
|
|
352
|
+
});
|
|
353
|
+
let body;
|
|
354
|
+
nock('https://oauth2.googleapis.com').post('/token', (b) => { body = b; return true; })
|
|
355
|
+
.reply(200, { access_token: 'at-new', expires_in: 3599, token_type: 'Bearer' });
|
|
356
|
+
|
|
357
|
+
const token = await service.getAccessToken(PROJECT, connection._id);
|
|
358
|
+
|
|
359
|
+
expect(token.access_token).to.equal('at-new');
|
|
360
|
+
expect(body).to.include({ grant_type: 'refresh_token', refresh_token: 'rt-old', client_id: 'google-client-id', client_secret: 'google-client-secret' });
|
|
361
|
+
const saved = await McpOAuthConnection.findById(connection._id);
|
|
362
|
+
expect(mcpOAuthCrypto.decrypt(saved.refresh_token_enc)).to.equal('rt-old');
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
describe('manual mode', () => {
|
|
367
|
+
|
|
368
|
+
it('stores the client secret encrypted and uses client_secret_basic', async () => {
|
|
369
|
+
const result = await service.startAuthorization(PROJECT, 'user-1', {
|
|
370
|
+
mode: 'manual',
|
|
371
|
+
server_url: SERVER_URL,
|
|
372
|
+
manual: {
|
|
373
|
+
authorization_endpoint: AUTH_ORIGIN + '/authorize',
|
|
374
|
+
token_endpoint: AUTH_ORIGIN + '/token',
|
|
375
|
+
client_id: 'manual client',
|
|
376
|
+
client_secret: 's3cr:et',
|
|
377
|
+
token_endpoint_auth_method: 'client_secret_basic'
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
const connection = await McpOAuthConnection.findById(result.connection_id);
|
|
381
|
+
expect(connection.client_secret_enc).to.not.contain('s3cr');
|
|
382
|
+
expect(mcpOAuthCrypto.decrypt(connection.client_secret_enc)).to.equal('s3cr:et');
|
|
383
|
+
|
|
384
|
+
const state = new URL(result.authorization_url).searchParams.get('state');
|
|
385
|
+
nock(AUTH_ORIGIN, { reqheaders: { authorization: 'Basic ' + Buffer.from('manual%20client:s3cr%3Aet').toString('base64') } })
|
|
386
|
+
.post('/token', (body) => !body.client_secret && !body.client_id)
|
|
387
|
+
.reply(200, { access_token: 'at-1', refresh_token: 'rt-1', expires_in: 3600 });
|
|
388
|
+
|
|
389
|
+
const callback = await service.completeAuthorization({ code: 'c', state });
|
|
390
|
+
expect(callback.error).to.equal(undefined);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it('updates the scopes of an existing connection without sending the client again', async () => {
|
|
394
|
+
await service.startAuthorization(PROJECT, 'user-1', {
|
|
395
|
+
mode: 'manual',
|
|
396
|
+
server_url: SERVER_URL,
|
|
397
|
+
scopes: ['read'],
|
|
398
|
+
manual: {
|
|
399
|
+
authorization_endpoint: AUTH_ORIGIN + '/authorize',
|
|
400
|
+
token_endpoint: AUTH_ORIGIN + '/token',
|
|
401
|
+
revocation_endpoint: AUTH_ORIGIN + '/revoke',
|
|
402
|
+
client_id: 'manual-client',
|
|
403
|
+
client_secret: 's3cr:et'
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
const updated = await service.startAuthorization(PROJECT, 'user-1', {
|
|
408
|
+
mode: 'manual',
|
|
409
|
+
server_url: SERVER_URL,
|
|
410
|
+
scopes: ['read', 'write']
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
const connection = await McpOAuthConnection.findById(updated.connection_id);
|
|
414
|
+
expect(connection.scopes).to.deep.equal(['read', 'write']);
|
|
415
|
+
expect(connection.client_id).to.equal('manual-client');
|
|
416
|
+
expect(connection.authorization_endpoint).to.equal(AUTH_ORIGIN + '/authorize');
|
|
417
|
+
expect(connection.revocation_endpoint).to.equal(AUTH_ORIGIN + '/revoke');
|
|
418
|
+
expect(mcpOAuthCrypto.decrypt(connection.client_secret_enc)).to.equal('s3cr:et');
|
|
419
|
+
expect(new URL(updated.authorization_url).searchParams.get('scope')).to.equal('read write');
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('requires the manual endpoints', async () => {
|
|
423
|
+
const error = await rejection(service.startAuthorization(PROJECT, 'user-1', { mode: 'manual', server_url: SERVER_URL, manual: { client_id: 'x' } }));
|
|
424
|
+
expect(error.status).to.equal(400);
|
|
425
|
+
expect(error.message).to.contain('manual.authorization_endpoint');
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it('blocks a private token endpoint', async () => {
|
|
429
|
+
const error = await rejection(service.startAuthorization(PROJECT, 'user-1', {
|
|
430
|
+
mode: 'manual',
|
|
431
|
+
server_url: SERVER_URL,
|
|
432
|
+
manual: { authorization_endpoint: AUTH_ORIGIN + '/authorize', token_endpoint: 'https://10.0.0.3/token', client_id: 'x' }
|
|
433
|
+
}));
|
|
434
|
+
expect(error.status).to.equal(400);
|
|
435
|
+
expect(error.message).to.contain('private or reserved address');
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
describe('validation', () => {
|
|
440
|
+
|
|
441
|
+
it('rejects a return_url outside the allowed origins', async () => {
|
|
442
|
+
const error = await rejection(service.startAuthorization(PROJECT, 'user-1', {
|
|
443
|
+
mode: 'google', server_url: SERVER_URL, scopes: ['openid'], return_url: 'https://evil.example.com/x'
|
|
444
|
+
}));
|
|
445
|
+
expect(error.status).to.equal(400);
|
|
446
|
+
expect(error.message).to.equal('return_url is not allowed');
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it('rejects an unknown mode', async () => {
|
|
450
|
+
const error = await rejection(service.startAuthorization(PROJECT, 'user-1', { mode: 'other', server_url: SERVER_URL }));
|
|
451
|
+
expect(error.status).to.equal(400);
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
it('answers 503 without an encryption key', async () => {
|
|
455
|
+
delete process.env.MCP_OAUTH_ENCRYPTION_KEY;
|
|
456
|
+
try {
|
|
457
|
+
const error = await rejection(service.startAuthorization(PROJECT, 'user-1', { mode: 'google', server_url: SERVER_URL, scopes: ['openid'] }));
|
|
458
|
+
expect(error.status).to.equal(503);
|
|
459
|
+
expect(error.code).to.equal('not_configured');
|
|
460
|
+
} finally {
|
|
461
|
+
process.env.MCP_OAUTH_ENCRYPTION_KEY = ENV.MCP_OAUTH_ENCRYPTION_KEY;
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
describe('getAccessToken', () => {
|
|
467
|
+
|
|
468
|
+
it('returns a fresh token without calling the provider', async () => {
|
|
469
|
+
const connection = await seedActive({ expires_at: new Date(Date.now() + 3600 * 1000) });
|
|
470
|
+
|
|
471
|
+
const token = await service.getAccessToken(PROJECT, connection._id);
|
|
472
|
+
|
|
473
|
+
expect(token.access_token).to.equal('at-old');
|
|
474
|
+
expect(token.token_type).to.equal('Bearer');
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it('refreshes a token that expires within five minutes', async () => {
|
|
478
|
+
const connection = await seedActive();
|
|
479
|
+
let body;
|
|
480
|
+
mockToken({ body: { access_token: 'at-new', expires_in: 3600 } }, (b) => { body = b; });
|
|
481
|
+
|
|
482
|
+
const token = await service.getAccessToken(PROJECT, connection._id);
|
|
483
|
+
|
|
484
|
+
expect(token.access_token).to.equal('at-new');
|
|
485
|
+
expect(body).to.include({ grant_type: 'refresh_token', refresh_token: 'rt-old', client_id: 'manual-client' });
|
|
486
|
+
const saved = await McpOAuthConnection.findById(connection._id);
|
|
487
|
+
expect(saved.last_refresh_at.getTime()).to.be.closeTo(Date.now(), 5000);
|
|
488
|
+
expect(mcpOAuthCrypto.decrypt(saved.refresh_token_enc)).to.equal('rt-old');
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it('stores a rotated refresh token', async () => {
|
|
492
|
+
const connection = await seedActive();
|
|
493
|
+
mockToken({ body: { access_token: 'at-new', refresh_token: 'rt-new', expires_in: 3600 } });
|
|
494
|
+
|
|
495
|
+
await service.getAccessToken(PROJECT, connection._id);
|
|
496
|
+
|
|
497
|
+
const saved = await McpOAuthConnection.findById(connection._id);
|
|
498
|
+
expect(mcpOAuthCrypto.decrypt(saved.refresh_token_enc)).to.equal('rt-new');
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it('sends the resource on refresh in mcp mode', async () => {
|
|
502
|
+
const connection = await seedActive({ mode: 'mcp', resource: MCP_ORIGIN + '/' });
|
|
503
|
+
let body;
|
|
504
|
+
mockToken({ body: { access_token: 'at-new', expires_in: 3600 } }, (b) => { body = b; });
|
|
505
|
+
|
|
506
|
+
await service.getAccessToken(PROJECT, connection._id);
|
|
507
|
+
|
|
508
|
+
expect(body.resource).to.equal(MCP_ORIGIN + '/');
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it('marks the connection needs_reauth on invalid_grant and stops calling the provider', async () => {
|
|
512
|
+
const connection = await seedActive();
|
|
513
|
+
mockToken({ status: 400, body: { error: 'invalid_grant', error_description: 'Token revoked' } });
|
|
514
|
+
|
|
515
|
+
const first = await rejection(service.getAccessToken(PROJECT, connection._id));
|
|
516
|
+
const second = await rejection(service.getAccessToken(PROJECT, connection._id));
|
|
517
|
+
|
|
518
|
+
expect(first.status).to.equal(409);
|
|
519
|
+
expect(first.code).to.equal('needs_reauth');
|
|
520
|
+
expect(second.status).to.equal(409);
|
|
521
|
+
const saved = await McpOAuthConnection.findById(connection._id);
|
|
522
|
+
expect(saved.status).to.equal('needs_reauth');
|
|
523
|
+
expect(saved.last_error.code).to.equal('invalid_grant');
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it('drops the registered client on invalid_client in mcp mode', async () => {
|
|
527
|
+
const connection = await seedActive({ mode: 'mcp', resource: MCP_ORIGIN + '/' });
|
|
528
|
+
mockToken({ status: 401, body: { error: 'invalid_client' } });
|
|
529
|
+
|
|
530
|
+
const error = await rejection(service.getAccessToken(PROJECT, connection._id));
|
|
531
|
+
|
|
532
|
+
expect(error.status).to.equal(409);
|
|
533
|
+
const saved = await McpOAuthConnection.findById(connection._id);
|
|
534
|
+
expect(saved.client_id).to.equal(undefined);
|
|
535
|
+
expect(saved.status).to.equal('needs_reauth');
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it('answers 503 and keeps the connection active when the provider fails', async () => {
|
|
539
|
+
const connection = await seedActive();
|
|
540
|
+
mockToken({ status: 500, body: { error: 'server_error' } });
|
|
541
|
+
|
|
542
|
+
const error = await rejection(service.getAccessToken(PROJECT, connection._id));
|
|
543
|
+
|
|
544
|
+
expect(error.status).to.equal(503);
|
|
545
|
+
const saved = await McpOAuthConnection.findById(connection._id);
|
|
546
|
+
expect(saved.status).to.equal('active');
|
|
547
|
+
expect(saved.last_error.code).to.equal('server_error');
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it('marks needs_reauth when the token expired and there is no refresh token', async () => {
|
|
551
|
+
const connection = await seedActive({ refresh_token_enc: undefined, expires_at: new Date(Date.now() - 1000) });
|
|
552
|
+
|
|
553
|
+
const error = await rejection(service.getAccessToken(PROJECT, connection._id));
|
|
554
|
+
|
|
555
|
+
expect(error.status).to.equal(409);
|
|
556
|
+
expect((await McpOAuthConnection.findById(connection._id)).last_error.code).to.equal('no_refresh_token');
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
it('refreshes once for concurrent requests in one instance', async () => {
|
|
560
|
+
const connection = await seedActive();
|
|
561
|
+
mockToken({ body: { access_token: 'at-new', expires_in: 3600 } });
|
|
562
|
+
|
|
563
|
+
const tokens = await Promise.all([
|
|
564
|
+
service.getAccessToken(PROJECT, connection._id),
|
|
565
|
+
service.getAccessToken(PROJECT, connection._id),
|
|
566
|
+
service.getAccessToken(PROJECT, connection._id)
|
|
567
|
+
]);
|
|
568
|
+
|
|
569
|
+
tokens.forEach((token) => expect(token.access_token).to.equal('at-new'));
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
it('refreshes once for concurrent requests across instances sharing Redis', async () => {
|
|
573
|
+
const connection = await seedActive();
|
|
574
|
+
const other = new McpOAuthService();
|
|
575
|
+
other.setTdCache(tdCache);
|
|
576
|
+
nock(AUTH_ORIGIN).post('/token').delay(600).reply(200, { access_token: 'at-new', expires_in: 3600 });
|
|
577
|
+
|
|
578
|
+
const tokens = await Promise.all([
|
|
579
|
+
service.getAccessToken(PROJECT, connection._id),
|
|
580
|
+
other.getAccessToken(PROJECT, connection._id)
|
|
581
|
+
]);
|
|
582
|
+
|
|
583
|
+
expect(tokens.map((token) => token.access_token)).to.deep.equal(['at-new', 'at-new']);
|
|
584
|
+
expect(await tdCache.get('mcp_oauth:refresh_lock:' + connection._id)).to.equal(null);
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
it('does not serve a connection of another project', async () => {
|
|
588
|
+
const connection = await seedActive({ id_project: OTHER_PROJECT });
|
|
589
|
+
|
|
590
|
+
const error = await rejection(service.getAccessToken(PROJECT, connection._id));
|
|
591
|
+
|
|
592
|
+
expect(error.status).to.equal(404);
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
it('answers 404 for an invalid id', async () => {
|
|
596
|
+
const error = await rejection(service.getAccessToken(PROJECT, 'not-an-id'));
|
|
597
|
+
expect(error.status).to.equal(404);
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
describe('refreshDue', () => {
|
|
602
|
+
|
|
603
|
+
it('queries through the { status, expires_at } index', async () => {
|
|
604
|
+
await McpOAuthConnection.syncIndexes();
|
|
605
|
+
const plan = await McpOAuthConnection.find({
|
|
606
|
+
status: 'active',
|
|
607
|
+
refresh_token_enc: { $exists: true, $ne: null },
|
|
608
|
+
expires_at: { $ne: null, $lte: new Date() }
|
|
609
|
+
}).explain('queryPlanner');
|
|
610
|
+
expect(JSON.stringify(plan)).to.include('status_1_expires_at_1');
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it('refreshes only active connections expiring within the window, isolating failures', async () => {
|
|
614
|
+
const due = await seedActive({ server_url: MCP_ORIGIN + '/a', token_endpoint: AUTH_ORIGIN + '/token-a' });
|
|
615
|
+
const failing = await seedActive({ server_url: MCP_ORIGIN + '/b', token_endpoint: AUTH_ORIGIN + '/token-b' });
|
|
616
|
+
const revoked = await seedActive({ server_url: MCP_ORIGIN + '/c', token_endpoint: AUTH_ORIGIN + '/token-c' });
|
|
617
|
+
await seedActive({ server_url: MCP_ORIGIN + '/d', expires_at: new Date(Date.now() + 3 * 3600 * 1000) });
|
|
618
|
+
await seedActive({ server_url: MCP_ORIGIN + '/e', status: 'needs_reauth' });
|
|
619
|
+
await seedActive({ server_url: MCP_ORIGIN + '/f', refresh_token_enc: undefined });
|
|
620
|
+
nock(AUTH_ORIGIN)
|
|
621
|
+
.post('/token-a').reply(200, { access_token: 'at-a', expires_in: 3600 })
|
|
622
|
+
.post('/token-b').reply(502, 'Bad Gateway')
|
|
623
|
+
.post('/token-c').reply(400, { error: 'invalid_grant' });
|
|
624
|
+
|
|
625
|
+
const result = await service.refreshDue(900);
|
|
626
|
+
|
|
627
|
+
expect(result).to.deep.equal({ refreshed: 1, failed: 1, needs_reauth: 1 });
|
|
628
|
+
expect(mcpOAuthCrypto.decrypt((await McpOAuthConnection.findById(due._id)).access_token_enc)).to.equal('at-a');
|
|
629
|
+
expect((await McpOAuthConnection.findById(failing._id)).status).to.equal('active');
|
|
630
|
+
expect((await McpOAuthConnection.findById(revoked._id)).status).to.equal('needs_reauth');
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
it('refreshes one connection by id', async () => {
|
|
634
|
+
const connection = await seedActive({ expires_at: new Date(Date.now() + 3600 * 1000) });
|
|
635
|
+
mockToken({ body: { access_token: 'at-forced', expires_in: 3600 } });
|
|
636
|
+
|
|
637
|
+
const token = await service.refreshConnection(connection._id.toString());
|
|
638
|
+
|
|
639
|
+
expect(token.access_token).to.equal('at-forced');
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
describe('revokeAndDelete', () => {
|
|
644
|
+
|
|
645
|
+
it('revokes the tokens and deletes the connection', async () => {
|
|
646
|
+
const connection = await seedActive();
|
|
647
|
+
const hints = [];
|
|
648
|
+
nock(AUTH_ORIGIN)
|
|
649
|
+
.post('/revoke', (body) => { hints.push([body.token_type_hint, body.token]); return true; }).reply(200)
|
|
650
|
+
.post('/revoke', (body) => { hints.push([body.token_type_hint, body.token]); return true; }).reply(200);
|
|
651
|
+
|
|
652
|
+
const result = await service.revokeAndDelete(PROJECT, connection._id);
|
|
653
|
+
|
|
654
|
+
expect(result).to.deep.equal({ revoked: true });
|
|
655
|
+
expect(hints).to.deep.equal([['refresh_token', 'rt-old'], ['access_token', 'at-old']]);
|
|
656
|
+
expect(await McpOAuthConnection.findById(connection._id)).to.equal(null);
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
it('deletes the connection even when revocation fails', async () => {
|
|
660
|
+
const connection = await seedActive();
|
|
661
|
+
nock(AUTH_ORIGIN).post('/revoke').reply(500).post('/revoke').reply(500);
|
|
662
|
+
|
|
663
|
+
const result = await service.revokeAndDelete(PROJECT, connection._id);
|
|
664
|
+
|
|
665
|
+
expect(result).to.deep.equal({ revoked: false });
|
|
666
|
+
expect(await McpOAuthConnection.findById(connection._id)).to.equal(null);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it('does not delete a connection of another project', async () => {
|
|
670
|
+
const connection = await seedActive({ id_project: OTHER_PROJECT });
|
|
671
|
+
|
|
672
|
+
const error = await rejection(service.revokeAndDelete(PROJECT, connection._id));
|
|
673
|
+
|
|
674
|
+
expect(error.status).to.equal(404);
|
|
675
|
+
expect(await McpOAuthConnection.findById(connection._id)).to.not.equal(null);
|
|
676
|
+
});
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
describe('without Redis', () => {
|
|
680
|
+
|
|
681
|
+
it('keeps states single use in memory', async () => {
|
|
682
|
+
const memoryService = new McpOAuthService();
|
|
683
|
+
const { authorization_url } = await memoryService.startAuthorization(PROJECT, 'user-1', { mode: 'google', server_url: SERVER_URL, scopes: ['openid'] });
|
|
684
|
+
const state = new URL(authorization_url).searchParams.get('state');
|
|
685
|
+
nock('https://oauth2.googleapis.com').post('/token').reply(200, { access_token: 'at-1', expires_in: 3600 });
|
|
686
|
+
|
|
687
|
+
await memoryService.completeAuthorization({ code: 'c', state });
|
|
688
|
+
const error = await rejection(memoryService.completeAuthorization({ code: 'c', state }));
|
|
689
|
+
|
|
690
|
+
expect(error.status).to.equal(400);
|
|
691
|
+
});
|
|
692
|
+
});
|
|
693
|
+
});
|