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 24 25 26
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
 */
public class AuthGithubRequest implements AuthRequest {

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

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

    @Override
48
    public AuthResponse login(AuthConfig config, String code) {
智布道's avatar
智布道 已提交
49 50 51 52 53 54
        if (!AuthConfigChecker.isSupportedGithub()) {
            return AuthResponse.builder()
                    .code(ResponseStatus.UNSUPPORTED.getCode())
                    .msg(ResponseStatus.UNSUPPORTED.getMsg())
                    .build();
        }
55
        String accessTokenUrl = UrlBuilder.getGithubAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
智布道's avatar
智布道 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        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();
    }
}