Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add login module #314

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hubble-be/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>1.9.4</version>
<version>1.9.5</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AuthClientConfiguration {
@Bean(AUTH_CLIENT_NAME)
public HugeClient authClient(HugeConfig config) {
String authUrl = config.get(HubbleOptions.AUTH_REMOTE_URL);
String authGraph = config.get(HubbleOptions.AUTH_GRAPH_STORE);
String authGraph = config.get(HubbleOptions.AUTH_GRAPH);
return HugeClient.builder(authUrl, authGraph).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import com.baidu.hugegraph.entity.schema.UsingCheckEntity;
import com.baidu.hugegraph.exception.ExternalException;
import com.baidu.hugegraph.service.schema.PropertyKeyService;
import com.baidu.hugegraph.util.HubbleUtil;
import com.baidu.hugegraph.util.Ex;
import com.baidu.hugegraph.util.HubbleUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;

import lombok.extern.log4j.Log4j2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,31 @@
import com.baidu.hugegraph.entity.login.LoginBody;
import com.baidu.hugegraph.entity.login.LoginResult;
import com.baidu.hugegraph.entity.user.HubbleUser;
import com.baidu.hugegraph.service.system.LoginService;
import com.baidu.hugegraph.service.system.TokenService;
import com.baidu.hugegraph.service.system.AuthService;
import com.baidu.hugegraph.util.E;

@RestController
@RequestMapping(Constant.API_VERSION + "graph-connections/login")
public class LoginController extends BaseController {

@Autowired
private LoginService loginService;
@Autowired
private TokenService tokenService;
private AuthService authService;

@PostMapping
public LoginResult login(@RequestBody LoginBody loginBody) {
return this.loginService.login(loginBody);
return this.authService.login(loginBody);
}

@DeleteMapping
public void logout() {
this.loginService.logout();
this.authService.logout();
}

@GetMapping("/user")
public HubbleUser currentUser(@RequestHeader(HttpHeaders.AUTHORIZATION)
String token) {
E.checkArgumentNotNull(token,
"Request header Authorization must not be null");
return this.tokenService.getUser();
return this.authService.getCurrentUser();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public abstract class LabelUpdateEntity implements Typifiable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class HubbleUser extends AuthElement {
@JsonProperty("user_email")
private String email;
corgiboygsj marked this conversation as resolved.
Show resolved Hide resolved

@JsonProperty("user_description")
private String description;

@JsonProperty("user_groups")
private List<Group> groups;
}
76 changes: 71 additions & 5 deletions hubble-be/src/main/java/com/baidu/hugegraph/filter/AuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package com.baidu.hugegraph.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import java.util.function.Supplier;

import javax.annotation.Resource;
import javax.servlet.Filter;
Expand All @@ -29,19 +32,32 @@
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baidu.hugegraph.driver.HugeClient;
import com.baidu.hugegraph.common.Constant;
import com.baidu.hugegraph.common.Response;
import com.baidu.hugegraph.config.AuthClientConfiguration;
import com.baidu.hugegraph.driver.HugeClient;
import com.baidu.hugegraph.util.JsonUtil;
import com.google.common.collect.ImmutableSet;

import lombok.extern.log4j.Log4j2;

@Log4j2
@WebFilter(filterName = "authFilter", urlPatterns = "/*")
public class AuthFilter implements Filter {

private static final String BEARER_TOKEN_PREFIX = "Bearer ";

private static final Set<String> WHITE_API = ImmutableSet.of(
buildPath(RequestMethod.POST,
Constant.API_VERSION + "graph-connections/login")
);

@Resource(name = AuthClientConfiguration.AUTH_CLIENT_NAME)
private HugeClient client;

Expand All @@ -51,15 +67,65 @@ public void doFilter(ServletRequest servletRequest,
FilterChain filterChain)
throws IOException, ServletException {
try {
String authorization = ((HttpServletRequest) servletRequest)
.getHeader(HttpHeaders.AUTHORIZATION);
if (StringUtils.isNotEmpty(authorization)) {
this.client.setAuthContext(authorization);
HttpServletRequest request = (HttpServletRequest) servletRequest;
String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);

// Missed token and request uri not in white list
if (StringUtils.isEmpty(authorization) && !isWhiteAPI(request)) {
String msg = "Missed authorization token";
writeResponse(servletResponse, () -> {
return Response.builder()
.status(Constant.STATUS_BAD_REQUEST)
.message(msg)
.build();
});
return;
}
// Illegal token format
if (StringUtils.isNotEmpty(authorization) &&
!authorization.startsWith(BEARER_TOKEN_PREFIX)) {
String msg = "Only HTTP Bearer authentication is supported";
writeResponse(servletResponse, () -> {
return Response.builder()
.status(Constant.STATUS_BAD_REQUEST)
.message(msg)
.build();
});
return;
}

this.client.setAuthContext(authorization);

filterChain.doFilter(servletRequest, servletResponse);
} finally {
this.client.resetAuthContext();
}
}

private static String buildPath(RequestMethod method, String path) {
return buildPath(method.name(), path);
}

private static String buildPath(String method, String path) {
return String.join(":", method, path);
}

private static boolean isWhiteAPI(HttpServletRequest request) {
String url = request.getRequestURI();
return WHITE_API.contains(buildPath(request.getMethod(), url));
}

private void writeResponse(ServletResponse servletResponse,
Supplier<Response> responseSupplier) {
Response response = responseSupplier.get();

servletResponse.setCharacterEncoding("UTF-8");
servletResponse.setContentType(MediaType.APPLICATION_JSON);

try (PrintWriter writer = servletResponse.getWriter()) {
writer.print(JsonUtil.toJson(response));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems can call servletResponse.sendError()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not have this api

} catch (IOException e) {
log.error("Response error",e);
corgiboygsj marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public static synchronized HubbleOptions instance() {
"http://127.0.0.1:8080"
);

public static final ConfigOption<String> AUTH_GRAPH_STORE =
public static final ConfigOption<String> AUTH_GRAPH =
new ConfigOption<>(
"auth.graph",
"The graph name of auth-server.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@

import org.springframework.stereotype.Service;

import com.baidu.hugegraph.config.AuthClientConfiguration;
import com.baidu.hugegraph.driver.HugeClient;
import com.baidu.hugegraph.entity.login.LoginBody;
import com.baidu.hugegraph.config.AuthClientConfiguration;
import com.baidu.hugegraph.entity.login.LoginResult;
import com.baidu.hugegraph.entity.user.HubbleUser;
import com.baidu.hugegraph.structure.auth.Login;
import com.baidu.hugegraph.structure.auth.TokenPayload;
import com.baidu.hugegraph.structure.auth.User;

@Service
public class LoginService {
public class AuthService {

@Resource(name = AuthClientConfiguration.AUTH_CLIENT_NAME)
private HugeClient authClient;
Expand All @@ -49,4 +52,19 @@ public LoginResult login(LoginBody loginEntity) {
public void logout() {
this.authClient.auth().logout();
}

public HubbleUser getCurrentUser() {
TokenPayload payload = this.authClient.auth().verifyToken();
String userId = payload.userId();
User user = this.authClient.auth().getUser(userId);

// TODO Set user auth info
return HubbleUser.builder()
.username(user.name())
.password(user.password())
.phone(user.phone())
.email(user.email())
.description(user.description())
.build();
}
}

This file was deleted.