AuthRenrenRequest.java 4.0 KB
Newer Older
H
Hongwei Peng 已提交
1 2
package me.zhyd.oauth.request;

智布道's avatar
智布道 已提交
3
import cn.hutool.http.HttpRequest;
H
Hongwei Peng 已提交
4 5 6
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
7
import me.zhyd.oauth.cache.AuthStateCache;
H
Hongwei Peng 已提交
8
import me.zhyd.oauth.config.AuthConfig;
智布道's avatar
智布道 已提交
9
import me.zhyd.oauth.enums.AuthUserGender;
H
Hongwei Peng 已提交
10
import me.zhyd.oauth.exception.AuthException;
11 12 13 14
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
不合群的混子 已提交
15
import me.zhyd.oauth.utils.UrlBuilder;
H
Hongwei Peng 已提交
16 17 18

import java.util.Objects;

19
import static me.zhyd.oauth.config.AuthDefaultSource.RENREN;
20
import static me.zhyd.oauth.enums.AuthResponseStatus.SUCCESS;
H
Hongwei Peng 已提交
21 22 23 24 25

/**
 * 人人登录
 *
 * @author hongwei.peng (pengisgood(at)gmail(dot)com)
智布道's avatar
智布道 已提交
26
 * @since 1.9.0
H
Hongwei Peng 已提交
27 28 29 30
 */
public class AuthRenrenRequest extends AuthDefaultRequest {

    public AuthRenrenRequest(AuthConfig config) {
不合群的混子's avatar
不合群的混子 已提交
31
        super(config, RENREN);
H
Hongwei Peng 已提交
32 33
    }

34 35 36 37
    public AuthRenrenRequest(AuthConfig config, AuthStateCache authStateCache) {
        super(config, RENREN, authStateCache);
    }

H
Hongwei Peng 已提交
38 39
    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
智布道's avatar
智布道 已提交
40
        return this.getToken(accessTokenUrl(authCallback.getCode()));
H
Hongwei Peng 已提交
41 42 43 44
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
不合群的混子's avatar
不合群的混子 已提交
45
        HttpResponse response = doGetUserInfo(authToken);
H
Hongwei Peng 已提交
46 47 48 49 50 51 52 53 54
        JSONObject userObj = JSONObject.parseObject(response.body()).getJSONObject("response");

        return AuthUser.builder()
            .uuid(userObj.getString("id"))
            .avatar(getAvatarUrl(userObj))
            .nickname(userObj.getString("name"))
            .company(getCompany(userObj))
            .gender(getGender(userObj))
            .token(authToken)
55
            .source(source.toString())
H
Hongwei Peng 已提交
56 57 58
            .build();
    }

H
Hongwei Peng 已提交
59 60 61 62
    @Override
    public AuthResponse refresh(AuthToken authToken) {
        return AuthResponse.builder()
            .code(SUCCESS.getCode())
智布道's avatar
智布道 已提交
63
            .data(getToken(this.refreshTokenUrl(authToken.getRefreshToken())))
H
Hongwei Peng 已提交
64 65 66 67 68 69
            .build();
    }

    private AuthToken getToken(String url) {
        HttpResponse response = HttpRequest.post(url).execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body());
智布道's avatar
智布道 已提交
70
        if (jsonObject.containsKey("error")) {
H
Hongwei Peng 已提交
71 72 73 74 75 76 77 78 79 80 81 82
            throw new AuthException("Failed to get token from Renren: " + jsonObject);
        }

        return AuthToken.builder()
            .tokenType(jsonObject.getString("token_type"))
            .expireIn(jsonObject.getIntValue("expires_in"))
            .accessToken(jsonObject.getString("access_token"))
            .refreshToken(jsonObject.getString("refresh_token"))
            .openId(jsonObject.getJSONObject("user").getString("id"))
            .build();
    }

H
Hongwei Peng 已提交
83 84 85 86 87 88 89 90 91 92 93
    private String getAvatarUrl(JSONObject userObj) {
        JSONArray jsonArray = userObj.getJSONArray("avatar");
        if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) {
            return null;
        }
        return jsonArray.getJSONObject(0).getString("url");
    }

    private AuthUserGender getGender(JSONObject userObj) {
        JSONObject basicInformation = userObj.getJSONObject("basicInformation");
        if (Objects.isNull(basicInformation)) {
H
Hongwei Peng 已提交
94
            return AuthUserGender.UNKNOWN;
H
Hongwei Peng 已提交
95 96 97 98 99 100 101 102 103 104 105
        }
        return AuthUserGender.getRealGender(basicInformation.getString("sex"));
    }

    private String getCompany(JSONObject userObj) {
        JSONArray jsonArray = userObj.getJSONArray("work");
        if (Objects.isNull(jsonArray) || jsonArray.isEmpty()) {
            return null;
        }
        return jsonArray.getJSONObject(0).getString("name");
    }
不合群的混子's avatar
不合群的混子 已提交
106 107 108 109

    /**
     * 返回获取userInfo的url
     *
110
     * @param authToken 用户授权后的token
不合群的混子's avatar
不合群的混子 已提交
111 112 113 114 115 116 117 118 119
     * @return 返回获取userInfo的url
     */
    @Override
    protected String userInfoUrl(AuthToken authToken) {
        return UrlBuilder.fromBaseUrl(source.userInfo())
            .queryParam("access_token", authToken.getAccessToken())
            .queryParam("userId", authToken.getOpenId())
            .build();
    }
H
Hongwei Peng 已提交
120
}