bugraoz93 commented on code in PR #51657:
URL: https://github.com/apache/airflow/pull/51657#discussion_r2164785376


##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py:
##########
@@ -324,3 +326,40 @@ def _get_headers(access_token):
             "Authorization": f"Bearer {access_token}",
             "Content-Type": "application/x-www-form-urlencoded",
         }
+
+    @staticmethod
+    def get_keycloak_client() -> KeycloakOpenID:
+        client_id = conf.get(CONF_SECTION_NAME, CONF_CLIENT_ID_KEY)
+        client_secret = conf.get(CONF_SECTION_NAME, CONF_CLIENT_SECRET_KEY)
+        realm = conf.get(CONF_SECTION_NAME, CONF_REALM_KEY)
+        server_url = conf.get(CONF_SECTION_NAME, CONF_SERVER_URL_KEY)
+
+        return KeycloakOpenID(
+            server_url=server_url,
+            client_id=client_id,
+            client_secret_key=client_secret,
+            realm_name=realm,
+        )
+
+    def refresh_token(self, user: KeycloakAuthManagerUser) -> 
KeycloakAuthManagerUser | None:
+        """Refresh the access token for the user."""
+        if self._is_token_expired(user=user) and user.access_token and 
user.refresh_token:
+            client = self.get_keycloak_client()
+            tokens = client.refresh_token(user.refresh_token)
+            user.access_token = tokens["access_token"]
+            user.refresh_token = tokens["refresh_token"]
+        return user
+
+    def _is_token_expired(self, user: KeycloakAuthManagerUser) -> bool:
+        """Check if the access token is expired."""
+        if not user.access_token:
+            return False
+        client = self.get_keycloak_client()
+        try:
+            result = client.introspect(user.access_token)
+            if not result["active"]:
+                log.warning("Nonono is not active")

Review Comment:
   Yes, I forgot and needed to away from keyboard for a while :) 
   https://github.com/apache/airflow/pull/51657#discussion_r2162444609



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to