AuthGithubRequest.java 3.0 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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.AuthResponse;
import me.zhyd.oauth.model.AuthSource;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.AuthConfigChecker;
import me.zhyd.oauth.utils.UrlBuilder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2019/1/31 16:31
 * @since 1.8
 */
24 25 26 27 28
public class AuthGithubRequest extends BaseAuthRequest implements AuthRequest {

    public AuthGithubRequest(AuthConfig config) {
        super(config);
    }
智布道's avatar
智布道 已提交
29 30

    @Override
31
    public void authorize(HttpServletResponse response) {
智布道's avatar
智布道 已提交
32 33 34
        if (!AuthConfigChecker.isSupportedGithub()) {
            throw new AuthException(ResponseStatus.UNSUPPORTED);
        }
35
        String authorizeUrl = UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
智布道's avatar
智布道 已提交
36 37 38 39 40 41 42 43
        try {
            response.sendRedirect(authorizeUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
44
    public String authorize() {
智布道's avatar
智布道 已提交
45 46 47
        if (!AuthConfigChecker.isSupportedGithub()) {
            throw new AuthException(ResponseStatus.UNSUPPORTED);
        }
48
        return UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
智布道's avatar
智布道 已提交
49 50 51
    }

    @Override
52
    public AuthResponse login(String code) {
智布道's avatar
智布道 已提交
53 54 55 56 57 58
        if (!AuthConfigChecker.isSupportedGithub()) {
            return AuthResponse.builder()
                    .code(ResponseStatus.UNSUPPORTED.getCode())
                    .msg(ResponseStatus.UNSUPPORTED.getMsg())
                    .build();
        }
59
        String accessTokenUrl = UrlBuilder.getGithubAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
智布道's avatar
智布道 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
        String accessTokenStr = response.body();
        String accessToken = accessTokenStr.split("&")[0];
        response = HttpRequest.get(UrlBuilder.getGithubUserInfoUrl(accessToken)).execute();
        String userInfo = response.body();
        JSONObject object = JSONObject.parseObject(userInfo);
        return AuthResponse.builder()
                .data(AuthUser.builder()
                        .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"))
                        .source(AuthSource.GITHUB)
                        .build())
                .build();
    }
}