Search in sources :

Example 1 with WxAuthInfo

use of com.stardata.starshop2.authcontext.domain.user.WxAuthInfo in project starshop by beautautumn.

the class WxSessionCheckingClientAdapter method code2session.

@Override
public void code2session(String code, WxAuthInfo wxAuthInfo) throws WxLoginErrorException {
    final WxMaService wxService = WxConfiguration.getMaService();
    try {
        WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
        wxAuthInfo.setOpenid(session.getOpenid());
        wxAuthInfo.setSessionKey(session.getSessionKey());
    } catch (WxErrorException e) {
        WxError wxError = e.getError();
        throw new WxLoginErrorException(wxError.getErrorCode(), wxError.getErrorMsg());
    }
}
Also used : WxError(me.chanjar.weixin.common.error.WxError) WxLoginErrorException(com.stardata.starshop2.authcontext.domain.WxLoginErrorException) WxMaService(cn.binarywang.wx.miniapp.api.WxMaService) WxMaJscode2SessionResult(cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Example 2 with WxAuthInfo

use of com.stardata.starshop2.authcontext.domain.user.WxAuthInfo in project starshop by beautautumn.

the class StarshopAuthDecryptingTests method should_get_mobile_phone_correctly_given_exists_userid_and_correct_encrypted_data_iv_by_domain_service.

// 1.1. 正常的已有用户、加密数据encryptedData、iv,成功解密手机号,并更新用户对象手机号
@Test
@Transactional
@Rollback(true)
void should_get_mobile_phone_correctly_given_exists_userid_and_correct_encrypted_data_iv_by_domain_service() {
    // given: 正常的已有用户、加密数据encryptedData、iv
    String code = "011NXJ000zQ8VN1iJZ000DWjjS2NXJ0e";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "3901b7322c7920ef4a5077eeba54b0d633585eef";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User requestUser = User.of("深清秋", 1).avatarUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132").country("中国").province("江苏").city("南京").language("zh_CN");
    User user = loginWithTokenService.loginWithToken(code, wxAuthInfo, requestUser);
    userRepository.add(user);
    entityManager.flush();
    LongIdentity userId = user.getId();
    String encryptedData = "NkSHcnyy8jJZwvsTBpb8Kw7jKIdYz1UnVqJ9Gf2NGFAn3DEeObh7W2Vh5hlgvs5zvtfNsV/IW+oHxEtzN4DY1E/tsGWnhiZYN+pYQun/8gNPrUNsjlR8saZIYoTGNcKpchNp+QEPzP/JFtGqIH+Etpk11RVRWepNJdYNzG3aNLUVmYQRqgCom7kYRrbM7g7GDu/jnqzvMn65ps48GawfQA==";
    String iv = "S2r8F/Gr0aUEs7BoerD5ZQ==";
    // when: 解密手机号
    MobileNumber mobileNumber = mobileNumberDecryptingService.decryptWxMobileNumber(userId, encryptedData, iv);
    userRepository.update(user);
    entityManager.flush();
    User loadedUser = userRepository.instanceOf(userId);
    // then: 判定解密手机号是否正确
    assertNotNull(mobileNumber);
    assertEquals(mobileNumber.value(), "18652012976");
    assertEquals(loadedUser.getMobileNumber().value(), "18652012976");
}
Also used : MobileNumber(com.stardata.starshop2.sharedcontext.domain.MobileNumber) SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) LongIdentity(com.stardata.starshop2.sharedcontext.domain.LongIdentity) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(jakarta.transaction.Transactional)

Example 3 with WxAuthInfo

use of com.stardata.starshop2.authcontext.domain.user.WxAuthInfo in project starshop by beautautumn.

the class StarshopAuthDecryptingTests method should_throw_exception_given_not_exists_user_and_correct_encrypted_data_iv_by_domain_service.

// 1.2. 系统中不存在该用户,解密报业务异常 ApplicationValidationException
@Test
@Transactional
@Rollback(true)
void should_throw_exception_given_not_exists_user_and_correct_encrypted_data_iv_by_domain_service() {
    // given: 正确的加密数据encryptedData、iv,不存在的userId
    String code = "011NXJ000zQ8VN1iJZ000DWjjS2NXJ0e";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "3901b7322c7920ef4a5077eeba54b0d633585eef";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User requestUser = User.of("深清秋", 1).avatarUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132").country("中国").province("江苏").city("南京").language("zh_CN");
    User user = loginWithTokenService.loginWithToken(code, wxAuthInfo, requestUser);
    userRepository.add(user);
    entityManager.flush();
    LongIdentity userId = LongIdentity.snowflakeId();
    String encryptedData = "NkSHcnyy8jJZwvsTBpb8Kw7jKIdYz1UnVqJ9Gf2NGFAn3DEeObh7W2Vh5hlgvs5zvtfNsV/IW+oHxEtzN4DY1E/tsGWnhiZYN+pYQun/8gNPrUNsjlR8saZIYoTGNcKpchNp+QEPzP/JFtGqIH+Etpk11RVRWepNJdYNzG3aNLUVmYQRqgCom7kYRrbM7g7GDu/jnqzvMn65ps48GawfQA==";
    String iv = "S2r8F/Gr0aUEs7BoerD5ZQ==";
    try {
        // when: 解密手机号
        MobileNumber mobileNumber = mobileNumberDecryptingService.decryptWxMobileNumber(userId, encryptedData, iv);
        // 下面这些不应该被执行到
        userRepository.update(user);
        entityManager.flush();
        User loadedUser = userRepository.instanceOf(userId);
        // then: 判定解密手机号是否正确
        assertNotNull(mobileNumber);
        assertEquals(mobileNumber.value(), "18652012976");
        assertEquals(loadedUser.getMobileNumber().value(), "18652012976");
    } catch (ApplicationValidationException e) {
        assertNotNull(e);
        assertEquals(e.getErrCode(), ApplicationValidationException.INVALID_REQUEST_ENTITY);
    }
}
Also used : MobileNumber(com.stardata.starshop2.sharedcontext.domain.MobileNumber) SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) ApplicationValidationException(com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) LongIdentity(com.stardata.starshop2.sharedcontext.domain.LongIdentity) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(jakarta.transaction.Transactional)

