AuthGiteeRequest.java 2.7 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
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.model.AuthResponse;
import me.zhyd.oauth.model.AuthSource;
import me.zhyd.oauth.model.AuthUser;
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
 */
智布道's avatar
智布道 已提交
22
public class AuthGiteeRequest extends BaseAuthRequest {
23 24 25 26

    public AuthGiteeRequest(AuthConfig config) {
        super(config);
    }
智布道's avatar
智布道 已提交
27 28

    @Override
29
    public void authorize(HttpServletResponse response) {
30
        String authorizeUrl = UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
智布道's avatar
智布道 已提交
31 32 33 34 35 36 37 38
        try {
            response.sendRedirect(authorizeUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
39
    public String authorize() {
40
        return UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
智布道's avatar
智布道 已提交
41 42 43
    }

    @Override
44
    public AuthResponse login(String code) {
45
        String accessTokenUrl = UrlBuilder.getGiteeAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
智布道's avatar
智布道 已提交
46
        HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
智布道's avatar
智布道 已提交
47
        JSONObject accessTokenObject = JSONObject.parseObject(response.body());
智布道's avatar
智布道 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        if (accessTokenObject.containsKey("error")) {
            return AuthResponse.builder()
                    .code(500)
                    .msg("Unable to get token from gitee using code [" + code + "]")
                    .build();
        }
        String accessToken = accessTokenObject.getString("access_token");
        response = HttpRequest.get(UrlBuilder.getGiteeUserInfoUrl(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("address"))
                        .email(object.getString("email"))
                        .remark(object.getString("bio"))
                        .source(AuthSource.GITEE)
                        .build())
                .build();
    }
}