AuthStackOverflowRequest.java 3.8 KB
Newer Older
H
Hongwei Peng 已提交
1 2 3
package me.zhyd.oauth.request;

import com.alibaba.fastjson.JSONObject;
4 5 6
import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.util.MapUtil;
7
import me.zhyd.oauth.cache.AuthStateCache;
H
Hongwei Peng 已提交
8
import me.zhyd.oauth.config.AuthConfig;
智布道's avatar
智布道 已提交
9
import me.zhyd.oauth.enums.AuthUserGender;
10
import me.zhyd.oauth.enums.scope.AuthStackoverflowScope;
H
Hongwei Peng 已提交
11 12 13 14
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
智布道's avatar
智布道 已提交
15
import me.zhyd.oauth.utils.AuthScopeUtils;
16
import me.zhyd.oauth.utils.HttpUtils;
智布道's avatar
智布道 已提交
17
import me.zhyd.oauth.utils.UrlBuilder;
H
Hongwei Peng 已提交
18

19 20
import java.util.Map;

21
import static me.zhyd.oauth.config.AuthDefaultSource.STACK_OVERFLOW;
H
Hongwei Peng 已提交
22 23 24 25 26

/**
 * Stack Overflow登录
 *
 * @author hongwei.peng (pengisgood(at)gmail(dot)com)
智布道's avatar
智布道 已提交
27
 * @since 1.9.0
H
Hongwei Peng 已提交
28 29 30 31
 */
public class AuthStackOverflowRequest extends AuthDefaultRequest {

    public AuthStackOverflowRequest(AuthConfig config) {
智布道's avatar
智布道 已提交
32
        super(config, STACK_OVERFLOW);
H
Hongwei Peng 已提交
33 34
    }

35 36 37 38
    public AuthStackOverflowRequest(AuthConfig config, AuthStateCache authStateCache) {
        super(config, STACK_OVERFLOW, authStateCache);
    }

H
Hongwei Peng 已提交
39 40
    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
智布道's avatar
智布道 已提交
41
        String accessTokenUrl = accessTokenUrl(authCallback.getCode());
42 43 44
        Map<String, String> form = MapUtil.parseStringToMap(accessTokenUrl, false);
        HttpHeader httpHeader = new HttpHeader();
        httpHeader.add(Constants.CONTENT_TYPE, "application/x-www-form-urlencoded");
45
        String response = new HttpUtils(config.getHttpConfig()).post(accessTokenUrl, form, httpHeader, false).getBody();
46 47

        JSONObject accessTokenObject = JSONObject.parseObject(response);
智布道's avatar
智布道 已提交
48
        this.checkResponse(accessTokenObject);
H
Hongwei Peng 已提交
49 50 51 52 53 54 55 56 57

        return AuthToken.builder()
            .accessToken(accessTokenObject.getString("access_token"))
            .expireIn(accessTokenObject.getIntValue("expires"))
            .build();
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
智布道's avatar
智布道 已提交
58 59
        String userInfoUrl = UrlBuilder.fromBaseUrl(this.source.userInfo())
            .queryParam("access_token", authToken.getAccessToken())
智布道's avatar
智布道 已提交
60 61 62
            .queryParam("site", "stackoverflow")
            .queryParam("key", this.config.getStackOverflowKey())
            .build();
63
        String response = new HttpUtils(config.getHttpConfig()).get(userInfoUrl).getBody();
64
        JSONObject object = JSONObject.parseObject(response);
智布道's avatar
智布道 已提交
65 66
        this.checkResponse(object);
        JSONObject userObj = object.getJSONArray("items").getJSONObject(0);
H
Hongwei Peng 已提交
67 68

        return AuthUser.builder()
69
            .rawUserInfo(userObj)
H
Hongwei Peng 已提交
70 71 72 73 74 75 76
            .uuid(userObj.getString("user_id"))
            .avatar(userObj.getString("profile_image"))
            .location(userObj.getString("location"))
            .nickname(userObj.getString("display_name"))
            .blog(userObj.getString("website_url"))
            .gender(AuthUserGender.UNKNOWN)
            .token(authToken)
77
            .source(source.toString())
H
Hongwei Peng 已提交
78 79
            .build();
    }
智布道's avatar
智布道 已提交
80

81
    /**
82
     * 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
83 84 85
     *
     * @param state state 验证授权流程的参数,可以防止csrf
     * @return 返回授权地址
智布道's avatar
智布道 已提交
86
     * @since 1.9.3
87
     */
智布道's avatar
智布道 已提交
88
    @Override
89
    public String authorize(String state) {
90
        return UrlBuilder.fromBaseUrl(super.authorize(state))
智布道's avatar
智布道 已提交
91
            .queryParam("scope", this.getScopes(",", false, AuthScopeUtils.getDefaultScopes(AuthStackoverflowScope.values())))
智布道's avatar
智布道 已提交
92 93
            .build();
    }
智布道's avatar
智布道 已提交
94 95 96 97 98 99 100 101 102 103 104

    /**
     * 检查响应内容是否正确
     *
     * @param object 请求响应内容
     */
    private void checkResponse(JSONObject object) {
        if (object.containsKey("error")) {
            throw new AuthException(object.getString("error_description"));
        }
    }
H
Hongwei Peng 已提交
105
}