Skip to content

Commit cdcf9a5

Browse files
committed
🔥 支持支付宝证书模式登录
1 parent 354f006 commit cdcf9a5

File tree

4 files changed

+157
-4
lines changed

4 files changed

+157
-4
lines changed

CHANGELOGS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
## 1.16.7
22

3-
### 2024/08/03
3+
### 2024/12/14
44

55
- 新增
66
- 添加`appleid`社交登录能力。 [Github#192](https://github.com/justauth/JustAuth/pull/192)
7+
- 添加`支付宝证书模式`登录能力(原支持的公钥登录模式依然可用)。
78
- 添加`figma`社交登录能力。 [Gitee#41](https://gitee.com/yadong.zhang/JustAuth/pulls/41)
89
- 添加新版`企业微信扫码`登录能力。 [Github Issue#165](https://github.com/justauth/JustAuth/issues/165)
910
- 添加新版`钉钉扫码`登录能力。 [Gitee Issue#I73FZL](https://gitee.com/yadong.zhang/JustAuth/issues/I73FZL)
1011
- 添加新版`华为`登录能力,原`AuthHuaweiRequest`会在后面版本被弃用,如有使用,请切换到`AuthHuaweiV3Request`
11-
- 新增微信小程序授权登录
12+
- 添加`微信小程序`登录能力。
1213
- 优化
1314
- 更新 Google 端点地址。[Github #198](https://github.com/justauth/JustAuth/pull/198)
1415
- Amazon PKCE 中的 `code_verifier` 基于 `state` 缓存

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<lombok-version>1.18.30</lombok-version>
6262
<junit-version>4.13.2</junit-version>
6363
<fastjson-version>1.2.83</fastjson-version>
64-
<alipay-sdk-version>4.17.5.ALL</alipay-sdk-version>
64+
<alipay-sdk-version>4.39.165.ALL</alipay-sdk-version>
6565
<jacoco-version>0.8.2</jacoco-version>
6666
<jwt.version>0.12.3</jwt.version>
6767
<bcpkix-jdk18on.version>1.78</bcpkix-jdk18on.version>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package me.zhyd.oauth.request;
2+
3+
4+
import com.alibaba.fastjson.JSONObject;
5+
import com.alipay.api.AlipayApiException;
6+
import com.alipay.api.AlipayClient;
7+
import com.alipay.api.AlipayConfig;
8+
import com.alipay.api.DefaultAlipayClient;
9+
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
10+
import com.alipay.api.request.AlipayUserInfoShareRequest;
11+
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
12+
import com.alipay.api.response.AlipayUserInfoShareResponse;
13+
import me.zhyd.oauth.config.AuthConfig;
14+
import me.zhyd.oauth.enums.AuthResponseStatus;
15+
import me.zhyd.oauth.enums.AuthUserGender;
16+
import me.zhyd.oauth.exception.AuthException;
17+
import me.zhyd.oauth.model.AuthCallback;
18+
import me.zhyd.oauth.model.AuthResponse;
19+
import me.zhyd.oauth.model.AuthToken;
20+
import me.zhyd.oauth.model.AuthUser;
21+
import me.zhyd.oauth.utils.StringUtils;
22+
import me.zhyd.oauth.utils.UrlBuilder;
23+
24+
import static me.zhyd.oauth.config.AuthDefaultSource.ALIPAY;
25+
26+
/**
27+
* 支付宝证书模式登录
28+
*
29+
* @since 1.16.7
30+
*/
31+
public class AuthAlipayCertRequest extends AuthDefaultRequest {
32+
33+
private final AlipayClient alipayClient;
34+
35+
public AuthAlipayCertRequest(AuthConfig config, AlipayConfig alipayConfig) {
36+
super(config, ALIPAY);
37+
try {
38+
this.alipayClient = new DefaultAlipayClient(alipayConfig);
39+
} catch (AlipayApiException e) {
40+
throw new AuthException(e);
41+
}
42+
}
43+
44+
@Override
45+
protected void checkCode(AuthCallback authCallback) {
46+
if (StringUtils.isEmpty(authCallback.getAuth_code())) {
47+
throw new AuthException(AuthResponseStatus.ILLEGAL_CODE, source);
48+
}
49+
}
50+
51+
@Override
52+
public AuthToken getAccessToken(AuthCallback authCallback) {
53+
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
54+
request.setGrantType("authorization_code");
55+
request.setCode(authCallback.getAuth_code());
56+
AlipaySystemOauthTokenResponse response;
57+
try {
58+
response = this.alipayClient.certificateExecute(request);
59+
} catch (Exception e) {
60+
throw new AuthException(e);
61+
}
62+
if (!response.isSuccess()) {
63+
throw new AuthException(response.getSubMsg());
64+
}
65+
return AuthToken.builder()
66+
.accessToken(response.getAccessToken())
67+
.uid(response.getUserId())
68+
.expireIn(Integer.parseInt(response.getExpiresIn()))
69+
.refreshToken(response.getRefreshToken())
70+
.build();
71+
}
72+
73+
74+
/**
75+
* 刷新access token (续期)
76+
*
77+
* @param authToken 登录成功后返回的Token信息
78+
* @return AuthResponse
79+
*/
80+
@Override
81+
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
82+
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
83+
request.setGrantType("refresh_token");
84+
request.setRefreshToken(authToken.getRefreshToken());
85+
AlipaySystemOauthTokenResponse response = null;
86+
try {
87+
response = this.alipayClient.certificateExecute(request);
88+
} catch (Exception e) {
89+
throw new AuthException(e);
90+
}
91+
if (!response.isSuccess()) {
92+
throw new AuthException(response.getSubMsg());
93+
}
94+
return AuthResponse.<AuthToken>builder()
95+
.code(AuthResponseStatus.SUCCESS.getCode())
96+
.data(AuthToken.builder()
97+
.accessToken(response.getAccessToken())
98+
.uid(response.getUserId())
99+
.expireIn(Integer.parseInt(response.getExpiresIn()))
100+
.refreshToken(response.getRefreshToken())
101+
.build())
102+
.build();
103+
}
104+
105+
@Override
106+
public AuthUser getUserInfo(AuthToken authToken) {
107+
String accessToken = authToken.getAccessToken();
108+
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
109+
AlipayUserInfoShareResponse response = null;
110+
try {
111+
response = this.alipayClient.certificateExecute(request, accessToken);
112+
} catch (AlipayApiException e) {
113+
throw new AuthException(e.getErrMsg(), e);
114+
}
115+
if (!response.isSuccess()) {
116+
throw new AuthException(response.getSubMsg());
117+
}
118+
119+
String province = response.getProvince(), city = response.getCity();
120+
String location = String.format("%s %s", StringUtils.isEmpty(province) ? "" : province, StringUtils.isEmpty(city) ? "" : city);
121+
122+
return AuthUser.builder()
123+
.rawUserInfo(JSONObject.parseObject(JSONObject.toJSONString(response)))
124+
.uuid(response.getOpenId())
125+
.username(StringUtils.isEmpty(response.getUserName()) ? response.getNickName() : response.getUserName())
126+
.nickname(response.getNickName())
127+
.avatar(response.getAvatar())
128+
.location(location)
129+
.gender(AuthUserGender.getRealGender(response.getGender()))
130+
.token(authToken)
131+
.source(source.toString())
132+
.build();
133+
}
134+
135+
136+
/**
137+
* 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
138+
*
139+
* @param state state 验证授权流程的参数,可以防止csrf
140+
* @return 返回授权地址
141+
* @since 1.9.3
142+
*/
143+
@Override
144+
public String authorize(String state) {
145+
return UrlBuilder.fromBaseUrl(source.authorize())
146+
.queryParam("app_id", config.getClientId())
147+
.queryParam("scope", "auth_user")
148+
.queryParam("redirect_uri", config.getRedirectUri())
149+
.queryParam("state", getRealState(state))
150+
.build();
151+
}
152+
}

src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.net.InetSocketAddress;
2727

2828
/**
29-
* 支付宝登录
29+
* 支付宝公钥模式登录
3030
*
3131
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
3232
* @since 1.0.1

0 commit comments

Comments
 (0)