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

Auth module 3.0.2 #323

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion hubble-be/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.8.8</version>
<version>1.8.9</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
Expand Down Expand Up @@ -111,8 +111,17 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
<exclusion>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>1.9.6</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

Expand All @@ -32,6 +33,7 @@
@SpringBootApplication
@EnableScheduling
@MapperScan("com.baidu.hugegraph.mapper")
@ServletComponentScan(basePackages = "com.baidu.hugegraph.filter")
public class HugeGraphHubble extends SpringBootServletInitializer {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ public final class Constant {
);

public static final String[] LIKE_WILDCARDS = {"%", "_", "^", "[", "]"};

public static final String BEARER_TOKEN_PREFIX = "Bearer ";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baidu.hugegraph.driver.HugeClient;
import com.baidu.hugegraph.driver.HugeClientBuilder;
import com.baidu.hugegraph.options.HubbleOptions;
import com.baidu.hugegraph.util.SessionUtil;

import lombok.extern.log4j.Log4j2;

@Log4j2
@Configuration
public class ClientConfiguration {

public static final String AUTH_CLIENT_NAME = "authClient";
public static final String ADMIN_CLIENT_NAME = "adminClient";

@Bean(AUTH_CLIENT_NAME)
public HugeClient authClient(HugeConfig config) {
String authUrl = config.get(HubbleOptions.AUTH_REMOTE_URL);
String authGraph = config.get(HubbleOptions.AUTH_GRAPH);
HugeClient authClient = HugeClient.builder(authUrl, authGraph).build();

SessionUtil.authClient = authClient;

return authClient;
}

@Bean(ADMIN_CLIENT_NAME)
public HugeClient adminClient(HugeConfig config) {
String authUrl = config.get(HubbleOptions.AUTH_REMOTE_URL);
String authGraph = config.get(HubbleOptions.AUTH_GRAPH);
String adminPassword = config.get(HubbleOptions.ADMIN_PASSWORD);
HugeClient adminClient = new HugeClientBuilder(authUrl, authGraph)
.configUser("admin", adminPassword)
.build();

SessionUtil.adminClient = adminClient;

return adminClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return mapper;
}

public static class ResponseSerailizer extends JsonSerializer<Response> {
public static class ResponseSerializer extends JsonSerializer<Response> {

@Override
public void serialize(Response response, JsonGenerator generator,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.controller.project;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baidu.hugegraph.common.Constant;
import com.baidu.hugegraph.entity.project.ProjectEntity;
import com.baidu.hugegraph.service.project.ProjectService;
import com.baidu.hugegraph.util.Ex;
import com.baomidou.mybatisplus.core.metadata.IPage;

@RestController
@RequestMapping(Constant.API_VERSION + "graph-connections/project")
public class ProjectController {

@Autowired
private ProjectService projectService;

@PostMapping
public ProjectEntity createProject(@RequestBody ProjectEntity project) {
this.checkParamsValid(project, true);
return this.projectService.createProject(project);
}

@PutMapping
public ProjectEntity updateProject(@RequestBody ProjectEntity project) {
this.checkParamsValid(project, false);
return this.projectService.updateProject(project);
}

@DeleteMapping("{id}")
public void deleteProject(@PathVariable("id") String projectId) {
this.projectService.deleteProject(projectId);
}

@GetMapping("/list")
public IPage<ProjectEntity> list(@RequestParam(value = "project_name",
required = false)
String projectName,
@RequestParam(name = "page_no",
required = false,
defaultValue = "1")
int pageNo,
@RequestParam(name = "page_size",
required = false,
defaultValue = "10")
int pageSize) {
return this.projectService.list(projectName, pageNo, pageSize);
}

private void checkParamsValid(ProjectEntity entity, boolean create) {
Ex.check(StringUtils.isNotEmpty(entity.getName()),
"common.param.cannot-be-null-or-empty", "project_name");
Ex.check(CollectionUtils.isNotEmpty(entity.getAdminUsers()),
"common.param.cannot-be-null-or-empty", "admin_users");
Ex.check(CollectionUtils.isNotEmpty(entity.getOpUsers()),
"common.param.cannot-be-null-or-empty", "op_users");
if (create) {
Ex.check(entity.getId() == null,
"common.param.must-be-null", "id");
} else {
Ex.check(entity.getId() != null,
"common.param.cannot-be-null", "id");
}
}
}
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
@@ -0,0 +1,64 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.controller.system;

import org.apache.http.HttpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baidu.hugegraph.common.Constant;
import com.baidu.hugegraph.controller.BaseController;
import com.baidu.hugegraph.entity.login.LoginBody;
import com.baidu.hugegraph.entity.login.LoginResult;
import com.baidu.hugegraph.entity.user.UserEntity;
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 AuthService authService;

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

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

@GetMapping("/user")
public UserEntity currentUser(@RequestHeader(HttpHeaders.AUTHORIZATION)
String token) {
E.checkArgumentNotNull(token,
"Request header Authorization must not be null");
return this.authService.getCurrentUser();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.controller.user;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baidu.hugegraph.common.Constant;
import com.baidu.hugegraph.entity.user.UserEntity;
import com.baidu.hugegraph.service.user.UserService;
import com.baidu.hugegraph.util.Ex;
import com.baomidou.mybatisplus.core.metadata.IPage;

@RestController
@RequestMapping(Constant.API_VERSION + "graph-connections/user")
public class UserController {

@Autowired
private UserService userService;

@PostMapping
public UserEntity create(@RequestBody UserEntity user) {
this.checkUser(user, true);
return this.userService.createUser(user);
}

@PutMapping
public UserEntity update(@RequestBody UserEntity user) {
this.checkUser(user, false);
return this.userService.updateUser(user);
}

@DeleteMapping
public void delete(@RequestBody List<String> userIds) {
this.userService.deleteUser(userIds);
}

@GetMapping
public IPage<UserEntity> list(@RequestParam(value = "user_name",
required = false)
String userName,
@RequestParam(name = "page_no",
required = false,
defaultValue = "1")
int pageNo,
@RequestParam(name = "page_size",
required = false,
defaultValue = "10")
int pageSize) {
return this.userService.list(userName, pageNo, pageSize);
}

private void checkUser(UserEntity entity, boolean create) {
Ex.check(StringUtils.isNotEmpty(entity.getUsername()),
"common.param.cannot-be-null-or-empty", "user_name");
Ex.check(StringUtils.isNotEmpty(entity.getPassword()),
"common.param.cannot-be-null-or-empty", "user_password");
if (create) {
Ex.check(entity.getId() == null,
"common.param.must-be-null", "id");
} else {
Ex.check(entity.getId() != null,
"common.param.cannot-be-null", "id");
}
}
}
Loading