From 8665be1b429b89c71c33e09696e6d882ffcd02fc Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Sat, 28 Nov 2015 17:11:39 -0500 Subject: [PATCH] temp add --- .../WaffleAuthenticationMechanism.java | 28 ++++--- .../waffle/wildfly/WaffleLogonModule.java | 78 +++++++++++++++++++ 2 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 Source/JNA/waffle-wildfly/src/main/java/waffle/wildfly/WaffleLogonModule.java diff --git a/Source/JNA/waffle-wildfly/src/main/java/waffle/wildfly/WaffleAuthenticationMechanism.java b/Source/JNA/waffle-wildfly/src/main/java/waffle/wildfly/WaffleAuthenticationMechanism.java index faccb9e7ba..69934734e2 100644 --- a/Source/JNA/waffle-wildfly/src/main/java/waffle/wildfly/WaffleAuthenticationMechanism.java +++ b/Source/JNA/waffle-wildfly/src/main/java/waffle/wildfly/WaffleAuthenticationMechanism.java @@ -14,9 +14,9 @@ package waffle.wildfly; import java.security.Principal; -import java.util.Collections; import java.util.Map; +import javax.security.auth.login.LoginException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -32,6 +32,7 @@ import io.undertow.server.HttpServerExchange; import io.undertow.server.handlers.form.FormParserFactory; import io.undertow.servlet.handlers.ServletRequestContext; +import waffle.jaas.WindowsLoginModule; import waffle.windows.auth.impl.WindowsAccountImpl; /** @@ -76,10 +77,10 @@ public class WaffleAuthenticationMechanism implements AuthenticationMechanism { @Override public AuthenticationMechanismOutcome authenticate(final HttpServerExchange exchange, final SecurityContext securityContext) { - final ServletRequestContext servletRequestContext = exchange + final ServletRequestContext context = exchange .getAttachment(ServletRequestContext.ATTACHMENT_KEY); - final HttpServletRequest request = servletRequestContext.getOriginalRequest(); - final HttpServletResponse response = servletRequestContext.getOriginalResponse(); + final HttpServletRequest request = context.getOriginalRequest(); + final HttpServletResponse response = context.getOriginalResponse(); /** Temp to see what's in context **/ // WaffleHandler handler = new WaffleHandler(); @@ -88,12 +89,20 @@ public AuthenticationMechanismOutcome authenticate(final HttpServerExchange exch // } catch (Exception e1) { // // Do nothing // } + WaffleLogonModule logon = new WaffleLogonModule(); + try { + logon.login(); + } catch (LoginException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + Principal principal = null; String accountName = WindowsAccountImpl.getCurrentUsername(); try { // If accountName is null try using authenticator but that requires my hard-coded identity. - if (accountName == null && this.authenticator.authenticate(request, response)) { - final Principal principal = this.authenticator.doLogin(WindowsAccountImpl.getCurrentUsername(), "XXXXXXXX"); + if (!this.authenticator.authenticate(request, response)) { + principal = this.authenticator.doLogin(WindowsAccountImpl.getCurrentUsername(), "Hookah0%"); accountName = principal == null ? null : principal.getName(); } } catch (final Exception e) { @@ -105,11 +114,12 @@ public AuthenticationMechanismOutcome authenticate(final HttpServerExchange exch return AuthenticationMechanismOutcome.NOT_AUTHENTICATED; } - final SimplePrincipal principal = new SimplePrincipal(accountName, String.valueOf(UPTIME)); + if (principal == null) { + principal = new SimplePrincipal(accountName, String.valueOf(UPTIME)); + } final IdentityManager identityManager = securityContext.getIdentityManager(); - Account account = identityManager - .verify(new AccountImpl(principal, Collections. emptySet(), principal.getCredential())); + Account account = identityManager.verify(new AccountImpl(principal)); if (account == null) { account = new AccountImpl(accountName); } diff --git a/Source/JNA/waffle-wildfly/src/main/java/waffle/wildfly/WaffleLogonModule.java b/Source/JNA/waffle-wildfly/src/main/java/waffle/wildfly/WaffleLogonModule.java new file mode 100644 index 0000000000..e52c241ed3 --- /dev/null +++ b/Source/JNA/waffle-wildfly/src/main/java/waffle/wildfly/WaffleLogonModule.java @@ -0,0 +1,78 @@ +package waffle.wildfly; + +import javax.security.auth.Subject; +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.login.LoginException; + +import waffle.jaas.WindowsLoginModule; + +import java.security.Principal; +import java.util.Map; + +public class WaffleLogonModule extends WindowsLoginModule { + + private Subject subject; + private Principal principal; + private Map sharedState; + private CallbackHandler callbackHandler; + + @Override + public void initialize(Subject newSubject, CallbackHandler newCallbackHandler, Map newSharedState, + Map options) { + this.subject = newSubject; + this.sharedState = newSharedState; + this.callbackHandler = newCallbackHandler; + } + + @Override + @SuppressWarnings("unchecked") + public boolean login() throws LoginException { + + NameCallback nc = new NameCallback("name"); + PasswordCallback pc = new PasswordCallback("password", false); + try { + this.callbackHandler.handle(new Callback[] { nc, pc }); + } catch (Exception x) { + throw new LoginException(x.getMessage()); + } + + String name = nc.getName(); + char[] passwordChar = pc.getPassword(); + String credential = passwordChar != null ? new String(passwordChar) : null; + + long loginTime = Long.parseLong(credential); + if (loginTime < WaffleAuthenticationMechanism.UPTIME) { + return false; + } + + SimplePrincipal simplePrincipal = new SimplePrincipal(name, credential); + + this.sharedState.put("javax.security.auth.login.name", simplePrincipal.getName()); + this.sharedState.put("javax.security.auth.login.password", simplePrincipal.getCredential()); + + this.principal = simplePrincipal; + return true; + } + + @Override + public boolean commit() throws LoginException { + if (this.principal == null) { + return false; + } + this.subject.getPrincipals().add(this.principal); + return true; + } + + @Override + public boolean abort() throws LoginException { + return false; + } + + @Override + public boolean logout() throws LoginException { + return false; + } +}