forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding SAML protocol mapper to map organization membership
Closes keycloak#28732 Signed-off-by: Pedro Igor <[email protected]>
- Loading branch information
Showing
6 changed files
with
233 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...in/java/org/keycloak/organization/protocol/mappers/saml/OrganizationMembershipMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package org.keycloak.organization.protocol.mappers.saml; | ||
|
||
import java.util.List; | ||
|
||
import org.keycloak.Config.Scope; | ||
import org.keycloak.common.Profile; | ||
import org.keycloak.common.Profile.Feature; | ||
import org.keycloak.dom.saml.v2.assertion.AttributeStatementType; | ||
import org.keycloak.dom.saml.v2.assertion.AttributeType; | ||
import org.keycloak.models.AuthenticatedClientSessionModel; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.OrganizationModel; | ||
import org.keycloak.models.ProtocolMapperModel; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.models.UserSessionModel; | ||
import org.keycloak.organization.OrganizationProvider; | ||
import org.keycloak.protocol.saml.SamlProtocol; | ||
import org.keycloak.protocol.saml.mappers.AbstractSAMLProtocolMapper; | ||
import org.keycloak.protocol.saml.mappers.AttributeStatementHelper; | ||
import org.keycloak.protocol.saml.mappers.SAMLAttributeStatementMapper; | ||
import org.keycloak.provider.EnvironmentDependentProviderFactory; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
import org.keycloak.saml.common.constants.JBossSAMLURIConstants; | ||
|
||
public class OrganizationMembershipMapper extends AbstractSAMLProtocolMapper implements SAMLAttributeStatementMapper, EnvironmentDependentProviderFactory { | ||
|
||
public static final String ID = "saml-organization-membership-mapper"; | ||
public static final String ORGANIZATION_ATTRIBUTE_NAME = "organization"; | ||
|
||
public static ProtocolMapperModel create() { | ||
ProtocolMapperModel mapper = new ProtocolMapperModel(); | ||
|
||
mapper.setName("organization"); | ||
mapper.setProtocolMapper(ID); | ||
mapper.setProtocol(SamlProtocol.LOGIN_PROTOCOL); | ||
|
||
return mapper; | ||
|
||
} | ||
|
||
@Override | ||
public void transformAttributeStatement(AttributeStatementType attributeStatement, ProtocolMapperModel mappingModel, KeycloakSession session, UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) { | ||
OrganizationProvider provider = session.getProvider(OrganizationProvider.class); | ||
|
||
if (!provider.isEnabled()) { | ||
return; | ||
} | ||
|
||
UserModel user = userSession.getUser(); | ||
OrganizationModel organization = provider.getByMember(user); | ||
|
||
if (organization == null || !organization.isEnabled()) { | ||
return; | ||
} | ||
|
||
AttributeType attribute = new AttributeType(ORGANIZATION_ATTRIBUTE_NAME); | ||
attribute.setFriendlyName(ORGANIZATION_ATTRIBUTE_NAME); | ||
attribute.setNameFormat(JBossSAMLURIConstants.ATTRIBUTE_FORMAT_BASIC.get()); | ||
attribute.addAttributeValue(organization.getName()); | ||
attributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(attribute)); | ||
} | ||
|
||
@Override | ||
public List<ProviderConfigProperty> getConfigProperties() { | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return ID; | ||
} | ||
|
||
@Override | ||
public String getDisplayType() { | ||
return "Organization Membership"; | ||
} | ||
|
||
@Override | ||
public String getDisplayCategory() { | ||
return AttributeStatementHelper.ATTRIBUTE_STATEMENT_CATEGORY; | ||
} | ||
|
||
@Override | ||
public String getHelpText() { | ||
return "Add an attribute to the assertion with information about the organization membership."; | ||
} | ||
|
||
@Override | ||
public boolean isSupported(Scope config) { | ||
return Profile.isFeatureEnabled(Feature.ORGANIZATION); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...st/java/org/keycloak/testsuite/organization/admin/OrganizationSAMLProtocolMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package org.keycloak.testsuite.organization.admin; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.keycloak.testsuite.util.SamlStreams.assertionsUnencrypted; | ||
import static org.keycloak.testsuite.util.SamlStreams.attributeStatements; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import jakarta.ws.rs.core.UriBuilder; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.keycloak.admin.client.resource.OrganizationResource; | ||
import org.keycloak.common.Profile.Feature; | ||
import org.keycloak.dom.saml.v2.assertion.AttributeStatementType; | ||
import org.keycloak.dom.saml.v2.assertion.AttributeStatementType.ASTChoiceType; | ||
import org.keycloak.dom.saml.v2.assertion.AttributeType; | ||
import org.keycloak.protocol.saml.SamlConfigAttributes; | ||
import org.keycloak.protocol.saml.SamlProtocol; | ||
import org.keycloak.organization.protocol.mappers.saml.OrganizationMembershipMapper; | ||
import org.keycloak.saml.common.constants.JBossSAMLURIConstants; | ||
import org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder; | ||
import org.keycloak.services.resources.RealmsResource; | ||
import org.keycloak.testsuite.arquillian.annotation.EnableFeature; | ||
import org.keycloak.testsuite.saml.RoleMapperTest; | ||
import org.keycloak.testsuite.util.ClientBuilder; | ||
import org.keycloak.testsuite.util.Matchers; | ||
import org.keycloak.testsuite.util.SamlClient; | ||
import org.keycloak.testsuite.util.SamlClientBuilder; | ||
|
||
@EnableFeature(Feature.ORGANIZATION) | ||
public class OrganizationSAMLProtocolMapperTest extends AbstractOrganizationTest { | ||
|
||
@Test | ||
public void testAttribute() { | ||
OrganizationResource organization = testRealm().organizations().get(createOrganization().getId()); | ||
addMember(organization); | ||
String clientId = "saml-client"; | ||
testRealm().clients().create(ClientBuilder.create() | ||
.protocol(SamlProtocol.LOGIN_PROTOCOL) | ||
.clientId(clientId) | ||
.redirectUris("*") | ||
.attribute(SamlConfigAttributes.SAML_CLIENT_SIGNATURE_ATTRIBUTE, Boolean.FALSE.toString()) | ||
.build()).close(); | ||
|
||
SAMLDocumentHolder samlResponse = new SamlClientBuilder() | ||
.authnRequest(RealmsResource | ||
.protocolUrl(UriBuilder.fromUri(getAuthServerRoot())) | ||
.build(TEST_REALM_NAME, SamlProtocol.LOGIN_PROTOCOL), clientId, RoleMapperTest.SAML_ASSERTION_CONSUMER_URL_EMPLOYEE_2, SamlClient.Binding.POST) | ||
.build() | ||
.login().user(memberEmail, memberPassword).build() | ||
.login().user(memberEmail, memberPassword).build() | ||
.getSamlResponse(SamlClient.Binding.POST); | ||
|
||
assertThat(samlResponse.getSamlObject(), Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS)); | ||
AttributeType orgAttribute = attributeStatements(assertionsUnencrypted(samlResponse.getSamlObject())) | ||
.flatMap((Function<AttributeStatementType, Stream<ASTChoiceType>>) attributeStatementType -> attributeStatementType.getAttributes().stream()) | ||
.map(ASTChoiceType::getAttribute) | ||
.filter(attribute -> OrganizationMembershipMapper.ORGANIZATION_ATTRIBUTE_NAME.equals(attribute.getName())) | ||
.findAny() | ||
.orElse(null); | ||
Assert.assertNotNull(orgAttribute); | ||
List<Object> values = orgAttribute.getAttributeValue(); | ||
Assert.assertEquals(1, values.size()); | ||
Assert.assertEquals(organizationName, values.get(0)); | ||
} | ||
} |