|
| 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 | +} |
0 commit comments