Skip to content
Permalink
Browse files
Add login api (#125)
  • Loading branch information
corgiboygsj committed Jun 25, 2021
1 parent 1fa5d8f commit 7cce85e036658f87377a90a9885135d6b837efcd
Showing 21 changed files with 661 additions and 17 deletions.
@@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-16.04
env:
TRAVIS_DIR: assembly/travis
COMMIT_ID: 23e3c31a5445cd472e16c6a625a119ca0ab4b04d
COMMIT_ID: 9ee9fe12f3349a54bbceb161d002200d3689863c
steps:
- name: Install JDK 8
uses: actions/setup-java@v2
@@ -5,7 +5,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>1.9.4</version>
<version>1.9.5</version>
<packaging>jar</packaging>

<name>hugegraph-client</name>
@@ -53,7 +53,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source>1.8</compiler.source>
<compiler.target>1.8</compiler.target>
<hugegraph.common.version>1.8.4</hugegraph.common.version>
<hugegraph.common.version>1.8.9</hugegraph.common.version>
<jersey.version>2.22</jersey.version>
<mockito.version>2.8.47</mockito.version>
</properties>
@@ -0,0 +1,43 @@
/*
* 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.api.auth;

import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.structure.auth.Login;
import com.baidu.hugegraph.structure.auth.LoginResult;
import com.baidu.hugegraph.structure.constant.HugeType;

public class LoginAPI extends AuthAPI {

public LoginAPI(RestClient client, String graph) {
super(client, graph);
}

@Override
protected String type() {
return HugeType.LOGIN.string();
}

public LoginResult login(Login login) {
RestResult result = this.client.post(this.path(), login);
return result.readObject(LoginResult.class);
}
}
@@ -0,0 +1,40 @@
/*
* 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.api.auth;

import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.structure.constant.HugeType;
import com.google.common.collect.ImmutableMap;

public class LogoutAPI extends AuthAPI {

public LogoutAPI(RestClient client, String graph) {
super(client, graph);
}

@Override
protected String type() {
return HugeType.LOGOUT.string();
}

public void logout() {
this.client.delete(this.path(), ImmutableMap.of());
}
}
@@ -0,0 +1,42 @@
/*
* 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.api.auth;

import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.structure.auth.TokenPayload;
import com.baidu.hugegraph.structure.constant.HugeType;

public class TokenAPI extends AuthAPI {

public TokenAPI(RestClient client, String graph) {
super(client, graph);
}

@Override
protected String type() {
return HugeType.TOKEN_VERIFY.string();
}

public TokenPayload verifyToken() {
RestResult result = this.client.get(this.path());
return result.readObject(TokenPayload.class);
}
}
@@ -24,30 +24,42 @@
import com.baidu.hugegraph.api.auth.AccessAPI;
import com.baidu.hugegraph.api.auth.BelongAPI;
import com.baidu.hugegraph.api.auth.GroupAPI;
import com.baidu.hugegraph.api.auth.LoginAPI;
import com.baidu.hugegraph.api.auth.LogoutAPI;
import com.baidu.hugegraph.api.auth.TargetAPI;
import com.baidu.hugegraph.api.auth.TokenAPI;
import com.baidu.hugegraph.api.auth.UserAPI;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.structure.auth.Access;
import com.baidu.hugegraph.structure.auth.Belong;
import com.baidu.hugegraph.structure.auth.Group;
import com.baidu.hugegraph.structure.auth.Login;
import com.baidu.hugegraph.structure.auth.LoginResult;
import com.baidu.hugegraph.structure.auth.Target;
import com.baidu.hugegraph.structure.auth.TokenPayload;
import com.baidu.hugegraph.structure.auth.User;
import com.baidu.hugegraph.structure.auth.User.UserRole;

public class AuthManager {

private TargetAPI targetAPI;
private GroupAPI groupAPI;
private UserAPI userAPI;
private AccessAPI accessAPI;
private BelongAPI belongAPI;
private final TargetAPI targetAPI;
private final GroupAPI groupAPI;
private final UserAPI userAPI;
private final AccessAPI accessAPI;
private final BelongAPI belongAPI;
private final LoginAPI loginAPI;
private final LogoutAPI logoutAPI;
private final TokenAPI tokenAPI;

public AuthManager(RestClient client, String graph) {
this.targetAPI = new TargetAPI(client, graph);
this.groupAPI = new GroupAPI(client, graph);
this.userAPI = new UserAPI(client, graph);
this.accessAPI = new AccessAPI(client, graph);
this.belongAPI = new BelongAPI(client, graph);
this.loginAPI = new LoginAPI(client, graph);
this.logoutAPI = new LogoutAPI(client, graph);
this.tokenAPI = new TokenAPI(client, graph);
}

public List<Target> listTargets() {
@@ -211,4 +223,16 @@ public void deleteAll() {
this.deleteTarget(target.id());
}
}

public LoginResult login(Login login) {
return this.loginAPI.login(login);
}

public void logout() {
this.logoutAPI.logout();
}

public TokenPayload verifyToken() {
return this.tokenAPI.verifyToken();
}
}
@@ -97,7 +97,7 @@ private void initManagers(RestClient client, String graph) {
private void checkServerApiVersion() {
VersionUtil.Version apiVersion = VersionUtil.Version.of(
this.version.getApiVersion());
VersionUtil.check(apiVersion, "0.38", "0.63",
VersionUtil.check(apiVersion, "0.38", "0.64",
"hugegraph-api in server");
this.client.apiVersion(apiVersion);
}
@@ -141,4 +141,16 @@ public AuthManager auth() {
public MetricsManager metrics() {
return this.metrics;
}

public void setAuthContext(String auth) {
this.client.setAuthContext(auth);
}

public String getAuthContext() {
return this.client.getAuthContext();
}

public void resetAuthContext() {
this.client.resetAuthContext();
}
}
@@ -0,0 +1,47 @@
/*
* 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.structure.auth;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Login {

@JsonProperty("user_name")
private String name;

@JsonProperty("user_password")
private String password;

public void name(String name) {
this.name = name;
}

public String name() {
return this.name;
}

public void password(String password) {
this.password = password;
}

public String password() {
return this.password;
}
}
@@ -0,0 +1,36 @@
/*
* 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.structure.auth;

import com.fasterxml.jackson.annotation.JsonProperty;

public class LoginResult {

@JsonProperty("token")
private String token;

public void token(String token) {
this.token = token;
}

public String token() {
return this.token;
}
}
@@ -0,0 +1,50 @@
/*
* 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.structure.auth;

import com.fasterxml.jackson.annotation.JsonProperty;

public class TokenPayload {

@JsonProperty("user_id")
private String userId;

@JsonProperty("user_name")
private String username;

private TokenPayload() {
}

public String userId() {
return this.userId;
}

public void userId(String userId) {
this.userId = userId;
}

public String username() {
return this.username;
}

public void username(String username) {
this.username = username;
}
}

0 comments on commit 7cce85e

Please sign in to comment.