AuthStackOverflowRequest.java 3.0 KB
Newer Older
H
Hongwei Peng 已提交
1 2 3 4 5 6 7 8 9 10
package me.zhyd.oauth.request;

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

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

/**
 * Stack Overflow登录
 *
 * @author hongwei.peng (pengisgood(at)gmail(dot)com)
 * @version 1.9.0
 * @since 1.9.0
 */
public class AuthStackOverflowRequest extends AuthDefaultRequest {

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

    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
智布道's avatar
智布道 已提交
32
        String accessTokenUrl = accessTokenUrl(authCallback.getCode());
H
Hongwei Peng 已提交
33 34
        HttpResponse response = HttpRequest.post(accessTokenUrl)
            .contentType("application/x-www-form-urlencoded")
H
Hongwei Peng 已提交
35
            .form(parseQueryToMap(accessTokenUrl))
H
Hongwei Peng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
            .execute();
        JSONObject accessTokenObject = JSONObject.parseObject(response.body());
        if (!response.isOk()) {
            throw new AuthException("Unable to get token from Stack Overflow using code [" + authCallback.getCode() + "]: " + accessTokenObject);
        }

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

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

        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)
            .source(STACK_OVERFLOW)
            .build();
    }
智布道's avatar
智布道 已提交
69 70 71 72 73 74 75 76 77 78 79

    @Override
    public String authorize() {
        return UrlBuilder.fromBaseUrl(source.authorize())
            .queryParam("response_type", "code")
            .queryParam("client_id", config.getClientId())
            .queryParam("redirect_uri", config.getRedirectUri())
            .queryParam("state", getRealState(config.getState()))
            .queryParam("scope", "read_inbox")
            .build();
    }
H
Hongwei Peng 已提交
80
}