AuthTeambitionRequest.java 4.2 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3
package me.zhyd.oauth.request;

import com.alibaba.fastjson.JSONObject;
智布道's avatar
智布道 已提交
4
import me.zhyd.oauth.utils.HttpUtils;
5
import com.xkcoding.http.support.HttpHeader;
6
import me.zhyd.oauth.cache.AuthStateCache;
智布道's avatar
智布道 已提交
7
import me.zhyd.oauth.config.AuthConfig;
8
import me.zhyd.oauth.config.AuthDefaultSource;
9
import me.zhyd.oauth.enums.AuthResponseStatus;
智布道's avatar
智布道 已提交
10
import me.zhyd.oauth.enums.AuthUserGender;
智布道's avatar
智布道 已提交
11
import me.zhyd.oauth.exception.AuthException;
12 13 14 15
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
智布道's avatar
智布道 已提交
16

17 18 19
import java.util.HashMap;
import java.util.Map;

智布道's avatar
智布道 已提交
20
/**
智布道's avatar
智布道 已提交
21
 * Teambition授权登录
智布道's avatar
智布道 已提交
22 23
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
智布道's avatar
智布道 已提交
24
 * @since 1.9.0
智布道's avatar
智布道 已提交
25 26
 */
public class AuthTeambitionRequest extends AuthDefaultRequest {
智布道's avatar
智布道 已提交
27

智布道's avatar
智布道 已提交
28
    public AuthTeambitionRequest(AuthConfig config) {
29
        super(config, AuthDefaultSource.TEAMBITION);
智布道's avatar
智布道 已提交
30 31
    }

32
    public AuthTeambitionRequest(AuthConfig config, AuthStateCache authStateCache) {
33
        super(config, AuthDefaultSource.TEAMBITION, authStateCache);
34 35
    }

智布道's avatar
智布道 已提交
36 37 38 39 40 41
    /**
     * @param authCallback 回调返回的参数
     * @return 所有信息
     */
    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
42
        Map<String, String> form = new HashMap<>(7);
43 44 45 46 47
        form.put("client_id", config.getClientId());
        form.put("client_secret", config.getClientSecret());
        form.put("code", authCallback.getCode());
        form.put("grant_type", "code");

48
        String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), form, false).getBody();
49
        JSONObject accessTokenObject = JSONObject.parseObject(response);
智布道's avatar
智布道 已提交
50 51 52 53

        this.checkResponse(accessTokenObject);

        return AuthToken.builder()
不合群的混子's avatar
不合群的混子 已提交
54 55 56
            .accessToken(accessTokenObject.getString("access_token"))
            .refreshToken(accessTokenObject.getString("refresh_token"))
            .build();
智布道's avatar
智布道 已提交
57 58 59 60 61 62
    }

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

63 64 65
        HttpHeader httpHeader = new HttpHeader();
        httpHeader.add("Authorization", "OAuth2 " + accessToken);

66 67
        String response = new HttpUtils(config.getHttpConfig())
            .get(source.userInfo(), null, httpHeader, false).getBody();
68
        JSONObject object = JSONObject.parseObject(response);
智布道's avatar
智布道 已提交
69 70 71 72 73 74

        this.checkResponse(object);

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

        return AuthUser.builder()
75
            .rawUserInfo(object)
不合群的混子's avatar
不合群的混子 已提交
76 77 78 79 80 81 82 83 84
            .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"))
            .gender(AuthUserGender.UNKNOWN)
            .token(authToken)
85
            .source(source.toString())
不合群的混子's avatar
不合群的混子 已提交
86
            .build();
智布道's avatar
智布道 已提交
87 88 89 90 91 92
    }

    @Override
    public AuthResponse refresh(AuthToken oldToken) {
        String uid = oldToken.getUid();
        String refreshToken = oldToken.getRefreshToken();
93

94
        Map<String, String> form = new HashMap<>(4);
95 96
        form.put("_userId", uid);
        form.put("refresh_token", refreshToken);
97
        String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false).getBody();
98
        JSONObject refreshTokenObject = JSONObject.parseObject(response);
智布道's avatar
智布道 已提交
99 100 101 102

        this.checkResponse(refreshTokenObject);

        return AuthResponse.builder()
不合群的混子's avatar
不合群的混子 已提交
103 104 105 106 107 108
            .code(AuthResponseStatus.SUCCESS.getCode())
            .data(AuthToken.builder()
                .accessToken(refreshTokenObject.getString("access_token"))
                .refreshToken(refreshTokenObject.getString("refresh_token"))
                .build())
            .build();
智布道's avatar
智布道 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
    }

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