GlobalAuthUtilsTest.java 10.5 KB
Newer Older
1 2
package me.zhyd.oauth.utils;

智布道's avatar
智布道 已提交
3
import com.alibaba.fastjson.JSON;
H
Hongwei Peng 已提交
4 5 6
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
7 8 9
import org.junit.Assert;
import org.junit.Test;

10 11
import java.util.HashMap;
import java.util.Map;
智布道's avatar
智布道 已提交
12
import java.util.TreeMap;
13

H
Hongwei Peng 已提交
14 15
import static me.zhyd.oauth.config.AuthDefaultSource.TWITTER;
import static org.junit.Assert.assertEquals;
智布道's avatar
智布道 已提交
16
import static org.junit.Assert.assertNotNull;
H
Hongwei Peng 已提交
17

18
public class GlobalAuthUtilsTest {
19 20

    @Test
智布道's avatar
智布道 已提交
21
    public void generateDingTalkSignature() {
22
        assertEquals("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D", GlobalAuthUtils.generateDingTalkSignature("SHA-256", "1562325753000 "));
23 24 25
    }

    @Test
智布道's avatar
智布道 已提交
26
    public void urlDecode() {
27 28 29
        assertEquals("", GlobalAuthUtils.urlDecode(null));
        assertEquals("https://www.foo.bar", GlobalAuthUtils.urlDecode("https://www.foo.bar"));
        assertEquals("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe/Nis5lq9ik=", GlobalAuthUtils.urlDecode("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D"));
30 31 32
    }

    @Test
智布道's avatar
智布道 已提交
33
    public void parseStringToMap() {
34 35
        Map expected = new HashMap();
        expected.put("bar", "baz");
36
        assertEquals(expected, GlobalAuthUtils.parseStringToMap("foo&bar=baz"));
37 38 39
    }

    @Test
智布道's avatar
智布道 已提交
40
    public void isHttpProtocol() {
41 42
        Assert.assertFalse(GlobalAuthUtils.isHttpProtocol(""));
        Assert.assertFalse(GlobalAuthUtils.isHttpProtocol("foo"));
43

44
        Assert.assertTrue(GlobalAuthUtils.isHttpProtocol("http://www.foo.bar"));
45 46 47
    }

    @Test
智布道's avatar
智布道 已提交
48
    public void isHttpsProtocol() {
49 50
        Assert.assertFalse(GlobalAuthUtils.isHttpsProtocol(""));
        Assert.assertFalse(GlobalAuthUtils.isHttpsProtocol("foo"));
51

52
        Assert.assertTrue(GlobalAuthUtils.isHttpsProtocol("https://www.foo.bar"));
53 54 55
    }

    @Test
智布道's avatar
智布道 已提交
56
    public void isLocalHost() {
57
        Assert.assertFalse(GlobalAuthUtils.isLocalHost("foo"));
58

59 60 61
        Assert.assertTrue(GlobalAuthUtils.isLocalHost(""));
        Assert.assertTrue(GlobalAuthUtils.isLocalHost("127.0.0.1"));
        Assert.assertTrue(GlobalAuthUtils.isLocalHost("localhost"));
62
    }
H
Hongwei Peng 已提交
63 64 65 66 67 68 69 70

