Oauth2UtilTest.java 14.1 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
智布道's avatar
智布道 已提交
16 17
package com.fujieid.jap.oauth2;

智布道's avatar
智布道 已提交
18 19
import com.fujieid.jap.core.cache.JapCache;
import com.fujieid.jap.core.cache.JapLocalCache;
智布道's avatar
智布道 已提交
20 21
import com.fujieid.jap.core.context.JapAuthentication;
import com.fujieid.jap.core.context.JapContext;
智布道's avatar
智布道 已提交
22
import com.fujieid.jap.core.exception.JapOauth2Exception;
23
import com.fujieid.jap.http.JapHttpRequest;
24
import com.fujieid.jap.http.adapter.jakarta.JakartaRequestAdapter;
25
import com.fujieid.jap.oauth2.pkce.PkceCodeChallengeMethod;
智布道's avatar
智布道 已提交
26
import com.xkcoding.json.util.Kv;
智布道's avatar
智布道 已提交
27
import org.junit.Assert;
智布道's avatar
智布道 已提交
28
import org.junit.Before;
智布道's avatar
智布道 已提交
29
import org.junit.Test;
智布道's avatar
智布道 已提交
30 31 32 33
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import javax.servlet.http.HttpServletRequest;
智布道's avatar
智布道 已提交
34

智布道's avatar
智布道 已提交
35 36 37 38 39 40 41
/**
 * unit test
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0.0
 * @since 1.0.0
 */
智布道's avatar
智布道 已提交
42 43
public class Oauth2UtilTest {

44
    public JapHttpRequest request;
智布道's avatar
智布道 已提交
45 46 47 48 49 50
    @Mock
    private HttpServletRequest httpServletRequestMock;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
51
        this.request = new JakartaRequestAdapter(httpServletRequestMock);
智布道's avatar
智布道 已提交
52 53
    }

智布道's avatar
智布道 已提交
54
    @Test
智布道's avatar
智布道 已提交
55 56 57 58 59 60 61 62 63
    public void generateCodeChallengeCodeChallengeMethodIsS256() {
        String s256Challenge = Oauth2Util.generateCodeChallenge(PkceCodeChallengeMethod.S256, "asdasdasdasd");
        Assert.assertEquals("ZrETKgFzkQsB7joV705pWDu_L38eRGLJnvvhuatb-Ag", s256Challenge);
    }

    @Test
    public void generateCodeChallengeCodeChallengeMethodIsPlain() {
        String plainChallenge = Oauth2Util.generateCodeChallenge(PkceCodeChallengeMethod.PLAIN, "asdasdasdasd");
        Assert.assertEquals("asdasdasdasd", plainChallenge);
智布道's avatar
智布道 已提交
64 65 66 67 68 69
    }

    @Test
    public void generateCodeVerifier() {
        String verifier = Oauth2Util.generateCodeVerifier();
        System.out.println(verifier);
智布道's avatar
智布道 已提交
70
        Assert.assertEquals(68, verifier.length());
智布道's avatar
智布道 已提交
71 72 73 74 75 76
    }

    @Test
    public void checkOauthResponseNoError() {
        Kv kv = new Kv();
        String errorMsg = "errorMsg";
智布道's avatar
智布道 已提交
77
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthResponse(kv, errorMsg));
智布道's avatar
智布道 已提交
78 79 80 81 82 83 84 85
    }

    @Test
    public void checkOauthResponseHasEmptyError() {
        Kv kv = new Kv();
        kv.put("error", "");
        kv.put("error_description", "invalid_request_description");
        String errorMsg = "errorMsg";
智布道's avatar
智布道 已提交
86
        Oauth2Util.checkOauthResponse(kv, errorMsg);
智布道's avatar
智布道 已提交
87 88 89 90 91 92 93 94
    }

    @Test
    public void checkOauthResponseHasNullError() {
        Kv kv = new Kv();
        kv.put("error", null);
        kv.put("error_description", "invalid_request_description");
        String errorMsg = "errorMsg";
智布道's avatar
智布道 已提交
95
        Oauth2Util.checkOauthResponse(kv, errorMsg);
智布道's avatar
智布道 已提交
96 97 98 99 100 101 102 103
    }

    @Test
    public void checkOauthResponseHasError() {
        Kv kv = new Kv();
        kv.put("error", "invalid_request");
        kv.put("error_description", "invalid_request_description");
        String errorMsg = "errorMsg";
智布道's avatar
智布道 已提交
104
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthResponse(kv, errorMsg));
智布道's avatar
智布道 已提交
105 106 107 108 109 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
    }

    @Test
    public void checkOauthCallbackRequestNullError() {
        Oauth2Util.checkOauthCallbackRequest(null, null, null);
    }

    @Test
    public void checkOauthCallbackRequestEmptyError() {
        Oauth2Util.checkOauthCallbackRequest("", null, null);
    }

    @Test
    public void checkOauthCallbackRequestHasError() {
        String requestErrorDescParam = "requestErrorDescParam";
        String bizErrorMsg = "bizErrorMsg";
        String requestErrorParam = "requestErrorParam";
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthCallbackRequest(requestErrorParam, requestErrorDescParam, bizErrorMsg));
    }

    @Test
    public void checkOauthCallbackRequestHasErrorAndEmptyBizErrorMsg() {
        String requestErrorDescParam = "requestErrorDescParam";
        String bizErrorMsg = "";
        String requestErrorParam = "requestErrorParam";
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthCallbackRequest(requestErrorParam, requestErrorDescParam, bizErrorMsg));
    }

    @Test
    public void checkOauthCallbackRequestHasErrorAndNullBizErrorMsg() {
        String requestErrorDescParam = "requestErrorDescParam";
        String bizErrorMsg = null;
        String requestErrorParam = "requestErrorParam";
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthCallbackRequest(requestErrorParam, requestErrorDescParam, bizErrorMsg));
    }

