[java-identity-provider] branch main updated: IDP-1922 - Add more customization to HTTP connection in "simple" CLI

Scott Cantor cantor.2 at osu.edu
Thu Mar 17 18:52:13 UTC 2022


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=1fd7f2b2e8159ec0aa0f07e442f17abeee574659

The following commit(s) were added to refs/heads/main by this push:
     new 1fd7f2b2e IDP-1922 - Add more customization to HTTP connection in "simple" CLI
1fd7f2b2e is described below

commit 1fd7f2b2e8159ec0aa0f07e442f17abeee574659
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Mar 17 14:52:09 2022 -0400

    IDP-1922 - Add more customization to HTTP connection in "simple" CLI
    
    https://shibboleth.atlassian.net/browse/IDP-1922
    
    Factored in basic-auth.
---
 .../idp/cli/AbstractCommandLineArguments.java      | 49 ++++++++++++++++++++++
 .../src/main/java/net/shibboleth/idp/cli/CLI.java  |  9 +++-
 .../shibboleth/idp/cli/CommandLineArguments.java   | 12 ++++++
 3 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/idp-core/src/main/java/net/shibboleth/idp/cli/AbstractCommandLineArguments.java b/idp-core/src/main/java/net/shibboleth/idp/cli/AbstractCommandLineArguments.java
index 7c0e95eef..a034696db 100644
--- a/idp-core/src/main/java/net/shibboleth/idp/cli/AbstractCommandLineArguments.java
+++ b/idp-core/src/main/java/net/shibboleth/idp/cli/AbstractCommandLineArguments.java
@@ -19,6 +19,7 @@ package net.shibboleth.idp.cli;
 
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -27,6 +28,9 @@ import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLSession;
 
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.codec.Base64Support;
+import net.shibboleth.utilities.java.support.codec.EncodingException;
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
 
 import com.beust.jcommander.Parameter;
 import com.beust.jcommander.Parameters;
@@ -69,6 +73,15 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
     @Parameter(names = {"-tp", "--trustStorePassword"}, description = "Password to a trust store for SSL connections")
     @Nullable private String trustStorePassword;
 
+    /** Username to be used in the HTTP-Basic authentication. */
+    @Parameter(names = {"--username"}, required = false, description = "Username to be used in HTTP-Basic Auth")
+    @Nullable private String username;
+
+    /** Password to be used in the HTTP-Basic authentication. */
+    @Parameter(names = {"--password"}, required = false, password = true,
+            description = "Password to be used in HTTP-Basic Auth")
+    @Nullable private String password;
+
     /** Constructor. */
     public AbstractCommandLineArguments() {
         url = System.getProperty(BASEURL_PROPERTY);
@@ -141,6 +154,28 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
     public boolean isDisableNameChecking() {
         return disableNameChecking;
     }
+    
+    /**
+     * Value of "username" parameter.
+     * 
+     * @return username parameter
+     * 
+     * @since 4.2.0
+     */
+    @Nullable public String getUsername() {
+        return username;
+    }
+
+    /**
+     * Value of "password" parameter.
+     * 
+     * @return password parameter
+     * 
+     * @since 4.2.0
+     */
+    @Nullable public String getPassword() {
+        return password;
+    }
 
     /** {@inheritDoc} */
     @Override
@@ -192,6 +227,20 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
         return builder;
     }
     
+    /** {@inheritDoc} */
+    @Nullable public String getBasicAuthHeader() {
+        if (StringSupport.trimOrNull(username) == null || StringSupport.trimOrNull(password) == null) {
+            return null;
+        }
+        final String rawHeader = username + ":" + password;
+        try {
+            return "Basic " + Base64Support.encode(rawHeader.getBytes(StandardCharsets.UTF_8), false);
+        } catch (final EncodingException e) {
+            System.err.print(e.getMessage());
+            return null;
+        }
+    }
+    
     /** Use the configured parameters to set global JVM trust store parameters for SSL connectivity. */
     private void installTrustStore() {
         if (trustStoreType != null) {
diff --git a/idp-core/src/main/java/net/shibboleth/idp/cli/CLI.java b/idp-core/src/main/java/net/shibboleth/idp/cli/CLI.java
index 6ec457292..b62a73114 100644
--- a/idp-core/src/main/java/net/shibboleth/idp/cli/CLI.java
+++ b/idp-core/src/main/java/net/shibboleth/idp/cli/CLI.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.lang.reflect.Constructor;
+import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 
@@ -101,7 +102,13 @@ public final class CLI {
         URL url = null;
         try {
             url = args.buildURL();
-            try (final InputStream stream = url.openStream()) {
+            final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+            final String authorization = args.getBasicAuthHeader();
+            if (authorization != null) {
+                System.out.println("Using HTTP-Basic authentication");
+                connection.setRequestProperty("Authorization", authorization);
+            }
+            try (final InputStream stream = connection.getInputStream()) {
                 try (final InputStreamReader reader = new InputStreamReader(stream)) {
                     try (final BufferedReader in = new BufferedReader(reader)) {
                         String line;
diff --git a/idp-core/src/main/java/net/shibboleth/idp/cli/CommandLineArguments.java b/idp-core/src/main/java/net/shibboleth/idp/cli/CommandLineArguments.java
index de4bb6799..fb41f0cbd 100644
--- a/idp-core/src/main/java/net/shibboleth/idp/cli/CommandLineArguments.java
+++ b/idp-core/src/main/java/net/shibboleth/idp/cli/CommandLineArguments.java
@@ -21,6 +21,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
 /**
  * Interface for JCommander command line argument handling for an HTTP-based remote service call. 
@@ -50,4 +51,15 @@ public interface CommandLineArguments {
      */
     @Nonnull public URL buildURL() throws MalformedURLException;
     
+    /**
+     * Builds the HTTP-Basic value to be used in the Authorization -header, containing username and password.
+     *
+     * @return The value to be used in the Authorization -header, or null if username or password didn't have a value.
+     * 
+     * @since 4.2.0
+     */
+    @Nullable public default String getBasicAuthHeader() {
+        return null;
+    }
+
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list