AuthAlipayRequest.java 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
package me.zhyd.oauth.request;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
10
import me.zhyd.oauth.cache.AuthStateCache;
11
import me.zhyd.oauth.config.AuthConfig;
12
import me.zhyd.oauth.config.AuthDefaultSource;
智布道's avatar
智布道 已提交
13
import me.zhyd.oauth.enums.AuthUserGender;
14
import me.zhyd.oauth.exception.AuthException;
15
import me.zhyd.oauth.model.AuthCallback;
16
import me.zhyd.oauth.model.AuthToken;
17 18
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.StringUtils;
不合群的混子's avatar
不合群的混子 已提交
19
import me.zhyd.oauth.utils.UrlBuilder;
20 21

/**
22 23
 * 支付宝登录
 *
24
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
智布道's avatar
智布道 已提交
25
 * @since 1.0.1
26
 */
智布道's avatar
智布道 已提交
27
public class AuthAlipayRequest extends AuthDefaultRequest {
28 29 30 31

    private AlipayClient alipayClient;

    public AuthAlipayRequest(AuthConfig config) {
32 33
        super(config, AuthDefaultSource.ALIPAY);
        this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config
不合群的混子's avatar
不合群的混子 已提交
34
            .getAlipayPublicKey(), "RSA2");
35 36
    }

37
    public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache) {
38 39
        super(config, AuthDefaultSource.ALIPAY, authStateCache);
        this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config
40 41 42
            .getAlipayPublicKey(), "RSA2");
    }

43
    @Override
44
    protected AuthToken getAccessToken(AuthCallback authCallback) {
45 46
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
智布道's avatar
智布道 已提交
47
        request.setCode(authCallback.getAuth_code());
48 49 50 51
        AlipaySystemOauthTokenResponse response = null;
        try {
            response = this.alipayClient.execute(request);
        } catch (Exception e) {
智布道's avatar
智布道 已提交
52
            throw new AuthException(e);
53 54 55 56
        }
        if (!response.isSuccess()) {
            throw new AuthException(response.getSubMsg());
        }
57
        return AuthToken.builder()
不合群的混子's avatar
不合群的混子 已提交
58 59 60 61 62
            .accessToken(response.getAccessToken())
            .uid(response.getUserId())
            .expireIn(Integer.parseInt(response.getExpiresIn()))
            .refreshToken(response.getRefreshToken())
            .build();
63 64 65
    }

    @Override
66 67
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
68 69 70 71 72 73 74 75 76 77
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse response = null;
        try {
            response = this.alipayClient.execute(request, accessToken);
        } catch (AlipayApiException e) {
            throw new AuthException(e.getErrMsg(), e);
        }
        if (!response.isSuccess()) {
            throw new AuthException(response.getSubMsg());
        }
智布道's avatar
智布道 已提交
78

不合群的混子's avatar
不合群的混子 已提交
79
        String province = response.getProvince(), city = response.getCity();
智布道's avatar
智布道 已提交
80 81
        String location = String.format("%s %s", StringUtils.isEmpty(province) ? "" : province, StringUtils.isEmpty(city) ? "" : city);

82
        return AuthUser.builder()
不合群的混子's avatar
不合群的混子 已提交
83 84 85 86 87 88 89
            .uuid(response.getUserId())
            .username(StringUtils.isEmpty(response.getUserName()) ? response.getNickName() : response.getUserName())
            .nickname(response.getNickName())
            .avatar(response.getAvatar())
            .location(location)
            .gender(AuthUserGender.getRealGender(response.getGender()))
            .token(authToken)
90
            .source(source.toString())
不合群的混子's avatar
不合群的混子 已提交
91 92 93 94
            .build();
    }

    /**
95
     * 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
不合群的混子's avatar
不合群的混子 已提交
96
     *
97
     * @param state state 验证授权流程的参数,可以防止csrf
不合群的混子's avatar
不合群的混子 已提交
98
     * @return 返回授权地址
智布道's avatar
智布道 已提交
99
     * @since 1.9.3
不合群的混子's avatar
不合群的混子 已提交
100 101
     */
    @Override
102
    public String authorize(String state) {
不合群的混子's avatar
不合群的混子 已提交
103 104 105 106
        return UrlBuilder.fromBaseUrl(source.authorize())
            .queryParam("app_id", config.getClientId())
            .queryParam("scope", "auth_user")
            .queryParam("redirect_uri", config.getRedirectUri())
107
            .queryParam("state", getRealState(state))
不合群的混子's avatar
不合群的混子 已提交
108
            .build();
109 110
    }
}