|
|
|
|
@ -2,6 +2,7 @@ package cn.iocoder.mall.user.service;
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.common.framework.exception.ServiceException;
|
|
|
|
|
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
|
|
|
|
import cn.iocoder.common.framework.vo.CommonResult;
|
|
|
|
|
import cn.iocoder.mall.user.convert.OAuth2Convert;
|
|
|
|
|
import cn.iocoder.mall.user.dao.OAuth2AccessTokenMapper;
|
|
|
|
|
import cn.iocoder.mall.user.dao.OAuth2RefreshTokenMapper;
|
|
|
|
|
@ -11,12 +12,13 @@ import cn.iocoder.mall.user.dataobject.OAuth2RefreshTokenDO;
|
|
|
|
|
import cn.iocoder.mall.user.dataobject.UserDO;
|
|
|
|
|
import cn.iocoder.mall.user.service.api.OAuth2Service;
|
|
|
|
|
import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum;
|
|
|
|
|
import cn.iocoder.mall.user.service.api.dto.OAuth2AccessTokenBO;
|
|
|
|
|
import cn.iocoder.mall.user.service.api.dto.OAuth2AuthenticationDTO;
|
|
|
|
|
import cn.iocoder.mall.user.service.api.bo.OAuth2AccessTokenBO;
|
|
|
|
|
import cn.iocoder.mall.user.service.api.bo.OAuth2AuthenticationBO;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
@ -61,15 +63,39 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|
|
|
|
// 创建刷新令牌
|
|
|
|
|
OAuth2RefreshTokenDO oauth2RefreshTokenDO = createOAuth2RefreshToken(userDO.getId());
|
|
|
|
|
// 创建访问令牌
|
|
|
|
|
OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(userDO.getId(), oauth2RefreshTokenDO.getTokenId());
|
|
|
|
|
OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(userDO.getId(), oauth2RefreshTokenDO.getId());
|
|
|
|
|
// 标记已使用
|
|
|
|
|
mobileCodeService.useMobileCode(mobileCodeDO.getId(), userDO.getId());
|
|
|
|
|
// 转换返回
|
|
|
|
|
return OAuth2Convert.INSTANCE.convertWithExpiresIn(oauth2AccessTokenDO);
|
|
|
|
|
return OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public OAuth2AuthenticationDTO checkToken(String accessToken) throws ServiceException {
|
|
|
|
|
@Transactional
|
|
|
|
|
public CommonResult<OAuth2AccessTokenBO> getAccessToken2(String mobile, String code) {
|
|
|
|
|
// 校验传入的 mobile 和 code 是否合法
|
|
|
|
|
CommonResult<MobileCodeDO> result = mobileCodeService.validLastMobileCode2(mobile, code);
|
|
|
|
|
if (result.isError()) {
|
|
|
|
|
return CommonResult.error(result);
|
|
|
|
|
}
|
|
|
|
|
// 获取用户
|
|
|
|
|
UserDO userDO = userService.getUser(mobile);
|
|
|
|
|
if (userDO == null) { // 用户不存在,则进行创建用户
|
|
|
|
|
userDO = userService.createUser(mobile);
|
|
|
|
|
Assert.notNull(userDO, "创建用户必然成功");
|
|
|
|
|
}
|
|
|
|
|
// 创建刷新令牌
|
|
|
|
|
OAuth2RefreshTokenDO oauth2RefreshTokenDO = createOAuth2RefreshToken(userDO.getId());
|
|
|
|
|
// 创建访问令牌
|
|
|
|
|
OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(userDO.getId(), oauth2RefreshTokenDO.getId());
|
|
|
|
|
// 标记已使用
|
|
|
|
|
// mobileCodeService.useMobileCode(result.getData().getId(), userDO.getId());
|
|
|
|
|
// 转换返回
|
|
|
|
|
return CommonResult.success(OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public OAuth2AuthenticationBO checkToken(String accessToken) throws ServiceException {
|
|
|
|
|
OAuth2AccessTokenDO accessTokenDO = oauth2AccessTokenMapper.selectByTokenId(accessToken);
|
|
|
|
|
if (accessTokenDO == null) { // 不存在
|
|
|
|
|
throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_TOKEN_NOT_FOUND.getCode());
|
|
|
|
|
@ -81,11 +107,11 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|
|
|
|
throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_TOKEN_INVALID.getCode());
|
|
|
|
|
}
|
|
|
|
|
// 转换返回
|
|
|
|
|
return new OAuth2AuthenticationDTO().setUid(accessTokenDO.getUid());
|
|
|
|
|
return OAuth2Convert.INSTANCE.convertToAuthentication(accessTokenDO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OAuth2AccessTokenDO createOAuth2AccessToken(Long uid, String refreshToken) {
|
|
|
|
|
OAuth2AccessTokenDO accessToken = new OAuth2AccessTokenDO().setTokenId(generateAccessToken())
|
|
|
|
|
OAuth2AccessTokenDO accessToken = new OAuth2AccessTokenDO().setId(generateAccessToken())
|
|
|
|
|
.setRefreshToken(refreshToken)
|
|
|
|
|
.setUid(uid)
|
|
|
|
|
.setExpiresTime(new Date(System.currentTimeMillis() + accessTokenExpireTimeMillis))
|
|
|
|
|
@ -95,7 +121,7 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OAuth2RefreshTokenDO createOAuth2RefreshToken(Long uid) {
|
|
|
|
|
OAuth2RefreshTokenDO refreshToken = new OAuth2RefreshTokenDO().setTokenId(generateRefreshToken())
|
|
|
|
|
OAuth2RefreshTokenDO refreshToken = new OAuth2RefreshTokenDO().setId(generateRefreshToken())
|
|
|
|
|
.setUid(uid)
|
|
|
|
|
.setExpiresTime(new Date(System.currentTimeMillis() + refreshTokenExpireTimeMillis))
|
|
|
|
|
.setValid(true);
|
|
|
|
|
|