Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public WxMaCode2VerifyInfoResult getCode2VerifyInfo(String code, String checkcod

@Override
public boolean checkSessionKey(String openid, String sessionKey) throws WxErrorException {
String signature = SignUtils.createHmacSha256Sign(openid, sessionKey);
String signature = SignUtils.createHmacSha256Sign("", sessionKey).toLowerCase();
String url = String.format(CHECK_SESSION_KEY_URL, openid, signature);
this.service.get(url, null);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public interface User {
String CODE_2_VERIFY_INFO_URL = "https://api.weixin.qq.com/wxa/sec/checkcode2verifyinfo";
/** 检查登录态接口 */
String CHECK_SESSION_KEY_URL =
"https://api.weixin.qq.com/wxa/checksessionkey?openid=%s&signature=%s&sig_method=hmac_sha256";
"https://api.weixin.qq.com/wxa/checksession?openid=%s&signature=%s&sig_method=hmac_sha256";
}

public interface Ocr {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.binarywang.wx.miniapp.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.SignUtils;
import org.mockito.ArgumentCaptor;
import org.testng.annotations.Test;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

/**
* {@link WxMaUserServiceImpl} 检查登录态接口的单元测试。
*/
public class WxMaUserServiceImplCheckSessionKeyTest {
Comment thread
binarywang marked this conversation as resolved.

@Test
Comment thread
binarywang marked this conversation as resolved.
public void shouldUseCheckSessionUrlAndSignEmptyStringWhenCheckingSessionKey() throws WxErrorException {
WxMaService wxMaService = mock(WxMaService.class);
when(wxMaService.get(anyString(), isNull())).thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}");

new WxMaUserServiceImpl(wxMaService).checkSessionKey("user-openid", "session-key");

ArgumentCaptor<String> url = ArgumentCaptor.forClass(String.class);
verify(wxMaService).get(url.capture(), isNull());
assertEquals(url.getValue(),
"https://api.weixin.qq.com/wxa/checksession?openid=user-openid&signature="
+ SignUtils.createHmacSha256Sign("", "session-key").toLowerCase()
+ "&sig_method=hmac_sha256");
}
}
1 change: 1 addition & 0 deletions weixin-java-miniapp/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<test name="Unit_Test">
<classes>
<class name="cn.binarywang.wx.miniapp.api.impl.WxMaSubscribeServiceImplUrlTest"/>
<class name="cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImplCheckSessionKeyTest"/>
</classes>
</test>
</suite>