-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_auth.py
More file actions
897 lines (714 loc) · 35.2 KB
/
test_auth.py
File metadata and controls
897 lines (714 loc) · 35.2 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
"""
Tests for OAuth client authentication implementation.
"""
import base64
import hashlib
import time
from unittest.mock import AsyncMock, Mock, patch
from urllib.parse import parse_qs, urlparse
import httpx
import pytest
from inline_snapshot import snapshot
from pydantic import AnyHttpUrl, AnyUrl
from mcp.client.auth import OAuthClientProvider
from mcp.server.auth.routes import build_metadata
from mcp.server.auth.settings import ClientRegistrationOptions, RevocationOptions
from mcp.shared.auth import (
OAuthClientInformationFull,
OAuthClientMetadata,
OAuthMetadata,
OAuthToken,
)
class MockTokenStorage:
"""Mock token storage for testing."""
def __init__(self):
self._tokens: OAuthToken | None = None
self._client_info: OAuthClientInformationFull | None = None
async def get_tokens(self) -> OAuthToken | None:
return self._tokens
async def set_tokens(self, tokens: OAuthToken) -> None:
self._tokens = tokens
async def get_client_info(self) -> OAuthClientInformationFull | None:
return self._client_info
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
self._client_info = client_info
@pytest.fixture
def mock_storage():
return MockTokenStorage()
@pytest.fixture
def client_metadata():
return OAuthClientMetadata(
redirect_uris=[AnyUrl("http://localhost:3000/callback")],
client_name="Test Client",
grant_types=["authorization_code", "refresh_token"],
response_types=["code"],
scope="read write",
)
@pytest.fixture
def oauth_metadata():
return OAuthMetadata(
issuer=AnyHttpUrl("https://auth.example.com"),
authorization_endpoint=AnyHttpUrl("https://auth.example.com/authorize"),
token_endpoint=AnyHttpUrl("https://auth.example.com/token"),
registration_endpoint=AnyHttpUrl("https://auth.example.com/register"),
scopes_supported=["read", "write", "admin"],
response_types_supported=["code"],
grant_types_supported=["authorization_code", "refresh_token"],
code_challenge_methods_supported=["S256"],
)
@pytest.fixture
def oauth_client_info():
return OAuthClientInformationFull(
client_id="test_client_id",
client_secret="test_client_secret",
redirect_uris=[AnyUrl("http://localhost:3000/callback")],
client_name="Test Client",
grant_types=["authorization_code", "refresh_token"],
response_types=["code"],
scope="read write",
)
@pytest.fixture
def oauth_token():
return OAuthToken(
access_token="test_access_token",
token_type="Bearer",
expires_in=3600,
refresh_token="test_refresh_token",
scope="read write",
)
@pytest.fixture
def oauth_provider(client_metadata, mock_storage):
async def mock_redirect_handler(url: str) -> None:
pass
async def mock_callback_handler() -> tuple[str, str | None]:
return "test_auth_code", "test_state"
return OAuthClientProvider(
server_url="https://api.example.com/v1/mcp",
client_metadata=client_metadata,
storage=mock_storage,
redirect_handler=mock_redirect_handler,
callback_handler=mock_callback_handler,
)
class TestOAuthClientProvider:
"""Test OAuth client provider functionality."""
@pytest.mark.anyio
async def test_init(self, oauth_provider, client_metadata, mock_storage):
"""Test OAuth provider initialization."""
assert oauth_provider.server_url == "https://api.example.com/v1/mcp"
assert oauth_provider.client_metadata == client_metadata
assert oauth_provider.storage == mock_storage
assert oauth_provider.timeout == 300.0
def test_generate_code_verifier(self, oauth_provider):
"""Test PKCE code verifier generation."""
verifier = oauth_provider._generate_code_verifier()
# Check length (128 characters)
assert len(verifier) == 128
# Check charset (RFC 7636: A-Z, a-z, 0-9, "-", ".", "_", "~")
allowed_chars = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~")
assert set(verifier) <= allowed_chars
# Check uniqueness (generate multiple and ensure they're different)
verifiers = {oauth_provider._generate_code_verifier() for _ in range(10)}
assert len(verifiers) == 10
@pytest.mark.anyio
async def test_generate_code_challenge(self, oauth_provider):
"""Test PKCE code challenge generation."""
verifier = "test_code_verifier_123"
challenge = oauth_provider._generate_code_challenge(verifier)
# Manually calculate expected challenge
expected_digest = hashlib.sha256(verifier.encode()).digest()
expected_challenge = base64.urlsafe_b64encode(expected_digest).decode().rstrip("=")
assert challenge == expected_challenge
# Verify it's base64url without padding
assert "=" not in challenge
assert "+" not in challenge
assert "/" not in challenge
@pytest.mark.anyio
async def test_get_authorization_base_url(self, oauth_provider):
"""Test authorization base URL extraction."""
# Test with path
assert oauth_provider._get_authorization_base_url("https://api.example.com/v1/mcp") == "https://api.example.com"
# Test with no path
assert oauth_provider._get_authorization_base_url("https://api.example.com") == "https://api.example.com"
# Test with port
assert (
oauth_provider._get_authorization_base_url("https://api.example.com:8080/path/to/mcp")
== "https://api.example.com:8080"
)
@pytest.mark.anyio
async def test_discover_oauth_metadata_success(self, oauth_provider, oauth_metadata):
"""Test successful OAuth metadata discovery."""
metadata_response = oauth_metadata.model_dump(by_alias=True, mode="json")
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = metadata_response
mock_client.get.return_value = mock_response
result = await oauth_provider._discover_oauth_metadata("https://api.example.com/v1/mcp")
assert result is not None
assert result.authorization_endpoint == oauth_metadata.authorization_endpoint
assert result.token_endpoint == oauth_metadata.token_endpoint
# Verify correct URL was called
mock_client.get.assert_called_once()
call_args = mock_client.get.call_args[0]
assert call_args[0] == "https://api.example.com/.well-known/oauth-authorization-server"
@pytest.mark.anyio
async def test_discover_oauth_metadata_not_found(self, oauth_provider):
"""Test OAuth metadata discovery when not found."""
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 404
mock_client.get.return_value = mock_response
result = await oauth_provider._discover_oauth_metadata("https://api.example.com/v1/mcp")
assert result is None
@pytest.mark.anyio
async def test_discover_oauth_metadata_cors_fallback(self, oauth_provider, oauth_metadata):
"""Test OAuth metadata discovery with CORS fallback."""
metadata_response = oauth_metadata.model_dump(by_alias=True, mode="json")
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
# First call fails (CORS), second succeeds
mock_response_success = Mock()
mock_response_success.status_code = 200
mock_response_success.json.return_value = metadata_response
mock_client.get.side_effect = [
TypeError("CORS error"), # First call fails
mock_response_success, # Second call succeeds
]
result = await oauth_provider._discover_oauth_metadata("https://api.example.com/v1/mcp")
assert result is not None
assert mock_client.get.call_count == 2
@pytest.mark.anyio
async def test_register_oauth_client_success(self, oauth_provider, oauth_metadata, oauth_client_info):
"""Test successful OAuth client registration."""
registration_response = oauth_client_info.model_dump(by_alias=True, mode="json")
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 201
mock_response.json.return_value = registration_response
mock_client.post.return_value = mock_response
result = await oauth_provider._register_oauth_client(
"https://api.example.com/v1/mcp",
oauth_provider.client_metadata,
oauth_metadata,
)
assert result.client_id == oauth_client_info.client_id
assert result.client_secret == oauth_client_info.client_secret
# Verify correct registration endpoint was used
mock_client.post.assert_called_once()
call_args = mock_client.post.call_args
assert call_args[0][0] == str(oauth_metadata.registration_endpoint)
@pytest.mark.anyio
async def test_register_oauth_client_fallback_endpoint(self, oauth_provider, oauth_client_info):
"""Test OAuth client registration with fallback endpoint."""
registration_response = oauth_client_info.model_dump(by_alias=True, mode="json")
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 201
mock_response.json.return_value = registration_response
mock_client.post.return_value = mock_response
# Mock metadata discovery to return None (fallback)
with patch.object(oauth_provider, "_discover_oauth_metadata", return_value=None):
result = await oauth_provider._register_oauth_client(
"https://api.example.com/v1/mcp",
oauth_provider.client_metadata,
None,
)
assert result.client_id == oauth_client_info.client_id
# Verify fallback endpoint was used
mock_client.post.assert_called_once()
call_args = mock_client.post.call_args
assert call_args[0][0] == "https://api.example.com/register"
@pytest.mark.anyio
async def test_register_oauth_client_failure(self, oauth_provider):
"""Test OAuth client registration failure."""
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 400
mock_response.text = "Bad Request"
mock_client.post.return_value = mock_response
# Mock metadata discovery to return None (fallback)
with patch.object(oauth_provider, "_discover_oauth_metadata", return_value=None):
with pytest.raises(httpx.HTTPStatusError):
await oauth_provider._register_oauth_client(
"https://api.example.com/v1/mcp",
oauth_provider.client_metadata,
None,
)
@pytest.mark.anyio
async def test_has_valid_token_no_token(self, oauth_provider):
"""Test token validation with no token."""
assert not oauth_provider._has_valid_token()
@pytest.mark.anyio
async def test_has_valid_token_valid(self, oauth_provider, oauth_token):
"""Test token validation with valid token."""
oauth_provider._current_tokens = oauth_token
oauth_provider._token_expiry_time = time.time() + 3600 # Future expiry
assert oauth_provider._has_valid_token()
@pytest.mark.anyio
async def test_has_valid_token_expired(self, oauth_provider, oauth_token):
"""Test token validation with expired token."""
oauth_provider._current_tokens = oauth_token
oauth_provider._token_expiry_time = time.time() - 3600 # Past expiry
assert not oauth_provider._has_valid_token()
@pytest.mark.anyio
async def test_validate_token_scopes_no_scope(self, oauth_provider):
"""Test scope validation with no scope returned."""
token = OAuthToken(access_token="test", token_type="Bearer")
# Should not raise exception
await oauth_provider._validate_token_scopes(token)
@pytest.mark.anyio
async def test_validate_token_scopes_valid(self, oauth_provider, client_metadata):
"""Test scope validation with valid scopes."""
oauth_provider.client_metadata = client_metadata
token = OAuthToken(
access_token="test",
token_type="Bearer",
scope="read write",
)
# Should not raise exception
await oauth_provider._validate_token_scopes(token)
@pytest.mark.anyio
async def test_validate_token_scopes_subset(self, oauth_provider, client_metadata):
"""Test scope validation with subset of requested scopes."""
oauth_provider.client_metadata = client_metadata
token = OAuthToken(
access_token="test",
token_type="Bearer",
scope="read",
)
# Should not raise exception (servers can grant subset)
await oauth_provider._validate_token_scopes(token)
@pytest.mark.anyio
async def test_validate_token_scopes_unauthorized(self, oauth_provider, client_metadata):
"""Test scope validation with unauthorized scopes."""
oauth_provider.client_metadata = client_metadata
token = OAuthToken(
access_token="test",
token_type="Bearer",
scope="read write admin", # Includes unauthorized "admin"
)
with pytest.raises(Exception, match="Server granted unauthorized scopes"):
await oauth_provider._validate_token_scopes(token)
@pytest.mark.anyio
async def test_validate_token_scopes_no_requested(self, oauth_provider):
"""Test scope validation with no requested scopes accepts any server scopes."""
# No scope in client metadata
oauth_provider.client_metadata.scope = None
token = OAuthToken(
access_token="test",
token_type="Bearer",
scope="admin super",
)
# Should not raise exception when no scopes were explicitly requested
# (accepts server defaults)
await oauth_provider._validate_token_scopes(token)
@pytest.mark.anyio
async def test_initialize(self, oauth_provider, mock_storage, oauth_token, oauth_client_info):
"""Test initialization loading from storage."""
mock_storage._tokens = oauth_token
mock_storage._client_info = oauth_client_info
await oauth_provider.initialize()
assert oauth_provider._current_tokens == oauth_token
assert oauth_provider._client_info == oauth_client_info
@pytest.mark.anyio
async def test_get_or_register_client_existing(self, oauth_provider, oauth_client_info):
"""Test getting existing client info."""
oauth_provider._client_info = oauth_client_info
result = await oauth_provider._get_or_register_client()
assert result == oauth_client_info
@pytest.mark.anyio
async def test_get_or_register_client_register_new(self, oauth_provider, oauth_client_info):
"""Test registering new client."""
with patch.object(oauth_provider, "_register_oauth_client", return_value=oauth_client_info) as mock_register:
result = await oauth_provider._get_or_register_client()
assert result == oauth_client_info
assert oauth_provider._client_info == oauth_client_info
mock_register.assert_called_once()
@pytest.mark.anyio
async def test_exchange_code_for_token_success(self, oauth_provider, oauth_client_info, oauth_token):
"""Test successful code exchange for token."""
oauth_provider._code_verifier = "test_verifier"
token_response = oauth_token.model_dump(by_alias=True, mode="json")
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = token_response
mock_client.post.return_value = mock_response
with patch.object(oauth_provider, "_validate_token_scopes") as mock_validate:
await oauth_provider._exchange_code_for_token("test_auth_code", oauth_client_info)
assert oauth_provider._current_tokens.access_token == oauth_token.access_token
mock_validate.assert_called_once()
@pytest.mark.anyio
async def test_exchange_code_for_token_failure(self, oauth_provider, oauth_client_info):
"""Test failed code exchange for token."""
oauth_provider._code_verifier = "test_verifier"
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 400
mock_response.text = "Invalid grant"
mock_client.post.return_value = mock_response
with pytest.raises(Exception, match="Token exchange failed"):
await oauth_provider._exchange_code_for_token("invalid_auth_code", oauth_client_info)
@pytest.mark.anyio
async def test_refresh_access_token_success(self, oauth_provider, oauth_client_info, oauth_token):
"""Test successful token refresh."""
oauth_provider._current_tokens = oauth_token
oauth_provider._client_info = oauth_client_info
new_token = OAuthToken(
access_token="new_access_token",
token_type="Bearer",
expires_in=3600,
refresh_token="new_refresh_token",
scope="read write",
)
token_response = new_token.model_dump(by_alias=True, mode="json")
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = token_response
mock_client.post.return_value = mock_response
with patch.object(oauth_provider, "_validate_token_scopes") as mock_validate:
result = await oauth_provider._refresh_access_token()
assert result is True
assert oauth_provider._current_tokens.access_token == new_token.access_token
mock_validate.assert_called_once()
@pytest.mark.anyio
async def test_refresh_access_token_no_refresh_token(self, oauth_provider):
"""Test token refresh with no refresh token."""
oauth_provider._current_tokens = OAuthToken(
access_token="test",
token_type="Bearer",
# No refresh_token
)
result = await oauth_provider._refresh_access_token()
assert result is False
@pytest.mark.anyio
async def test_refresh_access_token_failure(self, oauth_provider, oauth_client_info, oauth_token):
"""Test failed token refresh."""
oauth_provider._current_tokens = oauth_token
oauth_provider._client_info = oauth_client_info
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_response = Mock()
mock_response.status_code = 400
mock_client.post.return_value = mock_response
result = await oauth_provider._refresh_access_token()
assert result is False
@pytest.mark.anyio
async def test_perform_oauth_flow_success(self, oauth_provider, oauth_metadata, oauth_client_info):
"""Test successful OAuth flow."""
oauth_provider._metadata = oauth_metadata
oauth_provider._client_info = oauth_client_info
# Mock the redirect handler to capture the auth URL
auth_url_captured = None
async def mock_redirect_handler(url: str) -> None:
nonlocal auth_url_captured
auth_url_captured = url
oauth_provider.redirect_handler = mock_redirect_handler
# Mock callback handler with matching state
async def mock_callback_handler() -> tuple[str, str | None]:
# Extract state from auth URL to return matching value
if auth_url_captured:
parsed_url = urlparse(auth_url_captured)
query_params = parse_qs(parsed_url.query)
state = query_params.get("state", [None])[0]
return "test_auth_code", state
return "test_auth_code", "test_state"
oauth_provider.callback_handler = mock_callback_handler
with patch.object(oauth_provider, "_exchange_code_for_token") as mock_exchange:
await oauth_provider._perform_oauth_flow()
# Verify auth URL was generated correctly
assert auth_url_captured is not None
parsed_url = urlparse(auth_url_captured)
query_params = parse_qs(parsed_url.query)
assert query_params["response_type"][0] == "code"
assert query_params["client_id"][0] == oauth_client_info.client_id
assert query_params["code_challenge_method"][0] == "S256"
assert "code_challenge" in query_params
assert "state" in query_params
# Verify code exchange was called
mock_exchange.assert_called_once_with("test_auth_code", oauth_client_info)
@pytest.mark.anyio
async def test_perform_oauth_flow_state_mismatch(self, oauth_provider, oauth_metadata, oauth_client_info):
"""Test OAuth flow with state parameter mismatch."""
oauth_provider._metadata = oauth_metadata
oauth_provider._client_info = oauth_client_info
# Mock callback handler to return mismatched state
async def mock_callback_handler() -> tuple[str, str | None]:
return "test_auth_code", "wrong_state"
oauth_provider.callback_handler = mock_callback_handler
async def mock_redirect_handler(url: str) -> None:
pass
oauth_provider.redirect_handler = mock_redirect_handler
with pytest.raises(Exception, match="State parameter mismatch"):
await oauth_provider._perform_oauth_flow()
@pytest.mark.anyio
async def test_ensure_token_existing_valid(self, oauth_provider, oauth_token):
"""Test ensure_token with existing valid token."""
oauth_provider._current_tokens = oauth_token
oauth_provider._token_expiry_time = time.time() + 3600
await oauth_provider.ensure_token()
# Should not trigger new auth flow
assert oauth_provider._current_tokens == oauth_token
@pytest.mark.anyio
async def test_ensure_token_refresh(self, oauth_provider, oauth_token):
"""Test ensure_token with expired token that can be refreshed."""
oauth_provider._current_tokens = oauth_token
oauth_provider._token_expiry_time = time.time() - 3600 # Expired
with patch.object(oauth_provider, "_refresh_access_token", return_value=True) as mock_refresh:
await oauth_provider.ensure_token()
mock_refresh.assert_called_once()
@pytest.mark.anyio
async def test_ensure_token_full_flow(self, oauth_provider):
"""Test ensure_token triggering full OAuth flow."""
# No existing token
with patch.object(oauth_provider, "_perform_oauth_flow") as mock_flow:
await oauth_provider.ensure_token()
mock_flow.assert_called_once()
@pytest.mark.anyio
async def test_async_auth_flow_add_token(self, oauth_provider, oauth_token):
"""Test async auth flow adding Bearer token to request."""
oauth_provider._current_tokens = oauth_token
oauth_provider._token_expiry_time = time.time() + 3600
request = httpx.Request("GET", "https://api.example.com/data")
# Mock response
mock_response = Mock()
mock_response.status_code = 200
auth_flow = oauth_provider.async_auth_flow(request)
updated_request = await auth_flow.__anext__()
assert updated_request.headers["Authorization"] == f"Bearer {oauth_token.access_token}"
# Send mock response
try:
await auth_flow.asend(mock_response)
except StopAsyncIteration:
pass
@pytest.mark.anyio
async def test_async_auth_flow_401_response(self, oauth_provider, oauth_token):
"""Test async auth flow handling 401 response."""
oauth_provider._current_tokens = oauth_token
oauth_provider._token_expiry_time = time.time() + 3600
request = httpx.Request("GET", "https://api.example.com/data")
# Mock 401 response
mock_response = Mock()
mock_response.status_code = 401
auth_flow = oauth_provider.async_auth_flow(request)
await auth_flow.__anext__()
# Send 401 response
try:
await auth_flow.asend(mock_response)
except StopAsyncIteration:
pass
# Should clear current tokens
assert oauth_provider._current_tokens is None
@pytest.mark.anyio
async def test_async_auth_flow_no_token(self, oauth_provider):
"""Test async auth flow with no token triggers auth flow."""
request = httpx.Request("GET", "https://api.example.com/data")
with (
patch.object(oauth_provider, "initialize") as mock_init,
patch.object(oauth_provider, "ensure_token") as mock_ensure,
):
auth_flow = oauth_provider.async_auth_flow(request)
updated_request = await auth_flow.__anext__()
mock_init.assert_called_once()
mock_ensure.assert_called_once()
# No Authorization header should be added if no token
assert "Authorization" not in updated_request.headers
@pytest.mark.anyio
async def test_scope_priority_client_metadata_first(self, oauth_provider, oauth_client_info):
"""Test that client metadata scope takes priority."""
oauth_provider.client_metadata.scope = "read write"
oauth_provider._client_info = oauth_client_info
oauth_provider._client_info.scope = "admin"
# Build auth params to test scope logic
auth_params = {
"response_type": "code",
"client_id": "test_client",
"redirect_uri": "http://localhost:3000/callback",
"state": "test_state",
"code_challenge": "test_challenge",
"code_challenge_method": "S256",
}
# Apply scope logic from _perform_oauth_flow
if oauth_provider.client_metadata.scope:
auth_params["scope"] = oauth_provider.client_metadata.scope
elif hasattr(oauth_provider._client_info, "scope") and oauth_provider._client_info.scope:
auth_params["scope"] = oauth_provider._client_info.scope
assert auth_params["scope"] == "read write"
@pytest.mark.anyio
async def test_scope_priority_no_client_metadata_scope(self, oauth_provider, oauth_client_info):
"""Test that no scope parameter is set when client metadata has no scope."""
oauth_provider.client_metadata.scope = None
oauth_provider._client_info = oauth_client_info
oauth_provider._client_info.scope = "admin"
# Build auth params to test scope logic
auth_params = {
"response_type": "code",
"client_id": "test_client",
"redirect_uri": "http://localhost:3000/callback",
"state": "test_state",
"code_challenge": "test_challenge",
"code_challenge_method": "S256",
}
# Apply simplified scope logic from _perform_oauth_flow
if oauth_provider.client_metadata.scope:
auth_params["scope"] = oauth_provider.client_metadata.scope
# No fallback to client_info scope in simplified logic
# No scope should be set since client metadata doesn't have explicit scope
assert "scope" not in auth_params
@pytest.mark.anyio
async def test_scope_priority_no_scope(self, oauth_provider, oauth_client_info):
"""Test that no scope parameter is set when no scopes specified."""
oauth_provider.client_metadata.scope = None
oauth_provider._client_info = oauth_client_info
oauth_provider._client_info.scope = None
# Build auth params to test scope logic
auth_params = {
"response_type": "code",
"client_id": "test_client",
"redirect_uri": "http://localhost:3000/callback",
"state": "test_state",
"code_challenge": "test_challenge",
"code_challenge_method": "S256",
}
# Apply scope logic from _perform_oauth_flow
if oauth_provider.client_metadata.scope:
auth_params["scope"] = oauth_provider.client_metadata.scope
elif hasattr(oauth_provider._client_info, "scope") and oauth_provider._client_info.scope:
auth_params["scope"] = oauth_provider._client_info.scope
# No scope should be set
assert "scope" not in auth_params
@pytest.mark.anyio
async def test_state_parameter_validation_uses_constant_time(
self, oauth_provider, oauth_metadata, oauth_client_info
):
"""Test that state parameter validation uses constant-time comparison."""
oauth_provider._metadata = oauth_metadata
oauth_provider._client_info = oauth_client_info
# Mock callback handler to return mismatched state
async def mock_callback_handler() -> tuple[str, str | None]:
return "test_auth_code", "wrong_state"
oauth_provider.callback_handler = mock_callback_handler
async def mock_redirect_handler(url: str) -> None:
pass
oauth_provider.redirect_handler = mock_redirect_handler
# Patch secrets.compare_digest to verify it's being called
with patch("mcp.client.auth.secrets.compare_digest", return_value=False) as mock_compare:
with pytest.raises(Exception, match="State parameter mismatch"):
await oauth_provider._perform_oauth_flow()
# Verify constant-time comparison was used
mock_compare.assert_called_once()
@pytest.mark.anyio
async def test_state_parameter_validation_none_state(self, oauth_provider, oauth_metadata, oauth_client_info):
"""Test that None state is handled correctly."""
oauth_provider._metadata = oauth_metadata
oauth_provider._client_info = oauth_client_info
# Mock callback handler to return None state
async def mock_callback_handler() -> tuple[str, str | None]:
return "test_auth_code", None
oauth_provider.callback_handler = mock_callback_handler
async def mock_redirect_handler(url: str) -> None:
pass
oauth_provider.redirect_handler = mock_redirect_handler
with pytest.raises(Exception, match="State parameter mismatch"):
await oauth_provider._perform_oauth_flow()
@pytest.mark.anyio
async def test_token_exchange_error_basic(self, oauth_provider, oauth_client_info):
"""Test token exchange error handling (basic)."""
oauth_provider._code_verifier = "test_verifier"
with patch("httpx.AsyncClient") as mock_client_class:
mock_client = AsyncMock()
mock_client_class.return_value.__aenter__.return_value = mock_client
# Mock error response
mock_response = Mock()
mock_response.status_code = 400
mock_response.text = "Bad Request"
mock_client.post.return_value = mock_response
with pytest.raises(Exception, match="Token exchange failed"):
await oauth_provider._exchange_code_for_token("invalid_auth_code", oauth_client_info)
@pytest.mark.parametrize(
(
"issuer_url",
"service_documentation_url",
"authorization_endpoint",
"token_endpoint",
"registration_endpoint",
"revocation_endpoint",
),
(
pytest.param(
"https://auth.example.com",
"https://auth.example.com/docs",
"https://auth.example.com/authorize",
"https://auth.example.com/token",
"https://auth.example.com/register",
"https://auth.example.com/revoke",
id="simple-url",
),
pytest.param(
"https://auth.example.com/",
"https://auth.example.com/docs",
"https://auth.example.com/authorize",
"https://auth.example.com/token",
"https://auth.example.com/register",
"https://auth.example.com/revoke",
id="with-trailing-slash",
),
pytest.param(
"https://auth.example.com/v1/mcp",
"https://auth.example.com/v1/mcp/docs",
"https://auth.example.com/v1/mcp/authorize",
"https://auth.example.com/v1/mcp/token",
"https://auth.example.com/v1/mcp/register",
"https://auth.example.com/v1/mcp/revoke",
id="with-path-param",
),
),
)
def test_build_metadata(
issuer_url: str,
service_documentation_url: str,
authorization_endpoint: str,
token_endpoint: str,
registration_endpoint: str,
revocation_endpoint: str,
):
metadata = build_metadata(
issuer_url=AnyHttpUrl(issuer_url),
service_documentation_url=AnyHttpUrl(service_documentation_url),
client_registration_options=ClientRegistrationOptions(enabled=True, valid_scopes=["read", "write", "admin"]),
revocation_options=RevocationOptions(enabled=True),
)
assert metadata == snapshot(
OAuthMetadata(
issuer=AnyHttpUrl(issuer_url),
authorization_endpoint=AnyHttpUrl(authorization_endpoint),
token_endpoint=AnyHttpUrl(token_endpoint),
registration_endpoint=AnyHttpUrl(registration_endpoint),
scopes_supported=["read", "write", "admin"],
grant_types_supported=["authorization_code", "refresh_token"],
token_endpoint_auth_methods_supported=["client_secret_post"],
service_documentation=AnyHttpUrl(service_documentation_url),
revocation_endpoint=AnyHttpUrl(revocation_endpoint),
revocation_endpoint_auth_methods_supported=["client_secret_post"],
code_challenge_methods_supported=["S256"],
)
)