AuthAlipayRequest.java 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
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;
import me.zhyd.oauth.config.AuthConfig;
11
import me.zhyd.oauth.config.AuthSource;
12
import me.zhyd.oauth.exception.AuthException;
13
import me.zhyd.oauth.model.AuthCallback;
14
import me.zhyd.oauth.model.AuthToken;
15 16
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender;
17 18
import me.zhyd.oauth.url.AlipayUrlBuilder;
import me.zhyd.oauth.url.entity.AuthAuthorizeEntity;
19 20 21
import me.zhyd.oauth.utils.StringUtils;

/**
22 23
 * 支付宝登录
 *
24 25 26 27 28 29 30 31 32
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @since 1.8
 */
public class AuthAlipayRequest extends BaseAuthRequest {

    private AlipayClient alipayClient;

    public AuthAlipayRequest(AuthConfig config) {
33
        super(config, AuthSource.ALIPAY, new AlipayUrlBuilder());
34 35
        this.alipayClient = new DefaultAlipayClient(AuthSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config
                .getAlipayPublicKey(), "RSA2");
36 37 38
    }

    @Override
39
    protected AuthToken getAccessToken(AuthCallback authCallback) {
40 41
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
智布道's avatar
智布道 已提交
42
        request.setCode(authCallback.getAuth_code());
43 44 45 46
        AlipaySystemOauthTokenResponse response = null;
        try {
            response = this.alipayClient.execute(request);
        } catch (Exception e) {
智布道's avatar
智布道 已提交
47
            throw new AuthException("Unable to get token from alipay using code [" + authCallback.getAuth_code() + "]", e);
48 49 50 51
        }
        if (!response.isSuccess()) {
            throw new AuthException(response.getSubMsg());
        }
52 53
        return AuthToken.builder()
                .accessToken(response.getAccessToken())
智布道's avatar
智布道 已提交
54 55 56
                .uid(response.getUserId())
                .expireIn(Integer.parseInt(response.getExpiresIn()))
                .refreshToken(response.getRefreshToken())
57
                .build();
58 59 60
    }

    @Override
61 62
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
63 64 65 66 67 68 69 70 71 72
        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
智布道 已提交
73

74 75
        String province = response.getProvince(),
                city = response.getCity();
智布道's avatar
智布道 已提交
76 77
        String location = String.format("%s %s", StringUtils.isEmpty(province) ? "" : province, StringUtils.isEmpty(city) ? "" : city);

78
        return AuthUser.builder()
智布道's avatar
智布道 已提交
79
                .uuid(response.getUserId())
80
                .username(StringUtils.isEmpty(response.getUserName()) ? response.getNickName() : response.getUserName())
81 82
                .nickname(response.getNickName())
                .avatar(response.getAvatar())
智布道's avatar
智布道 已提交
83
                .location(location)
84
                .gender(AuthUserGender.getRealGender(response.getGender()))
85
                .token(authToken)
86 87 88
                .source(AuthSource.ALIPAY)
                .build();
    }
89 90 91 92 93 94 95 96

    /**
     * 返回认证url,可自行跳转页面
     *
     * @return 返回授权地址
     */
    @Override
    public String authorize() {
97 98 99
        return this.urlBuilder.getAuthorizeUrl(AuthAuthorizeEntity.builder()
                .config(config)
                .build());
100
    }
101
}