dimas-b commented on code in PR #704:
URL: https://github.com/apache/polaris/pull/704#discussion_r1917476530


##########
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();

Review Comment:
   Thanks for the clarification, @cgpoh !
   
   From my POV, I'd question the current text in the `callContextResolver & 
realmIdResolver` doc section, actually. 
   
   Like I said, getting realm ID from a token is interesting, but it works well 
only for Polaris own tokens. If a token is from an external IdP (a feature 
being discussed on the `dev` mailing list), validating the token may not be as 
trivial. I can imagine that validation may require some knowledge about the 
IdP, which would naturally be part of the "realm" definition.
   
   I'd propose to alter the approach a bit and perhaps try to refactor 
authenticators to also be able to produce realm information. This way, IMHO, 
the runtime flow will be more coherent: 1) the authenticator receives a "raw" 
request; 2) the authenticator uses headers (including Bearer token) to make 
sure that the request is valid (authentic) possibly taking both the realm 
header and JWT into account; 3) the authenticator declares that the request is 
for a) some realm, b) some specific principal.
   
   WDYT?



-- 
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