cgpoh commented on code in PR #704:
URL: https://github.com/apache/polaris/pull/704#discussion_r1914613389


##########
service/common/src/main/java/org/apache/polaris/service/context/PolarisContextResolver.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.
+ */
+package org.apache.polaris.service.context;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTVerifier;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.exceptions.JWTVerificationException;
+import com.auth0.jwt.interfaces.DecodedJWT;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.polaris.core.context.RealmContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@JsonTypeName("polaris")
+public class PolarisContextResolver implements RealmContextResolver {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PolarisContextResolver.class);
+
+  public static final String REALM_PROPERTY_KEY = "realm";
+
+  public static final String PRINCIPAL_PROPERTY_KEY = "principalId";
+
+  public static final String ISS_PROPERTY_KEY = "iss";
+  public static final String ISS_PROPERTY_DEFAULT_VALUE = "polaris";
+
+  private String defaultRealm = "polaris";
+
+  @Override
+  public RealmContext resolveRealmContext(
+      String requestURL, String method, String path, Map<String, String> 
headers) {
+    final Map<String, String> parsedProperties = 
extractPropsFromBearerToken(headers);
+
+    if (!parsedProperties.containsKey(REALM_PROPERTY_KEY)
+        && headers.containsKey(REALM_PROPERTY_KEY)) {
+      parsedProperties.put(REALM_PROPERTY_KEY, 
headers.get(REALM_PROPERTY_KEY));
+    }
+
+    if (!parsedProperties.containsKey(REALM_PROPERTY_KEY)) {
+      LOGGER.warn(
+          "Failed to parse {} from headers; using {}", REALM_PROPERTY_KEY, 
getDefaultRealm());
+      parsedProperties.put(REALM_PROPERTY_KEY, getDefaultRealm());
+    }
+    return () -> parsedProperties.get(REALM_PROPERTY_KEY);
+  }
+
+  @Override
+  public void setDefaultRealm(String defaultRealm) {
+    this.defaultRealm = defaultRealm;
+  }
+
+  @Override
+  public String getDefaultRealm() {
+    return this.defaultRealm;
+  }
+
+  private static Map<String, String> extractPropsFromBearerToken(Map<String, 
String> headers) {
+    Map<String, String> parsedProperties = new HashMap<>();
+    if (headers != null) {
+      String authHeader = headers.get("Authorization");
+
+      if (authHeader != null) {
+        String[] parts = authHeader.split(" ");
+        if (parts.length == 2 && "Bearer".equalsIgnoreCase(parts[0])) {
+          // Extract the token from the bearer prefix
+          String token = parts[1];
+
+          JWTVerifier verifier =
+              JWT.require(Algorithm.HMAC256("polaris"))
+                  .withIssuer(ISS_PROPERTY_DEFAULT_VALUE)
+                  .build();
+          try {
+            DecodedJWT decodedJWT = verifier.verify(token);
+            String realm = decodedJWT.getClaim(ISS_PROPERTY_KEY).asString();
+            String principalId = 
decodedJWT.getClaim(PRINCIPAL_PROPERTY_KEY).asString();
+
+            parsedProperties.put(REALM_PROPERTY_KEY, realm);
+            parsedProperties.put(PRINCIPAL_PROPERTY_KEY, principalId);

Review Comment:
   Will remove these unused properties



-- 
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: issues-unsubscr...@polaris.apache.org

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

Reply via email to