hsnusonic commented on a change in pull request #3006:
URL: https://github.com/apache/hive/pull/3006#discussion_r829592891



##########
File path: 
service/src/java/org/apache/hive/service/auth/jwt/URLBasedJWKSProvider.java
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.hive.service.auth.jwt;
+
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jose.jwk.JWKMatcher;
+import com.nimbusds.jose.jwk.JWKSelector;
+import com.nimbusds.jose.jwk.JWKSet;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URL;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Provides a way to get JWKS json. Hive will use this to verify the incoming 
JWTs.
+ */
+public class URLBasedJWKSProvider {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(URLBasedJWKSProvider.class.getName());
+  private final HiveConf conf;
+  private List<JWKSet> jwkSets = new ArrayList<>();
+
+  public URLBasedJWKSProvider(HiveConf conf) throws IOException, 
ParseException {
+    this.conf = conf;
+    loadJWKSets();
+  }
+
+  /**
+   * Fetches the JWKS and stores into memory. The JWKS are expected to be in 
the standard form as defined here -
+   * https://datatracker.ietf.org/doc/html/rfc7517#appendix-A.
+   */
+  private void loadJWKSets() throws IOException, ParseException {
+    String jwksURL = HiveConf.getVar(conf, 
HiveConf.ConfVars.HIVE_SERVER2_AUTHENTICATION_JWT_JWKS_URL);
+    String[] jwksURLs = jwksURL.split(",");
+    for (String urlString : jwksURLs) {
+      URL url = new URL(urlString);
+      jwkSets.add(JWKSet.load(url));
+      LOG.info("Loaded JWKS from " + urlString);
+    }
+  }
+
+  /**
+   * Returns filtered JWKS by one or more criteria, such as kid, typ, alg.
+   */
+  public List<JWK> getJWKs(JWSHeader header) {
+    List<JWK> jwks = new ArrayList<>();
+    JWKSelector selector = new JWKSelector(JWKMatcher.forJWSHeader(header));
+    for (JWKSet jwkSet : jwkSets) {
+      List<JWK> selectedJwks = selector.select(jwkSet);
+      if (selectedJwks != null) {

Review comment:
       Thanks for noticing this.




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to