Skip to content

Commit 38710bd

Browse files
committed
unit tests for HEART mode
1 parent 74ea428 commit 38710bd

File tree

3 files changed

+234
-3
lines changed

3 files changed

+234
-3
lines changed

openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultOAuth2ClientDetailsEntityService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private void checkHeartMode(ClientDetailsEntity client) {
226226
}
227227

228228
// make sure we've got the right authentication method
229-
if (client.getTokenEndpointAuthMethod() == null || client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY)) {
229+
if (client.getTokenEndpointAuthMethod() == null || !client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY)) {
230230
throw new IllegalArgumentException("[HEART mode] Authorization code clients must use the private_key authentication method");
231231
}
232232

@@ -243,7 +243,7 @@ private void checkHeartMode(ClientDetailsEntity client) {
243243
}
244244

245245
// make sure we've got the right authentication method
246-
if (client.getTokenEndpointAuthMethod() == null || client.getTokenEndpointAuthMethod().equals(AuthMethod.NONE)) {
246+
if (client.getTokenEndpointAuthMethod() == null || !client.getTokenEndpointAuthMethod().equals(AuthMethod.NONE)) {
247247
throw new IllegalArgumentException("[HEART mode] Implicit clients must use the none authentication method");
248248
}
249249

@@ -260,7 +260,7 @@ private void checkHeartMode(ClientDetailsEntity client) {
260260
}
261261

262262
// make sure we've got the right authentication method
263-
if (client.getTokenEndpointAuthMethod() == null || client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY)) {
263+
if (client.getTokenEndpointAuthMethod() == null || !client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY)) {
264264
throw new IllegalArgumentException("[HEART mode] Client credentials clients must use the private_key authentication method");
265265
}
266266

openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestBlacklistAwareRedirectResolver.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.Before;
2121
import org.junit.Test;
2222
import org.junit.runner.RunWith;
23+
import org.mitre.openid.connect.config.ConfigurationPropertiesBean;
2324
import org.mitre.openid.connect.service.BlacklistedSiteService;
2425
import org.mockito.InjectMocks;
2526
import org.mockito.Mock;
@@ -51,6 +52,9 @@ public class TestBlacklistAwareRedirectResolver {
5152
@Mock
5253
private ClientDetails client;
5354

55+
@Mock
56+
private ConfigurationPropertiesBean config;
57+
5458
@InjectMocks
5559
private BlacklistAwareRedirectResolver resolver;
5660

@@ -72,6 +76,7 @@ public void setUp() throws Exception {
7276
when(client.getAuthorizedGrantTypes()).thenReturn(ImmutableSet.of("authorization_code"));
7377
when(client.getRegisteredRedirectUri()).thenReturn(ImmutableSet.of(goodUri, blacklistedUri));
7478

79+
when(config.isHeartMode()).thenReturn(false);
7580
}
7681

7782
@Test
@@ -128,5 +133,20 @@ public void testRedirectMatches_default() {
128133
assertThat(res2, is(true));
129134

130135
}
136+
137+
@Test
138+
public void testHeartMode() {
139+
when(config.isHeartMode()).thenReturn(true);
140+
141+
// this is not an exact match
142+
boolean res1 = resolver.redirectMatches(pathUri, goodUri);
143+
144+
assertThat(res1, is(false));
145+
146+
// this is an exact match
147+
boolean res2 = resolver.redirectMatches(goodUri, goodUri);
148+
149+
assertThat(res2, is(true));
150+
}
131151

132152
}

openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultOAuth2ClientDetailsEntityService.java

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@
1616
*******************************************************************************/
1717
package org.mitre.oauth2.service.impl;
1818

19+
import static org.mockito.Matchers.anyString;
20+
1921
import java.util.HashSet;
22+
import java.util.LinkedHashSet;
2023
import java.util.Set;
2124

2225
import org.junit.Before;
2326
import org.junit.Test;
2427
import org.junit.runner.RunWith;
2528
import org.mitre.oauth2.model.ClientDetailsEntity;
29+
import org.mitre.oauth2.model.ClientDetailsEntity.AuthMethod;
2630
import org.mitre.oauth2.model.SystemScope;
2731
import org.mitre.oauth2.repository.OAuth2ClientRepository;
2832
import org.mitre.oauth2.repository.OAuth2TokenRepository;
2933
import org.mitre.oauth2.service.SystemScopeService;
34+
import org.mitre.openid.connect.config.ConfigurationPropertiesBean;
3035
import org.mitre.openid.connect.model.WhitelistedSite;
3136
import org.mitre.openid.connect.service.ApprovedSiteService;
3237
import org.mitre.openid.connect.service.BlacklistedSiteService;
@@ -46,9 +51,11 @@
4651

4752
import com.google.common.collect.Sets;
4853

54+
import static org.hamcrest.CoreMatchers.any;
4955
import static org.hamcrest.CoreMatchers.equalTo;
5056
import static org.hamcrest.CoreMatchers.is;
5157
import static org.hamcrest.CoreMatchers.notNullValue;
58+
import static org.hamcrest.CoreMatchers.nullValue;
5259

5360
import static org.junit.Assert.assertThat;
5461
import static org.junit.Assert.fail;
@@ -83,6 +90,9 @@ public class TestDefaultOAuth2ClientDetailsEntityService {
8390

8491
@Mock
8592
private StatsService statsService;
93+
94+
@Mock
95+
private ConfigurationPropertiesBean config;
8696

8797
@InjectMocks
8898
private DefaultOAuth2ClientDetailsEntityService service;
@@ -135,6 +145,8 @@ public Set<String> answer(InvocationOnMock invocation) throws Throwable {
135145

136146
// we're not testing reserved scopes here, just pass through when it's called
137147
Mockito.when(scopeService.removeReservedScopes(Matchers.anySet())).then(AdditionalAnswers.returnsFirstArg());
148+
149+
Mockito.when(config.isHeartMode()).thenReturn(false);
138150

139151
}
140152

@@ -353,4 +365,203 @@ public void updateClient_noOfflineAccess() {
353365

354366
assertThat(client.getScope().contains(SystemScopeService.OFFLINE_ACCESS), is(equalTo(false)));
355367
}
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+
356567
}

0 commit comments

Comments
 (0)