mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-11 12:03:09 +00:00
Fix dspace-api module per new code style
This commit is contained in:
@@ -13,7 +13,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@@ -42,27 +41,34 @@ import org.dspace.services.factory.DSpaceServicesFactory;
|
||||
* 111.222,-111.222.333.
|
||||
* <p>
|
||||
* For supported IP ranges see {@link org.dspace.authenticate.IPMatcher}.
|
||||
*
|
||||
* @version $Revision$
|
||||
*
|
||||
* @author Robert Tansley
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class IPAuthentication implements AuthenticationMethod
|
||||
{
|
||||
/** Our logger */
|
||||
public class IPAuthentication implements AuthenticationMethod {
|
||||
/**
|
||||
* Our logger
|
||||
*/
|
||||
private static Logger log = Logger.getLogger(IPAuthentication.class);
|
||||
|
||||
/** Whether to look for x-forwarded headers for logging IP addresses */
|
||||
/**
|
||||
* Whether to look for x-forwarded headers for logging IP addresses
|
||||
*/
|
||||
protected static Boolean useProxies;
|
||||
|
||||
/** All the IP matchers */
|
||||
/**
|
||||
* All the IP matchers
|
||||
*/
|
||||
protected List<IPMatcher> ipMatchers;
|
||||
|
||||
/** All the negative IP matchers */
|
||||
/**
|
||||
* All the negative IP matchers
|
||||
*/
|
||||
protected List<IPMatcher> ipNegativeMatchers;
|
||||
|
||||
protected GroupService groupService;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Maps IPMatchers to group names when we don't know group DB ID yet. When
|
||||
* the DB ID is known, the IPMatcher is moved to ipMatcherGroupIDs and then
|
||||
@@ -70,112 +76,94 @@ public class IPAuthentication implements AuthenticationMethod
|
||||
*/
|
||||
protected Map<IPMatcher, String> ipMatcherGroupNames;
|
||||
|
||||
/** Maps IPMatchers to group IDs (Integers) where we know the group DB ID */
|
||||
/**
|
||||
* Maps IPMatchers to group IDs (Integers) where we know the group DB ID
|
||||
*/
|
||||
protected Map<IPMatcher, UUID> ipMatcherGroupIDs;
|
||||
|
||||
/**
|
||||
* Initialize an IP authenticator, reading in the configuration. Note this
|
||||
* will never fail if the configuration is bad -- a warning will be logged.
|
||||
*/
|
||||
public IPAuthentication()
|
||||
{
|
||||
public IPAuthentication() {
|
||||
ipMatchers = new ArrayList<IPMatcher>();
|
||||
ipNegativeMatchers = new ArrayList<IPMatcher>();
|
||||
ipMatcherGroupIDs = new HashMap<>();
|
||||
ipMatcherGroupNames = new HashMap<>();
|
||||
groupService = EPersonServiceFactory.getInstance().getGroupService();
|
||||
|
||||
List<String> propNames = DSpaceServicesFactory.getInstance().getConfigurationService().getPropertyKeys("authentication-ip");
|
||||
List<String> propNames = DSpaceServicesFactory.getInstance().getConfigurationService()
|
||||
.getPropertyKeys("authentication-ip");
|
||||
|
||||
for(String propName : propNames)
|
||||
{
|
||||
for (String propName : propNames) {
|
||||
String[] nameParts = propName.split("\\.");
|
||||
|
||||
if (nameParts.length == 2)
|
||||
{
|
||||
addMatchers(nameParts[1], DSpaceServicesFactory.getInstance().getConfigurationService().getArrayProperty(propName));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (nameParts.length == 2) {
|
||||
addMatchers(nameParts[1],
|
||||
DSpaceServicesFactory.getInstance().getConfigurationService().getArrayProperty(propName));
|
||||
} else {
|
||||
log.warn("Malformed configuration property name: "
|
||||
+ propName);
|
||||
+ propName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add matchers for the given comma-delimited IP ranges and group.
|
||||
*
|
||||
* @param groupName
|
||||
* name of group
|
||||
* @param ipRanges
|
||||
* IP ranges
|
||||
*
|
||||
* @param groupName name of group
|
||||
* @param ipRanges IP ranges
|
||||
*/
|
||||
protected void addMatchers(String groupName, String[] ipRanges)
|
||||
{
|
||||
for (String entry : ipRanges)
|
||||
{
|
||||
try
|
||||
{
|
||||
protected void addMatchers(String groupName, String[] ipRanges) {
|
||||
for (String entry : ipRanges) {
|
||||
try {
|
||||
IPMatcher ipm;
|
||||
if (entry.startsWith("-"))
|
||||
{
|
||||
if (entry.startsWith("-")) {
|
||||
ipm = new IPMatcher(entry.substring(1));
|
||||
ipNegativeMatchers.add(ipm);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ipm = new IPMatcher(entry);
|
||||
ipMatchers.add(ipm);
|
||||
}
|
||||
ipMatcherGroupNames.put(ipm, groupName);
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Configured " + entry + " for special group "
|
||||
+ groupName);
|
||||
+ groupName);
|
||||
}
|
||||
}
|
||||
catch (IPMatcherException ipme)
|
||||
{
|
||||
} catch (IPMatcherException ipme) {
|
||||
log.warn("Malformed IP range specified for group " + groupName,
|
||||
ipme);
|
||||
ipme);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSelfRegister(Context context, HttpServletRequest request,
|
||||
String username) throws SQLException
|
||||
{
|
||||
String username) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initEPerson(Context context, HttpServletRequest request,
|
||||
EPerson eperson) throws SQLException
|
||||
{
|
||||
EPerson eperson) throws SQLException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowSetPassword(Context context,
|
||||
HttpServletRequest request, String username) throws SQLException
|
||||
{
|
||||
HttpServletRequest request, String username) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isImplicit()
|
||||
{
|
||||
public boolean isImplicit() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Group> getSpecialGroups(Context context, HttpServletRequest request)
|
||||
throws SQLException
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throws SQLException {
|
||||
if (request == null) {
|
||||
return ListUtils.EMPTY_LIST;
|
||||
}
|
||||
List<Group> groups = new ArrayList<Group>();
|
||||
@@ -185,119 +173,91 @@ public class IPAuthentication implements AuthenticationMethod
|
||||
if (useProxies == null) {
|
||||
useProxies = ConfigurationManager.getBooleanProperty("useProxies", false);
|
||||
}
|
||||
if (useProxies && request.getHeader("X-Forwarded-For") != null)
|
||||
{
|
||||
if (useProxies && request.getHeader("X-Forwarded-For") != null) {
|
||||
/* This header is a comma delimited list */
|
||||
for(String xfip : request.getHeader("X-Forwarded-For").split(","))
|
||||
{
|
||||
if(!request.getHeader("X-Forwarded-For").contains(addr))
|
||||
{
|
||||
for (String xfip : request.getHeader("X-Forwarded-For").split(",")) {
|
||||
if (!request.getHeader("X-Forwarded-For").contains(addr)) {
|
||||
addr = xfip.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (IPMatcher ipm : ipMatchers)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ipm.match(addr))
|
||||
{
|
||||
for (IPMatcher ipm : ipMatchers) {
|
||||
try {
|
||||
if (ipm.match(addr)) {
|
||||
// Do we know group ID?
|
||||
UUID g = ipMatcherGroupIDs.get(ipm);
|
||||
if (g != null)
|
||||
{
|
||||
if (g != null) {
|
||||
groups.add(groupService.find(context, g));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// See if we have a group name
|
||||
String groupName = ipMatcherGroupNames.get(ipm);
|
||||
|
||||
if (groupName != null)
|
||||
{
|
||||
if (groupName != null) {
|
||||
Group group = groupService.findByName(context, groupName);
|
||||
if (group != null)
|
||||
{
|
||||
if (group != null) {
|
||||
// Add ID so we won't have to do lookup again
|
||||
ipMatcherGroupIDs.put(ipm, (group.getID()));
|
||||
ipMatcherGroupNames.remove(ipm);
|
||||
|
||||
groups.add(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
log.warn(LogManager.getHeader(context,
|
||||
"configuration_error", "unknown_group="
|
||||
+ groupName));
|
||||
"configuration_error", "unknown_group="
|
||||
+ groupName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IPMatcherException ipme)
|
||||
{
|
||||
} catch (IPMatcherException ipme) {
|
||||
log.warn(LogManager.getHeader(context, "configuration_error",
|
||||
"bad_ip=" + addr), ipme);
|
||||
"bad_ip=" + addr), ipme);
|
||||
}
|
||||
}
|
||||
|
||||
// Now remove any negative matches
|
||||
for (IPMatcher ipm : ipNegativeMatchers)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ipm.match(addr))
|
||||
{
|
||||
for (IPMatcher ipm : ipNegativeMatchers) {
|
||||
try {
|
||||
if (ipm.match(addr)) {
|
||||
// Do we know group ID?
|
||||
UUID g = ipMatcherGroupIDs.get(ipm);
|
||||
if (g != null)
|
||||
{
|
||||
if (g != null) {
|
||||
groups.remove(groupService.find(context, g));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// See if we have a group name
|
||||
String groupName = ipMatcherGroupNames.get(ipm);
|
||||
|
||||
if (groupName != null)
|
||||
{
|
||||
if (groupName != null) {
|
||||
Group group = groupService.findByName(context, groupName);
|
||||
if (group != null)
|
||||
{
|
||||
if (group != null) {
|
||||
// Add ID so we won't have to do lookup again
|
||||
ipMatcherGroupIDs.put(ipm, group.getID());
|
||||
ipMatcherGroupNames.remove(ipm);
|
||||
|
||||
groups.remove(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
log.warn(LogManager.getHeader(context,
|
||||
"configuration_error", "unknown_group="
|
||||
+ groupName));
|
||||
"configuration_error", "unknown_group="
|
||||
+ groupName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IPMatcherException ipme)
|
||||
{
|
||||
} catch (IPMatcherException ipme) {
|
||||
log.warn(LogManager.getHeader(context, "configuration_error",
|
||||
"bad_ip=" + addr), ipme);
|
||||
"bad_ip=" + addr), ipme);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
if (log.isDebugEnabled()) {
|
||||
StringBuilder gsb = new StringBuilder();
|
||||
for (Group group : groups) {
|
||||
gsb.append(group.getID()).append(", ");
|
||||
}
|
||||
|
||||
log.debug(LogManager.getHeader(context, "authenticated",
|
||||
"special_groups=" + gsb.toString()));
|
||||
"special_groups=" + gsb.toString()));
|
||||
}
|
||||
|
||||
return groups;
|
||||
@@ -305,16 +265,13 @@ public class IPAuthentication implements AuthenticationMethod
|
||||
|
||||
@Override
|
||||
public int authenticate(Context context, String username, String password,
|
||||
String realm, HttpServletRequest request) throws SQLException
|
||||
{
|
||||
String realm, HttpServletRequest request) throws SQLException {
|
||||
return BAD_ARGS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String loginPageURL(Context context, HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
{
|
||||
HttpServletResponse response) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user