AuthGithubRequest.java 2.4 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3 4
package me.zhyd.oauth.request;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
5
import me.zhyd.oauth.cache.AuthStateCache;
智布道's avatar
智布道 已提交
6
import me.zhyd.oauth.config.AuthConfig;
7
import me.zhyd.oauth.config.AuthSource;
智布道's avatar
智布道 已提交
8
import me.zhyd.oauth.enums.AuthUserGender;
智布道's avatar
智布道 已提交
9
import me.zhyd.oauth.exception.AuthException;
10
import me.zhyd.oauth.model.AuthCallback;
11
import me.zhyd.oauth.model.AuthToken;
智布道's avatar
智布道 已提交
12
import me.zhyd.oauth.model.AuthUser;
13 14 15
import me.zhyd.oauth.utils.GlobalAuthUtil;

import java.util.Map;
智布道's avatar
智布道 已提交
16

智布道's avatar
智布道 已提交
17
/**
18 19
 * Github登录
 *
智布道's avatar
智布道 已提交
20
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
智布道's avatar
智布道 已提交
21
 * @since 1.0.0
智布道's avatar
智布道 已提交
22
 */
智布道's avatar
智布道 已提交
23
public class AuthGithubRequest extends AuthDefaultRequest {
24 25

    public AuthGithubRequest(AuthConfig config) {
不合群的混子's avatar
不合群的混子 已提交
26
        super(config, AuthSource.GITHUB);
智布道's avatar
智布道 已提交
27 28
    }

29 30 31 32
    public AuthGithubRequest(AuthConfig config, AuthStateCache authStateCache) {
        super(config, AuthSource.GITHUB, authStateCache);
    }

智布道's avatar
智布道 已提交
33
    @Override
34
    protected AuthToken getAccessToken(AuthCallback authCallback) {
不合群的混子's avatar
不合群的混子 已提交
35
        HttpResponse response = doPostAuthorizationCode(authCallback.getCode());
36 37 38 39
        Map<String, String> res = GlobalAuthUtil.parseStringToMap(response.body());
        if (res.containsKey("error")) {
            throw new AuthException(res.get("error") + ":" + res.get("error_description"));
        }
智布道's avatar
智布道 已提交
40
        return AuthToken.builder()
41 42 43
            .accessToken(res.get("access_token"))
            .scope(res.get("scope"))
            .tokenType(res.get("token_type"))
智布道's avatar
智布道 已提交
44
            .build();
45 46 47
    }

    @Override
48
    protected AuthUser getUserInfo(AuthToken authToken) {
不合群的混子's avatar
不合群的混子 已提交
49
        HttpResponse response = doGetUserInfo(authToken);
智布道's avatar
智布道 已提交
50
        JSONObject object = JSONObject.parseObject(response.body());
51 52 53
        if (object.containsKey("error")) {
            throw new AuthException(object.getString("error_description"));
        }
54
        return AuthUser.builder()
不合群的混子's avatar
不合群的混子 已提交
55 56 57 58 59 60 61 62 63 64 65
            .uuid(object.getString("id"))
            .username(object.getString("login"))
            .avatar(object.getString("avatar_url"))
            .blog(object.getString("blog"))
            .nickname(object.getString("name"))
            .company(object.getString("company"))
            .location(object.getString("location"))
            .email(object.getString("email"))
            .remark(object.getString("bio"))
            .gender(AuthUserGender.UNKNOWN)
            .token(authToken)
66
            .source(source)
不合群的混子's avatar
不合群的混子 已提交
67
            .build();
智布道's avatar
智布道 已提交
68
    }
智布道's avatar
智布道 已提交
69

智布道's avatar
智布道 已提交
70
}