♻️ 抽取 HTTP,具体实现交给使用者,解耦 hutool-http
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package me.zhyd.oauth.sdk;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xkcoding.http.HttpUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
@@ -15,26 +17,28 @@ public class ThirdPartSdkTest {
|
||||
@Test
|
||||
public void huawei() {
|
||||
String code = "CF1IwmFc6uZABI9Y795BkhXfvHidIFFw04I4Zc4KML4n+vlXxwNUcQKS4xlopjFDpEk6LzQbjwdTNxvjZ9jqnd/1m5nswhx8X7e0/dL2kyGAMVZWFgVq9ClxNN18b+Z0xtfJjkm7bDnfC3W5h4COgTCoLSjiWKSHWp5hCunp6pQRo1FHovZXm13TLNlhF9mCVtJx3kTQ";
|
||||
HttpResponse response = HttpRequest.post("https://oauth-login.cloud.huawei.com/oauth2/v2/token")
|
||||
.form("grant_type", "authorization_code")
|
||||
.form("code", code)
|
||||
.form("client_id", "100994535")
|
||||
.form("client_secret", "22aea400bef603fef26d15a79c806eb477b35de0a529758f2a3b1bda32bfb80d")
|
||||
.form("redirect_uri", "http://127.0.0.1:8443/oauth/callback/huawei")
|
||||
.execute();
|
||||
System.out.println(response.body());
|
||||
|
||||
Map<String, String> form = new HashMap<>(5);
|
||||
form.put("grant_type", "authorization_code");
|
||||
form.put("code", code);
|
||||
form.put("client_id", "100994535");
|
||||
form.put("client_secret", "22aea400bef603fef26d15a79c806eb477b35de0a529758f2a3b1bda32bfb80d");
|
||||
form.put("redirect_uri", "http://127.0.0.1:8443/oauth/callback/huawei");
|
||||
|
||||
String response = HttpUtil.post("https://oauth-login.cloud.huawei.com/oauth2/v2/token", form, false);
|
||||
System.out.println(response);
|
||||
|
||||
// {"access_token":"accessToken","expires_in":3600,"refresh_token":"refreshToken","scope":"https:\/\/www.huawei.com\/auth\/account\/base.profile","token_type":"Bearer"}
|
||||
|
||||
//
|
||||
HttpResponse response2 = HttpRequest.post("https://api.vmall.com/rest.php")
|
||||
.form("nsp_ts", System.currentTimeMillis())
|
||||
.form("access_token", JSONObject.parseObject(response.body()).getString("access_token"))
|
||||
.form("nsp_fmt", "JS")
|
||||
// .form("nsp_cb", "_jqjsp")
|
||||
.form("nsp_svc", "OpenUP.User.getInfo")
|
||||
.execute();
|
||||
System.out.println(response2.body());
|
||||
form.clear();
|
||||
form.put("nsp_ts", System.currentTimeMillis() + "");
|
||||
form.put("access_token", JSONObject.parseObject(response).getString("access_token"));
|
||||
form.put("nsp_fmt", "JS");
|
||||
// form.put("nsp_cb", "_jqjsp");
|
||||
form.put("nsp_svc", "OpenUP.User.getInfo");
|
||||
String response2 = HttpUtil.post("https://api.vmall.com/rest.php", form, false);
|
||||
System.out.println(response2);
|
||||
// 华为性别 0是男,女是1
|
||||
// {"gender":1,"headPictureURL":"https://upfile-drcn.platform.hicloud.com/FileServer/image/b.0260086000226601572.20190415065228.iBKdTsqaNkdPXSz4N7pIRWAgeu45ec3k.1000.9A5467309F9284B267ECA33B59D3D7DA4A71BC732D3BB24EC6B880A73DEE9BAB.jpg","languageCode":"zh-CN","userID":"260086000226601572","userName":"151****2326","userState":1,"userValidStatus":1}
|
||||
}
|
||||
|
||||
@@ -10,56 +10,54 @@ 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;
|
||||
import static me.zhyd.oauth.utils.GlobalAuthUtils.generateTwitterSignature;
|
||||
import static me.zhyd.oauth.utils.GlobalAuthUtils.urlEncode;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class GlobalAuthUtilTest {
|
||||
public class GlobalAuthUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testGenerateDingTalkSignature() {
|
||||
assertEquals("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D",
|
||||
GlobalAuthUtil.generateDingTalkSignature("SHA-256", "1562325753000 "));
|
||||
assertEquals("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D", GlobalAuthUtils.generateDingTalkSignature("SHA-256", "1562325753000 "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlDecode() {
|
||||
assertEquals("", GlobalAuthUtil.urlDecode(null));
|
||||
assertEquals("https://www.foo.bar", GlobalAuthUtil.urlDecode("https://www.foo.bar"));
|
||||
assertEquals("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe/Nis5lq9ik=",
|
||||
GlobalAuthUtil.urlDecode("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D"));
|
||||
assertEquals("", GlobalAuthUtils.urlDecode(null));
|
||||
assertEquals("https://www.foo.bar", GlobalAuthUtils.urlDecode("https://www.foo.bar"));
|
||||
assertEquals("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe/Nis5lq9ik=", GlobalAuthUtils.urlDecode("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseStringToMap() {
|
||||
Map expected = new HashMap();
|
||||
expected.put("bar", "baz");
|
||||
assertEquals(expected, GlobalAuthUtil.parseStringToMap("foo&bar=baz"));
|
||||
assertEquals(expected, GlobalAuthUtils.parseStringToMap("foo&bar=baz"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsHttpProtocol() {
|
||||
Assert.assertFalse(GlobalAuthUtil.isHttpProtocol(""));
|
||||
Assert.assertFalse(GlobalAuthUtil.isHttpProtocol("foo"));
|
||||
Assert.assertFalse(GlobalAuthUtils.isHttpProtocol(""));
|
||||
Assert.assertFalse(GlobalAuthUtils.isHttpProtocol("foo"));
|
||||
|
||||
Assert.assertTrue(GlobalAuthUtil.isHttpProtocol("http://www.foo.bar"));
|
||||
Assert.assertTrue(GlobalAuthUtils.isHttpProtocol("http://www.foo.bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsHttpsProtocol() {
|
||||
Assert.assertFalse(GlobalAuthUtil.isHttpsProtocol(""));
|
||||
Assert.assertFalse(GlobalAuthUtil.isHttpsProtocol("foo"));
|
||||
Assert.assertFalse(GlobalAuthUtils.isHttpsProtocol(""));
|
||||
Assert.assertFalse(GlobalAuthUtils.isHttpsProtocol("foo"));
|
||||
|
||||
Assert.assertTrue(GlobalAuthUtil.isHttpsProtocol("https://www.foo.bar"));
|
||||
Assert.assertTrue(GlobalAuthUtils.isHttpsProtocol("https://www.foo.bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsLocalHost() {
|
||||
Assert.assertFalse(GlobalAuthUtil.isLocalHost("foo"));
|
||||
Assert.assertFalse(GlobalAuthUtils.isLocalHost("foo"));
|
||||
|
||||
Assert.assertTrue(GlobalAuthUtil.isLocalHost(""));
|
||||
Assert.assertTrue(GlobalAuthUtil.isLocalHost("127.0.0.1"));
|
||||
Assert.assertTrue(GlobalAuthUtil.isLocalHost("localhost"));
|
||||
Assert.assertTrue(GlobalAuthUtils.isLocalHost(""));
|
||||
Assert.assertTrue(GlobalAuthUtils.isLocalHost("127.0.0.1"));
|
||||
Assert.assertTrue(GlobalAuthUtils.isLocalHost("localhost"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -69,7 +67,7 @@ public class GlobalAuthUtilTest {
|
||||
.clientSecret("0YX3RH2DnPiT77pgzLzFdfpMKX8ENLIWQKYQ7lG5TERuZNgXN5")
|
||||
.redirectUri("https://codinglife.tech")
|
||||
.build();
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("oauth_consumer_key", config.getClientId());
|
||||
params.put("oauth_nonce", "sTj7Ivg73u052eXstpoS1AWQCynuDEPN");
|
||||
params.put("oauth_signature_method", "HMAC-SHA1");
|
||||
@@ -81,7 +79,7 @@ public class GlobalAuthUtilTest {
|
||||
params.put("oauth_signature", generateTwitterSignature(params, "POST", baseUrl, config.getClientSecret(), null));
|
||||
|
||||
params.forEach((k, v) -> params.put(k, "\"" + urlEncode(v.toString()) + "\""));
|
||||
String actual = "OAuth " + GlobalAuthUtil.parseMapToString(params, false).replaceAll("&", ", ");
|
||||
String actual = "OAuth " + GlobalAuthUtils.parseMapToString(params, false).replaceAll("&", ", ");
|
||||
|
||||
assertEquals("OAuth oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"%2BL5Jq%2FTaKubge04cWw%2B4yfjFlaU%3D\", oauth_callback=\"https%3A%2F%2Fcodinglife.tech\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569750981\", oauth_version=\"1.0\"", actual);
|
||||
}
|
||||
@@ -96,7 +94,7 @@ public class GlobalAuthUtilTest {
|
||||
.oauthToken("W_KLmAAAAAAAxq5LAAABbXxJeD0")
|
||||
.oauthVerifier("lYou4gxfA6S5KioUa8VF8HCShzA2nSxp")
|
||||
.build();
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("oauth_consumer_key", config.getClientId());
|
||||
params.put("oauth_nonce", "sTj7Ivg73u052eXstpoS1AWQCynuDEPN");
|
||||
params.put("oauth_signature_method", "HMAC-SHA1");
|
||||
@@ -105,10 +103,11 @@ public class GlobalAuthUtilTest {
|
||||
params.put("oauth_verifier", authCallback.getOauthVerifier());
|
||||
params.put("oauth_version", "1.0");
|
||||
|
||||
params.put("oauth_signature", generateTwitterSignature(params, "POST", TWITTER.accessToken(), config.getClientSecret(), authCallback.getOauthToken()));
|
||||
params.put("oauth_signature", generateTwitterSignature(params, "POST", TWITTER.accessToken(), config.getClientSecret(), authCallback
|
||||
.getOauthToken()));
|
||||
|
||||
params.forEach((k, v) -> params.put(k, "\"" + urlEncode(v.toString()) + "\""));
|
||||
String actual = "OAuth " + GlobalAuthUtil.parseMapToString(params, false).replaceAll("&", ", ");
|
||||
String actual = "OAuth " + GlobalAuthUtils.parseMapToString(params, false).replaceAll("&", ", ");
|
||||
|
||||
assertEquals("OAuth oauth_verifier=\"lYou4gxfA6S5KioUa8VF8HCShzA2nSxp\", oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"9i0lmWgvphtkl2KcCO9VyZ3K2%2F0%3D\", oauth_token=\"W_KLmAAAAAAAxq5LAAABbXxJeD0\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569751082\", oauth_version=\"1.0\"", actual);
|
||||
}
|
||||
@@ -126,7 +125,7 @@ public class GlobalAuthUtilTest {
|
||||
.screenName("pengisgood")
|
||||
.build();
|
||||
|
||||
Map<String, Object> oauthParams = new HashMap<>();
|
||||
Map<String, String> oauthParams = new HashMap<>();
|
||||
oauthParams.put("oauth_consumer_key", config.getClientId());
|
||||
oauthParams.put("oauth_nonce", "sTj7Ivg73u052eXstpoS1AWQCynuDEPN");
|
||||
oauthParams.put("oauth_signature_method", "HMAC-SHA1");
|
||||
@@ -134,17 +133,17 @@ public class GlobalAuthUtilTest {
|
||||
oauthParams.put("oauth_token", authToken.getOauthToken());
|
||||
oauthParams.put("oauth_version", "1.0");
|
||||
|
||||
Map<String, Object> queryParams = new HashMap<>();
|
||||
Map<String, String> queryParams = new HashMap<>();
|
||||
queryParams.put("user_id", authToken.getUserId());
|
||||
queryParams.put("screen_name", authToken.getScreenName());
|
||||
queryParams.put("include_entities", true);
|
||||
queryParams.put("include_entities", Boolean.toString(true));
|
||||
|
||||
Map<String, Object> params = new HashMap<>(oauthParams);
|
||||
params.putAll(queryParams);
|
||||
oauthParams.put("oauth_signature", generateTwitterSignature(params, "GET", TWITTER.userInfo(), config.getClientSecret(), authToken.getOauthTokenSecret()));
|
||||
Map<String, String> params = new HashMap<>(queryParams);
|
||||
oauthParams.put("oauth_signature", generateTwitterSignature(params, "GET", TWITTER.userInfo(), config.getClientSecret(), authToken
|
||||
.getOauthTokenSecret()));
|
||||
oauthParams.forEach((k, v) -> oauthParams.put(k, "\"" + urlEncode(v.toString()) + "\""));
|
||||
|
||||
String actual = "OAuth "+ GlobalAuthUtil.parseMapToString(oauthParams, false).replaceAll("&", ", ");
|
||||
String actual = "OAuth " + GlobalAuthUtils.parseMapToString(oauthParams, false).replaceAll("&", ", ");
|
||||
assertEquals("OAuth oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"elV04U%2FiLm%2Ff3ue1dSrZeChFkEM%3D\", oauth_token=\"1961977975-PcFQaCnpN9h9xqtqHwHlpGBXFrHJ9bOLy7OtGAL\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569751082\", oauth_version=\"1.0\"", actual);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user