    @Test
    public void testGenerateTwitterSignatureForRequestToken() {
        AuthConfig config = AuthConfig.builder()
            .clientId("HD0XLqzi5Wz0G08rh45Cg8mgh")
            .clientSecret("0YX3RH2DnPiT77pgzLzFdfpMKX8ENLIWQKYQ7lG5TERuZNgXN5")
            .redirectUri("https://codinglife.tech")
            .build();
71
        Map<String, String> params = new HashMap<>();
H
Hongwei Peng 已提交
72 73 74 75 76 77 78 79
        params.put("oauth_consumer_key", config.getClientId());
        params.put("oauth_nonce", "sTj7Ivg73u052eXstpoS1AWQCynuDEPN");
        params.put("oauth_signature_method", "HMAC-SHA1");
        params.put("oauth_timestamp", "1569750981");
        params.put("oauth_callback", config.getRedirectUri());
        params.put("oauth_version", "1.0");

        String baseUrl = "https://api.twitter.com/oauth/request_token";
智布道's avatar
智布道 已提交
80
        params.put("oauth_signature", GlobalAuthUtils.generateTwitterSignature(params, "POST", baseUrl, config.getClientSecret(), null));
H
Hongwei Peng 已提交
81

智布道's avatar
智布道 已提交
82
        params.forEach((k, v) -> params.put(k, "\"" + GlobalAuthUtils.urlEncode(v) + "\""));
83
        String actual = "OAuth " + GlobalAuthUtils.parseMapToString(params, false).replaceAll("&", ", ");
H
Hongwei Peng 已提交
84 85 86 87 88 89 90 91 92 93 94

        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);
    }

    @Test
    public void testGenerateTwitterSignatureForAccessToken() {
        AuthConfig config = AuthConfig.builder()
            .clientId("HD0XLqzi5Wz0G08rh45Cg8mgh")
            .clientSecret("0YX3RH2DnPiT77pgzLzFdfpMKX8ENLIWQKYQ7lG5TERuZNgXN5")
            .build();
        AuthCallback authCallback = AuthCallback.builder()
95 96
            .oauth_token("W_KLmAAAAAAAxq5LAAABbXxJeD0")
            .oauth_verifier("lYou4gxfA6S5KioUa8VF8HCShzA2nSxp")
H
Hongwei Peng 已提交
97
            .build();
98
        Map<String, String> params = new HashMap<>();
H
Hongwei Peng 已提交
99 100 101 102
        params.put("oauth_consumer_key", config.getClientId());
        params.put("oauth_nonce", "sTj7Ivg73u052eXstpoS1AWQCynuDEPN");
        params.put("oauth_signature_method", "HMAC-SHA1");
        params.put("oauth_timestamp", "1569751082");
103 104
        params.put("oauth_token", authCallback.getOauth_token());
        params.put("oauth_verifier", authCallback.getOauth_verifier());
H
Hongwei Peng 已提交
105 106
        params.put("oauth_version", "1.0");

智布道's avatar
智布道 已提交
107
        params.put("oauth_signature", GlobalAuthUtils.generateTwitterSignature(params, "POST", TWITTER.accessToken(), config.getClientSecret(), authCallback
108
            .getOauth_token()));
H
Hongwei Peng 已提交
109

智布道's avatar
智布道 已提交
110
        params.forEach((k, v) -> params.put(k, "\"" + GlobalAuthUtils.urlEncode(v) + "\""));
111
        String actual = "OAuth " + GlobalAuthUtils.parseMapToString(params, false).replaceAll("&", ", ");
H
Hongwei Peng 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

        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);
    }

    @Test
    public void testGenerateTwitterSignatureForUserInfo() {
        AuthConfig config = AuthConfig.builder()
            .clientId("HD0XLqzi5Wz0G08rh45Cg8mgh")
            .clientSecret("0YX3RH2DnPiT77pgzLzFdfpMKX8ENLIWQKYQ7lG5TERuZNgXN5")
            .build();
        AuthToken authToken = AuthToken.builder()
            .oauthToken("1961977975-PcFQaCnpN9h9xqtqHwHlpGBXFrHJ9bOLy7OtGAL")
            .oauthTokenSecret("ffyKe39GYYf8tAyhliSe3QmazpO65kZp5b49xOFX6wHho")
            .userId("1961977975")
            .screenName("pengisgood")
            .build();

129
        Map<String, String> oauthParams = new HashMap<>();
H
Hongwei Peng 已提交
130 131 132 133 134 135 136
        oauthParams.put("oauth_consumer_key", config.getClientId());
        oauthParams.put("oauth_nonce", "sTj7Ivg73u052eXstpoS1AWQCynuDEPN");
        oauthParams.put("oauth_signature_method", "HMAC-SHA1");
        oauthParams.put("oauth_timestamp", "1569751082");
        oauthParams.put("oauth_token", authToken.getOauthToken());
        oauthParams.put("oauth_version", "1.0");

137
        Map<String, String> queryParams = new HashMap<>();
H
Hongwei Peng 已提交
138 139
        queryParams.put("user_id", authToken.getUserId());
        queryParams.put("screen_name", authToken.getScreenName());
140
        queryParams.put("include_entities", Boolean.toString(true));
H
Hongwei Peng 已提交
141

142
        Map<String, String> params = new HashMap<>(queryParams);
智布道's avatar
智布道 已提交
143
        oauthParams.put("oauth_signature", GlobalAuthUtils.generateTwitterSignature(params, "GET", TWITTER.userInfo(), config.getClientSecret(), authToken
144
            .getOauthTokenSecret()));
智布道's avatar
智布道 已提交
145
        oauthParams.forEach((k, v) -> oauthParams.put(k, "\"" + GlobalAuthUtils.urlEncode(v) + "\""));
H
Hongwei Peng 已提交
146

147
        String actual = "OAuth " + GlobalAuthUtils.parseMapToString(oauthParams, false).replaceAll("&", ", ");
148
        assertEquals("OAuth oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"OsqHjRmBf7syxlz8lB7MRdzqEjY%3D\", oauth_token=\"1961977975-PcFQaCnpN9h9xqtqHwHlpGBXFrHJ9bOLy7OtGAL\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569751082\", oauth_version=\"1.0\"", actual);
H
Hongwei Peng 已提交
149
    }
