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

[Feature][Server&UI] Add token manager feature #471

Merged
merged 4 commits into from
Oct 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class DataVinesConstants {

public static String TOKEN = "token";

public static String LOGIN_USER = "login_user";

public static String WORKSPACE_ID = "workspace_id";
Expand Down Expand Up @@ -61,11 +63,11 @@ public class DataVinesConstants {

public static final String TOKEN_HEADER_STRING = "Authorization";

public static final String TOKEN_USER_NAME = "token_user_name";
public static final String TOKEN_USER_NAME = "un";

public static final String TOKEN_USER_PASSWORD = "token_user_password";
public static final String TOKEN_USER_PASSWORD = "up";

public static final String TOKEN_CREATE_TIME = "token_create_time";
public static final String TOKEN_CREATE_TIME = "ct";

public static final String TOKEN_VERIFICATION_CODE = "token_verification_code";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
package io.datavines.core.utils;

import io.datavines.core.constant.DataVinesConstants;
import io.datavines.core.exception.DataVinesServerException;
import io.jsonwebtoken.CompressionCodecs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand All @@ -35,6 +35,7 @@
import io.jsonwebtoken.SignatureAlgorithm;
import io.datavines.common.entity.TokenInfo;

@Slf4j
@Component
public class TokenManager {

Expand All @@ -44,7 +45,7 @@ public class TokenManager {
@Value("${jwt.token.timeout:8640000}")
private Long timeout;

@Value("${jwt.token.algorithm:HS512}")
@Value("${jwt.token.algorithm:HS256}")
private String algorithm;

public String generateToken(String username, String password) {
Expand All @@ -63,6 +64,21 @@ public String generateToken(TokenInfo tokenInfo) {
return generate(claims);
}

public String generateToken(String token, Long timeOutMillis) {
Map<String, Object> claims = new HashMap<>();
String username = getUsername(token);
String password = getPassword(token);
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
throw new DataVinesServerException("can not get the user info from token");
}

claims.put(DataVinesConstants.TOKEN_USER_NAME, username);
claims.put(DataVinesConstants.TOKEN_USER_PASSWORD, password);
claims.put(DataVinesConstants.TOKEN_CREATE_TIME, System.currentTimeMillis());

return toTokenString(timeOutMillis, claims);
}

public String refreshToken(String token) {
Claims claims = getClaims(token);
claims.put(DataVinesConstants.TOKEN_CREATE_TIME, System.currentTimeMillis());
Expand All @@ -88,6 +104,7 @@ public String generateContinuousToken(TokenInfo tokenInfo) {
.setClaims(claims)
.setSubject(claims.get(DataVinesConstants.TOKEN_USER_NAME).toString())
.signWith(SignatureAlgorithm.valueOf(algorithm), tokenSecret.getBytes(StandardCharsets.UTF_8))
.compressWith(CompressionCodecs.DEFLATE)
.compact();
}

Expand All @@ -105,6 +122,7 @@ public String toTokenString(Long timeOutMillis, Map<String, Object> claims) {
.setSubject(null == claims.get(DataVinesConstants.TOKEN_USER_NAME) ? null : claims.get(DataVinesConstants.TOKEN_USER_NAME).toString())
.setExpiration(new Date(expiration))
.signWith(SignatureAlgorithm.valueOf(algorithm), tokenSecret.getBytes(StandardCharsets.UTF_8))
.compressWith(CompressionCodecs.DEFLATE)
.compact();
}

Expand All @@ -114,7 +132,7 @@ public String getUsername(String token) {
final Claims claims = getClaims(token);
username = claims.get(DataVinesConstants.TOKEN_USER_NAME).toString();
} catch (Exception e) {
e.printStackTrace();
log.error("get username from token error : ", e);
}
return username;
}
Expand All @@ -125,7 +143,7 @@ public String getPassword(String token) {
final Claims claims = getClaims(token);
password = claims.get(DataVinesConstants.TOKEN_USER_PASSWORD).toString();
} catch (Exception e) {
e.printStackTrace();
log.error("get password from token error : ", e);
}
return password;
}
Expand All @@ -151,7 +169,7 @@ private Date getCreatedDate(String token) {
final Claims claims = getClaims(token);
created = new Date((Long) claims.get(DataVinesConstants.TOKEN_CREATE_TIME));
} catch (Exception e) {
e.printStackTrace();
log.error("get create time from token error : ", e);
}
return created;
}
Expand All @@ -162,7 +180,7 @@ private Date getExpirationDate(String token) {
final Claims claims = getClaims(token);
expiration = claims.getExpiration();
} catch (Exception e) {
e.printStackTrace();
log.error("get expiration time from token error : ", e);
}
return expiration;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 io.datavines.server.api.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckTokenExist {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 io.datavines.server.api.controller;

import io.datavines.core.aop.RefreshToken;
import io.datavines.core.constant.DataVinesConstants;
import io.datavines.core.exception.DataVinesServerException;
import io.datavines.server.api.dto.bo.token.TokenCreate;
import io.datavines.server.api.dto.bo.token.TokenUpdate;
import io.datavines.server.repository.service.AccessTokenService;
import io.datavines.server.utils.ContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(value = "token", tags = "token", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping(value = DataVinesConstants.BASE_API_PATH + "/token", produces = MediaType.APPLICATION_JSON_VALUE)
@RefreshToken
public class AccessTokenController {

@Resource
private AccessTokenService accessTokenService;

@ApiOperation(value = "create token")
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Object createToken(@Valid @RequestBody TokenCreate tokenCreate) throws DataVinesServerException {
return accessTokenService.create(tokenCreate);
}

@ApiOperation(value = "update token")
@PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Object updateToken(@Valid @RequestBody TokenUpdate tokenUpdate) throws DataVinesServerException {
return accessTokenService.update(tokenUpdate);
}

@ApiOperation(value = "delete token")
@DeleteMapping(value = "/{id}")
public Object deleteToken(@PathVariable Long id) {
// 加入黑名单,并且需要拦截器进行处理
return accessTokenService.deleteToken(id);
}

@ApiOperation(value = "page token")
@GetMapping(value = "/page")
public Object listByUserId(@RequestParam("workspaceId") Long workspaceId,
@RequestParam("pageNumber") Integer pageNumber,
@RequestParam("pageSize") Integer pageSize) {
return accessTokenService.page(workspaceId, ContextHolder.getUserId(), pageNumber, pageSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public class ConfigController {

@ApiOperation(value = "create config")
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Object createConfig(@Valid @RequestBody ConfigCreate ConfigCreate) throws DataVinesServerException {
return configService.create(ConfigCreate);
public Object createConfig(@Valid @RequestBody ConfigCreate configCreate) throws DataVinesServerException {
return configService.create(configCreate);
}

@ApiOperation(value = "update config")
@PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Object updateConfig(@Valid @RequestBody ConfigUpdate ConfigUpdate) throws DataVinesServerException {
return configService.update(ConfigUpdate)>0;
public Object updateConfig(@Valid @RequestBody ConfigUpdate configUpdate) throws DataVinesServerException {
return configService.update(configUpdate)>0;
}

@ApiOperation(value = "delete config")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 io.datavines.server.api.controller;

import io.datavines.core.aop.RefreshToken;
import io.datavines.core.constant.DataVinesConstants;
import io.datavines.core.exception.DataVinesServerException;
import io.datavines.server.api.annotation.CheckTokenExist;
import io.datavines.server.api.dto.vo.JobExecutionResultVO;
import io.datavines.server.repository.service.JobExecutionResultService;
import io.datavines.server.repository.service.JobExecutionService;
import io.datavines.server.repository.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "openapi", tags = "openapi", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping(value = DataVinesConstants.BASE_API_PATH + "/openapi", produces = MediaType.APPLICATION_JSON_VALUE)
@RefreshToken
@Validated
public class OpenApiController {

@Autowired
private JobService jobService;

@Autowired
private JobExecutionService jobExecutionService;

@Autowired
private JobExecutionResultService jobExecutionResultService;

@CheckTokenExist
@ApiOperation(value = "execute job")
@PostMapping(value = "/job/execute/{id}")
public Object executeJob(@PathVariable("id") Long jobId) throws DataVinesServerException {
return jobService.execute(jobId, null);
}

@CheckTokenExist
@ApiOperation(value = "kill job", response = Long.class)
@DeleteMapping(value = "/job/execution/kill/{executionId}")
public Object kill(@PathVariable("executionId") Long executionId) {
return jobExecutionService.killJob(executionId);
}

@CheckTokenExist
@ApiOperation(value = "get job execution status", response = String.class)
@GetMapping(value = "/job/execution/status/{executionId}")
public Object getTaskStatus(@PathVariable("executionId") Long executionId) {
return jobExecutionService.getById(executionId).getStatus().getCode();
}

@CheckTokenExist
@Deprecated
@ApiOperation(value = "get job execution result", response = JobExecutionResultVO.class)
@GetMapping(value = "/job/execution/result/{executionId}")
public Object getJobExecutionResultInfo(@PathVariable("executionId") Long executionId) {
return jobExecutionResultService.getCheckResultByJobExecutionId(executionId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 io.datavines.server.api.dto.bo.token;

import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Data
@NotNull(message = "Token Create cannot be null")
public class TokenCreate {

@NotNull(message = "WorkspaceId cannot be empty")
private long workspaceId;

@NotBlank(message = "expireTime cannot be empty")
private String expireTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 io.datavines.server.api.dto.bo.token;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotNull;

@Data
@EqualsAndHashCode(callSuper = true)
@NotNull(message = "Token Update cannot be null")
public class TokenUpdate extends TokenCreate {

@NotNull(message = "Config id cannot be null")
private Long id;
}
Loading
Loading