From 2f1a48bd3b74d6d404ed2fc1df9fc2f0ec74426e Mon Sep 17 00:00:00 2001 From: Enrique da Costa Cambio Date: Tue, 21 Jan 2014 18:51:56 -0800 Subject: [PATCH] Static username and password authentication added as a launcher option --- .../java/org/littleshoot/proxy/Launcher.java | 38 +++++++++++++++---- .../proxy/impl/StaticProxyAuthenticator.java | 31 +++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/littleshoot/proxy/impl/StaticProxyAuthenticator.java diff --git a/src/main/java/org/littleshoot/proxy/Launcher.java b/src/main/java/org/littleshoot/proxy/Launcher.java index f9b654a0e..398d9e49a 100755 --- a/src/main/java/org/littleshoot/proxy/Launcher.java +++ b/src/main/java/org/littleshoot/proxy/Launcher.java @@ -1,5 +1,9 @@ package org.littleshoot.proxy; +import java.io.File; +import java.net.InetSocketAddress; +import java.util.Arrays; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; @@ -12,20 +16,18 @@ import org.littleshoot.proxy.extras.SelfSignedMitmManager; import org.littleshoot.proxy.impl.DefaultHttpProxyServer; import org.littleshoot.proxy.impl.ProxyUtils; +import org.littleshoot.proxy.impl.StaticProxyAuthenticator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.net.InetSocketAddress; -import java.util.Arrays; - /** * Launches a new HTTP proxy. */ public class Launcher { - private static final Logger LOG = LoggerFactory.getLogger(Launcher.class); + private static final String DEFAULT_REALM = "LittleProxy"; + private static final String OPTION_DNSSEC = "dnssec"; private static final String OPTION_PORT = "port"; @@ -36,9 +38,15 @@ public class Launcher { private static final String OPTION_NIC = "nic"; + private static final String OPTION_REALM = "realm"; + + private static final String OPTION_USERNAME = "username"; + + private static final String OPTION_PASSWORD = "password"; + /** * Starts the proxy from the command line. - * + * * @param args * Any command line arguments. */ @@ -53,7 +61,13 @@ public static void main(final String... args) { options.addOption(null, OPTION_HELP, false, "Display command line help."); options.addOption(null, OPTION_MITM, false, "Run as man in the middle."); - + options.addOption(null, OPTION_USERNAME, true, + "Use a static authenticator with the speficied username."); + options.addOption(null, OPTION_PASSWORD, true, + "If " + OPTION_USERNAME + " is set then use this password, otherwise use a blank password."); + options.addOption(null, OPTION_REALM, true, + "If " + OPTION_USERNAME + " is set then use this realm, otherwise use '" + DEFAULT_REALM + "'."); + final CommandLineParser parser = new PosixParser(); final CommandLine cmd; try { @@ -98,11 +112,19 @@ public static void main(final String... args) { bootstrap.withNetworkInterface(new InetSocketAddress(val, 0)); } + if (cmd.hasOption(OPTION_USERNAME)) { + LOG.info("Running with static authenticator"); + final String userName = cmd.getOptionValue(OPTION_USERNAME); + final String password = cmd.hasOption(OPTION_PASSWORD) ? cmd.getOptionValue(OPTION_PASSWORD) : ""; + final String realm = cmd.hasOption(OPTION_REALM) ? cmd.getOptionValue(OPTION_REALM) : DEFAULT_REALM; + bootstrap.withProxyAuthenticator(new StaticProxyAuthenticator(userName, password, realm)); + } + if (cmd.hasOption(OPTION_MITM)) { LOG.info("Running as Man in the Middle"); bootstrap.withManInTheMiddle(new SelfSignedMitmManager()); } - + if (cmd.hasOption(OPTION_DNSSEC)) { final String val = cmd.getOptionValue(OPTION_DNSSEC); if (ProxyUtils.isTrue(val)) { diff --git a/src/main/java/org/littleshoot/proxy/impl/StaticProxyAuthenticator.java b/src/main/java/org/littleshoot/proxy/impl/StaticProxyAuthenticator.java new file mode 100644 index 000000000..5a072fb9f --- /dev/null +++ b/src/main/java/org/littleshoot/proxy/impl/StaticProxyAuthenticator.java @@ -0,0 +1,31 @@ +package org.littleshoot.proxy.impl; + +import org.littleshoot.proxy.ProxyAuthenticator; + +/** + * Basic authenticator with static username and password + */ +public class StaticProxyAuthenticator implements ProxyAuthenticator { + private final String userName; + + private final String password; + + private final String realm; + + public StaticProxyAuthenticator(String userName, String password, String realm) { + super(); + this.userName = userName; + this.password = password; + this.realm = realm; + } + + @Override + public boolean authenticate(String userName, String password) { + return this.userName.equals(userName) && this.password.equals(password); + } + + @Override + public String getRealm() { + return realm; + } +}