AuthTwitterRequest.java 6.6 KB
Newer Older
H
Hongwei Peng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package me.zhyd.oauth.request;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.GlobalAuthUtil;
import me.zhyd.oauth.utils.UrlBuilder;

import java.util.HashMap;
import java.util.Map;

import static me.zhyd.oauth.config.AuthDefaultSource.TWITTER;
import static me.zhyd.oauth.utils.GlobalAuthUtil.generateTwitterSignature;
import static me.zhyd.oauth.utils.GlobalAuthUtil.urlEncode;

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

H
Hongwei Peng 已提交
30 31
    private static final String PREAMBLE = "OAuth";

H
Hongwei Peng 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    public AuthTwitterRequest(AuthConfig config) {
        super(config, TWITTER);
    }

    public AuthTwitterRequest(AuthConfig config, AuthStateCache authStateCache) {
        super(config, TWITTER, authStateCache);
    }

    /**
     * Obtaining a request token
     * https://developer.twitter.com/en/docs/twitter-for-websites/log-in-with-twitter/guides/implementing-sign-in-with-twitter
     *
     * @return request token
     */
    public AuthToken getRequestToken() {
        String baseUrl = "https://api.twitter.com/oauth/request_token";

        Map<String, Object> oauthParams = buildOauthParams();
        oauthParams.put("oauth_callback", config.getRedirectUri());
        oauthParams.put("oauth_signature", generateTwitterSignature(oauthParams, "POST", baseUrl, config.getClientSecret(), null));
H
Hongwei Peng 已提交
52
        String header = buildHeader(oauthParams);
H
Hongwei Peng 已提交
53
        HttpResponse requestToken = HttpRequest.post(baseUrl).header("Authorization", header).execute();
H
Hongwei Peng 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        checkResponse(requestToken);

        Map<String, Object> res = GlobalAuthUtil.parseQueryToMap(requestToken.body());

        return AuthToken.builder()
            .oauthToken(res.get("oauth_token").toString())
            .oauthTokenSecret(res.get("oauth_token_secret").toString())
            .oauthCallbackConfirmed(Boolean.valueOf(res.get("oauth_callback_confirmed").toString()))
            .build();
    }

    /**
     * Convert request token to access token
     * https://developer.twitter.com/en/docs/twitter-for-websites/log-in-with-twitter/guides/implementing-sign-in-with-twitter
     *
     * @return access token
     */
    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
        Map<String, Object> oauthParams = buildOauthParams();
        oauthParams.put("oauth_token", authCallback.getOauthToken());
        oauthParams.put("oauth_verifier", authCallback.getOauthVerifier());
        oauthParams.put("oauth_signature", generateTwitterSignature(oauthParams, "POST", source.accessToken(), config.getClientSecret(), authCallback.getOauthToken()));
H
Hongwei Peng 已提交
77
        String header = buildHeader(oauthParams);
H
Hongwei Peng 已提交
78
        HttpResponse response = HttpRequest.post(source.accessToken())
H
Hongwei Peng 已提交
79
            .header("Authorization", header)
H
Hongwei Peng 已提交
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
            .header("Content-Type", "application/x-www-form-urlencoded")
            .form("oauth_verifier", authCallback.getOauthVerifier())
            .execute();
        checkResponse(response);

        Map<String, Object> requestToken = GlobalAuthUtil.parseQueryToMap(response.body());

        return AuthToken.builder()
            .oauthToken(requestToken.get("oauth_token").toString())
            .oauthTokenSecret(requestToken.get("oauth_token_secret").toString())
            .userId(requestToken.get("user_id").toString())
            .screenName(requestToken.get("screen_name").toString())
            .build();
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("user_id", authToken.getUserId());
        queryParams.put("screen_name", authToken.getScreenName());
        queryParams.put("include_entities", true);

        Map<String, Object> oauthParams = buildOauthParams();
        oauthParams.put("oauth_token", authToken.getOauthToken());

        Map<String, Object> params = new HashMap<>(oauthParams);
        params.putAll(queryParams);
        oauthParams.put("oauth_signature", generateTwitterSignature(params, "GET", source.userInfo(), config.getClientSecret(), authToken.getOauthTokenSecret()));
H
Hongwei Peng 已提交
108
        String header = buildHeader(oauthParams);
H
Hongwei Peng 已提交
109
        HttpResponse response = HttpRequest.get(userInfoUrl(authToken)).header("Authorization", header).execute();
H
Hongwei Peng 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        checkResponse(response);
        JSONObject userInfo = JSONObject.parseObject(response.body());

        return AuthUser.builder()
            .uuid(userInfo.getString("id_str"))
            .username(userInfo.getString("screen_name"))
            .nickname(userInfo.getString("name"))
            .remark(userInfo.getString("description"))
            .avatar(userInfo.getString("profile_image_url_https"))
            .blog(userInfo.getString("url"))
            .location(userInfo.getString("location"))
            .source(source.toString())
            .token(authToken)
            .build();
    }

    @Override
    protected String userInfoUrl(AuthToken authToken) {
        return UrlBuilder.fromBaseUrl(source.userInfo())
            .queryParam("user_id", authToken.getUserId())
            .queryParam("screen_name", authToken.getScreenName())
            .queryParam("include_entities", true)
            .build();
    }

    private Map<String, Object> buildOauthParams() {
        Map<String, Object> params = new HashMap<>();
        params.put("oauth_consumer_key", config.getClientId());
        params.put("oauth_nonce", GlobalAuthUtil.generateNonce(32));
        params.put("oauth_signature_method", "HMAC-SHA1");
        params.put("oauth_timestamp", GlobalAuthUtil.getTimestamp());
        params.put("oauth_version", "1.0");
        return params;
    }

H
Hongwei Peng 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    private String buildHeader(Map<String, Object> oauthParams) {
        final StringBuilder sb = new StringBuilder(PREAMBLE);

        for (Map.Entry<String, Object> param : oauthParams.entrySet()) {
            if (sb.length() > PREAMBLE.length()) {
                sb.append(", ");
            }
            sb.append(param.getKey())
                .append("=\"")
                .append(urlEncode(param.getValue().toString()))
                .append('"');
        }

        return sb.toString();
    }

H
Hongwei Peng 已提交
161 162 163 164 165 166
    private void checkResponse(HttpResponse response) {
        if (!response.isOk()) {
            throw new AuthException(response.body());
        }
    }
}