Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.weblogic.security;
import weblogic.security.principal.WLSGroupImpl;
import weblogic.security.principal.WLSUserImpl;
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.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
import java.io.IOException;
import java.util.Map;
import java.util.Vector;
final public class SampleLoginModule implements LoginModule {
private Subject subject;
private CallbackHandler callbackHandler;
// Determine whether this is a login or assert identity
private boolean loginSucceeded;
private boolean principalsInSubject;
private Vector principalsForSubject = new Vector();
public void initialize(Subject subject, CallbackHandler callbackHandler, Map
sharedState, Map options) {
// only called (once!) after the constructor and before login
System.out.println("SampleLoginModule.initialize");
this.subject = subject;
this.callbackHandler = callbackHandler;
}
@SuppressWarnings({"SimplifiableIfStatement"})
public boolean login() throws LoginException {
// only called (once!) after initialize
System.out.println("SampleLoginModule.login");
Callback[] callbacks = getCallbacks();
if (callbacks == null) {
return loginSucceeded = false;
}
String userName = getUserName(callbacks);
return loginSucceeded = loginEndUser(userName);
}
public boolean loginEndUser(String userName) throws LoginException {
String[] roles = {"user", "ntt", "tt", "fk", "sf", "sr", "admin", "maninv", "digress", "installn", "cl", "prefadmin", "dm"
, "ca", "icp", "createca", "statusreg", "tb", "oper", "uv", "invoicecon", "perftool", "reference", "tr",
"xamine", "nr", "net", "hub", "selfbill", "dynrep", "pqxmltest", "dbom", "gro", "confcostdr", "cirrus_pa", "nlt", "cirrus_br",
"cirrus_CA", "cirrus_cb", "cirrus_ml"};
principalsForSubject.add(new WLSUserImpl(userName));
addGroupsForSubject(roles);
return true;
}
public boolean commit() throws LoginException {
System.out.println("SampleLoginModule.commit");
if (loginSucceeded) {
subject.getPrincipals().addAll(principalsForSubject);
principalsInSubject = true;
System.out.println("SampleLoginModule.commit.true");
return true;
} else {
return false;
}
}
public boolean abort() throws LoginException {
System.out.println("SampleLoginModule.abort");
if (principalsInSubject) {
subject.getPrincipals().removeAll(principalsForSubject);
principalsInSubject = false;
}
return true;
}
public boolean logout() throws LoginException {
// should never be called
System.out.println("SampleLoginModule.logout");
return true;
}
private void throwLoginException(String msg) throws LoginException {
System.out.println("Throwing LoginException(" + msg + ")");
throw new LoginException(msg);
}
private FailedLoginException makeFailedLoginException(String msg) throws FailedLoginException {
System.out.println("Throwing FailedLoginException(" + msg + ")");
return new FailedLoginException(msg);
}
private Callback[] getCallbacks() throws LoginException {
if (callbackHandler == null) {
throwLoginException("No CallbackHandler Specified");
}
Callback[] callbacks;
callbacks = new Callback[4];
callbacks[0] = new NameCallback("username:");
callbacks[1] = new LoginTypeCallback();
try {
callbackHandler.handle(callbacks);
} catch (IOException e) {
throw new LoginException(e.toString());
} catch (UnsupportedCallbackException e) {
System.out.println("Unsupported callback. " + e.getMessage());
return null;
}
return callbacks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment