AuthXmlyRequest.java 4.4 KB
Newer Older
Z
zwzch 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package me.zhyd.oauth.request;

import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.GlobalAuthUtils;
import me.zhyd.oauth.utils.UrlBuilder;
智布道's avatar
智布道 已提交
15

Z
zwzch 已提交
16 17
import java.util.HashMap;
import java.util.Map;
智布道's avatar
智布道 已提交
18
import java.util.Optional;
Z
zwzch 已提交
19
import java.util.TreeMap;
智布道's avatar
智布道 已提交
20

Z
zwzch 已提交
21 22 23 24
/**
 * 喜马拉雅登录
 *
 * @author zwzch (zwzch4j@gmail.com)
智布道's avatar
智布道 已提交
25
 * @since 1.15.9
Z
zwzch 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */
public class AuthXmlyRequest extends AuthDefaultRequest {

    public AuthXmlyRequest(AuthConfig config) {
        super(config, AuthDefaultSource.XMLY);
    }

    public AuthXmlyRequest(AuthConfig config, AuthStateCache authStateCache) {
        super(config, AuthDefaultSource.XMLY, authStateCache);
    }

    /**
     * 获取access token
     *
     * @param authCallback 授权成功后的回调参数
     * @return token
     * @see AuthDefaultRequest#authorize(String)
     */
    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
46
        Map<String, String> map = new HashMap<>(9);
Z
zwzch 已提交
47 48 49 50 51 52
        map.put("code", authCallback.getCode());
        map.put("client_id", config.getClientId());
        map.put("client_secret", config.getClientSecret());
        map.put("device_id", config.getDeviceId());
        map.put("grant_type", "authorization_code");
        map.put("redirect_uri", config.getRedirectUri());
53
        String response = HttpUtil.post(source.accessToken(), map, true).getBody();
Z
zwzch 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        JSONObject accessTokenObject = JSONObject.parseObject(response);
        this.checkResponse(accessTokenObject);

        return AuthToken.builder()
            .accessToken(accessTokenObject.getString("access_token"))
            .refreshToken(accessTokenObject.getString("refresh_token"))
            .expireIn(accessTokenObject.getIntValue("expires_in"))
            .uid(accessTokenObject.getString("uid"))
            .build();
    }

    /**
     * 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
     *
     * @param state state 验证授权流程的参数,可以防止csrf
     * @return 返回授权地址
     * @since 1.15.8
     */
    @Override
    public String authorize(String state) {
        return UrlBuilder.fromBaseUrl(source.authorize())
            .queryParam("response_type", "code")
            .queryParam("client_id", config.getClientId())
            .queryParam("redirect_uri", config.getRedirectUri())
            .queryParam("state", getRealState(state))
            .queryParam("client_os_type", "3")
            .queryParam("device_id", config.getDeviceId())
            .build();
    }

    /**
     * 使用token换取用户信息
     *
     * @param authToken token信息
     * @return 用户信息
     * @see AuthDefaultRequest#getAccessToken(AuthCallback)
     */
    @Override
    public AuthUser getUserInfo(AuthToken authToken) {
        Map<String, String> map = new TreeMap<>();
        map.put("app_key", config.getClientId());
智布道's avatar
智布道 已提交
95
        map.put("client_os_type", Optional.ofNullable(config.getClientOsType()).orElse(3).toString());
Z
zwzch 已提交
96 97 98 99
        map.put("device_id", config.getDeviceId());
        map.put("pack_id", config.getPackId());
        map.put("access_token", authToken.getAccessToken());
        map.put("sig", GlobalAuthUtils.generateXmlySignature(map, config.getClientSecret()));
100
        String rawUserInfo = HttpUtil.get(source.userInfo(), map, false).getBody();
Z
zwzch 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        JSONObject object = JSONObject.parseObject(rawUserInfo);
        checkResponse(object);
        return AuthUser.builder()
            .uuid(object.getString("id"))
            .nickname(object.getString("nickname"))
            .avatar(object.getString("avatar_url"))
            .rawUserInfo(object)
            .source(source.toString())
            .token(authToken)
            .gender(AuthUserGender.UNKNOWN)
            .build();
    }

    /**
     * 校验响应结果
     *
     * @param object 接口返回的结果
     */
智布道's avatar
智布道 已提交
119
    private void checkResponse(JSONObject object) {
Z
zwzch 已提交
120 121 122 123 124
        if (object.containsKey("errcode")) {
            throw new AuthException(object.getIntValue("error_no"), object.getString("error_desc"));
        }
    }
}