You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kw...@apache.org on 2024/08/16 08:06:20 UTC

(maven-resolver) branch feature/MRESOLVER-495-improve-authenticationselector created (now 6fe261f1)

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a change to branch feature/MRESOLVER-495-improve-authenticationselector
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


      at 6fe261f1 [MRESOLVER-495] Improve Authentication to work based on realm/host

This branch includes the following new commits:

     new 6fe261f1 [MRESOLVER-495] Improve Authentication to work based on realm/host

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(maven-resolver) 01/01: [MRESOLVER-495] Improve Authentication to work based on realm/host

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/MRESOLVER-495-improve-authenticationselector
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git

commit 6fe261f1f5a42d57816a73434d1c410c3688631c
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Aug 16 10:06:12 2024 +0200

    [MRESOLVER-495] Improve Authentication to work based on realm/host
    
    WIP
---
 .../aether/repository/AuthenticationScope.java     | 53 ++++++++++++++++++++++
 .../aether/repository/AuthenticationSelector.java  |  2 +
 ...Selector.java => AuthenticationSelectorV2.java} | 13 ++++--
 .../repository/DefaultAuthenticationSelector.java  | 40 +++++++++++++---
 .../util/repository/DefaultAuthenticator.java      |  7 +++
 5 files changed, 105 insertions(+), 10 deletions(-)

diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationScope.java b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationScope.java
new file mode 100644
index 00000000..83473a3a
--- /dev/null
+++ b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationScope.java
@@ -0,0 +1,53 @@
+package org.eclipse.aether.repository;
+
+import java.net.URI;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Allows to restrict authentication information to certain URIs (optionally considering additional attributes).
+ * This class encapsulates the restrictions in terms of
+ * <ol>
+ * <li>mandatory host pattern</li>
+ * <li>optional port</li>
+ * <li>optional protocol</li>
+ * <li>optional scheme</li>
+ * <li>optional realm</li>
+ * <ol>
+ */
+public class AuthenticationScope {
+
+    private final Pattern hostPattern; // mandatory
+    private final int port;   // optional, -1 if not relevant
+    private final String protocol; // protocol, null of not relevant
+    private final String scheme; // scheme, null of not relevant
+    private final String realm; // realm, null of not relevant
+
+    public AuthenticationScope(Pattern hostPattern, int port, String protocol, String scheme, String realm) {
+        this.hostPattern = hostPattern;
+        this.port = port;
+        this.protocol = protocol;
+        this.scheme = scheme;
+        this.realm = realm;
+    }
+
+    public boolean isMatching(URI uri, String scheme, String realm) {
+        Matcher hostMatcher = hostPattern.matcher(uri.getHost());
+        if (!hostMatcher.matches()) {
+            return false;
+        }
+        if (port != -1 && uri.getPort() != port) {
+            return false;
+        }
+        if (protocol != null && !uri.getScheme().equals(protocol)) {
+            return false;
+        }
+        if (this.scheme != null && !this.scheme.equals(scheme)) {
+            return false;
+        }
+        if (this.realm != null && !this.realm.equals(realm)) {
+            return false;
+        }
+        return true;
+    }
+}
diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelector.java b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelector.java
index 815fb35d..362ae7c6 100644
--- a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelector.java
+++ b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelector.java
@@ -22,7 +22,9 @@ package org.eclipse.aether.repository;
  * Selects authentication for a given remote repository.
  *
  * @see org.eclipse.aether.RepositorySystemSession#getAuthenticationSelector()
+ * @deprecated Use {@link AuthenticationSelectorV2}
  */
