AuthCodingRequest.java 2.7 KB
Newer Older
1 2 3 4 5 6
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;
7
import me.zhyd.oauth.config.AuthSource;
8
import me.zhyd.oauth.exception.AuthException;
9
import me.zhyd.oauth.model.AuthToken;
10 11 12 13 14
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender;
import me.zhyd.oauth.utils.UrlBuilder;

/**
15 16
 * Cooding登录
 *
17 18 19 20 21 22 23 24 25 26 27
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @since 1.8
 */
public class AuthCodingRequest extends BaseAuthRequest {

    public AuthCodingRequest(AuthConfig config) {
        super(config, AuthSource.CODING);
    }

    @Override
28
    protected AuthToken getAccessToken(String code) {
29
        String accessTokenUrl = UrlBuilder.getCodingAccessTokenUrl(config.getClientId(), config.getClientSecret(), code);
30
        HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
31 32 33 34
        JSONObject accessTokenObject = JSONObject.parseObject(response.body());
        if (accessTokenObject.getIntValue("code") != 0) {
            throw new AuthException("Unable to get token from coding using code [" + code + "]");
        }
35
        return AuthToken.builder().accessToken(accessTokenObject.getString("access_token")).build();
36 37 38
    }

    @Override
39 40
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
41 42 43 44 45
        HttpResponse response = HttpRequest.get(UrlBuilder.getCodingUserInfoUrl(accessToken)).execute();
        JSONObject object = JSONObject.parseObject(response.body());
        if (object.getIntValue("code") != 0) {
            throw new AuthException(object.getString("msg"));
        }
智布道's avatar
智布道 已提交
46

47
        object = object.getJSONObject("data");
48
        return AuthUser.builder()
智布道's avatar
智布道 已提交
49
                .uuid(object.getString("id"))
50
                .username(object.getString("name"))
51
                .avatar("https://coding.net/" + object.getString("avatar"))
52 53 54 55 56 57 58
                .blog("https://coding.net/" + object.getString("path"))
                .nickname(object.getString("name"))
                .company(object.getString("company"))
                .location(object.getString("location"))
                .gender(AuthUserGender.getRealGender(object.getString("sex")))
                .email(object.getString("email"))
                .remark(object.getString("slogan"))
59
                .token(authToken)
60 61 62
                .source(AuthSource.CODING)
                .build();
    }
63 64 65 66 67 68 69 70 71 72

    /**
     * 返回认证url,可自行跳转页面
     *
     * @return 返回授权地址
     */
    @Override
    public String authorize() {
        return UrlBuilder.getCodingAuthorizeUrl(config.getClientId(), config.getRedirectUri());
    }
73
}