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.
Signed-off-by: Pedro Igor <[email protected]>
- Loading branch information
Showing
6 changed files
with
194 additions
and
0 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
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
107 changes: 107 additions & 0 deletions
107
services/src/main/java/org/keycloak/protocol/oidc/mappers/OrganizationProtocolMapper.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,107 @@ | ||
/* | ||
* 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.protocol.oidc.mappers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.keycloak.Config.Scope; | ||
import org.keycloak.common.Profile; | ||
import org.keycloak.common.Profile.Feature; | ||
import org.keycloak.models.ClientSessionContext; | ||
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.oidc.OIDCLoginProtocol; | ||
import org.keycloak.provider.EnvironmentDependentProviderFactory; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
import org.keycloak.representations.AccessTokenResponse; | ||
import org.keycloak.representations.IDToken; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Pedro Igor</a> | ||
*/ | ||
public class OrganizationProtocolMapper extends AbstractOIDCProtocolMapper implements OIDCAccessTokenMapper, OIDCIDTokenMapper, TokenIntrospectionTokenMapper, EnvironmentDependentProviderFactory { | ||
|
||
private static final String PROVIDER_ID = "organization"; | ||
|
||
public static ProtocolMapperModel create() { | ||
ProtocolMapperModel mapper = new ProtocolMapperModel(); | ||
mapper.setName("organization"); | ||
mapper.setProtocolMapper(OrganizationProtocolMapper.PROVIDER_ID); | ||
mapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); | ||
Map<String, String> config = new HashMap<>(); | ||
config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ACCESS_TOKEN, "true"); | ||
config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ID_TOKEN, "true"); | ||
config.put(OIDCAttributeMapperHelper.INCLUDE_IN_INTROSPECTION, "true"); | ||
mapper.setConfig(config); | ||
return mapper; | ||
} | ||
|
||
@Override | ||
protected void setClaim(IDToken token, ProtocolMapperModel mappingModel, UserSessionModel userSession, KeycloakSession session, ClientSessionContext clientSessionCtx) { | ||
OrganizationProvider provider = session.getProvider(OrganizationProvider.class); | ||
UserModel user = userSession.getUser(); | ||
OrganizationModel organization = provider.getOrganizationByMember(session.getContext().getRealm(), user); | ||
|
||
if (organization == null) { | ||
return; | ||
} | ||
|
||
Map<String, Map<String, Object>> claim = new HashMap<>(); | ||
claim.put(organization.getName(), Map.of()); | ||
token.getOtherClaims().put("organization", claim); | ||
} | ||
|
||
@Override | ||
public String getDisplayCategory() { | ||
return TOKEN_MAPPER_CATEGORY; | ||
} | ||
|
||
@Override | ||
public String getDisplayType() { | ||
return "Organization"; | ||
} | ||
|
||
@Override | ||
public String getHelpText() { | ||
return "Sets metadata into tokens about the access context of a subject within a organization"; | ||
} | ||
|
||
@Override | ||
public List<ProviderConfigProperty> getConfigProperties() { | ||
ArrayList<ProviderConfigProperty> properties = new ArrayList<>(); | ||
OIDCAttributeMapperHelper.addIncludeInTokensConfig(properties, OrganizationProtocolMapper.class); | ||
return properties; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return PROVIDER_ID; | ||
} | ||
|
||
@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
52 changes: 52 additions & 0 deletions
52
...st/java/org/keycloak/testsuite/organization/admin/OrganizationOIDCProtocolMapperTest.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,52 @@ | ||
/* | ||
* 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.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
import org.keycloak.TokenVerifier; | ||
import org.keycloak.admin.client.resource.OrganizationResource; | ||
import org.keycloak.common.Profile.Feature; | ||
import org.keycloak.representations.AccessToken; | ||
import org.keycloak.testsuite.arquillian.annotation.EnableFeature; | ||
import org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse; | ||
|
||
@EnableFeature(Feature.ORGANIZATION) | ||
public class OrganizationOIDCProtocolMapperTest extends AbstractOrganizationTest { | ||
|
||
@Test | ||
public void testUpdate() throws Exception { | ||
OrganizationResource organization = testRealm().organizations().get(createOrganization().getId()); | ||
addMember(organization); | ||
|
||
oauth.clientId("direct-grant"); | ||
oauth.scope("openid organization"); | ||
AccessTokenResponse response = oauth.doGrantAccessTokenRequest("password", "[email protected]", "password"); | ||
String scope = response.getScope(); | ||
assertTrue(scope.contains("organization")); | ||
|
||
AccessToken accessToken = TokenVerifier.create(response.getAccessToken(), AccessToken.class).getToken(); | ||
Map<String, Object> claim = (Map<String, Object>) accessToken.getOtherClaims().get("organization"); | ||
assertNotNull(claim); | ||
assertNotNull(claim.get("neworg")); | ||
} | ||
} |