Example 4 with WxAuthInfo

use of com.stardata.starshop2.authcontext.domain.user.WxAuthInfo in project starshop by beautautumn.

the class StarshopAuthDecryptingTests method should_throw_exception_given_exists_user_and_incorrect_encrypted_data_iv_by_domain_service.

// 1.3. 加密数据encryptedData、iv内容不合格,解密报业务异常 ApplicationValidationException
@Test
@Transactional
@Rollback(true)
void should_throw_exception_given_exists_user_and_incorrect_encrypted_data_iv_by_domain_service() {
    // given: 正常的已有用户,不正确的加密数据encryptedData、iv
    String code = "011NXJ000zQ8VN1iJZ000DWjjS2NXJ0e";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "3901b7322c7920ef4a5077eeba54b0d633585eef";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User requestUser = User.of("深清秋", 1).avatarUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132").country("中国").province("江苏").city("南京").language("zh_CN");
    User user = loginWithTokenService.loginWithToken(code, wxAuthInfo, requestUser);
    userRepository.add(user);
    entityManager.flush();
    LongIdentity userId = user.getId();
    String encryptedData = "NkSHcnyy88jJZwvsTBpb8Kw7jKIdYz1UnVqJ9Gf2NGFAn3DEeObh7W2Vh5hlgvs5zvtfNsV/IW+oHxEtzN4DY1E/tsGWnhiZYN+pYQun/8gNPrUNsjlR8saZIYoTGNcKpchNp+QEPzP/JFtGqIH+Etpk11RVRWepNJdYNzG3aNLUVmYQRqgCom7kYRrbM7g7GDu/jnqzvMn65ps48GawfQA==";
    String iv = "S2r8F/Gr0aUEs7BoerD5ZQ==";
    try {
        // when: 解密手机号
        MobileNumber mobileNumber = mobileNumberDecryptingService.decryptWxMobileNumber(userId, encryptedData, iv);
        // 下面这些不应该被执行到
        userRepository.update(user);
        entityManager.flush();
        User loadedUser = userRepository.instanceOf(userId);
        // then: 判定解密手机号是否正确
        assertNotNull(mobileNumber);
        assertEquals(mobileNumber.value(), "18652012976");
        assertEquals(loadedUser.getMobileNumber().value(), "18652012976");
    } catch (ApplicationValidationException e) {
        assertNotNull(e);
        assertEquals(e.getErrCode(), ApplicationValidationException.INVALID_REQUEST_DATA);
    }
}
Also used : MobileNumber(com.stardata.starshop2.sharedcontext.domain.MobileNumber) SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) ApplicationValidationException(com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) LongIdentity(com.stardata.starshop2.sharedcontext.domain.LongIdentity) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(jakarta.transaction.Transactional)

Example 5 with WxAuthInfo

use of com.stardata.starshop2.authcontext.domain.user.WxAuthInfo in project starshop by beautautumn.

the class StarshopAuthLoginTests method should_wx_login_success_given_by_valid_and_unused_code.

// 3.1. 微信后台登录并校验;(组合任务,领域服务,微信code有效且未被使用过)
@Test
void should_wx_login_success_given_by_valid_and_unused_code() {
    // given: 有效的微信前端code、有效的微信认证用户rawData和signature
    String code = "081sloll28T8d94nl8nl2hxKhh3slolv";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "06846a4ba8b003af5d98fadfaf376a652e5d75d7";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    // when: 调用微信登录服务 WxLoginService.wxLogin
    WxOpenId wxOpenId = wxLoginService.wxLogin(code, wxAuthInfo);
    // then: 返回有效的openid
    assertNotNull(wxOpenId);
    assertFalse(StringUtils.isBlank(wxOpenId.toString()));
}
Also used : WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

WxAuthInfo (com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)10 Test (org.junit.jupiter.api.Test)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 User (com.stardata.starshop2.authcontext.domain.user.User)7 WxOpenId (com.stardata.starshop2.authcontext.domain.user.WxOpenId)7 Rollback (org.springframework.test.annotation.Rollback)5 SessionUser (com.stardata.starshop2.sharedcontext.pl.SessionUser)4 LongIdentity (com.stardata.starshop2.sharedcontext.domain.LongIdentity)3 MobileNumber (com.stardata.starshop2.sharedcontext.domain.MobileNumber)3 Transactional (jakarta.transaction.Transactional)3 UserToken (com.stardata.starshop2.authcontext.domain.user.UserToken)2 ApplicationValidationException (com.stardata.starshop2.sharedcontext.exception.ApplicationValidationException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 WxMaService (cn.binarywang.wx.miniapp.api.WxMaService)1 WxMaJscode2SessionResult (cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult)1 WxLoginErrorException (com.stardata.starshop2.authcontext.domain.WxLoginErrorException)1 WxError (me.chanjar.weixin.common.error.WxError)1 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)1