flyrain commented on code in PR #39:
URL: https://github.com/apache/polaris-tools/pull/39#discussion_r2520360636


##########
mcp-server/polaris_mcp/authorization.py:
##########
@@ -0,0 +1,136 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+"""Authorization helpers for the Polaris MCP server."""
+
+from __future__ import annotations
+
+import json
+import threading
+import time
+from abc import ABC, abstractmethod
+from typing import Optional
+from urllib.parse import urlencode
+
+import urllib3
+
+
+class AuthorizationProvider(ABC):
+    """Return Authorization header values for outgoing requests."""
+
+    @abstractmethod
+    def authorization_header(self) -> Optional[str]:
+        ...
+
+
+class StaticAuthorizationProvider(AuthorizationProvider):
+    """Wrap a static bearer token."""
+
+    def __init__(self, token: Optional[str]) -> None:
+        value = (token or "").strip()
+        self._header = f"Bearer {value}" if value else None
+
+    def authorization_header(self) -> Optional[str]:
+        return self._header
+
+
+class ClientCredentialsAuthorizationProvider(AuthorizationProvider):
+    """Implements the OAuth client-credentials flow with caching."""
+
+    def __init__(
+        self,
+        token_endpoint: str,
+        client_id: str,
+        client_secret: str,
+        scope: Optional[str],
+        http: urllib3.PoolManager,
+    ) -> None:
+        self._token_endpoint = token_endpoint
+        self._client_id = client_id
+        self._client_secret = client_secret
+        self._scope = scope
+        self._http = http
+        self._lock = threading.Lock()
+        self._cached: Optional[tuple[str, float]] = None  # (token, 
expires_at_epoch)
+
+    def authorization_header(self) -> Optional[str]:
+        token = self._current_token()
+        return f"Bearer {token}" if token else None
+
+    def _current_token(self) -> Optional[str]:
+        now = time.time()
+        cached = self._cached
+        if not cached or cached[1] - 60 <= now:

Review Comment:
   #47 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to