提交 da8ec82e 编写于 作者: H Hongwei Peng

refactor

上级 5ed7e156
...@@ -27,6 +27,8 @@ import static me.zhyd.oauth.utils.GlobalAuthUtil.urlEncode; ...@@ -27,6 +27,8 @@ import static me.zhyd.oauth.utils.GlobalAuthUtil.urlEncode;
*/ */
public class AuthTwitterRequest extends AuthDefaultRequest { public class AuthTwitterRequest extends AuthDefaultRequest {
private static final String PREAMBLE = "OAuth";
public AuthTwitterRequest(AuthConfig config) { public AuthTwitterRequest(AuthConfig config) {
super(config, TWITTER); super(config, TWITTER);
} }
...@@ -47,10 +49,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest { ...@@ -47,10 +49,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest {
Map<String, Object> oauthParams = buildOauthParams(); Map<String, Object> oauthParams = buildOauthParams();
oauthParams.put("oauth_callback", config.getRedirectUri()); oauthParams.put("oauth_callback", config.getRedirectUri());
oauthParams.put("oauth_signature", generateTwitterSignature(oauthParams, "POST", baseUrl, config.getClientSecret(), null)); oauthParams.put("oauth_signature", generateTwitterSignature(oauthParams, "POST", baseUrl, config.getClientSecret(), null));
oauthParams.forEach((k, v) -> oauthParams.put(k, "\"" + urlEncode(v.toString()) + "\"")); String header = buildHeader(oauthParams);
HttpResponse requestToken = HttpRequest.post(baseUrl) HttpResponse requestToken = HttpRequest.post(baseUrl)
.header("Authorization", "OAuth " + GlobalAuthUtil.parseMapToString(oauthParams, false).replaceAll("&", ", ")) .header("Authorization", header)
.execute(); .execute();
checkResponse(requestToken); checkResponse(requestToken);
...@@ -75,10 +76,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest { ...@@ -75,10 +76,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest {
oauthParams.put("oauth_token", authCallback.getOauthToken()); oauthParams.put("oauth_token", authCallback.getOauthToken());
oauthParams.put("oauth_verifier", authCallback.getOauthVerifier()); oauthParams.put("oauth_verifier", authCallback.getOauthVerifier());
oauthParams.put("oauth_signature", generateTwitterSignature(oauthParams, "POST", source.accessToken(), config.getClientSecret(), authCallback.getOauthToken())); oauthParams.put("oauth_signature", generateTwitterSignature(oauthParams, "POST", source.accessToken(), config.getClientSecret(), authCallback.getOauthToken()));
oauthParams.forEach((k, v) -> oauthParams.put(k, "\"" + urlEncode(v.toString()) + "\"")); String header = buildHeader(oauthParams);
HttpResponse response = HttpRequest.post(source.accessToken()) HttpResponse response = HttpRequest.post(source.accessToken())
.header("Authorization", "OAuth " + GlobalAuthUtil.parseMapToString(oauthParams, false).replaceAll("&", ", ")) .header("Authorization", header)
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.form("oauth_verifier", authCallback.getOauthVerifier()) .form("oauth_verifier", authCallback.getOauthVerifier())
.execute(); .execute();
...@@ -108,10 +108,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest { ...@@ -108,10 +108,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest {
Map<String, Object> params = new HashMap<>(oauthParams); Map<String, Object> params = new HashMap<>(oauthParams);
params.putAll(queryParams); params.putAll(queryParams);
oauthParams.put("oauth_signature", generateTwitterSignature(params, "GET", source.userInfo(), config.getClientSecret(), authToken.getOauthTokenSecret())); oauthParams.put("oauth_signature", generateTwitterSignature(params, "GET", source.userInfo(), config.getClientSecret(), authToken.getOauthTokenSecret()));
oauthParams.forEach((k, v) -> oauthParams.put(k, "\"" + urlEncode(v.toString()) + "\"")); String header = buildHeader(oauthParams);
HttpResponse response = HttpRequest.get(userInfoUrl(authToken)) HttpResponse response = HttpRequest.get(userInfoUrl(authToken))
.header("Authorization", "OAuth " + GlobalAuthUtil.parseMapToString(oauthParams, false).replaceAll("&", ", ")) .header("Authorization", header)
.execute(); .execute();
checkResponse(response); checkResponse(response);
JSONObject userInfo = JSONObject.parseObject(response.body()); JSONObject userInfo = JSONObject.parseObject(response.body());
...@@ -148,6 +147,22 @@ public class AuthTwitterRequest extends AuthDefaultRequest { ...@@ -148,6 +147,22 @@ public class AuthTwitterRequest extends AuthDefaultRequest {
return params; return params;
} }
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();
}
private void checkResponse(HttpResponse response) { private void checkResponse(HttpResponse response) {
if (!response.isOk()) { if (!response.isOk()) {
throw new AuthException(response.body()); throw new AuthException(response.body());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册