-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMcpOAuthController.php
More file actions
1018 lines (871 loc) · 38.9 KB
/
McpOAuthController.php
File metadata and controls
1018 lines (871 loc) · 38.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace DreamFactory\Core\McpServer\Http\Controllers;
use DreamFactory\Core\Http\Controllers\Controller;
use DreamFactory\Core\McpServer\Models\McpOAuthAccessToken;
use DreamFactory\Core\McpServer\Models\McpOAuthAuthorizationCode;
use DreamFactory\Core\McpServer\Models\McpOAuthClient;
use DreamFactory\Core\McpServer\Models\McpServerConfig;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
/**
* OAuth 2.0 Authorization Server for MCP
*
* Implements OAuth 2.0 Authorization Code flow with PKCE support.
*/
class McpOAuthController extends Controller
{
private string $dfUrl;
public function __construct()
{
// Internal API URL for server-to-server calls (not proxied)
$this->dfUrl = rtrim(config('app.url', env('DF_URL', 'http://localhost')), '/');
}
/**
* Get frontend URL for login redirects
*
* Uses the request's base URL (respecting X-Forwarded-* headers from proxies)
* combined with the frontend path. Can be overridden with DF_FRONTEND_URL.
*/
private function getFrontendUrl(Request $request): string
{
$frontendUrl = env('DF_FRONTEND_URL');
if ($frontendUrl) {
return rtrim($frontendUrl, '/');
}
$baseUrl = $this->getBaseUrl($request);
$frontendPath = env('DF_FRONTEND_PATH', '/dreamfactory/dist/#');
return rtrim($baseUrl . $frontendPath, '/');
}
/**
* OAuth Discovery: Protected Resource Metadata (RFC 9728)
* GET /mcp/{service}/.well-known/oauth-protected-resource
*/
public function protectedResourceMetadata(Request $request, string $mcpService)
{
$baseUrl = $this->getBaseUrl($request);
return response()->json([
'resource' => "{$baseUrl}/mcp/{$mcpService}",
'authorization_servers' => ["{$baseUrl}/mcp/{$mcpService}"],
'bearer_methods_supported' => ['header'],
]);
}
/**
* OAuth Discovery: Authorization Server Metadata (RFC 8414)
* GET /mcp/{service}/.well-known/oauth-authorization-server
*/
public function authorizationServerMetadata(Request $request, string $mcpService)
{
$baseUrl = $this->getBaseUrl($request);
return response()->json([
'issuer' => "{$baseUrl}/mcp/{$mcpService}",
'authorization_endpoint' => "{$baseUrl}/mcp/{$mcpService}/authorize",
'token_endpoint' => "{$baseUrl}/mcp/{$mcpService}/token",
'registration_endpoint' => "{$baseUrl}/mcp/{$mcpService}/register",
'response_types_supported' => ['code'],
'grant_types_supported' => ['authorization_code', 'refresh_token'],
'token_endpoint_auth_methods_supported' => ['none', 'client_secret_post'],
'code_challenge_methods_supported' => ['S256', 'plain'],
'scopes_supported' => ['mcp:tools', 'mcp:resources', 'mcp:prompts'],
]);
}
/**
* Dynamic Client Registration (RFC 7591)
* POST /mcp/{service}/register
*
* Supports MCP client registration for AI assistants like Claude Desktop.
* Returns pre-configured credentials along with client-provided redirect_uris.
*/
public function register(Request $request, string $mcpService)
{
$serviceConfig = $request->attributes->get('mcp_service_config');
if (!$serviceConfig || empty($serviceConfig['oauth_client_id'])) {
return response()->json([
'error' => 'invalid_request',
'error_description' => 'OAuth not configured for this MCP service.',
], 400);
}
// Get client-provided redirect_uris from the registration request
$requestedRedirectUris = $request->input('redirect_uris', []);
$clientName = $request->input('client_name', $mcpService);
// Validate redirect_uris - must be valid URLs
$validatedRedirectUris = [];
foreach ($requestedRedirectUris as $uri) {
if (!is_string($uri)) {
continue;
}
$parsed = parse_url($uri);
// Accept https:// URIs and localhost for development
if (!empty($parsed['scheme']) && !empty($parsed['host'])) {
if ($parsed['scheme'] === 'https' ||
($parsed['scheme'] === 'http' && in_array($parsed['host'], ['localhost', '127.0.0.1']))) {
$validatedRedirectUris[] = $uri;
}
}
}
// Always allow Claude's known redirect URIs for compatibility
$claudeRedirectUris = [
'https://claude.ai/api/mcp/auth_callback',
'https://claude.com/api/mcp/auth_callback',
];
// Merge with any existing redirect_uris, avoiding duplicates
$client = McpOAuthClient::findByClientId($serviceConfig['oauth_client_id']);
if ($client) {
$existingUris = $client->redirect_uris ?? [];
$allUris = array_unique(array_merge($existingUris, $validatedRedirectUris, $claudeRedirectUris));
$client->update(['redirect_uris' => array_values($allUris)]);
$validatedRedirectUris = $allUris;
} else {
// Create client entry if it doesn't exist
$allUris = array_unique(array_merge($validatedRedirectUris, $claudeRedirectUris));
McpOAuthClient::create([
'client_id' => $serviceConfig['oauth_client_id'],
'client_secret' => $serviceConfig['oauth_client_secret'],
'name' => $clientName,
'redirect_uris' => array_values($allUris),
'is_active' => true,
]);
$validatedRedirectUris = $allUris;
}
Log::info('MCP OAuth: Client registration', [
'client_name' => $clientName,
'redirect_uris' => $validatedRedirectUris,
'service' => $mcpService,
]);
// Return registration response per RFC 7591
// Include client_secret for MCP clients (required for token exchange)
return response()->json([
'client_id' => $serviceConfig['oauth_client_id'],
'client_secret' => $serviceConfig['oauth_client_secret'],
'client_name' => $clientName,
'redirect_uris' => array_values($validatedRedirectUris),
'grant_types' => ['authorization_code', 'refresh_token'],
'response_types' => ['code'],
'token_endpoint_auth_method' => 'client_secret_post',
'scope' => 'mcp:tools mcp:resources mcp:prompts',
]);
}
/**
* Authorization Endpoint - Check for existing session or show login page
* GET /mcp/{service}/authorize
*/
public function authorizeGet(Request $request, string $mcpService)
{
$clientId = $request->query('client_id');
$redirectUri = $request->query('redirect_uri');
$state = $request->query('state');
$codeChallenge = $request->query('code_challenge');
$codeChallengeMethod = $request->query('code_challenge_method', 'plain');
$responseType = $request->query('response_type', 'code');
$scope = $request->query('scope', 'mcp:tools mcp:resources mcp:prompts');
// Validate response_type
if ($responseType !== 'code') {
return $this->errorResponse('unsupported_response_type', 'Only code response type is supported');
}
// Validate client_id is provided
if (empty($clientId)) {
return $this->errorResponse('invalid_request', 'Missing client_id');
}
// Get service config and validate client_id matches
$serviceConfig = $request->attributes->get('mcp_service_config');
if (!$serviceConfig) {
return $this->errorResponse('server_error', 'MCP service not found');
}
// Validate client_id matches configured OAuth client
if (empty($serviceConfig['oauth_client_id'])) {
return $this->errorResponse('server_error', 'OAuth not configured for this service');
}
if ($clientId !== $serviceConfig['oauth_client_id']) {
Log::warning('MCP OAuth: Invalid client_id', [
'provided' => $clientId,
'expected' => $serviceConfig['oauth_client_id'],
'service' => $mcpService,
]);
return $this->errorResponse('invalid_client', 'Invalid client_id');
}
// Ensure client exists in the OAuth clients table (for foreign key constraints)
$client = McpOAuthClient::findByClientId($clientId);
if (!$client) {
// Create the client entry for the pre-configured credentials
$client = McpOAuthClient::create([
'client_id' => $clientId,
'client_secret' => $serviceConfig['oauth_client_secret'],
'name' => $mcpService,
'redirect_uris' => $redirectUri ? [$redirectUri] : [],
'is_active' => true,
]);
}
// Validate redirect URI is present and registered with the client
if (empty($redirectUri)) {
return $this->errorResponse('invalid_request', 'Missing redirect_uri');
}
$registeredUris = $client->redirect_uris ?? [];
if (!empty($registeredUris) && !in_array($redirectUri, $registeredUris, true)) {
Log::warning('MCP OAuth: redirect_uri not registered', [
'provided' => $redirectUri,
'registered' => $registeredUris,
'service' => $mcpService,
]);
return $this->errorResponse('invalid_request', 'redirect_uri is not registered for this client');
}
// ============================================================
// SSO CHECK: Try to find existing DreamFactory session
// If user is already logged in, skip the login page entirely
// ============================================================
$existingSession = $this->getExistingDfSession($request);
if ($existingSession) {
Log::info('MCP OAuth: Found existing DF session, skipping login page', [
'user_id' => $existingSession['id'],
'email' => $existingSession['email'],
]);
// User is already authenticated - generate code and redirect immediately
$authCode = McpOAuthAuthorizationCode::createCode([
'client_id' => $clientId,
'user_id' => $existingSession['id'],
'redirect_uri' => $redirectUri,
'code_challenge' => $codeChallenge,
'code_challenge_method' => $codeChallengeMethod ?: 'S256',
'scope' => $scope,
'df_session_token' => $existingSession['session_token'],
'user_email' => $existingSession['email'],
'user_name' => $existingSession['name'] ?? $existingSession['first_name'] ?? null,
]);
// Build redirect URL back to the client
$redirectParams = ['code' => $authCode->code];
if (!empty($state)) {
$redirectParams['state'] = $state;
}
$redirectUrl = $this->buildRedirectUrl($redirectUri, $redirectParams);
return response()->view('mcp::mcp-auth-success', [
'redirectUrl' => $redirectUrl,
]);
}
// ============================================================
// No existing session - redirect to login page
// ============================================================
// Generate internal state for tracking
$authState = bin2hex(random_bytes(16));
// Store pending authorization in cache
$pendingKey = 'mcp_oauth_pending_' . $authState;
cache()->put($pendingKey, [
'client_id' => $clientId,
'redirect_uri' => $redirectUri,
'state' => $state,
'code_challenge' => $codeChallenge,
'code_challenge_method' => $codeChallengeMethod,
'scope' => $scope,
'service' => $mcpService,
], now()->addMinutes(10));
$baseUrl = $this->getBaseUrl($request);
// ============================================================
// Check for custom login URL configuration
// ============================================================
if (!empty($serviceConfig['custom_login_url'])) {
// Validate the custom login URL
if (!McpServerConfig::isValidCustomLoginUrl($serviceConfig['custom_login_url'])) {
Log::error('MCP OAuth: Invalid custom login URL', [
'url' => $serviceConfig['custom_login_url'],
'service' => $mcpService,
]);
return $this->errorResponse('server_error', 'Invalid custom login URL configuration');
}
// Get available OAuth services to pass to custom login page
$oauthServices = $this->getAvailableOAuthServices();
// Redirect to custom login page with OAuth params
$customLoginUrl = $this->buildRedirectUrl($serviceConfig['custom_login_url'], [
'client_id' => $clientId,
'redirect_uri' => $redirectUri,
'state' => $authState,
'code_challenge' => $codeChallenge,
'code_challenge_method' => $codeChallengeMethod,
'scope' => $scope,
'original_state' => $state,
'service' => $mcpService,
'login_url' => "{$baseUrl}/mcp/{$mcpService}/login",
'oauth_services' => !empty($oauthServices) ? base64_encode(json_encode($oauthServices)) : '',
'oauth_callback_base' => "{$baseUrl}/mcp/{$mcpService}/oauth-complete",
]);
Log::info('MCP OAuth: Redirecting to custom login page', [
'custom_url' => $serviceConfig['custom_login_url'],
'service' => $mcpService,
]);
return redirect($customLoginUrl);
}
// ============================================================
// Default: Redirect to DreamFactory login
// ============================================================
// Build the callback URL that DF will redirect to after login
$callbackUrl = "{$baseUrl}/mcp/{$mcpService}/oauth-callback?auth_state={$authState}";
// Redirect to DreamFactory's main login page with redirect parameter
// DF login should redirect back to our callback after successful authentication
$dfLoginUrl = $this->getFrontendUrl($request) . "/auth/login?redirect=" . urlencode($callbackUrl);
return redirect($dfLoginUrl);
}
/**
* Handle login form submission
* POST /mcp/{service}/login
*/
public function login(Request $request, string $mcpService)
{
$email = $request->input('email');
$password = $request->input('password');
$state = $request->input('state');
$clientId = $request->input('client_id');
$redirectUri = $request->input('redirect_uri');
$codeChallenge = $request->input('code_challenge');
$codeChallengeMethod = $request->input('code_challenge_method', 'S256');
$scope = $request->input('scope', 'mcp:tools mcp:resources mcp:prompts');
if (empty($email) || empty($password)) {
return $this->errorResponse('invalid_request', 'Email and password are required');
}
// Authenticate against DreamFactory
try {
$dfSession = $this->authenticateWithDreamFactory($email, $password);
} catch (\Exception $e) {
Log::error('MCP OAuth login failed', ['error' => $e->getMessage()]);
return $this->errorResponse('access_denied', 'Invalid credentials');
}
// Generate authorization code
$authCode = McpOAuthAuthorizationCode::createCode([
'client_id' => $clientId,
'user_id' => $dfSession['id'],
'redirect_uri' => $redirectUri,
'code_challenge' => $codeChallenge,
'code_challenge_method' => $codeChallengeMethod,
'scope' => $scope,
'df_session_token' => $dfSession['session_token'],
'user_email' => $dfSession['email'],
'user_name' => $dfSession['name'] ?? null,
]);
// Build redirect URL
$redirectParams = ['code' => $authCode->code];
if (!empty($state)) {
// Get original state from pending authorization
$pendingKey = 'mcp_oauth_pending_' . $state;
$pending = cache()->get($pendingKey);
if ($pending && !empty($pending['state'])) {
$redirectParams['state'] = $pending['state'];
}
cache()->forget($pendingKey);
}
$redirectUrl = $this->buildRedirectUrl($redirectUri, $redirectParams);
return response()->view('mcp::mcp-auth-success', [
'redirectUrl' => $redirectUrl,
]);
}
/**
* Handle OAuth callback after DreamFactory login
* GET /mcp/{service}/oauth-callback
*
* This is called after user logs in via DreamFactory's main login page.
* At this point, the user should have a valid DF session.
*/
public function oauthCallback(Request $request, string $mcpService)
{
$authState = $request->query('auth_state');
$sessionTokenFromUrl = $request->query('session_token'); // Token passed in URL from DF login
if (empty($authState)) {
return $this->errorResponse('invalid_request', 'Missing auth_state parameter');
}
// Retrieve pending authorization
$pendingKey = 'mcp_oauth_pending_' . $authState;
$pending = cache()->get($pendingKey);
if (!$pending) {
return $this->errorResponse('invalid_request', 'Authorization session expired. Please try again.');
}
// Try to get the session - check URL param first, then cookies/headers
$existingSession = null;
// If token passed in URL (from DF login redirect), validate it directly
if ($sessionTokenFromUrl) {
try {
$existingSession = $this->validateDreamFactorySession($sessionTokenFromUrl);
$existingSession['session_token'] = $sessionTokenFromUrl;
} catch (\Exception $e) {
Log::debug('MCP OAuth: Session token from URL validation failed', [
'error' => $e->getMessage(),
]);
}
}
// Fallback to cookie/header check
if (!$existingSession) {
$existingSession = $this->getExistingDfSession($request);
}
if (!$existingSession) {
// User still not authenticated - redirect back to DF login
$baseUrl = $this->getBaseUrl($request);
$callbackUrl = "{$baseUrl}/mcp/{$mcpService}/oauth-callback?auth_state={$authState}";
$dfLoginUrl = $this->getFrontendUrl($request) . "/auth/login?redirect=" . urlencode($callbackUrl);
return redirect($dfLoginUrl);
}
// User is authenticated - generate authorization code
$authCode = McpOAuthAuthorizationCode::createCode([
'client_id' => $pending['client_id'],
'user_id' => $existingSession['id'],
'redirect_uri' => $pending['redirect_uri'],
'code_challenge' => $pending['code_challenge'],
'code_challenge_method' => $pending['code_challenge_method'] ?: 'S256',
'scope' => $pending['scope'] ?? 'mcp:tools mcp:resources mcp:prompts',
'df_session_token' => $existingSession['session_token'],
'user_email' => $existingSession['email'],
'user_name' => $existingSession['name'] ?? $existingSession['first_name'] ?? null,
]);
// Clean up pending authorization
cache()->forget($pendingKey);
// Build redirect URL back to the original client
$redirectParams = ['code' => $authCode->code];
if (!empty($pending['state'])) {
$redirectParams['state'] = $pending['state'];
}
$redirectUrl = $this->buildRedirectUrl($pending['redirect_uri'], $redirectParams);
return response()->view('mcp::mcp-auth-success', [
'redirectUrl' => $redirectUrl,
]);
}
/**
* Handle OAuth completion from DreamFactory OAuth redirect
* GET /mcp/{service}/oauth-complete
*
* This endpoint receives the session_token from DF OAuth redirect
* and completes the MCP OAuth authorization flow.
*/
public function oauthComplete(Request $request, string $mcpService)
{
$sessionToken = $request->query('session_token');
$state = $request->query('state');
if (empty($sessionToken)) {
return $this->errorResponse('invalid_request', 'Missing session_token from OAuth provider');
}
if (empty($state)) {
return $this->errorResponse('invalid_request', 'Missing state parameter');
}
// Retrieve pending authorization from cache
$pendingKey = 'mcp_oauth_pending_' . $state;
$pending = cache()->get($pendingKey);
if (!$pending) {
return $this->errorResponse('invalid_request', 'Authorization session expired. Please try again.');
}
// Validate session token with DreamFactory
try {
$dfSession = $this->validateDreamFactorySession($sessionToken);
$dfSession['session_token'] = $sessionToken;
} catch (\Exception $e) {
Log::error('MCP OAuth oauth-complete failed', ['error' => $e->getMessage()]);
return $this->errorResponse('access_denied', 'Invalid or expired session token');
}
// Generate authorization code
$authCode = McpOAuthAuthorizationCode::createCode([
'client_id' => $pending['client_id'],
'user_id' => $dfSession['id'],
'redirect_uri' => $pending['redirect_uri'],
'code_challenge' => $pending['code_challenge'],
'code_challenge_method' => $pending['code_challenge_method'] ?? 'S256',
'scope' => $pending['scope'] ?? 'mcp:tools mcp:resources mcp:prompts',
'df_session_token' => $sessionToken,
'user_email' => $dfSession['email'],
'user_name' => $dfSession['name'] ?? $dfSession['first_name'] ?? null,
]);
// Clean up pending authorization
cache()->forget($pendingKey);
// Build redirect URL back to the original client
$redirectParams = ['code' => $authCode->code];
if (!empty($pending['state'])) {
$redirectParams['state'] = $pending['state'];
}
$redirectUrl = $this->buildRedirectUrl($pending['redirect_uri'], $redirectParams);
Log::info('MCP OAuth: OAuth complete, redirecting to client', [
'redirect_uri' => $pending['redirect_uri'],
'user_email' => $dfSession['email'],
]);
return response()->view('mcp::mcp-auth-success', [
'redirectUrl' => $redirectUrl,
]);
}
/**
* Handle DreamFactory session token callback
* POST /mcp/{service}/df-callback
*/
public function dfCallback(Request $request, string $mcpService)
{
$sessionToken = $request->input('session_token');
$state = $request->input('state');
$clientId = $request->input('client_id');
$redirectUri = $request->input('redirect_uri');
$codeChallenge = $request->input('code_challenge');
$originalState = $request->input('original_state');
$scope = $request->input('scope', 'mcp:tools mcp:resources mcp:prompts');
if (empty($sessionToken)) {
return response()->json([
'error' => 'invalid_request',
'error_description' => 'Missing session_token',
], 400);
}
// Validate session token with DreamFactory
try {
$dfSession = $this->validateDreamFactorySession($sessionToken);
} catch (\Exception $e) {
Log::error('MCP OAuth df-callback failed', ['error' => $e->getMessage()]);
return response()->json([
'error' => 'invalid_token',
'error_description' => 'Invalid or expired session token',
], 401);
}
// Generate authorization code
$authCode = McpOAuthAuthorizationCode::createCode([
'client_id' => $clientId,
'user_id' => $dfSession['id'],
'redirect_uri' => $redirectUri,
'code_challenge' => $codeChallenge,
'code_challenge_method' => 'S256',
'scope' => $scope,
'df_session_token' => $sessionToken,
'user_email' => $dfSession['email'],
'user_name' => $dfSession['name'] ?? null,
]);
// Build redirect URL
$redirectParams = ['code' => $authCode->code];
if (!empty($originalState)) {
$redirectParams['state'] = $originalState;
}
$redirectUrl = $this->buildRedirectUrl($redirectUri, $redirectParams);
return response()->json([
'redirect' => $redirectUrl,
]);
}
/**
* Token Endpoint
* POST /mcp/{service}/token
*/
public function token(Request $request, string $mcpService)
{
Log::info('OAuth token request', [
'grant_type' => $request->input('grant_type'),
'code' => $request->input('code') ? substr($request->input('code'), 0, 20) . '...' : null,
'redirect_uri' => $request->input('redirect_uri'),
'client_id' => $request->input('client_id'),
'client_secret' => $request->input('client_secret') ? 'present' : 'missing',
'code_verifier' => $request->input('code_verifier') ? 'present' : 'missing',
]);
$grantType = $request->input('grant_type');
if ($grantType === 'authorization_code') {
return $this->handleAuthorizationCodeGrant($request, $mcpService);
} elseif ($grantType === 'refresh_token') {
return $this->handleRefreshTokenGrant($request, $mcpService);
}
return $this->tokenErrorResponse('unsupported_grant_type', 'Unsupported grant type');
}
/**
* Handle authorization_code grant
*/
private function handleAuthorizationCodeGrant(Request $request, string $mcpService)
{
$code = $request->input('code');
$redirectUri = $request->input('redirect_uri');
$clientId = $request->input('client_id');
$clientSecret = $request->input('client_secret');
$codeVerifier = $request->input('code_verifier');
if (empty($code)) {
Log::warning('OAuth token: Missing code');
return $this->tokenErrorResponse('invalid_request', 'Missing code');
}
// Get service config and validate client credentials
$serviceConfig = $request->attributes->get('mcp_service_config');
if (!$serviceConfig || empty($serviceConfig['oauth_client_id'])) {
return $this->tokenErrorResponse('server_error', 'OAuth not configured for this service');
}
// Validate client_id
if (empty($clientId) || $clientId !== $serviceConfig['oauth_client_id']) {
Log::warning('OAuth token: Invalid client_id', [
'provided' => $clientId,
'service' => $mcpService,
]);
return $this->tokenErrorResponse('invalid_client', 'Invalid client_id');
}
// Validate client_secret
if (empty($clientSecret) || $clientSecret !== $serviceConfig['oauth_client_secret']) {
Log::warning('OAuth token: Invalid client_secret', [
'service' => $mcpService,
]);
return $this->tokenErrorResponse('invalid_client', 'Invalid client_secret');
}
// Find and validate code
$authCode = McpOAuthAuthorizationCode::findValidCode($code);
if (!$authCode) {
Log::warning('OAuth token: Invalid or expired code', ['code' => substr($code, 0, 20) . '...']);
return $this->tokenErrorResponse('invalid_grant', 'Invalid or expired authorization code');
}
Log::info('OAuth token: Found auth code', [
'stored_client_id' => $authCode->client_id,
'request_client_id' => $clientId,
'stored_redirect_uri' => $authCode->redirect_uri,
'request_redirect_uri' => $redirectUri,
'has_code_challenge' => !empty($authCode->code_challenge),
]);
// Verify client matches the auth code
if ($authCode->client_id !== $clientId) {
Log::warning('OAuth token: Client mismatch with auth code');
$authCode->consume();
return $this->tokenErrorResponse('invalid_grant', 'Client mismatch');
}
// Verify redirect URI
if ($authCode->redirect_uri !== $redirectUri) {
Log::warning('OAuth token: Redirect URI mismatch', [
'stored' => $authCode->redirect_uri,
'request' => $redirectUri,
]);
$authCode->consume();
return $this->tokenErrorResponse('invalid_grant', 'Redirect URI mismatch');
}
// Verify PKCE
if (!empty($authCode->code_challenge) && !$authCode->verifyCodeChallenge($codeVerifier ?? '')) {
Log::warning('OAuth token: PKCE verification failed');
$authCode->consume();
return $this->tokenErrorResponse('invalid_grant', 'Invalid code_verifier');
}
// Create access token
$accessToken = McpOAuthAccessToken::createToken([
'client_id' => $authCode->client_id,
'user_id' => $authCode->user_id,
'df_session_token' => $authCode->df_session_token,
'user_email' => $authCode->user_email,
'user_name' => $authCode->user_name,
'scope' => $authCode->scope ?? 'mcp:tools mcp:resources mcp:prompts',
]);
// Consume the authorization code
$authCode->consume();
$tokenResponse = [
'access_token' => $accessToken->access_token,
'token_type' => 'Bearer',
'expires_in' => McpOAuthAccessToken::ACCESS_TOKEN_LIFETIME_HOURS * 3600,
'refresh_token' => $accessToken->refresh_token,
];
if (!empty($accessToken->scope)) {
$tokenResponse['scope'] = $accessToken->scope;
}
return response()->json($tokenResponse);
}
/**
* Handle refresh_token grant
*/
private function handleRefreshTokenGrant(Request $request, string $mcpService)
{
$refreshToken = $request->input('refresh_token');
$clientId = $request->input('client_id');
$clientSecret = $request->input('client_secret');
if (empty($refreshToken)) {
return $this->tokenErrorResponse('invalid_request', 'Missing refresh_token');
}
// Get service config and validate client credentials
$serviceConfig = $request->attributes->get('mcp_service_config');
if (!$serviceConfig || empty($serviceConfig['oauth_client_id'])) {
return $this->tokenErrorResponse('server_error', 'OAuth not configured for this service');
}
// Validate client_id
if (empty($clientId) || $clientId !== $serviceConfig['oauth_client_id']) {
return $this->tokenErrorResponse('invalid_client', 'Invalid client_id');
}
// Validate client_secret
if (empty($clientSecret) || $clientSecret !== $serviceConfig['oauth_client_secret']) {
return $this->tokenErrorResponse('invalid_client', 'Invalid client_secret');
}
$token = McpOAuthAccessToken::findValidRefreshToken($refreshToken);
if (!$token) {
return $this->tokenErrorResponse('invalid_grant', 'Invalid or expired refresh token');
}
// Verify client matches token
if ($token->client_id !== $clientId) {
return $this->tokenErrorResponse('invalid_grant', 'Client mismatch');
}
// Validate DreamFactory session is still valid
try {
$this->validateDreamFactorySession($token->df_session_token);
} catch (\Exception $e) {
$token->revoke();
return $this->tokenErrorResponse('invalid_grant', 'DreamFactory session expired');
}
// Refresh the token
$token->refresh();
$tokenResponse = [
'access_token' => $token->access_token,
'token_type' => 'Bearer',
'expires_in' => McpOAuthAccessToken::ACCESS_TOKEN_LIFETIME_HOURS * 3600,
'refresh_token' => $token->refresh_token,
];
if (!empty($token->scope)) {
$tokenResponse['scope'] = $token->scope;
}
return response()->json($tokenResponse);
}
/**
* Authenticate with DreamFactory
*/
private function authenticateWithDreamFactory(string $email, string $password): array
{
$client = new Client(['timeout' => 30]);
// Try user session first
try {
$response = $client->post("{$this->dfUrl}/api/v2/user/session", [
'json' => [
'email' => $email,
'password' => $password,
],
'headers' => [
'Accept' => 'application/json',
],
]);
return json_decode($response->getBody()->getContents(), true);
} catch (\Exception $e) {
// Fall back to admin session endpoint
Log::debug('MCP OAuth: User session failed, trying admin session', ['error' => $e->getMessage()]);
}
// Try admin session
$response = $client->post("{$this->dfUrl}/api/v2/system/admin/session", [
'json' => [
'email' => $email,
'password' => $password,
],
'headers' => [
'Accept' => 'application/json',
],
]);
return json_decode($response->getBody()->getContents(), true);
}
/**
* Validate DreamFactory session token
*/
private function validateDreamFactorySession(string $token): array
{
$client = new Client(['timeout' => 30]);
$response = $client->get("{$this->dfUrl}/api/v2/user/session", [
'headers' => [
'Accept' => 'application/json',
'X-DreamFactory-Session-Token' => $token,
],
]);
return json_decode($response->getBody()->getContents(), true);
}
/**
* Check for existing DreamFactory session from various sources
*
* This enables SSO - if user is already logged into DreamFactory,
* they don't need to log in again for MCP OAuth.
*
* Checks in order:
* 1. Authorization header (Bearer token)
* 2. X-DreamFactory-Session-Token header
* 3. Session cookie from Laravel session
* 4. session_token cookie (if DF sets one)
*
* @param Request $request
* @return array|null Session data with 'session_token' key, or null if no valid session
*/
private function getExistingDfSession(Request $request): ?array
{
$sessionToken = null;
// 1. Check Authorization header (Bearer token)
$authHeader = $request->header('Authorization');
if ($authHeader && str_starts_with($authHeader, 'Bearer ')) {
$sessionToken = substr($authHeader, 7);
}
// 2. Check X-DreamFactory-Session-Token header
if (!$sessionToken) {
$sessionToken = $request->header('X-DreamFactory-Session-Token');
}
// 3. Check Laravel session for stored DF token
if (!$sessionToken) {
$sessionToken = session('df_session_token');
}
// 4. Check session_token cookie (if DreamFactory sets one)
if (!$sessionToken) {
$sessionToken = $request->cookie('session_token');
}
// 5. Check JWT cookie that DreamFactory might set
if (!$sessionToken) {
$sessionToken = $request->cookie('jwt_token');
}
if (!$sessionToken) {
return null;
}
// Validate the token with DreamFactory
try {
$sessionData = $this->validateDreamFactorySession($sessionToken);
// Add the session token to the response so we can use it later
$sessionData['session_token'] = $sessionToken;
return $sessionData;
} catch (\Exception $e) {
Log::debug('MCP OAuth: Existing session token validation failed', [
'error' => $e->getMessage(),
]);
return null;
}
}
/**
* Get base URL from request
*
* Handles proxies like ngrok by checking X-Forwarded-* headers
*/
private function getBaseUrl(Request $request): string
{
// Prefer configured APP_URL to prevent host header injection attacks.
// Only fall back to request headers for local/dev environments.
$configuredUrl = rtrim(config('app.url', ''), '/');
if (!empty($configuredUrl) && $configuredUrl !== 'http://localhost') {
return $configuredUrl;
}
$scheme = $request->getScheme();
$host = $request->getHost();
$port = $request->getPort();
$url = "{$scheme}://{$host}";
if (($scheme === 'http' && $port != 80) || ($scheme === 'https' && $port != 443)) {
$url .= ":{$port}";
}
return $url;
}
/**
* Build URL with query parameters
*/
private function buildRedirectUrl(string $baseUrl, array $params): string
{
$separator = str_contains($baseUrl, '?') ? '&' : '?';
return $baseUrl . $separator . http_build_query($params);
}
/**
* Error response for authorization endpoint
*/
private function errorResponse(string $error, string $description)
{
return response()->json([
'error' => $error,
'error_description' => $description,
], 400);
}
/**
* Error response for token endpoint
*/
private function tokenErrorResponse(string $error, string $description)
{
$statusCode = ($error === 'invalid_client') ? 401 : 400;
return response()->json([
'error' => $error,
'error_description' => $description,
], $statusCode);
}
/**
* Get available OAuth services from DreamFactory environment
*/
private function getAvailableOAuthServices(): array
{