智布道's avatar
智布道 已提交
141 142 143 144 145 146
    @Test
    public void checkStateNullStateAndNullClientId() {
        boolean verifyState = true;
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkState(null, null, verifyState));
    }

智布道's avatar
智布道 已提交
147 148 149 150 151 152
    @Test
    public void checkStateNoVerify() {
        String state = "xxx";
        String clientId = "xx";
        boolean verifyState = false;
        Oauth2Util.checkState(state, clientId, verifyState);
智布道's avatar
智布道 已提交
153
    }
智布道's avatar
智布道 已提交
154 155 156 157 158 159 160 161

    @Test
    public void checkStateCacheExists() {
        String state = "xxx";
        String clientId = "xx";
        boolean verifyState = true;
        JapCache cache = new JapLocalCache();
        cache.set(Oauth2Const.STATE_CACHE_KEY.concat(clientId), state);
智布道's avatar
智布道 已提交
162
        JapAuthentication.setContext(new JapContext().setCache(cache));
智布道's avatar
智布道 已提交
163 164 165 166 167 168 169 170 171 172
        Oauth2Util.checkState(state, clientId, verifyState);
    }

    @Test
    public void checkStateCacheDoesNotExist() {
        String state = "xxx";
        String clientId = "xx";
        boolean verifyState = true;
        JapCache cache = new JapLocalCache();
        cache.set(Oauth2Const.STATE_CACHE_KEY.concat(clientId), "11");
智布道's avatar
智布道 已提交
173
        JapAuthentication.setContext(new JapContext().setCache(cache));
智布道's avatar
智布道 已提交
174 175 176 177
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkState(state, clientId, verifyState));
    }


智布道's avatar
智布道 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    @Test
    public void checkOauthConfigNullTokenUrl() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()));
    }


    @Test
    public void checkOauthConfigHasTokenUrlAndNullGrantType() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")));
    }


    @Test
    public void checkOauthConfigCodeResponseTypeAndGrantTypeIsNotAuthorizationCode() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
195 196
            .setResponseType(Oauth2ResponseType.CODE)
            .setGrantType(Oauth2GrantType.PASSWORD)));
智布道's avatar
智布道 已提交
197 198 199 200 201 202 203
    }


    @Test
    public void checkOauthConfigCodeResponseTypeAndClientSecretIsNullWhenPkceIsNotEnabled() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
204 205
            .setResponseType(Oauth2ResponseType.CODE)
            .setGrantType(Oauth2GrantType.AUTHORIZATION_CODE)
智布道's avatar
智布道 已提交
206 207 208 209 210 211 212 213
            .setClientSecret(null)));
    }


    @Test
    public void checkOauthConfigCodeResponseTypeAndClientSecretIsEmptyWhenPkceIsNotEnabled() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
214 215
            .setResponseType(Oauth2ResponseType.CODE)
            .setGrantType(Oauth2GrantType.AUTHORIZATION_CODE)
智布道's avatar
智布道 已提交
216 217 218 219 220 221 222 223
            .setClientSecret("")));
    }


    @Test
    public void checkOauthConfigCodeResponseTypeAndClientSecretIsNullWhenPkceIsEnabled() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
224 225
            .setResponseType(Oauth2ResponseType.CODE)
            .setGrantType(Oauth2GrantType.AUTHORIZATION_CODE)
智布道's avatar
智布道 已提交
226 227 228 229 230 231 232 233 234
            .setEnablePkce(true)
            .setClientSecret(null)));
    }


    @Test
    public void checkOauthConfigCodeResponseTypeAndClientSecretIsEmptyWhenPkceIsEnabled() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
235 236
            .setResponseType(Oauth2ResponseType.CODE)
            .setGrantType(Oauth2GrantType.AUTHORIZATION_CODE)
智布道's avatar
智布道 已提交
237 238 239 240 241 242 243 244 245
            .setEnablePkce(true)
            .setClientSecret("")));
    }


    @Test
    public void checkOauthConfigCodeResponseTypeAndClientSecretIsNotEmptyWhenPkceIsEnabled() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
