use of org.jboss.security.SimplePrincipal in project adempiere by adempiere.
the class AdempiereLoginModule method commit.
/**
* commit/complete the authentication project, add identity and roles to subject.
*/
public boolean commit() throws LoginException {
//note that jboss require all user role to be put under the group Roles
if (roles == null || roles.length == 0) {
//not authenticated or authentication failed
subject.getPrincipals().add(new SimplePrincipal(unauthenticatedIdentity));
SimpleGroup roleGroup = new SimpleGroup("Roles");
subject.getPrincipals().add(roleGroup);
} else {
subject.getPrincipals().add(new SimplePrincipal(name));
SimpleGroup roleGroup = new SimpleGroup("Roles");
//fixed role use in ejb deployment descriptor
roleGroup.addMember(new SimplePrincipal("adempiereUsers"));
//additional security check
for (int i = 0; i < roles.length; i++) {
roleGroup.addMember(new SimplePrincipal(roles[i].getName()));
}
subject.getPrincipals().add(roleGroup);
}
return true;
}
use of org.jboss.security.SimplePrincipal in project wildfly by wildfly.
the class SimpleSecurityManager method push.
/**
* Must be called from within a privileged action.
*
* @param securityDomain
*/
public void push(final String securityDomain) {
// TODO - Handle a null securityDomain here? Yes I think so.
final SecurityContext previous = SecurityContextAssociation.getSecurityContext();
contexts.push(previous);
SecurityContext current = establishSecurityContext(securityDomain);
if (propagate && previous != null) {
current.setSubjectInfo(getSubjectInfo(previous));
current.setIncomingRunAs(previous.getOutgoingRunAs());
}
RunAs currentRunAs = current.getIncomingRunAs();
boolean trusted = currentRunAs != null && currentRunAs instanceof RunAsIdentity;
if (trusted == false) {
/*
* We should only be switching to a context based on an identity from the Remoting connection if we don't already
* have a trusted identity - this allows for beans to reauthenticate as a different identity.
*/
if (SecurityActions.remotingContextIsSet()) {
// In this case the principal and credential will not have been set to set some random values.
SecurityContextUtil util = current.getUtil();
Connection connection = SecurityActions.remotingContextGetConnection();
Principal p = null;
Object credential = null;
SecurityIdentity localIdentity = connection.getLocalIdentity();
if (localIdentity != null) {
p = new SimplePrincipal(localIdentity.getPrincipal().getName());
IdentityCredentials privateCredentials = localIdentity.getPrivateCredentials();
PasswordCredential passwordCredential = privateCredentials.getCredential(PasswordCredential.class, ClearPassword.ALGORITHM_CLEAR);
if (passwordCredential != null) {
credential = new String(passwordCredential.getPassword(ClearPassword.class).getPassword());
} else {
credential = new RemotingConnectionCredential(connection);
}
} else {
throw SecurityLogger.ROOT_LOGGER.noUserPrincipalFound();
}
SecurityActions.remotingContextClear();
util.createSubjectInfo(p, credential, null);
}
}
}
use of org.jboss.security.SimplePrincipal in project wildfly by wildfly.
the class GuestDelegationLoginModule method login.
// Public methods --------------------------------------------------------
@SuppressWarnings("unchecked")
@Override
public boolean login() throws LoginException {
if (super.login() == true) {
log.debug("super.login()==true");
return true;
}
// Time to see if this is a delegation request.
NameCallback ncb = new NameCallback("Username:");
ObjectCallback ocb = new ObjectCallback("Password:");
try {
callbackHandler.handle(new Callback[] { ncb, ocb });
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
// If the CallbackHandler can not handle the required callbacks then no chance.
return false;
}
String name = ncb.getName();
Object credential = ocb.getCredential();
if (credential instanceof CurrentUserCredential) {
// This credential type will only be seen for a delegation request, if not seen then the request is not for us.
final CurrentUserCredential cuCredential = (CurrentUserCredential) credential;
// only the "guest" can be switched to another identity
if ("guest".equals(cuCredential.getUser())) {
identity = new SimplePrincipal(name);
if (getUseFirstPass()) {
String userName = identity.getName();
if (log.isDebugEnabled())
log.debug("Storing username '" + userName + "' and empty password");
// Add the username and an empty password to the shared state map
sharedState.put("javax.security.auth.login.name", identity);
sharedState.put("javax.security.auth.login.password", "");
}
loginOk = true;
return true;
}
}
// Attempted login but not successful.
return false;
}
use of org.jboss.security.SimplePrincipal in project wildfly by wildfly.
the class ServerSecurityInterceptor method aroundInvoke.
@AroundInvoke
public Object aroundInvoke(final InvocationContext invocationContext) throws Exception {
Principal desiredUser = null;
UserPrincipal connectionUser = null;
Map<String, Object> contextData = invocationContext.getContextData();
if (contextData.containsKey(DELEGATED_USER_KEY)) {
desiredUser = new SimplePrincipal((String) contextData.get(DELEGATED_USER_KEY));
Collection<Principal> principals = ConnectionSecurityContext.getConnectionPrincipals();
if (principals != null) {
for (Principal current : principals) {
if (current instanceof UserPrincipal) {
connectionUser = (UserPrincipal) current;
break;
}
}
} else {
throw new IllegalStateException("Delegation user requested but no user on connection found.");
}
}
ContextStateCache stateCache = null;
try {
if (desiredUser != null && connectionUser != null && (desiredUser.getName().equals(connectionUser.getName()) == false)) {
try {
// The final part of this check is to verify that the change does actually indicate a change in user.
// We have been requested to switch user and have successfully identified the user from the connection
// so now we attempt the switch.
stateCache = ConnectionSecurityContext.pushIdentity(desiredUser, new CurrentUserCredential(connectionUser.getName()));
} catch (Exception e) {
LOGGER.error("Failed to switch security context for user", e);
// Don't propagate the exception stacktrace back to the client for security reasons
throw new EJBAccessException("Unable to attempt switching of user.");
}
}
return invocationContext.proceed();
} finally {
// switch back to original security context
if (stateCache != null) {
ConnectionSecurityContext.popIdentity(stateCache);
}
}
}
use of org.jboss.security.SimplePrincipal in project wildfly by wildfly.
the class CustomEjbAccessingLoginModule method commit.
public boolean commit() throws LoginException {
Set<Principal> principals = subject.getPrincipals();
Group callerPrincipal = new SimpleGroup("CallerPrincipal");
callerPrincipal.addMember(new SimplePrincipal(username));
principals.add(callerPrincipal);
Group roles = new SimpleGroup("Roles");
if (username.equals("anil")) {
roles.addMember(new SimplePrincipal("gooduser"));
}
if (username.equals("marcus")) {
roles.addMember(new SimplePrincipal("superuser"));
}
principals.add(roles);
return true;
}
Aggregations