60 lines
3.8 KiB
Python
60 lines
3.8 KiB
Python
import base64
|
|
import hashlib
|
|
from urllib.parse import parse_qs, urlparse
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import TestCase
|
|
from oauth2_provider.models import Application, AccessToken
|
|
|
|
from .models import PersonalAccessToken
|
|
|
|
|
|
class McpOAuthTests(TestCase):
|
|
def test_authorization_code_issues_hashed_pat_and_revoke_synchronizes(self):
|
|
user = get_user_model().objects.create_user('oauth-test', password='test')
|
|
application = Application.objects.create(user=user, name='Test MCP client', client_type=Application.CLIENT_PUBLIC,
|
|
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
|
|
redirect_uris='http://localhost:9876/callback')
|
|
self.client.force_login(user)
|
|
verifier = 'v' * 64
|
|
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).decode().rstrip('=')
|
|
params = {'response_type': 'code', 'client_id': application.client_id,
|
|
'redirect_uri': 'http://localhost:9876/callback', 'scope': 'food:read food:write',
|
|
'state': 'test-state', 'code_challenge': challenge, 'code_challenge_method': 'S256',
|
|
'resource': 'http://localhost:8002/mcp'}
|
|
response = self.client.get('/o/authorize/', params, HTTP_HOST='localhost')
|
|
self.assertEqual(response.status_code, 200, response.content[:300])
|
|
form = response.context['form']
|
|
payload = {field.name: field.value() for field in form if field.field.widget.is_hidden and field.value() is not None}
|
|
payload['allow'] = 'Authorize'
|
|
response = self.client.post('/o/authorize/', payload, HTTP_HOST='localhost')
|
|
self.assertEqual(response.status_code, 302, response.content[:300])
|
|
code = parse_qs(urlparse(response['Location']).query)['code'][0]
|
|
response = self.client.post('/o/token/', {'grant_type': 'authorization_code', 'code': code,
|
|
'redirect_uri': 'http://localhost:9876/callback', 'client_id': application.client_id,
|
|
'code_verifier': verifier, 'resource': 'http://localhost:8002/mcp'}, HTTP_HOST='localhost')
|
|
self.assertEqual(response.status_code, 200, response.content[:300])
|
|
secret = response.json()['access_token']
|
|
self.assertTrue(secret.startswith('tn_'))
|
|
pat = PersonalAccessToken.objects.get(user=user, token_hash=hashlib.sha256(secret.encode()).hexdigest())
|
|
oauth = AccessToken.objects.get(user=user, token_checksum=pat.token_hash)
|
|
self.assertEqual(oauth.token, '')
|
|
self.assertEqual(self.client.get('/api/v1/auth/whoami/', HTTP_AUTHORIZATION=f'Bearer {secret}',
|
|
HTTP_HOST='localhost').status_code, 200)
|
|
refresh = response.json()['refresh_token']
|
|
response = self.client.post('/o/token/', {'grant_type': 'refresh_token',
|
|
'refresh_token': refresh, 'client_id': application.client_id,
|
|
'resource': 'http://localhost:8002/mcp'}, HTTP_HOST='localhost')
|
|
self.assertEqual(response.status_code, 200, response.content[:300])
|
|
renewed = response.json()['access_token']
|
|
self.assertNotEqual(secret, renewed)
|
|
self.assertEqual(self.client.get('/api/v1/auth/whoami/', HTTP_AUTHORIZATION=f'Bearer {secret}',
|
|
HTTP_HOST='localhost').status_code, 401)
|
|
secret = renewed
|
|
pat = PersonalAccessToken.objects.get(user=user, token_hash=hashlib.sha256(secret.encode()).hexdigest())
|
|
oauth = AccessToken.objects.get(user=user, token_checksum=pat.token_hash)
|
|
self.client.post(f'/food/settings/tokens/{pat.pk}/revoke/', HTTP_HOST='localhost')
|
|
self.assertFalse(AccessToken.objects.filter(pk=oauth.pk).exists())
|
|
self.assertEqual(self.client.get('/api/v1/auth/whoami/', HTTP_AUTHORIZATION=f'Bearer {secret}',
|
|
HTTP_HOST='localhost').status_code, 401)
|