AuthRenrenRequest.java 3.7 KB
Newer Older
H
Hongwei Peng 已提交
1 2 3 4 5 6 7 8 9
package me.zhyd.oauth.request;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
H
Hongwei Peng 已提交
10
import me.zhyd.oauth.model.AuthResponse;
H
Hongwei Peng 已提交
11 12 13 14 15 16 17 18 19
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender;
import me.zhyd.oauth.url.AuthRenrenUrlBuilder;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;

import java.util.Objects;

import static me.zhyd.oauth.config.AuthSource.RENREN;
H
Hongwei Peng 已提交
20
import static me.zhyd.oauth.model.AuthResponseStatus.SUCCESS;
H
Hongwei Peng 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

/**
 * 人人登录
 *
 * @author hongwei.peng (pengisgood(at)gmail(dot)com)
 * @version 1.8.1
 * @since 1.8.1
 */
public class AuthRenrenRequest extends AuthDefaultRequest {

    public AuthRenrenRequest(AuthConfig config) {
        super(config, RENREN, new AuthRenrenUrlBuilder());
    }

    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
H
Hongwei Peng 已提交
37
        return getToken(this.urlBuilder.getAccessTokenUrl(authCallback.getCode()));
H
Hongwei Peng 已提交
38 39 40 41 42 43
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
        HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder()
            .openId(authToken.getOpenId())
H
Hongwei Peng 已提交
44
            .accessToken(authToken.getAccessToken())
H
Hongwei Peng 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
            .build())).execute();
        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)
            .source(RENREN)
            .build();
    }

H
Hongwei Peng 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    @Override
    public AuthResponse refresh(AuthToken authToken) {
        return AuthResponse.builder()
            .code(SUCCESS.getCode())
            .data(getToken(this.urlBuilder.getRefreshUrl(authToken.getRefreshToken())))
            .build();
    }

    private AuthToken getToken(String url) {
        HttpResponse response = HttpRequest.post(url).execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body());
        if (!response.isOk()) {
            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 106
        }
        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");
    }
}