Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[AS7-2490] Add username/password support when connecting to the server. #5

Closed
wants to merge 1 commit into from

2 participants

@darranl

These values can be specified in the configuration, if not specified the user
will be prompted.

@darranl darranl [AS7-2490] Add username/password support when connecting to the server.
These values can be specified in the configuration, if not specified the user
will be prompted.
951251f
@jamezp
Collaborator

Merged

@jamezp jamezp closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 3, 2011
  1. @darranl

    [AS7-2490] Add username/password support when connecting to the server.

    darranl authored
    These values can be specified in the configuration, if not specified the user
    will be prompted.
This page is out of date. Refresh to see the latest.
View
4 README
@@ -26,6 +26,10 @@ Configuration information can also be specified. The following configurations ar
- <port /> The port the server is listening for administration requests on. The default is 9999.
+- <username /> The username to connect as if prompted to authenticate by the server.
+
+- <password /> The password to use for authentication if prompted by the server.
+
- <force /> Whether or not for force the actions, e.g. if the deploy goal is executed redeploy if it already exists. The default is true.
Goals:
View
22 src/main/java/org/jboss/as/plugin/deployment/common/AbstractServerConnection.java
@@ -56,6 +56,25 @@
*/
private int port;
+ /**
+ * Specifies the username to use if prompted to authenticate by the server.
+ *
+ * If no username is specified and the server requests authentication the user
+ * will be prompted to supply the username,
+ *
+ * @parameter
+ */
+ private String username;
+
+ /**
+ * Specifies the password to use if prompted to authenticate by the server.
+ *
+ * If no password is specified and the server requests authentication the user
+ * will be prompted to supply the password,
+ *
+ * @parameter
+ */
+ private String password;
/**
* The hostname to deploy the archive to. The default is localhost.
@@ -113,7 +132,8 @@ protected final ModelControllerClient client() throws UnknownHostException {
if (client == null) {
synchronized (this) {
if (client == null) {
- client = ModelControllerClient.Factory.create(hostAddress(), port());
+ client = ModelControllerClient.Factory.create(hostAddress(), port(),
+ new ClientCallbackHandler(username, password));
}
}
}
View
107 src/main/java/org/jboss/as/plugin/deployment/common/ClientCallbackHandler.java
@@ -0,0 +1,107 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.as.plugin.deployment.common;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.sasl.RealmCallback;
+import javax.security.sasl.RealmChoiceCallback;
+import java.io.IOException;
+
+/**
+ * A CallbackHandler implementation to supply the username and password if required when
+ * connecting to the server - if these are not available the user will be prompted to
+ * supply them.
+ *
+ * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
+ */
+class ClientCallbackHandler implements CallbackHandler {
+
+ private boolean promptShown = false;
+ private String username;
+ private char[] password;
+
+ ClientCallbackHandler(final String username, final String password) {
+ this.username = username;
+ if (password != null) {
+ this.password = password.toCharArray();
+ }
+ }
+
+ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+ // Special case for anonymous authentication to avoid prompting user for their name.
+ if (callbacks.length == 1 && callbacks[0] instanceof NameCallback) {
+ ((NameCallback) callbacks[0]).setName("anonymous demo user");
+ return;
+ }
+
+ for (Callback current : callbacks) {
+ if (current instanceof RealmCallback) {
+ RealmCallback rcb = (RealmCallback) current;
+ String defaultText = rcb.getDefaultText();
+ rcb.setText(defaultText); // For now just use the realm suggested.
+
+ prompt(defaultText);
+ } else if (current instanceof RealmChoiceCallback) {
+ throw new UnsupportedCallbackException(current, "Realm choice not currently supported.");
+ } else if (current instanceof NameCallback) {
+ NameCallback ncb = (NameCallback) current;
+ String userName = obtainUsername("Username:");
+
+ ncb.setName(userName);
+ } else if (current instanceof PasswordCallback) {
+ PasswordCallback pcb = (PasswordCallback) current;
+ char[] password = obtainPassword("Password:");
+
+ pcb.setPassword(password);
+ } else {
+ throw new UnsupportedCallbackException(current);
+ }
+ }
+ }
+
+ private void prompt(final String realm) {
+ if (promptShown == false) {
+ promptShown = true;
+ System.out.println("Authenticating against security realm: " + realm);
+ }
+ }
+
+ private String obtainUsername(final String prompt) {
+ if (username == null) {
+ username = System.console().readLine(prompt);
+ }
+ return username;
+ }
+
+ private char[] obtainPassword(final String prompt) {
+ if (password == null) {
+ password = System.console().readPassword(prompt);
+ }
+
+ return password;
+ }
+
+}
Something went wrong with that request. Please try again.