+@Deprecated
 public interface AuthenticationSelector {
 
     /**
diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelector.java b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelectorV2.java
similarity index 61%
copy from maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelector.java
copy to maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelectorV2.java
index 815fb35d..0ba27a31 100644
--- a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelector.java
+++ b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/AuthenticationSelectorV2.java
@@ -18,18 +18,23 @@
  */
 package org.eclipse.aether.repository;
 
+import java.net.URI;
+
 /**
  * Selects authentication for a given remote repository.
  *
  * @see org.eclipse.aether.RepositorySystemSession#getAuthenticationSelector()
  */
-public interface AuthenticationSelector {
+public interface AuthenticationSelectorV2 {
 
     /**
-     * Selects authentication for the specified remote repository.
+     * Selects authentication for the specified URI, scheme and realm.
      *
-     * @param repository The repository for which to select authentication, must not be {@code null}.
+     * @param uri the URI of the request for which to select the authentication, must not be {@code null}.
+     * @param scheme the authentication scheme being requested (may be {@code null}), for HTTP  one of the names outlined at <a href="https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml">Hypertext Transfer Protocol (HTTP) Authentication Scheme Registry</a>
+     * @param realm the authentication realm being requested (may be {@code null})
+     * 
      * @return The selected authentication or {@code null} if none.
      */
-    Authentication getAuthentication(RemoteRepository repository);
+    Authentication getAuthentication(URI uri, String scheme, String realm);
 }
diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/DefaultAuthenticationSelector.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/DefaultAuthenticationSelector.java
index 547d4429..c796d8e9 100644
--- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/DefaultAuthenticationSelector.java
+++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/DefaultAuthenticationSelector.java
@@ -18,11 +18,15 @@
  */
 package org.eclipse.aether.util.repository;
 
+import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.eclipse.aether.repository.Authentication;
+import org.eclipse.aether.repository.AuthenticationScope;
 import org.eclipse.aether.repository.AuthenticationSelector;
+import org.eclipse.aether.repository.AuthenticationSelectorV2;
 import org.eclipse.aether.repository.RemoteRepository;
 
 import static java.util.Objects.requireNonNull;
@@ -30,10 +34,11 @@ import static java.util.Objects.requireNonNull;
 /**
  * A simple authentication selector that selects authentication based on repository identifiers.
  */
-public final class DefaultAuthenticationSelector implements AuthenticationSelector {
-
-    private final Map<String, Authentication> repos = new HashMap<>();
+public final class DefaultAuthenticationSelector implements AuthenticationSelector, AuthenticationSelectorV2 {
 
+    private final Map<String, Authentication> reposById = new HashMap<>();
+    private final Map<AuthenticationScope, Authentication> reposByScope = new HashMap<>();
+    
     /**
      * Adds the specified authentication info for the given repository identifier.
      *
@@ -43,9 +48,26 @@ public final class DefaultAuthenticationSelector implements AuthenticationSelect
      */
     public DefaultAuthenticationSelector add(String id, Authentication auth) {
         if (auth != null) {
-            repos.put(id, auth);
+            reposById.put(id, auth);
+        } else {
+            reposById.remove(id);
+        }
+
+        return this;
+    }
+
+    /**
+     * Adds the specified authentication info for the given repository identifier.
+     *
+     * @param scope The scope to add the authentication for, must not be {@code null}.
+     * @param auth The authentication to add, may be {@code null}.
+     * @return This selector for chaining, never {@code null}.
+     */
+    public DefaultAuthenticationSelector add(AuthenticationScope scope, Authentication auth) {
+        if (auth != null) {
+            reposByScope.put(scope, auth);
         } else {
-            repos.remove(id);
+            reposByScope.remove(scope);
         }
 
         return this;
@@ -53,6 +75,12 @@ public final class DefaultAuthenticationSelector implements AuthenticationSelect
 
     public Authentication getAuthentication(RemoteRepository repository) {
         requireNonNull(repository, "repository cannot be null");
-        return repos.get(repository.getId());
+        return reposById.get(repository.getId());
+    }
+
+    @Override
+    public Authentication getAuthentication(URI uri, String scheme, String realm) {
+        requireNonNull(uri, "uri cannot be null");
+        return reposByScope.entrySet().stream().filter(e -> e.getKey().isMatching(uri, scheme, realm)).map(Entry::getValue).findFirst().orElse(null);
     }
 }
diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/DefaultAuthenticator.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/DefaultAuthenticator.java
new file mode 100644
index 00000000..02cc624d
--- /dev/null
+++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/DefaultAuthenticator.java
@@ -0,0 +1,7 @@
+package org.eclipse.aether.util.repository;
+
+import javax.naming.AuthenticationException;
+
+public class DefaultAuthenticator extends Authenticator {
+
+}