智布道's avatar
智布道 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    @Test
    public void md5() {
        String str = "helloworld,iamjustauth";
        String md5Str = GlobalAuthUtils.md5(str);
        assertEquals("b0d923de4289b69976448cac718528b8", md5Str);
    }

    @Test
    public void treemap() {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("user_id", "1");
        parameters.put("screen_name", "222");
        parameters.put("a", "222");
        parameters.put("include_entities", Boolean.toString(true));
        final Map<String, Object> sorted = new TreeMap<>(parameters);
        assertEquals("{\"a\":\"222\",\"include_entities\":\"true\",\"screen_name\":\"222\",\"user_id\":\"1\"}", JSON.toJSONString(sorted));
    }

    @Test
    public void urlEncode() {
        assertEquals("", GlobalAuthUtils.urlEncode(null));
        assertEquals("https%3A%2F%2Fwww.foo.bar", GlobalAuthUtils.urlEncode("https://www.foo.bar"));
        assertEquals("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%252FNis5lq9ik%253D", GlobalAuthUtils.urlEncode("mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D"));
    }

    @Test
    public void parseMapToString() {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("user_id", "1");
        parameters.put("screen_name", "史上最全的第三方授权登录库");
        parameters.put("include_entities", Boolean.toString(true));
        assertEquals("user_id=1&screen_name=史上最全的第三方授权登录库&include_entities=true", GlobalAuthUtils.parseMapToString(parameters, false));
        assertEquals("user_id=1&screen_name=%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8%E7%9A%84%E7%AC%AC%E4%B8%89%E6%96%B9%E6%8E%88%E6%9D%83%E7%99%BB%E5%BD%95%E5%BA%93&include_entities=true", GlobalAuthUtils.parseMapToString(parameters, true));
        assertEquals("", GlobalAuthUtils.parseMapToString(null, true));
    }

    @Test
    public void generateNonce() {
        assertEquals(10, GlobalAuthUtils.generateNonce(10).length());
    }

    @Test
    public void getTimestamp() {
        assertNotNull(GlobalAuthUtils.getTimestamp());
    }

    @Test
    public void generateTwitterSignature() {
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("name", "你好");
        queryParams.put("gender", "male");

203
        assertEquals("20FYnV2aZnxNQtp+I0tpMRTvcx0=", GlobalAuthUtils.generateTwitterSignature(queryParams, "GET", TWITTER.userInfo(), "xxxxx", "xxxxx"));
智布道's avatar
智布道 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    }

    @Test
    public void generateElemeSignature() {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("name", "你好");
        parameters.put("gender", "male");

        String appKey = "appKey";
        String secret = "appKey";
        long timestamp = 1233456789;
        String action = "appKey";
        String token = "appKey";

        assertEquals("26FEB8BF7E84FED2619D9C5D97F421BD", GlobalAuthUtils.generateElemeSignature(appKey, secret, timestamp, action, token, parameters));
    }

    @Test
    public void generateJdSignature() {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("name", "你好");
        parameters.put("gender", "male");

        String appSecret = "appKey";

        assertEquals("FE04EC03BA8A619802CF309959C2B43F", GlobalAuthUtils.generateJdSignature(appSecret, parameters));
    }
231
}