|
16 | 16 | *******************************************************************************/
|
17 | 17 | package org.mitre.oauth2.service.impl;
|
18 | 18 |
|
| 19 | +import static org.mockito.Matchers.anyString; |
| 20 | + |
19 | 21 | import java.util.HashSet;
|
| 22 | +import java.util.LinkedHashSet; |
20 | 23 | import java.util.Set;
|
21 | 24 |
|
22 | 25 | import org.junit.Before;
|
23 | 26 | import org.junit.Test;
|
24 | 27 | import org.junit.runner.RunWith;
|
25 | 28 | import org.mitre.oauth2.model.ClientDetailsEntity;
|
| 29 | +import org.mitre.oauth2.model.ClientDetailsEntity.AuthMethod; |
26 | 30 | import org.mitre.oauth2.model.SystemScope;
|
27 | 31 | import org.mitre.oauth2.repository.OAuth2ClientRepository;
|
28 | 32 | import org.mitre.oauth2.repository.OAuth2TokenRepository;
|
29 | 33 | import org.mitre.oauth2.service.SystemScopeService;
|
| 34 | +import org.mitre.openid.connect.config.ConfigurationPropertiesBean; |
30 | 35 | import org.mitre.openid.connect.model.WhitelistedSite;
|
31 | 36 | import org.mitre.openid.connect.service.ApprovedSiteService;
|
32 | 37 | import org.mitre.openid.connect.service.BlacklistedSiteService;
|
|
46 | 51 |
|
47 | 52 | import com.google.common.collect.Sets;
|
48 | 53 |
|
| 54 | +import static org.hamcrest.CoreMatchers.any; |
49 | 55 | import static org.hamcrest.CoreMatchers.equalTo;
|
50 | 56 | import static org.hamcrest.CoreMatchers.is;
|
51 | 57 | import static org.hamcrest.CoreMatchers.notNullValue;
|
| 58 | +import static org.hamcrest.CoreMatchers.nullValue; |
52 | 59 |
|
53 | 60 | import static org.junit.Assert.assertThat;
|
54 | 61 | import static org.junit.Assert.fail;
|
@@ -83,6 +90,9 @@ public class TestDefaultOAuth2ClientDetailsEntityService {
|
83 | 90 |
|
84 | 91 | @Mock
|
85 | 92 | private StatsService statsService;
|
| 93 | + |
| 94 | +@Mock |
| 95 | +private ConfigurationPropertiesBean config; |
86 | 96 |
|
87 | 97 | @InjectMocks
|
88 | 98 | private DefaultOAuth2ClientDetailsEntityService service;
|
@@ -135,6 +145,8 @@ public Set<String> answer(InvocationOnMock invocation) throws Throwable {
|
135 | 145 |
|
136 | 146 | // we're not testing reserved scopes here, just pass through when it's called
|
137 | 147 | Mockito.when(scopeService.removeReservedScopes(Matchers.anySet())).then(AdditionalAnswers.returnsFirstArg());
|
| 148 | + |
| 149 | +Mockito.when(config.isHeartMode()).thenReturn(false); |
138 | 150 |
|
139 | 151 | }
|
140 | 152 |
|
@@ -353,4 +365,203 @@ public void updateClient_noOfflineAccess() {
|
353 | 365 |
|
354 | 366 | assertThat(client.getScope().contains(SystemScopeService.OFFLINE_ACCESS), is(equalTo(false)));
|
355 | 367 | }
|
| 368 | + |
| 369 | +@Test(expected = IllegalArgumentException.class) |
| 370 | +public void heartMode_authcode_invalidGrants() { |
| 371 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 372 | + |
| 373 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 374 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 375 | +grantTypes.add("authorization_code"); |
| 376 | +grantTypes.add("implicit"); |
| 377 | +grantTypes.add("client_credentials"); |
| 378 | +client.setGrantTypes(grantTypes); |
| 379 | + |
| 380 | +service.saveNewClient(client); |
| 381 | + |
| 382 | +} |
| 383 | + |
| 384 | +@Test(expected = IllegalArgumentException.class) |
| 385 | +public void heartMode_implicit_invalidGrants() { |
| 386 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 387 | + |
| 388 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 389 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 390 | +grantTypes.add("implicit"); |
| 391 | +grantTypes.add("authorization_code"); |
| 392 | +grantTypes.add("client_credentials"); |
| 393 | +client.setGrantTypes(grantTypes); |
| 394 | + |
| 395 | +service.saveNewClient(client); |
| 396 | + |
| 397 | +} |
| 398 | + |
| 399 | +@Test(expected = IllegalArgumentException.class) |
| 400 | +public void heartMode_clientcreds_invalidGrants() { |
| 401 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 402 | + |
| 403 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 404 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 405 | +grantTypes.add("client_credentials"); |
| 406 | +grantTypes.add("authorization_code"); |
| 407 | +grantTypes.add("implicit"); |
| 408 | +client.setGrantTypes(grantTypes); |
| 409 | + |
| 410 | +service.saveNewClient(client); |
| 411 | + |
| 412 | +} |
| 413 | + |
| 414 | +@Test(expected = IllegalArgumentException.class) |
| 415 | +public void heartMode_authcode_authMethod() { |
| 416 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 417 | + |
| 418 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 419 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 420 | +grantTypes.add("authorization_code"); |
| 421 | +client.setGrantTypes(grantTypes); |
| 422 | + |
| 423 | +client.setTokenEndpointAuthMethod(AuthMethod.SECRET_POST); |
| 424 | + |
| 425 | +service.saveNewClient(client); |
| 426 | + |
| 427 | +} |
| 428 | + |
| 429 | +@Test(expected = IllegalArgumentException.class) |
| 430 | +public void heartMode_implicit_authMethod() { |
| 431 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 432 | + |
| 433 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 434 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 435 | +grantTypes.add("implicit"); |
| 436 | +client.setGrantTypes(grantTypes); |
| 437 | + |
| 438 | +client.setTokenEndpointAuthMethod(AuthMethod.PRIVATE_KEY); |
| 439 | + |
| 440 | +service.saveNewClient(client); |
| 441 | + |
| 442 | +} |
| 443 | + |
| 444 | +@Test(expected = IllegalArgumentException.class) |
| 445 | +public void heartMode_clientcreds_authMethod() { |
| 446 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 447 | + |
| 448 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 449 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 450 | +grantTypes.add("client_credentials"); |
| 451 | +client.setGrantTypes(grantTypes); |
| 452 | + |
| 453 | +client.setTokenEndpointAuthMethod(AuthMethod.SECRET_BASIC); |
| 454 | + |
| 455 | +service.saveNewClient(client); |
| 456 | + |
| 457 | +} |
| 458 | + |
| 459 | +@Test(expected = IllegalArgumentException.class) |
| 460 | +public void heartMode_authcode_redirectUris() { |
| 461 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 462 | + |
| 463 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 464 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 465 | +grantTypes.add("authorization_code"); |
| 466 | +client.setGrantTypes(grantTypes); |
| 467 | + |
| 468 | +client.setTokenEndpointAuthMethod(AuthMethod.PRIVATE_KEY); |
| 469 | + |
| 470 | +service.saveNewClient(client); |
| 471 | + |
| 472 | +} |
| 473 | + |
| 474 | +@Test(expected = IllegalArgumentException.class) |
| 475 | +public void heartMode_implicit_redirectUris() { |
| 476 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 477 | + |
| 478 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 479 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 480 | +grantTypes.add("implicit"); |
| 481 | +client.setGrantTypes(grantTypes); |
| 482 | + |
| 483 | +client.setTokenEndpointAuthMethod(AuthMethod.NONE); |
| 484 | + |
| 485 | +service.saveNewClient(client); |
| 486 | + |
| 487 | +} |
| 488 | + |
| 489 | +@Test(expected = IllegalArgumentException.class) |
| 490 | +public void heartMode_clientcreds_redirectUris() { |
| 491 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 492 | + |
| 493 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 494 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 495 | +grantTypes.add("client_credentials"); |
| 496 | +client.setGrantTypes(grantTypes); |
| 497 | + |
| 498 | +client.setTokenEndpointAuthMethod(AuthMethod.PRIVATE_KEY); |
| 499 | + |
| 500 | +client.setRedirectUris(Sets.newHashSet("http://foo.bar/")); |
| 501 | + |
| 502 | +service.saveNewClient(client); |
| 503 | + |
| 504 | +} |
| 505 | + |
| 506 | +@Test(expected = IllegalArgumentException.class) |
| 507 | +public void heartMode_clientSecret() { |
| 508 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 509 | + |
| 510 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 511 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 512 | +grantTypes.add("authorization_code"); |
| 513 | +client.setGrantTypes(grantTypes); |
| 514 | + |
| 515 | +client.setTokenEndpointAuthMethod(AuthMethod.PRIVATE_KEY); |
| 516 | + |
| 517 | +client.setRedirectUris(Sets.newHashSet("http://foo.bar/")); |
| 518 | + |
| 519 | +client.setClientSecret("secret!"); |
| 520 | + |
| 521 | +service.saveNewClient(client); |
| 522 | + |
| 523 | +} |
| 524 | + |
| 525 | +@Test(expected = IllegalArgumentException.class) |
| 526 | +public void heartMode_noJwks() { |
| 527 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 528 | + |
| 529 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 530 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 531 | +grantTypes.add("authorization_code"); |
| 532 | +client.setGrantTypes(grantTypes); |
| 533 | + |
| 534 | +client.setTokenEndpointAuthMethod(AuthMethod.PRIVATE_KEY); |
| 535 | + |
| 536 | +client.setRedirectUris(Sets.newHashSet("https://foo.bar/")); |
| 537 | + |
| 538 | +client.setJwks(null); |
| 539 | +client.setJwksUri(null); |
| 540 | + |
| 541 | +service.saveNewClient(client); |
| 542 | + |
| 543 | +} |
| 544 | + |
| 545 | +@Test |
| 546 | +public void heartMode_validAuthcodeClient() { |
| 547 | +Mockito.when(config.isHeartMode()).thenReturn(true); |
| 548 | + |
| 549 | +ClientDetailsEntity client = new ClientDetailsEntity(); |
| 550 | +Set<String> grantTypes = new LinkedHashSet<>(); |
| 551 | +grantTypes.add("authorization_code"); |
| 552 | +grantTypes.add("refresh_token"); |
| 553 | +client.setGrantTypes(grantTypes); |
| 554 | + |
| 555 | +client.setTokenEndpointAuthMethod(AuthMethod.PRIVATE_KEY); |
| 556 | + |
| 557 | +client.setRedirectUris(Sets.newHashSet("https://foo.bar/")); |
| 558 | + |
| 559 | +client.setJwksUri("https://foo.bar/jwks"); |
| 560 | + |
| 561 | +service.saveNewClient(client); |
| 562 | + |
| 563 | +assertThat(client.getClientId(), is(notNullValue(String.class))); |
| 564 | +assertThat(client.getClientSecret(), is(nullValue())); |
| 565 | +} |
| 566 | + |
356 | 567 | }
|
0 commit comments