Search in sources :

Example 6 with WxAuthInfo

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

the class AuthAppService method loginByWx.

public UserResponse loginByWx(@NotNull WxLoginRequest request) {
    String code = request.getCode();
    WxAuthInfo wxAuthInfo = request.getWxAuthInfo();
    User loginUser = request.getRequestUser();
    User user = loginWithTokenService.loginWithToken(code, wxAuthInfo, loginUser);
    logService.recordLogin(user, request.getRequestIp());
    return UserResponse.from(user);
}
Also used : SessionUser(com.stardata.starshop2.sharedcontext.pl.SessionUser) User(com.stardata.starshop2.authcontext.domain.user.User) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)

Example 7 with WxAuthInfo

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

the class StarshopAuthLoginTests method should_wx_login_login_biz_exception_given_valid_and_used_code.

// 3.3. 微信后台登录并校验;(组合任务,领域服务,微信code有效但已被使用过)
@Test
void should_wx_login_login_biz_exception_given_valid_and_used_code() {
    // given: 有效的微信前端code、有效的微信认证用户rawData和signature
    String code = "091FHp0w3KT3yY2VjV1w3aMiIB3FHp0B";
    String rawData = "{\"nickName\":\"深清秋\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKq2CRmib1mpu4hOFYtcIHgAmS7DicCEfYkUHoPmPQn74BXH5GerjoMOxIqib7iafNNBw2ZAicBj6gZGUQ/132\"}";
    String signature = "af36606701965ce329c2613b91f801d2c07b332d";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    try {
        // when: 调用微信登录服务 WxLoginService.wxLogin
        WxOpenId wxOpenId = wxLoginService.wxLogin(code, wxAuthInfo);
        // 正常情况下不应该执行到这儿
        assertEquals(0, 1);
    } catch (WxLoginErrorException e) {
        // then: 抛出"微信登录错误"异常
        assertTrue(e.getErrCode() == 40029 || e.getErrCode() == 45011 || e.getErrCode() == 40226);
        assertNotNull(e);
    }
}
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)

Example 8 with WxAuthInfo

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

the class StarshopAuthLoginTests method should_wx_login_by_invalid_code_given_login_biz_exception.

// 3.4. 微信后台登录并校验;(组合任务,领域服务,微信code无效)
@Test
void should_wx_login_by_invalid_code_given_login_biz_exception() {
    // given: 有效的微信前端code、有效的微信认证用户rawData和signature
    String code = "testCode";
    String rawData = "testRawData";
    String signature = "testSignature";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    try {
        // when: 调用微信登录服务 WxLoginService.wxLogin
        WxOpenId wxOpenId = wxLoginService.wxLogin(code, wxAuthInfo);
        // 正常情况下不应该执行到这儿
        assertEquals(0, 1);
    } catch (WxLoginErrorException e) {
        // then: 抛出"微信登录错误"异常
        assertTrue(e.getErrCode() == 40029 || e.getErrCode() == 45011 || e.getErrCode() == 40226);
        assertNotNull(e);
    }
}
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)

Example 9 with WxAuthInfo

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

the class StarshopAuthLoginTests method should_wx_login_exception_given_valid_and_unused_code_but_signature_error.

// 3.2. 微信后台登录并校验;(组合任务,领域服务,微信code有效且未被使用过,但signature 不正确)
@Test
void should_wx_login_exception_given_valid_and_unused_code_but_signature_error() {
    // 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 = "06846a4ba8b003af5d98fadfaf376a652e5d75d8";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    try {
        // when: 调用微信登录服务 WxLoginService.wxLogin
        WxOpenId wxOpenId = wxLoginService.wxLogin(code, wxAuthInfo);
    } catch (WxLoginErrorException e) {
        // then: 抛出"Checking userinfo integrity failed"异常
        assertNotNull(e);
        assertEquals(e.getErrCode(), -99);
    }
}
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)

Example 10 with WxAuthInfo

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

the class StarshopAuthLoginTests method should_wx_login_with_token_exception_given_valid_and_unused_code_but_signature_error.

// 4.2. 微信登录异常,未生成微信登录令牌;(组合任务,领域服务)
@Test
@Transactional
@Rollback(true)
void should_wx_login_with_token_exception_given_valid_and_unused_code_but_signature_error() {
    // 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 = "06846a4ba8b003af5d98fadfaf376a652e5d75d8";
    WxAuthInfo wxAuthInfo = new WxAuthInfo(rawData, signature);
    User frontUser = User.of("testUser", 2).avatarUrl("testUrl").country("testCountry").province("testProvince").city("testCity").language("testLanguage");
    try {
        // when: 调用wxLoginWithTokenService.loginWithToken()
        User loginUser = wxLoginWithTokenService.loginWithToken(code, wxAuthInfo, frontUser);
        // 如下代码应该不被执行
        UserToken token = loginUser.currentToken();
        WxOpenId openid = loginUser.getOpenid();
        assertNotNull(token);
        assertFalse(StringUtils.isBlank(token.getToken()));
        assertNotNull(openid);
        assertFalse(StringUtils.isBlank(openid.toString()));
    } catch (WxLoginErrorException e) {
        // then: 有效的用户令牌
        // then: 抛出"Checking userinfo integrity failed"异常
        assertNotNull(e);
        assertEquals(e.getErrCode(), -99);
    }
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxAuthInfo(com.stardata.starshop2.authcontext.domain.user.WxAuthInfo) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) UserToken(com.stardata.starshop2.authcontext.domain.user.UserToken) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

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