Skip to content

Commit

Permalink
Issue #5272 Remove unneeded o.e.j.jaas.spi.User class
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Nov 12, 2020
1 parent 49b7e35 commit 7c0358f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public abstract class AbstractDatabaseLoginModule extends AbstractLoginModule
*/
public abstract Connection getConnection() throws Exception;

public class JDBCUser extends User
public class JDBCUser extends JAASUser
{
public JDBCUser(UserPrincipal user)
{
Expand Down Expand Up @@ -103,7 +103,7 @@ public JAASUser getUser(String userName)
if (dbCredential == null)
return null;

return new JAASUser(new JDBCUser(new UserPrincipal(userName, Credential.getCredential(dbCredential))));
return new JDBCUser(new UserPrincipal(userName, Credential.getCredential(dbCredential)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
package org.eclipse.jetty.jaas.spi;

import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
Expand All @@ -34,10 +34,10 @@
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

import org.eclipse.jetty.jaas.JAASPrincipal;
import org.eclipse.jetty.jaas.JAASRole;
import org.eclipse.jetty.jaas.callback.ObjectCallback;
import org.eclipse.jetty.security.UserPrincipal;
import org.eclipse.jetty.util.thread.AutoLock;

/**
* AbstractLoginModule
Expand All @@ -54,31 +54,30 @@ public abstract class AbstractLoginModule implements LoginModule
private JAASUser currentUser;
private Subject subject;

public class JAASUser
public abstract class JAASUser
{
private User _user;
private UserPrincipal _user;
private List<JAASRole> _roles;

public JAASUser(User u)
public JAASUser(UserPrincipal u)
{
_user = u;
}

public String getUserName()
{
return _user.getUserName();
return _user.getName();
}

/**
* @param subject The subject
*/
public void setJAASInfo(Subject subject)
{
UserPrincipal principal = _user.getUserPrincipal();
if (principal == null)
if (_user == null)
return;

principal.configureSubject(subject);
_user.configureSubject(subject);
if (_roles != null)
subject.getPrincipals().addAll(_roles);
}
Expand All @@ -88,32 +87,26 @@ public void setJAASInfo(Subject subject)
*/
public void unsetJAASInfo(Subject subject)
{
UserPrincipal principal = _user.getUserPrincipal();
if (principal == null)
if (_user == null)
return;
principal.deconfigureSubject(subject);
_user.deconfigureSubject(subject);
if (_roles != null)
subject.getPrincipals().removeAll(_roles);
}

public boolean checkCredential(Object suppliedCredential)
{
return _user.checkCredential(suppliedCredential);
return _user.authenticate(suppliedCredential);
}

public void fetchRoles() throws Exception
{
_user.fetchRoles();
_roles = new ArrayList<JAASRole>();
if (_user.getRoleNames() != null)
{
Iterator<String> itor = _user.getRoleNames().iterator();
while (itor.hasNext())
{
_roles.add(new JAASRole(itor.next()));
}
}
List<String> rolenames = doFetchRoles();
if (rolenames != null)
_roles = rolenames.stream().map(JAASRole::new).collect(Collectors.toList());
}

public abstract List<String> doFetchRoles() throws Exception;
}

public abstract JAASUser getUser(String username) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,10 @@ public class LdapLoginModule extends AbstractLoginModule

private DirContext _rootContext;

public class LDAPUser extends User
public class LDAPUser extends JAASUser
{
Attributes attributes;

public LDAPUser(UserPrincipal user, List<String> rolenames, Attributes attributes)
{
super(user, rolenames);
this.attributes = attributes;
}

public LDAPUser(UserPrincipal user, Attributes attributes)
{
super(user);
Expand All @@ -203,7 +197,7 @@ public List<String> doFetchRoles() throws Exception
}
}

public class LDAPBindingUser extends User
public class LDAPBindingUser extends JAASUser
{
DirContext _context;
String _userDn;
Expand Down Expand Up @@ -245,8 +239,7 @@ public JAASUser getUser(String username) throws Exception

pwdCredential = convertCredentialLdapToJetty(pwdCredential);
Credential credential = Credential.getCredential(pwdCredential);
LDAPUser ldapUser = new LDAPUser(new UserPrincipal(username, credential), attributes);
return new JAASUser(ldapUser);
return new LDAPUser(new UserPrincipal(username, credential), attributes);
}

protected String doRFC2254Encoding(String inputString)
Expand Down Expand Up @@ -540,8 +533,7 @@ public boolean bindingLogin(String username, Object password) throws LoginExcept
try
{
DirContext dirContext = new InitialDirContext(environment);
LDAPBindingUser userInfo = new LDAPBindingUser(new UserPrincipal(username, null), dirContext, userDn);
setCurrentUser(new JAASUser(userInfo));
setCurrentUser(new LDAPBindingUser(new UserPrincipal(username, null), dirContext, userDn));
setAuthenticated(true);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public JAASUser getUser(String userName) throws Exception

List<RolePrincipal> rps = _store.getRolePrincipals(userName);
List<String> roles = rps == null ? Collections.emptyList() : rps.stream().map(RolePrincipal::getName).collect(Collectors.toList());
return new JAASUser(new User(up, roles));
return new JAASUser(up)
{
@Override
public List<String> doFetchRoles()
{
return roles;
}
};
}
}
109 changes: 0 additions & 109 deletions jetty-jaas/src/main/java/org/eclipse/jetty/jaas/spi/User.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

package org.eclipse.jetty.jaas;

import java.util.Collections;
import java.util.List;
import javax.security.auth.callback.Callback;
import javax.security.auth.login.LoginException;

import org.eclipse.jetty.jaas.callback.ServletRequestCallback;
import org.eclipse.jetty.jaas.spi.AbstractLoginModule;
import org.eclipse.jetty.jaas.spi.User;
import org.eclipse.jetty.security.UserPrincipal;
import org.eclipse.jetty.util.ArrayUtil;
import org.eclipse.jetty.util.security.Password;
Expand All @@ -37,7 +38,14 @@ public class TestLoginModule extends AbstractLoginModule
@Override
public JAASUser getUser(String username) throws Exception
{
return new JAASUser(new User(new UserPrincipal(username, new Password("aaa"))));
return new JAASUser(new UserPrincipal(username, new Password("aaa")))
{
@Override
public List<String> doFetchRoles() throws Exception
{
return Collections.emptyList();
}
};
}

@Override
Expand Down

0 comments on commit 7c0358f

Please sign in to comment.