You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2020/03/20 08:14:10 UTC

[GitHub] [cxf] coheigea commented on a change in pull request #652: CXF-8243 JwtAccessTokenValidator using the JWK Set URL

coheigea commented on a change in pull request #652: CXF-8243 JwtAccessTokenValidator using the JWK Set URL
URL: https://github.com/apache/cxf/pull/652#discussion_r395485495
 
 

 ##########
 File path: rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/filters/JwsJwksJwtAccessTokenValidator.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.cxf.rs.security.oauth2.filters;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Predicate;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.rs.security.jose.jaxrs.JsonWebKeysProvider;
+import org.apache.cxf.rs.security.jose.jwk.JsonWebKey;
+import org.apache.cxf.rs.security.jose.jwk.JsonWebKeys;
+import org.apache.cxf.rs.security.jose.jwk.PublicKeyUse;
+import org.apache.cxf.rs.security.jose.jws.JwsHeaders;
+import org.apache.cxf.rs.security.jose.jws.JwsSignatureVerifier;
+import org.apache.cxf.rs.security.jose.jws.JwsUtils;
+
+public class JwsJwksJwtAccessTokenValidator extends JwtAccessTokenValidator {
+
+    final Map<String, JwkHolder> jsonWebKeys = new ConcurrentHashMap<>();
+
+    private String jwksURL;
+
+    @Override
+    protected JwsSignatureVerifier getInitializedSignatureVerifier(JwsHeaders jwsHeaders) {
+        Objects.requireNonNull(jwsHeaders.getKeyId());
+        return jsonWebKeys.computeIfAbsent(jwsHeaders.getKeyId(), keyId -> updateJwk(keyId)).getJwsSignatureVerifier();
+    }
+
+    public void setJwksURL(String jwksURL) {
+        this.jwksURL = jwksURL;
+    }
+
+    @Override
+    public void setJwsVerifier(JwsSignatureVerifier theJwsVerifier) {
+        throw new IllegalArgumentException("Actual JwsSignatureVerifier will be populated from the JWK Set URL");
+    }
+
+    private JwkHolder updateJwk(String keyId) {
+        Objects.requireNonNull(jwksURL, "JWK Set URL must be specified");
+        JwkHolder jwkHolder = null;
+        final Set<String> kids = new HashSet<>();
+        for (JsonWebKey jwk : getJsonWebKeys().getKeys()) {
+            if (PublicKeyUse.ENCRYPT != jwk.getPublicKeyUse()) {
+                final String kid = jwk.getKeyId();
+                kids.add(kid);
+                final JwkHolder h = new JwkHolder(jwk);
+                if (keyId.equals(kid)) {
+                    jwkHolder = h;
+                } else {
+                    jsonWebKeys.putIfAbsent(kid, h);
 
 Review comment:
   I find it hard to follow the logic in this method. Why is the JwkHolder added to jsonWebKeys for kid if the key ids don't match (and then removed on line 77?)

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services