diff --git a/broker/oidc/pom.xml b/broker/oidc/pom.xml index 5bd4d543e6..05a971f106 100755 --- a/broker/oidc/pom.xml +++ b/broker/oidc/pom.xml @@ -52,6 +52,11 @@ jboss-logging provided + + junit + junit + test + diff --git a/broker/oidc/src/main/java/org/keycloak/broker/oidc/AbstractOAuth2IdentityProvider.java b/broker/oidc/src/main/java/org/keycloak/broker/oidc/AbstractOAuth2IdentityProvider.java index 8d57b2c81c..c65e8713ce 100755 --- a/broker/oidc/src/main/java/org/keycloak/broker/oidc/AbstractOAuth2IdentityProvider.java +++ b/broker/oidc/src/main/java/org/keycloak/broker/oidc/AbstractOAuth2IdentityProvider.java @@ -108,11 +108,22 @@ public abstract class AbstractOAuth2IdentityProvider notes = new HashMap<>(); + tested.getFederatedIdentity(notes, "cosi=sss"); + } + + @Test(expected = IdentityBrokerException.class) + public void getFederatedIdentity_responseJSON_tokenNotFound() { + TestProvider tested = getTested(); + Map notes = new HashMap<>(); + tested.getFederatedIdentity(notes, "{\"cosi\":\"sss\"}"); + } + + @Test(expected = IdentityBrokerException.class) + public void getFederatedIdentity_responseJSON_invalidFormat() { + TestProvider tested = getTested(); + Map notes = new HashMap<>(); + tested.getFederatedIdentity(notes, "{\"cosi\":\"sss\""); + } + + @Test(expected = IdentityBrokerException.class) + public void getFederatedIdentity_responseJSON_emptyTokenField() { + TestProvider tested = getTested(); + Map notes = new HashMap<>(); + tested.getFederatedIdentity(notes, "{\"" + + AbstractOAuth2IdentityProvider.OAUTH2_PARAMETER_ACCESS_TOKEN + "\" : \"\"}"); + } + + @Test(expected = IdentityBrokerException.class) + public void getFederatedIdentity_responseJSON_nullTokenField() { + TestProvider tested = getTested(); + Map notes = new HashMap<>(); + tested.getFederatedIdentity(notes, "{\"" + + AbstractOAuth2IdentityProvider.OAUTH2_PARAMETER_ACCESS_TOKEN + "\" : null}"); + } + + @Test + public void getFederatedIdentity_responseJSON() { + TestProvider tested = getTested(); + Map notes = new HashMap<>(); + FederatedIdentity fi = tested.getFederatedIdentity(notes, "{\"" + + AbstractOAuth2IdentityProvider.OAUTH2_PARAMETER_ACCESS_TOKEN + "\" : \"458rt\"}"); + Assert.assertNotNull(fi); + Assert.assertEquals("458rt", fi.getId()); + } + + @Test + public void getFederatedIdentity_responseUrlLine() { + TestProvider tested = getTested(); + Map notes = new HashMap<>(); + FederatedIdentity fi = tested.getFederatedIdentity(notes, "cosi=sss&" + + AbstractOAuth2IdentityProvider.OAUTH2_PARAMETER_ACCESS_TOKEN + "=458rtf&kdesi=ss}"); + Assert.assertNotNull(fi); + Assert.assertEquals("458rtf", fi.getId()); + } + + private TestProvider getTested() { + IdentityProviderModel model = new IdentityProviderModel(); + OAuth2IdentityProviderConfig config = new OAuth2IdentityProviderConfig(model); + return new TestProvider(config); + } + + private static class TestProvider extends AbstractOAuth2IdentityProvider { + + public TestProvider(OAuth2IdentityProviderConfig config) { + super(config); + } + + @Override + protected String getDefaultScopes() { + return "default"; + } + + protected FederatedIdentity doGetFederatedIdentity(String accessToken) { + return new FederatedIdentity(accessToken); + }; + + }; + +}