Search in sources :

Example 1 with AuthUser

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);
}
Also used : AuthRequest(me.zhyd.oauth.request.AuthRequest) AuthCallback(me.zhyd.oauth.model.AuthCallback) AuthUser(me.zhyd.oauth.model.AuthUser) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) InvalidGrantException(org.springframework.security.oauth2.common.exceptions.InvalidGrantException) LinkedHashMap(java.util.LinkedHashMap) AuthResponse(me.zhyd.oauth.model.AuthResponse) SocialAuthenticationToken(vip.mate.uaa.social.SocialAuthenticationToken) AccountStatusException(org.springframework.security.authentication.AccountStatusException) UserDeniedAuthorizationException(org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Authentication(org.springframework.security.core.Authentication)

Example 2 with AuthUser

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();
}
Also used : AuthRequest(me.zhyd.oauth.request.AuthRequest) AuthUser(me.zhyd.oauth.model.AuthUser)

Example 3 with AuthUser

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);
}
Also used : AuthUser(me.zhyd.oauth.model.AuthUser)

Example 4 with 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);
}
Also used : AuthUser(me.zhyd.oauth.model.AuthUser) SocialUserDO(cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO) BaseDbAndRedisUnitTest(cn.iocoder.yudao.module.system.test.BaseDbAndRedisUnitTest) Test(org.junit.jupiter.api.Test)

Example 5 with AuthUser

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());
}
Also used : AuthUser(me.zhyd.oauth.model.AuthUser) SocialUserDO(cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO) BaseDbAndRedisUnitTest(cn.iocoder.yudao.module.system.test.BaseDbAndRedisUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

AuthUser (me.zhyd.oauth.model.AuthUser)13 SocialUserDO (cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO)4 BaseDbAndRedisUnitTest (cn.iocoder.yudao.module.system.test.BaseDbAndRedisUnitTest)3 AuthRequest (me.zhyd.oauth.request.AuthRequest)3 Test (org.junit.jupiter.api.Test)3 AuthCallback (me.zhyd.oauth.model.AuthCallback)2 AuthResponse (me.zhyd.oauth.model.AuthResponse)2 JsonUtils.toJsonString (cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString)1 LoginUser (cn.iocoder.yudao.framework.security.core.LoginUser)1 JapUser (com.fujieid.jap.core.JapUser)1 SysUser (com.ruoyi.common.core.domain.entity.SysUser)1 SocialUser (com.ruoyi.iot.domain.SocialUser)1 AuthRequestWrap (com.ruoyi.iot.model.login.AuthRequestWrap)1 User (com.zyd.blog.business.entity.User)1 LinkedHashMap (java.util.LinkedHashMap)1 AuthException (me.zhyd.oauth.exception.AuthException)1 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)1 AccountStatusException (org.springframework.security.authentication.AccountStatusException)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 Authentication (org.springframework.security.core.Authentication)1