AuthTeambitionRequest.java 3.8 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3 4 5 6 7 8 9 10 11 12
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.config.AuthSource;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.*;
import me.zhyd.oauth.url.AuthTeambitionUrlBuilder;

/**
智布道's avatar
智布道 已提交
13
 * Teambition授权登录
智布道's avatar
智布道 已提交
14 15 16 17 18 19
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @since 1.8
 */
public class AuthTeambitionRequest extends AuthDefaultRequest {
智布道's avatar
智布道 已提交
20

智布道's avatar
智布道 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    public AuthTeambitionRequest(AuthConfig config) {
        super(config, AuthSource.TEAMBITION, new AuthTeambitionUrlBuilder());
    }

    /**
     * @param authCallback 回调返回的参数
     * @return 所有信息
     */
    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
        String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(authCallback.getCode());
        HttpResponse response = HttpRequest.post(accessTokenUrl)
                .form("client_id", config.getClientId())
                .form("client_secret", config.getClientSecret())
                .form("code", authCallback.getCode())
                .form("grant_type", "code")
                .execute();
        JSONObject accessTokenObject = JSONObject.parseObject(response.body());

        this.checkResponse(accessTokenObject);

        return AuthToken.builder()
                .accessToken(accessTokenObject.getString("access_token"))
                .refreshToken(accessTokenObject.getString("refresh_token"))
                .build();
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();

        HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(null))
                .header("Authorization", "OAuth2 " + accessToken)
                .execute();
        JSONObject object = JSONObject.parseObject(response.body());

        this.checkResponse(object);

        authToken.setUid(object.getString("_id"));

        return AuthUser.builder()
                .uuid(object.getString("_id"))
                .username(object.getString("name"))
                .nickname(object.getString("name"))
                .avatar(object.getString("avatarUrl"))
                .blog(object.getString("website"))
                .location(object.getString("location"))
                .email(object.getString("email"))
H
Hongwei Peng 已提交
69
                .gender(AuthUserGender.UNKNOWN)
智布道's avatar
智布道 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
                .token(authToken)
                .source(AuthSource.TEAMBITION)
                .build();
    }

    @Override
    public AuthResponse refresh(AuthToken oldToken) {
        String uid = oldToken.getUid();
        String refreshToken = oldToken.getRefreshToken();
        String refreshTokenUrl = this.urlBuilder.getRefreshUrl(refreshToken);
        HttpResponse response = HttpRequest.post(refreshTokenUrl)
                .form("_userId", uid)
                .form("refresh_token", refreshToken)
                .execute();
        JSONObject refreshTokenObject = JSONObject.parseObject(response.body());

        this.checkResponse(refreshTokenObject);

        return AuthResponse.builder()
                .code(AuthResponseStatus.SUCCESS.getCode())
                .data(AuthToken.builder()
                        .accessToken(refreshTokenObject.getString("access_token"))
                        .refreshToken(refreshTokenObject.getString("refresh_token"))
                        .build())
                .build();
    }

    /**
     * 检查响应内容是否正确
     *
     * @param object 请求响应内容
     */
    private void checkResponse(JSONObject object) {
        if ((object.containsKey("message") && object.containsKey("name"))) {
            throw new AuthException(object.getString("name") + ", " + object.getString("message"));
        }
    }
}