|
| 1 | +package org.keycloak.testsuite.script; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.stream.Stream; |
| 6 | + |
| 7 | +import javax.ws.rs.core.Response; |
| 8 | + |
| 9 | +import org.jboss.arquillian.container.test.api.Deployer; |
| 10 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 11 | +import org.jboss.arquillian.container.test.api.TargetsContainer; |
| 12 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 13 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 14 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 15 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Assert; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | +import org.keycloak.common.Profile; |
| 22 | +import org.keycloak.dom.saml.v2.assertion.AssertionType; |
| 23 | +import org.keycloak.dom.saml.v2.assertion.AttributeType; |
| 24 | +import org.keycloak.protocol.saml.SamlProtocol; |
| 25 | +import org.keycloak.protocol.saml.mappers.AttributeStatementHelper; |
| 26 | +import org.keycloak.protocol.saml.mappers.ScriptBasedMapper; |
| 27 | +import org.keycloak.provider.ProviderConfigProperty; |
| 28 | +import org.keycloak.representations.idm.ProtocolMapperRepresentation; |
| 29 | +import org.keycloak.representations.provider.ScriptProviderDescriptor; |
| 30 | +import org.keycloak.saml.common.constants.JBossSAMLURIConstants; |
| 31 | +import org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder; |
| 32 | +import org.keycloak.testsuite.arquillian.annotation.EnableFeature; |
| 33 | +import org.keycloak.testsuite.arquillian.annotation.EnableFeatures; |
| 34 | +import org.keycloak.testsuite.saml.AbstractSamlTest; |
| 35 | +import org.keycloak.testsuite.saml.RoleMapperTest; |
| 36 | +import org.keycloak.testsuite.updaters.ClientAttributeUpdater; |
| 37 | +import org.keycloak.testsuite.updaters.ProtocolMappersUpdater; |
| 38 | +import org.keycloak.testsuite.util.ContainerAssume; |
| 39 | +import org.keycloak.testsuite.util.Matchers; |
| 40 | +import org.keycloak.testsuite.util.SamlClient; |
| 41 | +import org.keycloak.testsuite.util.SamlClientBuilder; |
| 42 | +import org.keycloak.util.JsonSerialization; |
| 43 | + |
| 44 | +import static org.junit.Assert.assertFalse; |
| 45 | +import static org.junit.Assert.assertThat; |
| 46 | +import static org.keycloak.common.Profile.Feature.SCRIPTS; |
| 47 | +import static org.keycloak.testsuite.arquillian.DeploymentTargetModifier.AUTH_SERVER_CURRENT; |
| 48 | +import static org.keycloak.testsuite.saml.RoleMapperTest.createSamlProtocolMapper; |
| 49 | +import static org.keycloak.testsuite.util.SamlStreams.assertionsUnencrypted; |
| 50 | +import static org.keycloak.testsuite.util.SamlStreams.attributeStatements; |
| 51 | +import static org.keycloak.testsuite.util.SamlStreams.attributesUnecrypted; |
| 52 | + |
| 53 | +/** |
| 54 | + * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> |
| 55 | + */ |
| 56 | +public class DeployedSAMLScriptMapperTest extends AbstractSamlTest { |
| 57 | + |
| 58 | + private static final String SCRIPT_DEPLOYMENT_NAME = "scripts.jar"; |
| 59 | + |
| 60 | + private ClientAttributeUpdater cau; |
| 61 | + private ProtocolMappersUpdater pmu; |
| 62 | + |
| 63 | + @Deployment(name = SCRIPT_DEPLOYMENT_NAME, managed = false, testable = false) |
| 64 | + @TargetsContainer(AUTH_SERVER_CURRENT) |
| 65 | + public static JavaArchive deploy() throws IOException { |
| 66 | + ScriptProviderDescriptor representation = new ScriptProviderDescriptor(); |
| 67 | + |
| 68 | + representation.addSAMLMapper("My Mapper", "mapper-a.js"); |
| 69 | + |
| 70 | + return ShrinkWrap.create(JavaArchive.class, SCRIPT_DEPLOYMENT_NAME) |
| 71 | + .addAsManifestResource(new StringAsset(JsonSerialization.writeValueAsPrettyString(representation)), |
| 72 | + "keycloak-scripts.json") |
| 73 | + .addAsResource("scripts/mapper-example.js", "mapper-a.js"); |
| 74 | + } |
| 75 | + |
| 76 | + @BeforeClass |
| 77 | + public static void verifyEnvironment() { |
| 78 | + ContainerAssume.assumeNotAuthServerUndertow(); |
| 79 | + } |
| 80 | + |
| 81 | + @ArquillianResource |
| 82 | + private Deployer deployer; |
| 83 | + |
| 84 | + @Before |
| 85 | + public void deployScripts() throws Exception { |
| 86 | + deployer.deploy(SCRIPT_DEPLOYMENT_NAME); |
| 87 | + reconnectAdminClient(); |
| 88 | + } |
| 89 | + |
| 90 | + @Before |
| 91 | + public void cleanMappersAndScopes() { |
| 92 | + this.cau = ClientAttributeUpdater.forClient(adminClient, REALM_NAME, SAML_CLIENT_ID_EMPLOYEE_2) |
| 93 | + .setDefaultClientScopes(Collections.EMPTY_LIST) |
| 94 | + .update(); |
| 95 | + this.pmu = cau.protocolMappers() |
| 96 | + .clear() |
| 97 | + .update(); |
| 98 | + |
| 99 | + getCleanup(REALM_NAME) |
| 100 | + .addCleanup(this.cau) |
| 101 | + .addCleanup(this.pmu); |
| 102 | + } |
| 103 | + |
| 104 | + @After |
| 105 | + public void onAfter() throws Exception { |
| 106 | + deployer.undeploy(SCRIPT_DEPLOYMENT_NAME); |
| 107 | + reconnectAdminClient(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testScriptMapperNotAvailableThroughAdminRest() { |
| 112 | + assertFalse(adminClient.serverInfo().getInfo().getProtocolMapperTypes().get(SamlProtocol.LOGIN_PROTOCOL).stream() |
| 113 | + .anyMatch( |
| 114 | + mapper -> ScriptBasedMapper.PROVIDER_ID.equals(mapper.getId()))); |
| 115 | + |
| 116 | + // Doublecheck not possible to create mapper through admin REST |
| 117 | + ProtocolMapperRepresentation mapperRep = createSamlProtocolMapper(ScriptBasedMapper.PROVIDER_ID, |
| 118 | + ProviderConfigProperty.SCRIPT_TYPE, "'hello_' + user.username", |
| 119 | + AttributeStatementHelper.SAML_ATTRIBUTE_NAMEFORMAT, AttributeStatementHelper.BASIC, |
| 120 | + AttributeStatementHelper.SAML_ATTRIBUTE_NAME, "SCRIPT_ATTRIBUTE" |
| 121 | + ); |
| 122 | + |
| 123 | + Response response = pmu.getResource().createMapper(mapperRep); |
| 124 | + Assert.assertEquals(404, response.getStatus()); |
| 125 | + response.close(); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + @Test |
| 130 | + @EnableFeature(value = SCRIPTS, skipRestart = true, executeAsLast = false) |
| 131 | + public void testScriptMappingThroughServerDeploy() { |
| 132 | + // ScriptBasedMapper still not available even if SCRIPTS feature is enabled |
| 133 | + testScriptMapperNotAvailableThroughAdminRest(); |
| 134 | + |
| 135 | + pmu.add( |
| 136 | + createSamlProtocolMapper("script-mapper-a.js", |
| 137 | + AttributeStatementHelper.SAML_ATTRIBUTE_NAMEFORMAT, AttributeStatementHelper.BASIC, |
| 138 | + AttributeStatementHelper.SAML_ATTRIBUTE_NAME, "SCRIPT_ATTRIBUTE" |
| 139 | + ) |
| 140 | + ).update(); |
| 141 | + |
| 142 | + assertLoginSuccessWithAttributeAvailable(); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + private void assertLoginSuccessWithAttributeAvailable() { |
| 147 | + SAMLDocumentHolder samlResponse = new SamlClientBuilder() |
| 148 | + .authnRequest(getAuthServerSamlEndpoint(REALM_NAME), SAML_CLIENT_ID_EMPLOYEE_2, RoleMapperTest.SAML_ASSERTION_CONSUMER_URL_EMPLOYEE_2, SamlClient.Binding.POST) |
| 149 | + .build() |
| 150 | + .login().user(bburkeUser).build() |
| 151 | + .getSamlResponse(SamlClient.Binding.POST); |
| 152 | + |
| 153 | + assertThat(samlResponse.getSamlObject(), Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS)); |
| 154 | + |
| 155 | + Stream<AssertionType> assertions = assertionsUnencrypted(samlResponse.getSamlObject()); |
| 156 | + Stream<AttributeType> attributes = attributesUnecrypted(attributeStatements(assertions)); |
| 157 | + String scriptAttrValue = attributes |
| 158 | + .filter(attribute -> "SCRIPT_ATTRIBUTE".equals(attribute.getName())) |
| 159 | + .map(attribute -> attribute.getAttributeValue().get(0).toString()) |
| 160 | + .findFirst().orElseThrow(() -> new AssertionError("Attribute SCRIPT_ATTRIBUTE was not available in SAML assertion")); |
| 161 | + |
| 162 | + Assert.assertEquals("hello_bburke", scriptAttrValue); |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments