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

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
6
import me.zhyd.oauth.cache.AuthStateCache;
H
Hongwei Peng 已提交
7
import me.zhyd.oauth.config.AuthConfig;
智布道's avatar
智布道 已提交
8
import me.zhyd.oauth.enums.AuthUserGender;
H
Hongwei Peng 已提交
9 10 11 12
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
智布道 已提交
13
import me.zhyd.oauth.utils.UrlBuilder;
H
Hongwei Peng 已提交
14 15

import static me.zhyd.oauth.config.AuthSource.STACK_OVERFLOW;
H
Hongwei Peng 已提交
16
import static me.zhyd.oauth.utils.GlobalAuthUtil.parseQueryToMap;
H
Hongwei Peng 已提交
17 18 19 20 21

/**
 * Stack Overflow登录
 *
 * @author hongwei.peng (pengisgood(at)gmail(dot)com)
智布道's avatar
智布道 已提交
22
 * @since 1.9.0
H
Hongwei Peng 已提交
23 24 25 26
 */
public class AuthStackOverflowRequest extends AuthDefaultRequest {

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

30 31 32 33
    public AuthStackOverflowRequest(AuthConfig config, AuthStateCache authStateCache) {
        super(config, STACK_OVERFLOW, authStateCache);
    }

H
Hongwei Peng 已提交
34 35
    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
智布道's avatar
智布道 已提交
36
        String accessTokenUrl = accessTokenUrl(authCallback.getCode());
H
Hongwei Peng 已提交
37 38
        HttpResponse response = HttpRequest.post(accessTokenUrl)
            .contentType("application/x-www-form-urlencoded")
H
Hongwei Peng 已提交
39
            .form(parseQueryToMap(accessTokenUrl))
H
Hongwei Peng 已提交
40 41
            .execute();
        JSONObject accessTokenObject = JSONObject.parseObject(response.body());
智布道's avatar
智布道 已提交
42
        this.checkResponse(accessTokenObject);
H
Hongwei Peng 已提交
43 44 45 46 47 48 49 50 51

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

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

        return AuthUser.builder()
            .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)
70
            .source(source)
H
Hongwei Peng 已提交
71 72
            .build();
    }
智布道's avatar
智布道 已提交
73

74
    /**
75
     * 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
76 77 78
     *
     * @param state state 验证授权流程的参数,可以防止csrf
     * @return 返回授权地址
智布道's avatar
智布道 已提交
79
     * @since 1.9.3
80
     */
智布道's avatar
智布道 已提交
81
    @Override
82
    public String authorize(String state) {
智布道's avatar
智布道 已提交
83 84 85 86 87
        return UrlBuilder.fromBaseUrl(source.authorize())
            .queryParam("response_type", "code")
            .queryParam("client_id", config.getClientId())
            .queryParam("redirect_uri", config.getRedirectUri())
            .queryParam("scope", "read_inbox")
88
            .queryParam("state", getRealState(state))
智布道's avatar
智布道 已提交
89 90
            .build();
    }
智布道's avatar
智布道 已提交
91 92 93 94 95 96 97 98 99 100 101

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