Skip to content

Commit

Permalink
feat:white api when choose auth mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SunnyBoy-WYH committed Aug 26, 2023
1 parent c90bbe9 commit 03583fa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

package org.apache.hugegraph.api.filter;

import static org.apache.hugegraph.config.ServerOptions.WHITE_IP_STATUS;

import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.xml.bind.DatatypeConverter;

import org.apache.commons.lang3.StringUtils;
import org.apache.hugegraph.auth.HugeAuthenticator;
import org.apache.hugegraph.auth.HugeAuthenticator.RequiredPerm;
import org.apache.hugegraph.auth.HugeAuthenticator.RolePerm;
Expand All @@ -40,10 +42,12 @@
import org.glassfish.grizzly.utils.Charsets;
import org.slf4j.Logger;

import com.alipay.remoting.util.StringUtils;
import com.google.common.collect.ImmutableList;

import jakarta.annotation.Priority;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.ForbiddenException;
import jakarta.ws.rs.NotAuthorizedException;
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.container.ContainerRequestContext;
Expand Down Expand Up @@ -72,7 +76,8 @@ public class AuthenticationFilter implements ContainerRequestFilter {

private static String whiteIpStatus;

private static String STRING_WHITE_IP_LIST = "whiteiplist";
private static final String STRING_WHITE_IP_LIST = "whiteiplist";
private static final String STRING_ENABLE = "enable";

@Context
private jakarta.inject.Provider<GraphManager> managerProvider;
Expand Down Expand Up @@ -111,25 +116,25 @@ protected User authenticate(ContainerRequestContext context) {
path = request.getRequestURI();
}

//if (whiteIpStatus == null) {
// whiteIpStatus = this.configProvider.get().get(WHITE_IP_STATUS);
//}
//
//if (Objects.equals(whiteIpStatus, "enable") && request != null) {
// peer = request.getRemoteAddr() + ":" + request.getRemotePort();
// path = request.getRequestURI();
//
// // check white ip
// String remoteIp = request.getRemoteAddr();
// List<String> whiteIpList = manager.authManager().listWhiteIp();
// boolean whiteIpEnabled = manager.authManager().getWhiteIpStatus();
// if (!path.contains(STRING_WHITE_IP_LIST) && whiteIpEnabled &&
// !whiteIpList.contains(remoteIp)) {
// throw new ForbiddenException(
// String.format("Remote ip '%s' is not permitted",
// remoteIp));
// }
//}
// Check whiteIp
if (whiteIpStatus == null) {
whiteIpStatus = this.configProvider.get().get(WHITE_IP_STATUS);
}

if (Objects.equals(whiteIpStatus, STRING_ENABLE) && request != null) {
peer = request.getRemoteAddr() + ":" + request.getRemotePort();
path = request.getRequestURI();

String remoteIp = request.getRemoteAddr();
List<String> whiteIpList = manager.authManager().listWhiteIp();
boolean whiteIpEnabled = manager.authManager().getWhiteIpStatus();
if (!path.contains(STRING_WHITE_IP_LIST) && whiteIpEnabled &&
!whiteIpList.contains(remoteIp)) {
throw new ForbiddenException(
String.format("Remote ip '%s' is not permitted",
remoteIp));
}
}

Map<String, String> credentials = new HashMap<>();
// Extract authentication credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ public Map<String, Object> list(@Context GraphManager manager) {
public Map<String, Object> batch(@Context GraphManager manager,
Map<String, Object> actionMap) {
E.checkArgument(actionMap != null,
"Missing argument: actionMap");
"Missing argument: actionMap");
List<String> whiteIpList = manager.authManager().listWhiteIp();
List<String> ipList = (List) actionMap.get("ips");
Object ips = actionMap.get("ips");
E.checkArgument(ips instanceof List,
"Invalid ips type '%s', must be list", ips.getClass());
List<String> ipList = (List<String>) ips;
Object value = actionMap.get("action");
E.checkArgument(value != null,
"Missing argument: action");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,26 @@ public UserWithRole validateUser(String token) {
}
}

@Override
public List<String> listWhiteIp() {
return this.authManager.listWhiteIp();
}

@Override
public void setWhiteIpList(List<String> whiteIpList) {
this.authManager.setWhiteIpList(whiteIpList);
}

@Override
public boolean getWhiteIpStatus() {
return this.authManager.getWhiteIpStatus();
}

@Override
public void setWhiteIpStatus(boolean status) {
this.authManager.setWhiteIpStatus(status);
}

@Override
public String loginUser(String username, String password) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public interface AuthManager {

UserWithRole validateUser(String token);

public List<String> listWhiteIp();
List<String> listWhiteIp();

public void setWhiteIpList(List<String> whiteIpList);
void setWhiteIpList(List<String> whiteIpList);

public boolean getWhiteIpStatus();
boolean getWhiteIpStatus();

public void setWhiteIpStatus(boolean status);
void setWhiteIpStatus(boolean status);
}

0 comments on commit 03583fa

Please sign in to comment.