use of me.zhyd.oauth.model.AuthUser in project matecloud by matevip.
the class SocialTokenGranter method getOAuth2Authentication.
@Override
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
Map<String, String> parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters());
String code = parameters.get("code");
String state = parameters.get("state");
String codeFromRedis = redisService.get(PREFIX + state).toString();
if (StrUtil.isBlank(code)) {
throw new UserDeniedAuthorizationException("未传入请求参数");
}
if (codeFromRedis == null) {
throw new UserDeniedAuthorizationException("openId已过期,请重新发起授权请求");
}
String oauthType = code.split("-")[0];
code = code.split("-")[1];
AuthRequest authRequest = factory.get(oauthType);
AuthCallback authCallback = AuthCallback.builder().code(code).state(state).build();
AuthResponse response = authRequest.login(authCallback);
log.info("【response】= {}", JSON.toJSON(response));
AuthUser authUser = null;
// 第三方登录成功
if (response.getCode() == AuthResponseStatus.SUCCESS.getCode()) {
authUser = (AuthUser) response.getData();
}
log.error("authUser:{}", JSON.toJSON(authUser));
Authentication userAuth = new SocialAuthenticationToken(authUser);
((AbstractAuthenticationToken) userAuth).setDetails(parameters);
try {
userAuth = authenticationManager.authenticate(userAuth);
} catch (AccountStatusException | BadCredentialsException ase) {
// covers expired, locked, disabled cases (mentioned in section 5.2, draft 31)
throw new InvalidGrantException(ase.getMessage());
}
if (userAuth == null || !userAuth.isAuthenticated()) {
throw new InvalidGrantException("Could not authenticate user: " + authUser);
}
OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
return new OAuth2Authentication(storedOAuth2Request, userAuth);
}
use of me.zhyd.oauth.model.AuthUser in project ruoyi-vue-pro by YunaiV.
the class SocialUserServiceImpl method getAuthUser0.
/**
* 请求社交平台,获得授权的用户
*
* @param type 社交平台的类型
* @param authCallback 授权回调
* @return 授权的用户
*/
private AuthUser getAuthUser0(Integer type, AuthCallback authCallback) {
AuthRequest authRequest = authRequestFactory.get(SocialTypeEnum.valueOfType(type).getSource());
AuthResponse<?> authResponse = authRequest.login(authCallback);
log.info("[getAuthUser0][请求社交平台 type({}) request({}) response({})]", type, toJsonString(authCallback), toJsonString(authResponse));
if (!authResponse.ok()) {
throw exception(SOCIAL_USER_AUTH_FAILURE, authResponse.getMsg());
}
return (AuthUser) authResponse.getData();
}
use of me.zhyd.oauth.model.AuthUser in project ruoyi-vue-pro by YunaiV.
the class SocialUserServiceImpl method bindSocialUser.
@Override
public void bindSocialUser(SocialUserBindReqDTO reqDTO) {
// 使用 code 授权
AuthUser authUser = getAuthUser(reqDTO.getType(), reqDTO.getCode(), reqDTO.getState());
if (authUser == null) {
throw exception(SOCIAL_USER_NOT_FOUND);
}
// 绑定社交用户(新增)
bindSocialUser(reqDTO.getUserId(), reqDTO.getUserType(), reqDTO.getType(), authUser);
}
use of me.zhyd.oauth.model.AuthUser in project ruoyi-vue-pro by YunaiV.
the class SocialUserServiceTest method testBindSocialUser_create.
/**
* 情况一,创建 SocialUserDO 的情况
*/
@Test
public void testBindSocialUser_create() {
// mock 数据
// 准备参数
Long userId = randomLongId();
Integer type = randomEle(SocialTypeEnum.values()).getType();
AuthUser authUser = randomPojo(AuthUser.class);
// mock 方法
// 调用
socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser);
// 断言
List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
assertEquals(1, socialUsers.size());
assertBindSocialUser(socialUsers.get(0), authUser, userId, type);
}
use of me.zhyd.oauth.model.AuthUser in project ruoyi-vue-pro by YunaiV.
the class SocialUserServiceTest method testBindSocialUser_userId.
/**
* 情况一和二都存在的,逻辑二的场景
*/
@Test
public void testBindSocialUser_userId() {
// mock 数据
SocialUserDO dbSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
});
socialUserMapper.insert(dbSocialUser);
// 准备参数
Long userId = randomLongId();
Integer type = dbSocialUser.getType();
AuthUser authUser = randomPojo(AuthUser.class);
// mock 方法
// 调用
socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser);
// 断言
List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
assertEquals(1, socialUsers.size());
}
Aggregations