Skip to content

Commit

Permalink
temp add
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed Sep 22, 2024
1 parent edbd1f0 commit 8665be1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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.<String> emptySet(), principal.getCredential()));
Account account = identityManager.verify(new AccountImpl(principal));
if (account == null) {
account = new AccountImpl(accountName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, ?> newSharedState,
Map<String, ?> 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;
}
}

0 comments on commit 8665be1

Please sign in to comment.