-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathoauth.rs
More file actions
1195 lines (1054 loc) · 41.8 KB
/
Copy pathoauth.rs
File metadata and controls
1195 lines (1054 loc) · 41.8 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
//! OAuth Authorization Flow
//!
//! This module handles the OAuth 2.0 authorization code flow with PKCE
//! for MCP client approval.
//!
//! ## Flow Overview
//!
//! 1. MCP client (VS Code, Cursor, etc.) calls `/oauth/authorize` on gateway
//! 2. Gateway validates request, stores pending auth, returns HTML redirect page
//! 3. Browser opens, triggers `mcpmux://authorize?request_id=xxx` deep link
//! 4. Desktop app receives deep link, calls `get_pending_consent` to validate
//! 5. Backend validates request_id, returns full consent details from DB
//! 6. Desktop shows consent modal (only if valid)
//! 7. User approves → app calls `approve_oauth_consent`
//! 8. Backend atomically processes approval, issues code
//! 9. Desktop app opens redirect URL with code back to MCP client
//! 10. MCP client exchanges code for tokens via `/oauth/token`
//!
//! ## Security
//!
//! - Deep link only contains request_id (no spoofable client info)
//! - Backend validates request exists and isn't expired/processed
//! - Client name/scopes come from backend (authoritative source)
//! - Atomic approval prevents race conditions
//! - PKCE required for all authorization requests (RFC 7636)
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};
use mcpmux_core::branding;
use serde::{Deserialize, Serialize};
use tauri::{Emitter, Manager, State};
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};
use url::Url;
use super::gateway::GatewayAppState;
// ============================================================================
// Deep Link Handling
// ============================================================================
/// Holds a deep-link URL the app was cold-started with (Windows/Linux) until
/// the webview has mounted its listeners. Emitting `oauth-consent-request`
/// before React has subscribed drops the event — Tauri events are fire-and-
/// forget with no replay. The frontend calls `flush_pending_deep_link` once
/// its listener is live to process any buffered URL.
#[derive(Default)]
pub struct PendingInitialDeepLink {
pub url: Mutex<Option<String>>,
pub webview_ready: AtomicBool,
}
/// Called from `on_open_url`: route immediately if the webview has signalled
/// ready, otherwise buffer for later flush. Falls back to direct routing
/// if the state isn't managed yet (shouldn't happen after setup).
pub fn route_or_buffer_deep_link<R: tauri::Runtime>(app: &tauri::AppHandle<R>, url: &str) {
match app.try_state::<PendingInitialDeepLink>() {
Some(pending) if !pending.webview_ready.load(Ordering::Acquire) => {
info!("[DeepLink] Webview not ready — buffering URL: {}", url);
if let Ok(mut guard) = pending.url.lock() {
*guard = Some(url.to_string());
}
}
_ => handle_deep_link(app, url),
}
}
/// Invoked by the frontend once the `oauth-consent-request` listener is live.
/// Marks the webview ready so subsequent URLs route immediately, and drains
/// any URL that arrived before mount.
#[tauri::command]
pub fn flush_pending_deep_link(app: tauri::AppHandle, pending: State<'_, PendingInitialDeepLink>) {
pending.webview_ready.store(true, Ordering::Release);
let buffered = pending.url.lock().ok().and_then(|mut g| g.take());
if let Some(url) = buffered {
info!("[DeepLink] Flushing buffered cold-start URL: {}", url);
handle_deep_link(&app, &url);
}
}
/// Event name for OAuth consent requests sent to frontend
/// Now only contains request_id - frontend must call get_pending_consent
pub const OAUTH_CONSENT_EVENT: &str = "oauth-consent-request";
/// Event name for server install requests sent to frontend (from deep link)
pub const SERVER_INSTALL_EVENT: &str = "server-install-request";
/// Minimal deep link payload - only request_id
/// Frontend must call get_pending_consent to get full details
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OAuthDeepLinkPayload {
pub request_id: String,
}
/// Deep link payload for server installation requests
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerInstallDeepLinkPayload {
pub server_id: String,
}
/// Full consent request details returned by get_pending_consent
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ConsentRequestDetails {
pub request_id: String,
pub client_id: String,
pub client_name: String,
pub redirect_uri: String,
pub scope: String,
pub state: Option<String>,
/// When this request expires (Unix timestamp)
pub expires_at: i64,
/// Cryptographic consent token (shared only via this IPC call, never over HTTP).
/// Must be returned in the approval request to prove the caller is the
/// legitimate desktop app UI—not an external script or bot.
pub consent_token: String,
}
/// Window within which an identical deep-link URL is treated as a duplicate
/// and dropped. On a warm launch BOTH the deep-link plugin's `on_open_url` and
/// the single-instance callback fire for the same `mcpmux://` URL, so without
/// this guard the whole consent flow (deep-link emit → `get_pending_consent` →
/// consent modal) runs twice per approval.
const DEEP_LINK_DEDUP_WINDOW: Duration = Duration::from_secs(3);
/// Pure predicate: is `url` a repeat of `last_url` seen `elapsed` ago, within
/// `window`? Extracted so the dedup rule is unit-testable without the clock.
fn is_recent_duplicate_link(
last_url: Option<&str>,
elapsed: Duration,
url: &str,
window: Duration,
) -> bool {
last_url == Some(url) && elapsed < window
}
/// True if this exact URL was just handled within [`DEEP_LINK_DEDUP_WINDOW`].
/// Records `url` as the most-recent on a miss. Distinct authorizations carry a
/// fresh `request_id` (different URL), so legitimate back-to-back flows are
/// never collapsed.
fn deep_link_is_duplicate(url: &str) -> bool {
static LAST: OnceLock<Mutex<Option<(String, Instant)>>> = OnceLock::new();
let cell = LAST.get_or_init(|| Mutex::new(None));
let mut guard = cell.lock().unwrap_or_else(|p| p.into_inner());
let now = Instant::now();
let dup = guard.as_ref().is_some_and(|(last_url, seen_at)| {
is_recent_duplicate_link(
Some(last_url.as_str()),
now.duration_since(*seen_at),
url,
DEEP_LINK_DEDUP_WINDOW,
)
});
if !dup {
*guard = Some((url.to_string(), now));
}
dup
}
/// Handle an incoming deep link URL
///
/// Routes based on the URL path:
/// - `mcpmux://authorize` - OAuth authorization request (inbound - client approval)
/// - `mcpmux://callback/oauth` - OAuth callback (outbound - server connection)
pub fn handle_deep_link<R: tauri::Runtime>(app: &tauri::AppHandle<R>, url: &str) {
info!("[DeepLink] Received: {}", url);
// Validate URL scheme
if !branding::is_deep_link(url) {
warn!(
"[DeepLink] Invalid scheme, expected {}://",
branding::DEEP_LINK_SCHEME
);
return;
}
// Drop the duplicate that the on_open_url + single-instance paths both
// deliver for the same warm-launch URL — otherwise the consent modal and
// `get_pending_consent` fire twice per approval.
if deep_link_is_duplicate(url) {
info!("[DeepLink] Ignoring duplicate within {DEEP_LINK_DEDUP_WINDOW:?}: {url}");
return;
}
// Check for OAuth callback first (mcpmux://callback/oauth?...)
if branding::is_oauth_callback(url) {
let parsed = match Url::parse(url) {
Ok(u) => u,
Err(e) => {
error!("[DeepLink] Failed to parse OAuth callback URL: {}", e);
return;
}
};
handle_oauth_callback_deep_link(app, &parsed);
return;
}
// Parse URL for other routes
let parsed = match Url::parse(url) {
Ok(u) => u,
Err(e) => {
error!("[DeepLink] Failed to parse URL: {}", e);
return;
}
};
// Route based on host (for mcpmux://authorize, host is "authorize")
match parsed.host_str() {
Some("authorize") | Some("oauth") => {
// Inbound OAuth - client requesting approval
handle_authorize_deep_link(app, &parsed);
}
Some("install") => {
handle_install_deep_link(app, &parsed);
}
Some("test") => {
info!("[DeepLink] Test URL received successfully!");
}
_ => {
debug!("[DeepLink] Unknown route: {:?}", parsed.host_str());
}
}
}
/// Handle OAuth authorization deep link
///
/// Only extracts request_id and emits to frontend.
/// Frontend must call get_pending_consent to validate and get details.
fn handle_authorize_deep_link<R: tauri::Runtime>(app: &tauri::AppHandle<R>, url: &Url) {
let params: HashMap<_, _> = url.query_pairs().collect();
// Only require request_id - all other data comes from backend
let request_id = match params.get("request_id") {
Some(id) if !id.is_empty() => id.to_string(),
_ => {
error!("[DeepLink] Missing required parameter: request_id");
return;
}
};
info!(
"[DeepLink] Authorization request received: request_id='{}'",
request_id
);
// Emit minimal payload - frontend will fetch details from backend
let payload = OAuthDeepLinkPayload { request_id };
if let Err(e) = app.emit(OAUTH_CONSENT_EVENT, &payload) {
error!("[DeepLink] Failed to emit consent event: {}", e);
return;
}
// Focus the main window
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.unminimize();
let _ = window.set_focus();
}
}
/// Handle server install deep link
///
/// Extracts server_id and emits to frontend.
/// Frontend will look up the server definition and show install modal.
fn handle_install_deep_link<R: tauri::Runtime>(app: &tauri::AppHandle<R>, url: &Url) {
let params: HashMap<_, _> = url.query_pairs().collect();
let server_id = match params.get("server") {
Some(id) if !id.is_empty() => id.to_string(),
_ => {
error!("[DeepLink] Install link missing required parameter: server");
return;
}
};
info!(
"[DeepLink] Server install request: server_id='{}'",
server_id
);
let payload = ServerInstallDeepLinkPayload { server_id };
if let Err(e) = app.emit(SERVER_INSTALL_EVENT, &payload) {
error!("[DeepLink] Failed to emit server install event: {}", e);
return;
}
// Focus the main window
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.unminimize();
let _ = window.set_focus();
}
}
/// Handle OAuth callback deep link (legacy - for outbound OAuth server connections)
///
/// NOTE: The primary OAuth callback mechanism is now the loopback HTTP server
/// (per RFC 8252 Section 7.3) which handles callbacks directly. This deep link
/// handler is kept for backwards compatibility but is not the main path.
///
/// The loopback server provides universal compatibility with enterprise security
/// systems that may block custom URL schemes.
///
/// URL format: mcpmux://callback/oauth?code=XXX&state=YYY
/// Or on error: mcpmux://callback/oauth?error=XXX&error_description=YYY&state=ZZZ
fn handle_oauth_callback_deep_link<R: tauri::Runtime>(app: &tauri::AppHandle<R>, url: &Url) {
let params: HashMap<_, _> = url.query_pairs().collect();
// State is required for routing to the correct OAuth flow
let state = match params.get("state") {
Some(s) if !s.is_empty() => s.to_string(),
_ => {
error!("[DeepLink] OAuth callback missing required 'state' parameter");
return;
}
};
let state_short = if state.len() > 8 { &state[..8] } else { &state };
info!("[DeepLink] OAuth callback received: state={}", state_short);
// Build callback struct
let callback = mcpmux_gateway::OAuthCallback {
code: params.get("code").map(|s| s.to_string()),
state,
error: params.get("error").map(|s| s.to_string()),
error_description: params.get("error_description").map(|s| s.to_string()),
};
// Get the pool service and route the callback
let app_handle = app.clone();
tauri::async_runtime::spawn(async move {
// Get GatewayAppState
let gateway_state: tauri::State<'_, Arc<RwLock<GatewayAppState>>> = app_handle.state();
let app_state = gateway_state.read().await;
if let Some(ref pool_service) = app_state.pool_service {
// Route callback to OAuth manager
match pool_service.oauth_manager().handle_callback(callback) {
Ok(_) => {
info!("[DeepLink] OAuth callback successfully routed to handler");
}
Err(e) => {
error!("[DeepLink] Failed to route OAuth callback: {}", e);
}
}
} else {
error!("[DeepLink] Pool service not available to handle OAuth callback");
}
});
}
// ============================================================================
// Tauri Commands
// ============================================================================
/// Error type for consent operations
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ConsentError {
pub code: String,
pub message: String,
}
impl ConsentError {
fn not_found(request_id: &str) -> Self {
Self {
code: "NOT_FOUND".to_string(),
message: format!(
"Authorization request '{}' not found or expired",
request_id
),
}
}
fn expired(request_id: &str) -> Self {
Self {
code: "EXPIRED".to_string(),
message: format!("Authorization request '{}' has expired", request_id),
}
}
#[allow(dead_code)]
fn already_processed(request_id: &str) -> Self {
Self {
code: "ALREADY_PROCESSED".to_string(),
message: format!(
"Authorization request '{}' has already been processed",
request_id
),
}
}
fn gateway_unavailable() -> Self {
Self {
code: "GATEWAY_UNAVAILABLE".to_string(),
message: "Gateway is not running".to_string(),
}
}
}
/// Get pending consent request details from backend
///
/// This validates the request_id and returns full details from the authoritative
/// backend source. The frontend should call this after receiving a deep link
/// before showing the consent modal.
///
/// Returns:
/// - Ok(ConsentRequestDetails) if the request is valid and pending
/// - Err(ConsentError) if request not found, expired, or already processed
#[tauri::command]
pub async fn get_pending_consent(
request_id: String,
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
) -> Result<ConsentRequestDetails, ConsentError> {
info!(
"[OAuth] Fetching pending consent: request_id='{}'",
request_id
);
let app_state = gateway_state.read().await;
// Get gateway state
let gw_state = app_state
.gateway_state
.as_ref()
.ok_or_else(ConsentError::gateway_unavailable)?;
// Look up the pending authorization
let auth = {
let state = gw_state.read().await;
state.pending_authorizations.get(&request_id).cloned()
};
let auth = auth.ok_or_else(|| ConsentError::not_found(&request_id))?;
// Check if expired
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0);
if auth.expires_at < now {
warn!("[OAuth] Request '{}' has expired", request_id);
// Remove expired entry
let mut state = gw_state.write().await;
state.pending_authorizations.remove(&request_id);
return Err(ConsentError::expired(&request_id));
}
// Extract consent_token (required for security—ensures only the desktop
// app that retrieved this token via IPC can approve the request)
let consent_token = auth.consent_token.clone().ok_or_else(|| {
error!("[OAuth] Pending authorization missing consent_token");
ConsentError {
code: "NOT_FOUND".to_string(),
message: "Authorization request is missing consent token — it may have been created before this security update. Please retry.".to_string(),
}
})?;
// Build response with authoritative data from backend
// The client_name here comes from our database lookup in handlers.rs
let details = ConsentRequestDetails {
request_id: request_id.clone(),
client_id: auth.client_id.clone(),
client_name: auth
.client_name
.clone()
.unwrap_or_else(|| auth.client_id.clone()),
redirect_uri: auth.redirect_uri.clone(),
scope: auth.scope.clone().unwrap_or_default(),
state: auth.state.clone(),
expires_at: auth.expires_at,
consent_token,
};
info!(
"[OAuth] Consent details validated: client='{}' scopes='{}'",
details.client_name, details.scope
);
Ok(details)
}
/// Request to approve or deny OAuth consent
#[derive(Debug, Deserialize)]
pub struct ConsentApprovalRequest {
/// The request_id from the pending authorization
pub request_id: String,
/// Whether the user approved the request
pub approved: bool,
/// Cryptographic consent token (must match the one issued via get_pending_consent).
/// This proves the caller obtained the token through Tauri IPC, not HTTP scraping.
pub consent_token: String,
/// Optional alias name for the client (set during approval).
pub client_alias: Option<String>,
}
/// Response from consent approval
#[derive(Debug, Serialize, Deserialize)]
pub struct ConsentApprovalResponse {
/// Whether the approval was successful
pub success: bool,
/// The redirect URL for the client (with code or error)
pub redirect_url: String,
/// Optional error message
pub error: Option<String>,
}
/// Approve or deny an OAuth consent request (direct state access)
///
/// This command is called by the frontend after the user has reviewed
/// and approved (or denied) an OAuth authorization request.
#[tauri::command]
pub async fn approve_oauth_consent(
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
request: ConsentApprovalRequest,
) -> Result<ConsentApprovalResponse, String> {
info!(
"[OAuth] Frontend consent {} for request_id: {}",
if request.approved {
"approved"
} else {
"denied"
},
request.request_id
);
let app_state = gateway_state.read().await;
// Get gateway state
let Some(ref gw_state) = app_state.gateway_state else {
return Err("Gateway not running".to_string());
};
// Look up the pending authorization
let pending = {
let state = gw_state.read().await;
state
.pending_authorizations
.get(&request.request_id)
.cloned()
};
let Some(pending) = pending else {
error!("[OAuth] Consent approval failed: request_id not found");
return Ok(ConsentApprovalResponse {
success: false,
redirect_url: String::new(),
error: Some("Authorization request not found or expired".to_string()),
});
};
// Validate consent_token: proves the caller obtained this token via Tauri
// IPC (get_pending_consent), not by scraping the HTTP authorization page.
match &pending.consent_token {
Some(expected_token) => {
if request.consent_token != *expected_token {
error!(
"[OAuth] Consent token mismatch for request_id: {} — possible unauthorized approval attempt",
request.request_id
);
return Err("Invalid consent token".to_string());
}
}
None => {
error!(
"[OAuth] Pending authorization missing consent_token for request_id: {}",
request.request_id
);
return Err("Consent token not available".to_string());
}
}
// Remove the pending authorization (it's been processed)
{
let mut state = gw_state.write().await;
state.pending_authorizations.remove(&request.request_id);
}
if !request.approved {
// User denied - redirect with error
// Client registration remains (unapproved) so they can try again later
let mut redirect_url = pending.redirect_uri.clone();
redirect_url.push_str(if redirect_url.contains('?') { "&" } else { "?" });
redirect_url.push_str("error=access_denied&error_description=User+denied+the+request");
if let Some(ref state_param) = pending.state {
redirect_url.push_str(&format!("&state={}", urlencoding::encode(state_param)));
}
info!(
"[OAuth] User denied consent for client: {}",
pending.client_id
);
return Ok(ConsentApprovalResponse {
success: true,
redirect_url,
error: None,
});
}
// User approved - generate authorization code
use uuid::Uuid;
let code = format!("mc_{}", Uuid::new_v4().to_string().replace("-", ""));
// Auth codes expire in 10 minutes (standard OAuth)
let code_expires_at = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64 + 600) // 10 minutes
.unwrap_or(i64::MAX);
// Store the authorization with the new code and update client alias if provided
{
let mut state = gw_state.write().await;
// Clone pending fields for new authorization
let new_pending = mcpmux_gateway::PendingAuthorization {
client_id: pending.client_id.clone(),
client_name: pending.client_name.clone(),
redirect_uri: pending.redirect_uri.clone(),
scope: pending.scope.clone(),
state: pending.state.clone(),
code_challenge: pending.code_challenge.clone(),
code_challenge_method: pending.code_challenge_method.clone(),
expires_at: code_expires_at,
consent_token: None, // Auth code entries don't need consent tokens
};
state.store_pending_authorization(&code, new_pending);
// Mark client as approved and store any alias override.
if let Some(repo) = state.inbound_client_repository() {
if let Err(e) = repo.approve_client(&pending.client_id).await {
error!("[OAuth] Failed to approve client: {}", e);
} else {
info!("[OAuth] Client approved: {}", pending.client_id);
}
if let Some(alias) = request
.client_alias
.as_deref()
.filter(|s| !s.is_empty())
.map(String::from)
{
if let Err(e) = repo
.update_client_alias(&pending.client_id, Some(alias.clone()))
.await
{
error!("[OAuth] Failed to save client alias: {}", e);
} else {
info!(
"[OAuth] Set client alias '{}' for: {}",
alias, pending.client_id
);
}
}
}
// Emit domain event for client registration
state.emit_domain_event(mcpmux_core::DomainEvent::ClientRegistered {
client_id: pending.client_id.clone(),
client_name: pending.client_id.clone(), // Use client_name field
registration_type: Some("unknown".to_string()), // Will be updated when client metadata is fetched
});
}
// Build redirect URL with authorization code
let mut redirect_url = pending.redirect_uri.clone();
redirect_url.push_str(if redirect_url.contains('?') { "&" } else { "?" });
redirect_url.push_str(&format!("code={}", code));
if let Some(ref state_param) = pending.state {
redirect_url.push_str(&format!("&state={}", urlencoding::encode(state_param)));
}
info!(
"[OAuth] Authorization approved for client: {}, issuing code",
pending.client_id
);
info!("[OAuth] Redirect URL: {}", redirect_url);
Ok(ConsentApprovalResponse {
success: true,
redirect_url,
error: None,
})
}
/// Get list of connected OAuth clients (direct service access)
#[tauri::command]
pub async fn get_oauth_clients(
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
) -> Result<Vec<OAuthClientInfo>, String> {
let app_state = gateway_state.read().await;
// Get gateway state and inbound client repository (direct access)
let Some(ref gw_state) = app_state.gateway_state else {
return Err("Gateway not running".to_string());
};
let state = gw_state.read().await;
let Some(repo) = state.inbound_client_repository() else {
return Err("Database not available".to_string());
};
// Fetch clients directly from repository (no HTTP call)
// Only show approved clients in the UI
let all_clients = repo
.list_clients()
.await
.map_err(|e| format!("Failed to fetch clients: {}", e))?;
let clients: Vec<_> = all_clients.into_iter().filter(|c| c.approved).collect();
info!(
"[OAuth] Fetched {} approved clients from repository",
clients.len()
);
// Map to response format
let client_infos: Vec<OAuthClientInfo> = clients
.into_iter()
.map(|client| OAuthClientInfo {
client_id: client.client_id,
registration_type: client.registration_type.as_str().to_string(),
client_name: client.client_name,
client_alias: client.client_alias,
redirect_uris: client.redirect_uris,
scope: client.scope,
approved: client.approved,
logo_uri: client.logo_uri,
client_uri: client.client_uri,
software_id: client.software_id,
software_version: client.software_version,
metadata_url: client.metadata_url,
metadata_cached_at: client.metadata_cached_at,
metadata_cache_ttl: client.metadata_cache_ttl,
last_seen: client.last_seen,
created_at: client.created_at,
reports_roots: client.reports_roots,
roots_capability_known: client.roots_capability_known,
})
.collect();
Ok(client_infos)
}
/// Approve a registered OAuth client by ID (for E2E testing only).
///
/// Guarded by the `MCPMUX_E2E_TEST` environment variable. In production
/// builds this command is a no-op that returns an error.
#[tauri::command]
pub async fn approve_oauth_client(
client_id: String,
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
) -> Result<(), String> {
if std::env::var("MCPMUX_E2E_TEST").is_err() {
return Err("approve_oauth_client is only available in E2E test mode".to_string());
}
let app_state = gateway_state.read().await;
let Some(ref gw_state) = app_state.gateway_state else {
return Err("Gateway not running".to_string());
};
let state = gw_state.read().await;
let Some(repo) = state.inbound_client_repository() else {
return Err("Database not available".to_string());
};
repo.approve_client(&client_id)
.await
.map_err(|e| format!("Failed to approve client: {}", e))?;
info!(
"[OAuth] Approved client via E2E test command: {}",
client_id
);
Ok(())
}
/// Information about a connected OAuth client
#[derive(Debug, Serialize, Deserialize)]
pub struct OAuthClientInfo {
pub client_id: String,
pub registration_type: String,
pub client_name: String,
pub client_alias: Option<String>,
pub redirect_uris: Vec<String>,
pub scope: Option<String>,
// Approval status
pub approved: bool,
// RFC 7591 Client Metadata
#[serde(skip_serializing_if = "Option::is_none")]
pub logo_uri: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client_uri: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub software_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub software_version: Option<String>,
// CIMD-specific fields
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata_cached_at: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata_cache_ttl: Option<i64>,
pub last_seen: Option<String>,
pub created_at: String,
/// Sticky-positive bit: `true` once any session of this client
/// declared the MCP `roots` capability. Meaningful only when
/// `roots_capability_known` is `true` — for a brand-new client we
/// haven't seen `initialize` for yet, this defaults to `false` but
/// the UI must hide the "Rootless" badge instead of trusting it.
pub reports_roots: bool,
/// `true` once we've processed at least one `notifications/initialized`
/// for this client. Until then, the UI treats the capability as
/// unknown (no badge). Once known, the badge resolves to either
/// "Reports workspace" (`reports_roots = true`) or "Rootless"
/// (`reports_roots = false`).
pub roots_capability_known: bool,
}
/// Request to update client settings.
///
/// Only the alias is user-editable now — connection mode / space pin no
/// longer exist.
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateClientSettingsRequest {
pub client_alias: Option<String>,
}
/// Update an OAuth client's settings (direct service access)
#[tauri::command]
pub async fn update_oauth_client(
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
client_id: String,
settings: UpdateClientSettingsRequest,
) -> Result<OAuthClientInfo, String> {
let app_state = gateway_state.read().await;
// Get gateway state and inbound client repository
let Some(ref gw_state) = app_state.gateway_state else {
return Err("Gateway not running".to_string());
};
let state = gw_state.read().await;
let Some(repo) = state.inbound_client_repository() else {
return Err("Database not available".to_string());
};
repo.update_client_alias(&client_id, settings.client_alias)
.await
.map_err(|e| format!("Failed to update client: {}", e))?;
info!("[OAuth] Updated client: {}", client_id);
state.emit_domain_event(mcpmux_core::DomainEvent::ClientUpdated {
client_id: client_id.clone(),
});
let updated_client = repo
.get_client(&client_id)
.await
.map_err(|e| format!("Failed to get updated client: {}", e))?
.ok_or("Client not found after update")?;
Ok(OAuthClientInfo {
client_id: updated_client.client_id,
registration_type: updated_client.registration_type.as_str().to_string(),
client_name: updated_client.client_name,
client_alias: updated_client.client_alias,
redirect_uris: updated_client.redirect_uris,
scope: updated_client.scope,
approved: updated_client.approved,
logo_uri: updated_client.logo_uri,
client_uri: updated_client.client_uri,
software_id: updated_client.software_id,
software_version: updated_client.software_version,
metadata_url: updated_client.metadata_url,
metadata_cached_at: updated_client.metadata_cached_at,
metadata_cache_ttl: updated_client.metadata_cache_ttl,
last_seen: updated_client.last_seen,
created_at: updated_client.created_at,
reports_roots: updated_client.reports_roots,
roots_capability_known: updated_client.roots_capability_known,
})
}
/// Delete an OAuth client (direct service access)
#[tauri::command]
pub async fn delete_oauth_client(
gateway_state: State<'_, Arc<RwLock<GatewayAppState>>>,
client_id: String,
) -> Result<(), String> {
let app_state = gateway_state.read().await;
// Get gateway state and inbound client repository
let Some(ref gw_state) = app_state.gateway_state else {
return Err("Gateway not running".to_string());
};
let state = gw_state.read().await;
let Some(repo) = state.inbound_client_repository() else {
return Err("Database not available".to_string());
};
// Delete client directly via repository
repo.delete_client(&client_id)
.await
.map_err(|e| format!("Failed to delete client: {}", e))?;
info!("[OAuth] Deleted client: {}", client_id);
// Emit domain event
state.emit_domain_event(mcpmux_core::DomainEvent::ClientDeleted { client_id });
Ok(())
}
/// Open a URL without flashing a terminal window (Windows-specific)
#[cfg(target_os = "windows")]
fn open_url_no_flash(url: &str) -> Result<(), String> {
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::ptr;
#[link(name = "shell32")]
extern "system" {
fn ShellExecuteW(
hwnd: *mut std::ffi::c_void,
operation: *const u16,
file: *const u16,
parameters: *const u16,
directory: *const u16,
show_cmd: i32,
) -> isize;
}
let url_wide: Vec<u16> = OsStr::new(url).encode_wide().chain(Some(0)).collect();
let open_wide: Vec<u16> = OsStr::new("open").encode_wide().chain(Some(0)).collect();
// SW_SHOWNORMAL = 1
let result = unsafe {
ShellExecuteW(
ptr::null_mut(),
open_wide.as_ptr(),
url_wide.as_ptr(),
ptr::null(),
ptr::null(),
1,
)
};
// ShellExecuteW returns > 32 on success
if result > 32 {
Ok(())
} else {
Err(format!("ShellExecuteW failed with code: {}", result))
}
}
/// Open a URL using the default handler (non-Windows)
#[cfg(not(target_os = "windows"))]
fn open_url_no_flash(url: &str) -> Result<(), String> {
open::that(url).map_err(|e| format!("Failed to open URL: {}", e))
}
/// Open a URL or deliver OAuth callback
///
/// For localhost/127.0.0.1 URLs (like VS Code's OAuth callback), makes a direct
/// HTTP request instead of opening a browser - cleaner UX, no browser window.
///
/// For custom protocol URLs (like `cursor://`), uses the system handler.
/// For regular http/https URLs to remote hosts, opens in the default browser.
#[tauri::command]
pub async fn open_url(url: String) -> Result<(), String> {
info!("[OAuth] Processing redirect URL: {}", url);
// Parse the URL to determine how to handle it
let parsed = Url::parse(&url).map_err(|e| format!("Invalid URL: {}", e))?;
// Check if this is a localhost callback (VS Code, etc.)
let is_localhost = matches!(parsed.host_str(), Some("localhost") | Some("127.0.0.1"));
let is_http = parsed.scheme() == "http" || parsed.scheme() == "https";
if is_localhost && is_http {
// For localhost callbacks, make a direct HTTP request
// This avoids opening a browser window for a cleaner UX
info!("[OAuth] Delivering callback directly to localhost: {}", url);