246 247
            .setResponseType(Oauth2ResponseType.CODE)
            .setGrantType(Oauth2GrantType.AUTHORIZATION_CODE)
智布道's avatar
智布道 已提交
248 249 250 251 252 253 254 255 256
            .setEnablePkce(true)
            .setClientSecret("ClientSecret")));
    }


    @Test
    public void checkOauthConfigTokenResponseTypeAndClientSecretIsEmpty() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
257
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
258 259 260 261 262 263 264 265
            .setClientSecret("")));
    }


    @Test
    public void checkOauthConfigTokenResponseTypeAndClientSecretIsNull() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
266
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
267 268 269 270 271 272 273 274
            .setClientSecret(null)));
    }


    @Test
    public void checkOauthConfigCodeOrTokenResponseTypeAndClientIdIsNull() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
275
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
276 277 278 279 280 281 282 283
            .setClientSecret("ClientSecret")
            .setClientId(null)));
    }

    @Test
    public void checkOauthConfigCodeOrTokenResponseTypeAndClientIdIsEmpty() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
284
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
285 286 287 288 289 290 291 292
            .setClientSecret("ClientSecret")
            .setClientId("")));
    }

    @Test
    public void checkOauthConfigCodeOrTokenResponseTypeAndAuthorizationUrlIsEmpty() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
293
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
294 295 296 297 298 299 300 301 302
            .setClientSecret("ClientSecret")
            .setClientId("ClientId")
            .setAuthorizationUrl("")));
    }

    @Test
    public void checkOauthConfigCodeOrTokenResponseTypeAndAuthorizationUrlIsNull() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
303
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
304 305 306 307 308 309 310 311 312
            .setClientSecret("ClientSecret")
            .setClientId("ClientId")
            .setAuthorizationUrl(null)));
    }

    @Test
    public void checkOauthConfigCodeOrTokenResponseTypeAndUserinfoUrlIsNull() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
313
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
314 315 316 317 318 319 320 321 322 323
            .setClientSecret("ClientSecret")
            .setClientId("ClientId")
            .setAuthorizationUrl("AuthorizationUrl")
            .setUserinfoUrl(null)));
    }

    @Test
    public void checkOauthConfigCodeOrTokenResponseTypeAndUserinfoUrlIsEmpty() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
324
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
325 326 327 328 329 330 331 332 333 334
            .setClientSecret("ClientSecret")
            .setClientId("ClientId")
            .setAuthorizationUrl("AuthorizationUrl")
            .setUserinfoUrl("")));
    }

    @Test
    public void checkOauthConfigCodeOrTokenResponseType() {
        Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
335
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
336 337 338 339 340 341 342 343 344 345
            .setClientSecret("ClientSecret")
            .setClientId("ClientId")
            .setAuthorizationUrl("AuthorizationUrl")
            .setUserinfoUrl("UserinfoUrl"));
    }

    @Test
    public void checkOauthConfigClientCredentialsGrantType() {
        Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
346
            .setGrantType(Oauth2GrantType.CLIENT_CREDENTIALS));
智布道's avatar
智布道 已提交
347 348 349 350 351 352
    }

    @Test
    public void checkOauthConfigPasswordGrantTypeAndNullUsernameAndPassword() {
        Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
353
            .setGrantType(Oauth2GrantType.PASSWORD)));
智布道's avatar
智布道 已提交
354 355 356 357 358 359
    }

    @Test
    public void checkOauthConfigPasswordGrantTypeAndHasUsernameAndPassword() {
        Oauth2Util.checkOauthConfig(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
360
            .setGrantType(Oauth2GrantType.PASSWORD)
智布道's avatar
智布道 已提交
361 362 363 364 365 366
            .setUsername("username")
            .setPassword("password"));
    }

    @Test
    public void isCallbackCodeResponseType() {
367
        boolean res = Oauth2Util.isCallback(request, new OAuthConfig()
智布道's avatar
智布道 已提交
368
            .setResponseType(Oauth2ResponseType.CODE));
智布道's avatar
智布道 已提交
369 370 371 372 373
        Assert.assertFalse(res);
    }

    @Test
    public void isCallbackTokenResponseType() {
374
        boolean res = Oauth2Util.isCallback(request, new OAuthConfig()
智布道's avatar
智布道 已提交
375
            .setResponseType(Oauth2ResponseType.TOKEN));
智布道's avatar
智布道 已提交
376 377 378 379 380
        Assert.assertFalse(res);
    }

    @Test
    public void isCallbackNoneResponseType() {
381
        boolean res = Oauth2Util.isCallback(request, new OAuthConfig()
智布道's avatar
智布道 已提交
382
            .setResponseType(Oauth2ResponseType.NONE));
智布道's avatar
智布道 已提交
383 384
        Assert.assertFalse(res);
    }
智布道's avatar
智布道 已提交
385
}