@tiledesk/tiledesk-server 3.3.5 → 3.5.1
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 +20 -0
- package/app.js +7 -0
- package/deploynew.sh +7 -1
- package/docs/activities-client.md +27 -0
- package/docs/mcp-oauth2-it.md +279 -0
- package/docs/mcp-oauth2.md +279 -0
- package/docs/numeric-input-validation.md +91 -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/models/request.js +8 -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 +39 -11
- package/pubmodules/activities/test/execution-filter.test.js +133 -0
- package/pubmodules/activities/test/execution-messages.test.js +158 -0
- package/pubmodules/canned/cannedResponseRoute.js +6 -9
- package/pubmodules/events/eventRoute.js +6 -9
- 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/answered.js +7 -3
- package/routes/campaigns.js +7 -3
- package/routes/department.js +14 -3
- package/routes/executions-refire.js +148 -0
- package/routes/faq.js +6 -14
- package/routes/filesp.js +9 -4
- package/routes/kb.js +40 -15
- package/routes/lead.js +23 -28
- package/routes/logs.js +8 -1
- package/routes/mcp.js +11 -8
- package/routes/mcpOAuth.js +133 -0
- package/routes/mcpOAuthCallback.js +75 -0
- package/routes/message.js +3 -1
- package/routes/messagesRoot.js +5 -18
- package/routes/project.js +57 -12
- package/routes/project_user.js +21 -4
- package/routes/property.js +18 -20
- package/routes/request.js +36 -45
- package/routes/roles.js +8 -18
- package/routes/segment.js +8 -18
- package/routes/subscription.js +4 -5
- package/routes/tag.js +6 -9
- package/routes/unanswered.js +7 -3
- package/routes/user-request.js +29 -34
- 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/kbRoute.js +4 -7
- 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/numericInput.js +137 -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/numericInput.js +200 -0
- package/utils/outboundUrlGuard.js +131 -0
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const dns = require('dns');
|
|
5
|
+
const jwt = require('jsonwebtoken');
|
|
6
|
+
const nock = require('nock');
|
|
7
|
+
const chai = require('chai');
|
|
8
|
+
const chaiHttp = require('chai-http');
|
|
9
|
+
const expect = chai.expect;
|
|
10
|
+
|
|
11
|
+
const projectService = require('../services/projectService');
|
|
12
|
+
const userService = require('../services/userService');
|
|
13
|
+
const faqService = require('../services/faqService');
|
|
14
|
+
const Project_user = require('../models/project_user');
|
|
15
|
+
const RoleConstants = require('../models/roleConstants');
|
|
16
|
+
const Integration = require('../models/integrations');
|
|
17
|
+
const McpOAuthConnection = require('../models/mcpOAuthConnection');
|
|
18
|
+
const mcpOAuthCrypto = require('../utils/mcpOAuthCrypto');
|
|
19
|
+
const server = require('../app');
|
|
20
|
+
|
|
21
|
+
chai.use(chaiHttp);
|
|
22
|
+
|
|
23
|
+
const AUTH_ORIGIN = 'https://auth.example.com';
|
|
24
|
+
const SERVER_URL = 'https://mcp.example.com/mcp';
|
|
25
|
+
const GOOGLE_SERVER_URL = 'https://calendar.example.com/mcp';
|
|
26
|
+
const GOOGLE_SCOPE = 'https://www.googleapis.com/auth/calendar';
|
|
27
|
+
const RETURN_URL = 'https://panel.tiledesk.test/#/project/p1/integrations';
|
|
28
|
+
const REFRESH_TOKEN = 'scheduler-secret-token';
|
|
29
|
+
|
|
30
|
+
const ENV = {
|
|
31
|
+
MCP_OAUTH_ENCRYPTION_KEY: crypto.randomBytes(32).toString('base64'),
|
|
32
|
+
MCP_OAUTH_REDIRECT_BASE_URL: 'https://api.tiledesk.test/',
|
|
33
|
+
MCP_OAUTH_RETURN_ORIGINS: 'https://panel.tiledesk.test',
|
|
34
|
+
MCP_OAUTH_GOOGLE_CLIENT_ID: 'google-client-id',
|
|
35
|
+
MCP_OAUTH_GOOGLE_CLIENT_SECRET: 'google-client-secret',
|
|
36
|
+
MCP_OAUTH_REFRESH_TOKEN: REFRESH_TOKEN,
|
|
37
|
+
MCP_OAUTH_ALLOW_INSECURE_URLS: undefined
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
async function createProjectUser(role) {
|
|
41
|
+
const email = 'test-mcp-oauth-' + Date.now() + '-' + Math.random().toString(36).slice(2) + '@email.com';
|
|
42
|
+
const pwd = 'pwd';
|
|
43
|
+
const user = await userService.signup(email, pwd, 'Test Firstname', 'Test lastname');
|
|
44
|
+
const project = await projectService.create('test-mcp-oauth', user._id);
|
|
45
|
+
if (role) {
|
|
46
|
+
await Project_user.findOneAndUpdate({ id_project: project._id, id_user: user._id }, { role: role });
|
|
47
|
+
}
|
|
48
|
+
return { email, pwd, user, project, projectId: project._id.toString() };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function botToken(ctx) {
|
|
52
|
+
const bot = await faqService.create(ctx.project._id, ctx.user._id, { name: 'mcp-oauth-bot' });
|
|
53
|
+
return jwt.sign(bot.toObject(), bot.secret, {
|
|
54
|
+
subject: 'bot',
|
|
55
|
+
audience: 'https://tiledesk.com/bots/' + bot._id
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function seedActive(projectId, overrides = {}) {
|
|
60
|
+
return McpOAuthConnection.create({
|
|
61
|
+
id_project: projectId,
|
|
62
|
+
mode: 'manual',
|
|
63
|
+
server_url: SERVER_URL,
|
|
64
|
+
name: 'Example',
|
|
65
|
+
status: 'active',
|
|
66
|
+
client_id: 'manual-client',
|
|
67
|
+
token_endpoint_auth_method: 'none',
|
|
68
|
+
authorization_endpoint: AUTH_ORIGIN + '/authorize',
|
|
69
|
+
token_endpoint: AUTH_ORIGIN + '/token',
|
|
70
|
+
revocation_endpoint: AUTH_ORIGIN + '/revoke',
|
|
71
|
+
access_token_enc: mcpOAuthCrypto.encrypt('at-current'),
|
|
72
|
+
refresh_token_enc: mcpOAuthCrypto.encrypt('rt-current'),
|
|
73
|
+
token_type: 'Bearer',
|
|
74
|
+
expires_at: new Date(Date.now() + 3600 * 1000),
|
|
75
|
+
...overrides
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function mockToken(body) {
|
|
80
|
+
return nock(AUTH_ORIGIN).post('/token').reply(200, body);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function expectNoSecrets(connection) {
|
|
84
|
+
['access_token_enc', 'refresh_token_enc', 'client_secret_enc', 'client_id', 'access_token', 'refresh_token']
|
|
85
|
+
.forEach((field) => expect(connection).to.not.have.property(field));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
describe('MCP OAuth routes', function () {
|
|
89
|
+
this.timeout(30000);
|
|
90
|
+
|
|
91
|
+
const savedEnv = {};
|
|
92
|
+
const projectIds = [];
|
|
93
|
+
let originalLookup;
|
|
94
|
+
let admin;
|
|
95
|
+
|
|
96
|
+
function setEnv(values) {
|
|
97
|
+
Object.keys(values).forEach((key) => {
|
|
98
|
+
if (values[key] === undefined) {
|
|
99
|
+
delete process.env[key];
|
|
100
|
+
} else {
|
|
101
|
+
process.env[key] = values[key];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
before(async () => {
|
|
107
|
+
Object.keys(ENV).forEach((key) => { savedEnv[key] = process.env[key]; });
|
|
108
|
+
setEnv(ENV);
|
|
109
|
+
admin = await createProjectUser();
|
|
110
|
+
projectIds.push(admin.projectId);
|
|
111
|
+
nock.disableNetConnect();
|
|
112
|
+
nock.enableNetConnect(/(127\.0\.0\.1|localhost)/);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
after(async () => {
|
|
116
|
+
nock.cleanAll();
|
|
117
|
+
nock.enableNetConnect();
|
|
118
|
+
await McpOAuthConnection.deleteMany({ id_project: { $in: projectIds } });
|
|
119
|
+
setEnv(savedEnv);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
beforeEach(async () => {
|
|
123
|
+
setEnv(ENV);
|
|
124
|
+
originalLookup = dns.promises.lookup;
|
|
125
|
+
dns.promises.lookup = async () => [{ address: '93.184.216.34', family: 4 }];
|
|
126
|
+
await McpOAuthConnection.deleteMany({ id_project: { $in: projectIds } });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
afterEach(() => {
|
|
130
|
+
dns.promises.lookup = originalLookup;
|
|
131
|
+
const pending = nock.pendingMocks();
|
|
132
|
+
nock.cleanAll();
|
|
133
|
+
expect(pending, 'unused HTTP mocks').to.deep.equal([]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('admin authorization flow', () => {
|
|
137
|
+
it('authorizes a Google connection and completes it through the callback', async () => {
|
|
138
|
+
const authorize = await chai.request(server)
|
|
139
|
+
.post('/' + admin.projectId + '/mcp/oauth/authorize')
|
|
140
|
+
.auth(admin.email, admin.pwd)
|
|
141
|
+
.send({ mode: 'google', server_url: GOOGLE_SERVER_URL, scopes: [GOOGLE_SCOPE], return_url: RETURN_URL });
|
|
142
|
+
|
|
143
|
+
expect(authorize.status).to.equal(200);
|
|
144
|
+
expect(authorize.body.success).to.equal(true);
|
|
145
|
+
const authorizationUrl = new URL(authorize.body.authorization_url);
|
|
146
|
+
expect(authorizationUrl.origin).to.equal('https://accounts.google.com');
|
|
147
|
+
expect(authorizationUrl.searchParams.get('redirect_uri')).to.equal('https://api.tiledesk.test/oauth2/mcp/callback');
|
|
148
|
+
const state = authorizationUrl.searchParams.get('state');
|
|
149
|
+
const connectionId = authorize.body.connection_id;
|
|
150
|
+
|
|
151
|
+
nock('https://oauth2.googleapis.com').post('/token').reply(200, {
|
|
152
|
+
access_token: 'google-at', refresh_token: 'google-rt', token_type: 'Bearer', expires_in: 3599,
|
|
153
|
+
scope: GOOGLE_SCOPE
|
|
154
|
+
});
|
|
155
|
+
const callback = await chai.request(server)
|
|
156
|
+
.get('/oauth2/mcp/callback')
|
|
157
|
+
.query({ code: 'auth-code', state: state })
|
|
158
|
+
.redirects(0);
|
|
159
|
+
|
|
160
|
+
expect(callback.status).to.equal(302);
|
|
161
|
+
expect(callback.headers['referrer-policy']).to.equal('no-referrer');
|
|
162
|
+
expect(callback.headers.location).to.equal(RETURN_URL + '?mcp_oauth=success&connection_id=' + connectionId);
|
|
163
|
+
|
|
164
|
+
const list = await chai.request(server)
|
|
165
|
+
.get('/' + admin.projectId + '/mcp/oauth/connections')
|
|
166
|
+
.auth(admin.email, admin.pwd);
|
|
167
|
+
expect(list.status).to.equal(200);
|
|
168
|
+
expect(list.body).to.have.length(1);
|
|
169
|
+
expect(list.body[0]._id).to.equal(connectionId);
|
|
170
|
+
expect(list.body[0].status).to.equal('active');
|
|
171
|
+
expect(list.body[0].mode).to.equal('google');
|
|
172
|
+
expectNoSecrets(list.body[0]);
|
|
173
|
+
|
|
174
|
+
const replay = await chai.request(server)
|
|
175
|
+
.get('/oauth2/mcp/callback')
|
|
176
|
+
.query({ code: 'auth-code', state: state })
|
|
177
|
+
.redirects(0);
|
|
178
|
+
expect(replay.status).to.equal(400);
|
|
179
|
+
expect(replay.text).to.contain('invalid or has expired');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('redirects with the provider error when the user denies consent', async () => {
|
|
183
|
+
const authorize = await chai.request(server)
|
|
184
|
+
.post('/' + admin.projectId + '/mcp/oauth/authorize')
|
|
185
|
+
.auth(admin.email, admin.pwd)
|
|
186
|
+
.send({ mode: 'google', server_url: GOOGLE_SERVER_URL, scopes: [GOOGLE_SCOPE], return_url: RETURN_URL });
|
|
187
|
+
const state = new URL(authorize.body.authorization_url).searchParams.get('state');
|
|
188
|
+
|
|
189
|
+
const callback = await chai.request(server)
|
|
190
|
+
.get('/oauth2/mcp/callback')
|
|
191
|
+
.query({ error: 'access_denied', state: state })
|
|
192
|
+
.redirects(0);
|
|
193
|
+
|
|
194
|
+
expect(callback.status).to.equal(302);
|
|
195
|
+
const location = callback.headers.location;
|
|
196
|
+
expect(location).to.contain('mcp_oauth=error');
|
|
197
|
+
expect(location).to.contain('error=access_denied');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('shows a page instead of redirecting when no return_url was given', async () => {
|
|
201
|
+
const authorize = await chai.request(server)
|
|
202
|
+
.post('/' + admin.projectId + '/mcp/oauth/authorize')
|
|
203
|
+
.auth(admin.email, admin.pwd)
|
|
204
|
+
.send({ mode: 'google', server_url: GOOGLE_SERVER_URL, scopes: [GOOGLE_SCOPE] });
|
|
205
|
+
const state = new URL(authorize.body.authorization_url).searchParams.get('state');
|
|
206
|
+
|
|
207
|
+
nock('https://oauth2.googleapis.com').post('/token').reply(200, {
|
|
208
|
+
access_token: 'google-at', refresh_token: 'google-rt', token_type: 'Bearer', expires_in: 3599
|
|
209
|
+
});
|
|
210
|
+
const callback = await chai.request(server)
|
|
211
|
+
.get('/oauth2/mcp/callback')
|
|
212
|
+
.query({ code: 'auth-code', state: state })
|
|
213
|
+
.redirects(0);
|
|
214
|
+
|
|
215
|
+
expect(callback.status).to.equal(200);
|
|
216
|
+
expect(callback.text).to.contain('Connected');
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('rejects a return_url outside the allowed origins', async () => {
|
|
220
|
+
const res = await chai.request(server)
|
|
221
|
+
.post('/' + admin.projectId + '/mcp/oauth/authorize')
|
|
222
|
+
.auth(admin.email, admin.pwd)
|
|
223
|
+
.send({ mode: 'google', server_url: GOOGLE_SERVER_URL, return_url: 'https://evil.example.com/' });
|
|
224
|
+
expect(res.status).to.equal(400);
|
|
225
|
+
expect(res.body.success).to.equal(false);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('forbids agents from managing connections', async () => {
|
|
229
|
+
const agent = await createProjectUser(RoleConstants.AGENT);
|
|
230
|
+
projectIds.push(agent.projectId);
|
|
231
|
+
|
|
232
|
+
const authorize = await chai.request(server)
|
|
233
|
+
.post('/' + agent.projectId + '/mcp/oauth/authorize')
|
|
234
|
+
.auth(agent.email, agent.pwd)
|
|
235
|
+
.send({ mode: 'google', server_url: GOOGLE_SERVER_URL });
|
|
236
|
+
expect(authorize.status).to.equal(403);
|
|
237
|
+
|
|
238
|
+
const list = await chai.request(server)
|
|
239
|
+
.get('/' + agent.projectId + '/mcp/oauth/connections')
|
|
240
|
+
.auth(agent.email, agent.pwd);
|
|
241
|
+
expect(list.status).to.equal(403);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe('token endpoint', () => {
|
|
246
|
+
it('returns the token to a chatbot of the project', async () => {
|
|
247
|
+
const connection = await seedActive(admin.projectId);
|
|
248
|
+
const token = await botToken(admin);
|
|
249
|
+
|
|
250
|
+
const res = await chai.request(server)
|
|
251
|
+
.get('/' + admin.projectId + '/mcp/oauth/connections/' + connection._id + '/token')
|
|
252
|
+
.set('Authorization', 'JWT ' + token);
|
|
253
|
+
|
|
254
|
+
expect(res.status).to.equal(200);
|
|
255
|
+
expect(res.headers['cache-control']).to.equal('no-store');
|
|
256
|
+
expect(res.body.access_token).to.equal('at-current');
|
|
257
|
+
expect(res.body.token_type).to.equal('Bearer');
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('refreshes an expiring token before returning it', async () => {
|
|
261
|
+
const connection = await seedActive(admin.projectId, { expires_at: new Date(Date.now() + 30 * 1000) });
|
|
262
|
+
const token = await botToken(admin);
|
|
263
|
+
mockToken({ access_token: 'at-new', token_type: 'Bearer', expires_in: 3600 });
|
|
264
|
+
|
|
265
|
+
const res = await chai.request(server)
|
|
266
|
+
.get('/' + admin.projectId + '/mcp/oauth/connections/' + connection._id + '/token')
|
|
267
|
+
.set('Authorization', 'JWT ' + token);
|
|
268
|
+
|
|
269
|
+
expect(res.status).to.equal(200);
|
|
270
|
+
expect(res.body.access_token).to.equal('at-new');
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('refuses dashboard users, admins included', async () => {
|
|
274
|
+
const connection = await seedActive(admin.projectId);
|
|
275
|
+
|
|
276
|
+
const res = await chai.request(server)
|
|
277
|
+
.get('/' + admin.projectId + '/mcp/oauth/connections/' + connection._id + '/token')
|
|
278
|
+
.auth(admin.email, admin.pwd);
|
|
279
|
+
|
|
280
|
+
expect(res.status).to.equal(403);
|
|
281
|
+
expect(res.body).to.not.have.property('access_token');
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('does not serve a connection of another project', async () => {
|
|
285
|
+
const other = await createProjectUser();
|
|
286
|
+
projectIds.push(other.projectId);
|
|
287
|
+
const connection = await seedActive(admin.projectId);
|
|
288
|
+
const token = await botToken(other);
|
|
289
|
+
|
|
290
|
+
const res = await chai.request(server)
|
|
291
|
+
.get('/' + other.projectId + '/mcp/oauth/connections/' + connection._id + '/token')
|
|
292
|
+
.set('Authorization', 'JWT ' + token);
|
|
293
|
+
|
|
294
|
+
expect(res.status).to.equal(404);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('answers 409 when the connection needs a new authorization', async () => {
|
|
298
|
+
const connection = await seedActive(admin.projectId, { status: 'needs_reauth' });
|
|
299
|
+
const token = await botToken(admin);
|
|
300
|
+
|
|
301
|
+
const res = await chai.request(server)
|
|
302
|
+
.get('/' + admin.projectId + '/mcp/oauth/connections/' + connection._id + '/token')
|
|
303
|
+
.set('Authorization', 'JWT ' + token);
|
|
304
|
+
|
|
305
|
+
expect(res.status).to.equal(409);
|
|
306
|
+
expect(res.body.code).to.equal('needs_reauth');
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
describe('connection management', () => {
|
|
311
|
+
it('gets a single connection without secrets', async () => {
|
|
312
|
+
const connection = await seedActive(admin.projectId);
|
|
313
|
+
|
|
314
|
+
const res = await chai.request(server)
|
|
315
|
+
.get('/' + admin.projectId + '/mcp/oauth/connections/' + connection._id)
|
|
316
|
+
.auth(admin.email, admin.pwd);
|
|
317
|
+
|
|
318
|
+
expect(res.status).to.equal(200);
|
|
319
|
+
expect(res.body._id).to.equal(connection._id.toString());
|
|
320
|
+
expectNoSecrets(res.body);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('forces a refresh from the dashboard', async () => {
|
|
324
|
+
const connection = await seedActive(admin.projectId);
|
|
325
|
+
mockToken({ access_token: 'at-forced', token_type: 'Bearer', expires_in: 7200 });
|
|
326
|
+
|
|
327
|
+
const res = await chai.request(server)
|
|
328
|
+
.post('/' + admin.projectId + '/mcp/oauth/connections/' + connection._id + '/refresh')
|
|
329
|
+
.auth(admin.email, admin.pwd);
|
|
330
|
+
|
|
331
|
+
expect(res.status).to.equal(200);
|
|
332
|
+
expect(res.body.status).to.equal('active');
|
|
333
|
+
expect(new Date(res.body.expires_at).getTime()).to.be.greaterThan(Date.now() + 7000 * 1000);
|
|
334
|
+
expectNoSecrets(res.body);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('deletes a connection and detaches it from the MCP integration', async () => {
|
|
338
|
+
const connection = await seedActive(admin.projectId);
|
|
339
|
+
await Integration.deleteMany({ id_project: admin.projectId, name: 'mcp' });
|
|
340
|
+
await Integration.create({
|
|
341
|
+
id_project: admin.projectId,
|
|
342
|
+
name: 'mcp',
|
|
343
|
+
value: {
|
|
344
|
+
servers: [
|
|
345
|
+
{ name: 'Example', url: SERVER_URL, oauth: { connection_id: connection._id.toString(), mode: 'manual' } },
|
|
346
|
+
{ name: 'Other', url: 'https://other.example.com/mcp' }
|
|
347
|
+
]
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
nock(AUTH_ORIGIN).post('/revoke').times(2).reply(200, {});
|
|
351
|
+
|
|
352
|
+
const res = await chai.request(server)
|
|
353
|
+
.delete('/' + admin.projectId + '/mcp/oauth/connections/' + connection._id)
|
|
354
|
+
.auth(admin.email, admin.pwd);
|
|
355
|
+
|
|
356
|
+
expect(res.status).to.equal(200);
|
|
357
|
+
expect(res.body).to.deep.equal({ success: true, revoked: true });
|
|
358
|
+
expect(await McpOAuthConnection.findById(connection._id)).to.equal(null);
|
|
359
|
+
|
|
360
|
+
const integration = await Integration.findOne({ id_project: admin.projectId, name: 'mcp' });
|
|
361
|
+
expect(integration.value.servers).to.have.length(2);
|
|
362
|
+
expect(integration.value.servers[0]).to.deep.equal({ name: 'Example', url: SERVER_URL });
|
|
363
|
+
expect(integration.value.servers[1].name).to.equal('Other');
|
|
364
|
+
|
|
365
|
+
const again = await chai.request(server)
|
|
366
|
+
.get('/' + admin.projectId + '/mcp/oauth/connections/' + connection._id)
|
|
367
|
+
.auth(admin.email, admin.pwd);
|
|
368
|
+
expect(again.status).to.equal(404);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
describe('scheduler refresh webhook', () => {
|
|
373
|
+
it('refreshes one connection', async () => {
|
|
374
|
+
const connection = await seedActive(admin.projectId);
|
|
375
|
+
mockToken({ access_token: 'at-scheduled', token_type: 'Bearer', expires_in: 3600 });
|
|
376
|
+
|
|
377
|
+
const res = await chai.request(server)
|
|
378
|
+
.post('/webhook/mcp-oauth/refresh')
|
|
379
|
+
.set('x-auth-token', REFRESH_TOKEN)
|
|
380
|
+
.send({ connection_id: connection._id.toString() });
|
|
381
|
+
|
|
382
|
+
expect(res.status).to.equal(200);
|
|
383
|
+
expect(res.body.success).to.equal(true);
|
|
384
|
+
expect(res.body.connection_id).to.equal(connection._id.toString());
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it('refreshes every connection due within the window', async () => {
|
|
388
|
+
await seedActive(admin.projectId, { expires_at: new Date(Date.now() + 120 * 1000) });
|
|
389
|
+
await seedActive(admin.projectId, { server_url: 'https://later.example.com/mcp', expires_at: new Date(Date.now() + 5 * 3600 * 1000) });
|
|
390
|
+
mockToken({ access_token: 'at-due', token_type: 'Bearer', expires_in: 3600 });
|
|
391
|
+
|
|
392
|
+
const res = await chai.request(server)
|
|
393
|
+
.post('/webhook/mcp-oauth/refresh')
|
|
394
|
+
.set('x-auth-token', REFRESH_TOKEN)
|
|
395
|
+
.send({});
|
|
396
|
+
|
|
397
|
+
expect(res.status).to.equal(200);
|
|
398
|
+
expect(res.body).to.deep.equal({ success: true, refreshed: 1, failed: 0, needs_reauth: 0 });
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it('rejects a wrong token', async () => {
|
|
402
|
+
const res = await chai.request(server)
|
|
403
|
+
.post('/webhook/mcp-oauth/refresh')
|
|
404
|
+
.set('x-auth-token', 'wrong')
|
|
405
|
+
.send({});
|
|
406
|
+
expect(res.status).to.equal(401);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it('validates within_seconds', async () => {
|
|
410
|
+
for (const within of [10, 100000, 'abc', 90.5]) {
|
|
411
|
+
const res = await chai.request(server)
|
|
412
|
+
.post('/webhook/mcp-oauth/refresh')
|
|
413
|
+
.set('x-auth-token', REFRESH_TOKEN)
|
|
414
|
+
.send({ within_seconds: within });
|
|
415
|
+
expect(res.status, 'within_seconds ' + within).to.equal(400);
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('answers 404 for an unknown connection', async () => {
|
|
420
|
+
const res = await chai.request(server)
|
|
421
|
+
.post('/webhook/mcp-oauth/refresh')
|
|
422
|
+
.set('x-auth-token', REFRESH_TOKEN)
|
|
423
|
+
.send({ connection_id: '5f0000000000000000000000' });
|
|
424
|
+
expect(res.status).to.equal(404);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it('is unavailable without the scheduler token', async () => {
|
|
428
|
+
delete process.env.MCP_OAUTH_REFRESH_TOKEN;
|
|
429
|
+
const res = await chai.request(server)
|
|
430
|
+
.post('/webhook/mcp-oauth/refresh')
|
|
431
|
+
.set('x-auth-token', REFRESH_TOKEN)
|
|
432
|
+
.send({});
|
|
433
|
+
expect(res.status).to.equal(503);
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
describe('without MCP_OAUTH_ENCRYPTION_KEY', () => {
|
|
438
|
+
beforeEach(() => {
|
|
439
|
+
delete process.env.MCP_OAUTH_ENCRYPTION_KEY;
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it('answers 503 on every route', async () => {
|
|
443
|
+
const token = await botToken(admin);
|
|
444
|
+
const base = '/' + admin.projectId + '/mcp/oauth';
|
|
445
|
+
const responses = [
|
|
446
|
+
await chai.request(server).post(base + '/authorize').auth(admin.email, admin.pwd).send({ mode: 'google', server_url: GOOGLE_SERVER_URL }),
|
|
447
|
+
await chai.request(server).get(base + '/connections').auth(admin.email, admin.pwd),
|
|
448
|
+
await chai.request(server).get(base + '/connections/5f0000000000000000000000/token').set('Authorization', 'JWT ' + token),
|
|
449
|
+
await chai.request(server).get('/oauth2/mcp/callback').query({ code: 'c', state: 's' }).redirects(0),
|
|
450
|
+
await chai.request(server).post('/webhook/mcp-oauth/refresh').set('x-auth-token', REFRESH_TOKEN).send({})
|
|
451
|
+
];
|
|
452
|
+
responses.forEach((res) => expect(res.status).to.equal(503));
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
});
|