|
20 | 20 |
|
21 | 21 | import json |
22 | 22 | import logging |
| 23 | +import os |
23 | 24 |
|
24 | 25 | import pytest |
25 | 26 |
|
@@ -92,6 +93,80 @@ async def test_keycloak_oauth_client_credentials_discovery( |
92 | 93 | logger.info(f" Authorization endpoint: {authorization_endpoint}") |
93 | 94 |
|
94 | 95 |
|
| 96 | +async def test_keycloak_service_account_token_acquisition(keycloak_oauth_client): |
| 97 | + """Test service account token acquisition via client_credentials grant (ADR-002 Tier 1). |
| 98 | +
|
| 99 | + Verifies: |
| 100 | + - Service account token is acquired using client_credentials grant |
| 101 | + - Token response includes access_token, token_type, expires_in |
| 102 | + - Token can be used to access Nextcloud APIs |
| 103 | + - Token type is Bearer |
| 104 | +
|
| 105 | + This test validates ADR-002 Tier 1 implementation for Keycloak external IdP. |
| 106 | +
|
| 107 | + Note: For Nextcloud OIDC app (integrated mode), service account token acquisition |
| 108 | + is not yet implemented. See app.py:631-635 which states "OAuth client for token |
| 109 | + refresh not yet implemented for integrated mode". The KeycloakOAuthClient class |
| 110 | + works with any OIDC provider, so extending support to Nextcloud OIDC app is |
| 111 | + primarily a configuration/initialization issue rather than a fundamental limitation. |
| 112 | + """ |
| 113 | + # Get service account token with standard scopes |
| 114 | + token_response = await keycloak_oauth_client.get_service_account_token( |
| 115 | + scopes=["openid", "profile", "email"] |
| 116 | + ) |
| 117 | + |
| 118 | + # Verify token response structure |
| 119 | + assert "access_token" in token_response, "Missing access_token in response" |
| 120 | + assert "token_type" in token_response, "Missing token_type in response" |
| 121 | + assert "expires_in" in token_response, "Missing expires_in in response" |
| 122 | + |
| 123 | + assert token_response["token_type"].lower() == "bearer", ( |
| 124 | + f"Expected Bearer token type, got {token_response['token_type']}" |
| 125 | + ) |
| 126 | + assert isinstance(token_response["expires_in"], int), ( |
| 127 | + f"Expected integer expires_in, got {type(token_response['expires_in'])}" |
| 128 | + ) |
| 129 | + assert token_response["expires_in"] > 0, ( |
| 130 | + f"Expected positive expires_in, got {token_response['expires_in']}" |
| 131 | + ) |
| 132 | + |
| 133 | + logger.info("✓ Service account token acquired successfully") |
| 134 | + logger.info(f" Token type: {token_response['token_type']}") |
| 135 | + logger.info(f" Expires in: {token_response['expires_in']}s") |
| 136 | + logger.info(f" Scope: {token_response.get('scope', 'N/A')}") |
| 137 | + logger.info(f" Token length: {len(token_response['access_token'])} chars") |
| 138 | + |
| 139 | + # Verify token works with Nextcloud APIs |
| 140 | + # The service account token should be validated by Nextcloud's user_oidc app |
| 141 | + from nextcloud_mcp_server.client import NextcloudClient |
| 142 | + |
| 143 | + nextcloud_host = os.getenv("NEXTCLOUD_HOST", "http://localhost:8080") |
| 144 | + |
| 145 | + # Create a NextcloudClient using the service account token |
| 146 | + nc_client = NextcloudClient.from_token( |
| 147 | + base_url=nextcloud_host, |
| 148 | + token=token_response["access_token"], |
| 149 | + username="service-account-nextcloud-mcp-server", # Keycloak service account username |
| 150 | + ) |
| 151 | + |
| 152 | + try: |
| 153 | + # Verify token works with Nextcloud API (using OCS endpoint which works without patch) |
| 154 | + capabilities = await nc_client.capabilities() |
| 155 | + assert capabilities is not None, ( |
| 156 | + "Failed to get capabilities with service account token" |
| 157 | + ) |
| 158 | + |
| 159 | + logger.info("✓ Service account token works with Nextcloud APIs") |
| 160 | + logger.info( |
| 161 | + f" Nextcloud version: {capabilities.get('version', {}).get('string', 'unknown')}" |
| 162 | + ) |
| 163 | + |
| 164 | + finally: |
| 165 | + await nc_client.close() |
| 166 | + |
| 167 | + logger.info("✓ ADR-002 Tier 1 (Service Account Token) validated for Keycloak") |
| 168 | + |
| 169 | + |
95 | 170 | # ============================================================================ |
96 | 171 | # MCP Server Connectivity Tests |
97 | 172 | # ============================================================================ |
|
0 commit comments