Skip to content

Commit

Permalink
to feature 7401 (#7404)
Browse files Browse the repository at this point in the history
Co-authored-by: ouyangyewei <[email protected]>
  • Loading branch information
ouyangyewei429 and ouyangyewei authored Dec 14, 2021
1 parent 1f1edb2 commit 1434386
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,23 @@ public class AccessTokenController extends BaseController {
* @param loginUser login user
* @param userId token for user id
* @param expireTime expire time for the token
* @param token token
* @param token token string (if it is absent, it will be automatically generated)
* @return create result state code
*/
@ApiIgnore
@ApiOperation(value = "createToken", notes = "CREATE_TOKEN_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int"),
@ApiImplicitParam(name = "expireTime", value = "EXPIRE_TIME", required = true, dataType = "String", example = "2021-12-31 00:00:00"),
@ApiImplicitParam(name = "token", value = "TOKEN", required = false, dataType = "String", example = "xxxx")
})
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
@ApiException(CREATE_ACCESS_TOKEN_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result createToken(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam(value = "userId") int userId,
@RequestParam(value = "expireTime") String expireTime,
@RequestParam(value = "token") String token) {
@RequestParam(value = "token", required = false) String token) {

Map<String, Object> result = accessTokenService.createToken(loginUser, userId, expireTime, token);
return returnDataList(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public interface AccessTokenService {
*
* @param userId token for user
* @param expireTime token expire time
* @param token token string
* @param token token string (if it is absent, it will be automatically generated)
* @return create result code
*/
Map<String, Object> createToken(User loginUser, int userId, String expireTime, String token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.dolphinscheduler.api.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.AccessTokenService;
import org.apache.dolphinscheduler.api.utils.PageInfo;
Expand Down Expand Up @@ -108,30 +109,38 @@ public Map<String, Object> queryAccessTokenByUser(User loginUser, Integer userId
*
* @param userId token for user
* @param expireTime token expire time
* @param token token string
* @param token token string (if it is absent, it will be automatically generated)
* @return create result code
*/
@SuppressWarnings("checkstyle:WhitespaceAround")
@Override
public Map<String, Object> createToken(User loginUser, int userId, String expireTime, String token) {
Map<String, Object> result = new HashMap<>();

// 1. check permission
if (!hasPerm(loginUser,userId)) {
putMsg(result, Status.USER_NO_OPERATION_PERM);
return result;
}

// 2. check if user is existed
if (userId <= 0) {
throw new IllegalArgumentException("User id should not less than or equals to 0.");
}

// 3. generate access token if absent
if (StringUtils.isBlank(token)) {
token = EncryptionUtils.getMd5(userId + expireTime + System.currentTimeMillis());
}

// 4. persist to the database
AccessToken accessToken = new AccessToken();
accessToken.setUserId(userId);
accessToken.setExpireTime(DateUtils.stringToDate(expireTime));
accessToken.setToken(token);
accessToken.setCreateTime(new Date());
accessToken.setUpdateTime(new Date());

// insert
int insert = accessTokenMapper.insert(accessToken);

if (insert > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ QUERY_AUTHORIZED_PROJECT_NOTES=query authorized project
QUERY_AUTHORIZED_USER_NOTES=query authorized user
TASK_RECORD_TAG=task record related operation
QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging
CREATE_TOKEN_NOTES=create token ,note: please login first
CREATE_TOKEN_NOTES=create access token for specified user
TOKEN=access token string, it will be automatically generated when it absent
EXPIRE_TIME=expire time for the token
QUERY_ACCESS_TOKEN_LIST_NOTES=query access token list paging
QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user
SCHEDULE=schedule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ QUERY_AUTHORIZED_PROJECT_NOTES=query authorized project
QUERY_AUTHORIZED_USER_NOTES=query authorized user
TASK_RECORD_TAG=task record related operation
QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging
CREATE_TOKEN_NOTES=create token ,note: please login first
CREATE_TOKEN_NOTES=create access token for specified user
TOKEN=access token string, it will be automatically generated when it absent
EXPIRE_TIME=expire time for the token
QUERY_ACCESS_TOKEN_LIST_NOTES=query access token list paging
QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user
SCHEDULE=schedule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ QUERY_AUTHORIZED_PROJECT_NOTES=查询授权项目
QUERY_AUTHORIZED_USER_NOTES=查询拥有项目授权的用户
TASK_RECORD_TAG=任务记录相关操作
QUERY_TASK_RECORD_LIST_PAGING_NOTES=分页查询任务记录列表
CREATE_TOKEN_NOTES=创建token,注意需要先登录
CREATE_TOKEN_NOTES=为指定用户创建安全令牌
TOKEN=安全令牌字符串,若未显式指定将会自动生成
EXPIRE_TIME=安全令牌的过期时间
QUERY_ACCESS_TOKEN_LIST_NOTES=分页查询access token列表
QUERY_ACCESS_TOKEN_BY_USER_NOTES=查询指定用户的access token
SCHEDULE=定时
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ public void testCreateToken() throws Exception {
logger.info(mvcResult.getResponse().getContentAsString());
}

@Test
public void testCreateTokenWhenTokenAbsent() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("userId", "4");
paramsMap.add("expireTime", "2019-12-18 00:00:00");
paramsMap.add("token", null);

MvcResult mvcResult = this.mockMvc
.perform(post("/access-tokens")
.header("sessionId", this.sessionId)
.params(paramsMap))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();

Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString());
}

@Test
public void testExceptionHandler() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,16 @@ public void testQueryAccessTokenByUser() {

@Test
public void testCreateToken() {

// Given Token
when(accessTokenMapper.insert(any(AccessToken.class))).thenReturn(2);
Map<String, Object> result = accessTokenService.createToken(getLoginUser(), 1, getDate(), "AccessTokenServiceTest");
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));

// Token is absent
result = this.accessTokenService.createToken(getLoginUser(), 1, getDate(), null);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}

@Test
Expand Down

0 comments on commit 1434386

Please sign in to comment.