forked from jacekkow/keycloak-protocol-cas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
2 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
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
11 changes: 11 additions & 0 deletions
11
src/main/java/org/keycloak/protocol/cas/mappers/CASUsernameMapper.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,11 @@ | ||
package org.keycloak.protocol.cas.mappers; | ||
|
||
import org.keycloak.models.*; | ||
import org.keycloak.protocol.ProtocolMapper; | ||
|
||
public interface CASUsernameMapper extends ProtocolMapper { | ||
|
||
String getMappedUsername(ProtocolMapperModel mappingModel, KeycloakSession session, | ||
UserSessionModel userSession, AuthenticatedClientSessionModel clientSession); | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/org/keycloak/protocol/cas/mappers/UserAttributeCasUsernameMapper.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,77 @@ | ||
package org.keycloak.protocol.cas.mappers; | ||
|
||
import org.keycloak.Config; | ||
import org.keycloak.models.*; | ||
import org.keycloak.protocol.ProtocolMapper; | ||
import org.keycloak.protocol.ProtocolMapperUtils; | ||
import org.keycloak.protocol.cas.CASLoginProtocol; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class UserAttributeCasUsernameMapper extends AbstractCASProtocolMapper implements CASUsernameMapper { | ||
public static final String PROVIDER_ID = "cas-usermodel-username-mapper"; | ||
public static final String USERNAME_MAPPER_CATEGORY = "CAS Username Mapper"; | ||
private static final String CONF_FALLBACK_TO_USERNAME_IF_NULL = "username_fallback"; | ||
|
||
private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>(); | ||
static { | ||
ProviderConfigProperty property; | ||
property = new ProviderConfigProperty(); | ||
property.setName(ProtocolMapperUtils.USER_ATTRIBUTE); | ||
property.setLabel(ProtocolMapperUtils.USER_MODEL_PROPERTY_LABEL); | ||
property.setType(ProviderConfigProperty.STRING_TYPE); | ||
property.setHelpText(ProtocolMapperUtils.USER_MODEL_PROPERTY_HELP_TEXT); | ||
configProperties.add(property); | ||
|
||
property = new ProviderConfigProperty(); | ||
property.setName(CONF_FALLBACK_TO_USERNAME_IF_NULL); | ||
property.setLabel("Use username if attribute is missing"); | ||
property.setHelpText("Should the User's username be used if the specified attribute is blank?"); | ||
property.setType(ProviderConfigProperty.BOOLEAN_TYPE); | ||
property.setDefaultValue(false); | ||
configProperties.add(property); | ||
|
||
|
||
} | ||
|
||
@Override | ||
public final String getDisplayCategory() { | ||
return USERNAME_MAPPER_CATEGORY; | ||
} | ||
|
||
@Override | ||
public final String getId() { | ||
return PROVIDER_ID; | ||
} | ||
|
||
@Override | ||
public String getDisplayType() { | ||
return "User Attribute Mapper For CAS Username"; | ||
} | ||
|
||
@Override | ||
public String getHelpText() { | ||
return "Maps a user attribute to CAS Username value."; | ||
} | ||
|
||
@Override | ||
public List<ProviderConfigProperty> getConfigProperties() { | ||
return configProperties; | ||
} | ||
|
||
@Override | ||
public String getMappedUsername(ProtocolMapperModel mappingModel, KeycloakSession session, | ||
UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) { | ||
|
||
boolean defaultIfNull = Boolean.parseBoolean(mappingModel.getConfig().get(CONF_FALLBACK_TO_USERNAME_IF_NULL)); | ||
UserModel user = userSession.getUser(); | ||
String mappedUsername = user.getFirstAttribute(mappingModel.getConfig().get(ProtocolMapperUtils.USER_ATTRIBUTE)); | ||
|
||
if(mappedUsername == null && defaultIfNull) { | ||
mappedUsername = user.getUsername(); | ||
} | ||
return mappedUsername; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/keycloak/protocol/cas/utils/UsernameMapperHelper.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,32 @@ | ||
package org.keycloak.protocol.cas.utils; | ||
|
||
import org.keycloak.models.*; | ||
import org.keycloak.protocol.ProtocolMapper; | ||
import org.keycloak.protocol.ProtocolMapperUtils; | ||
import org.keycloak.protocol.cas.mappers.CASUsernameMapper; | ||
import org.keycloak.services.util.DefaultClientSessionContext; | ||
|
||
import java.util.Map; | ||
|
||
public class UsernameMapperHelper { | ||
public static String getMappedUsername(KeycloakSession session, AuthenticatedClientSessionModel clientSession) { | ||
// CAS protocol does not support scopes, so pass null scopeParam | ||
ClientSessionContext clientSessionCtx = DefaultClientSessionContext.fromClientSessionAndScopeParameter(clientSession, null, session); | ||
UserSessionModel userSession = clientSession.getUserSession(); | ||
|
||
|
||
Map.Entry<ProtocolMapperModel, ProtocolMapper> mapperPair = ProtocolMapperUtils.getSortedProtocolMappers(session,clientSessionCtx) | ||
.filter(e -> e.getValue() instanceof CASUsernameMapper) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
String mappedUsername = userSession.getUser().getUsername(); | ||
|
||
if(mapperPair != null) { | ||
ProtocolMapperModel mapping = mapperPair.getKey(); | ||
CASUsernameMapper casUsernameMapper = (CASUsernameMapper) mapperPair.getValue(); | ||
mappedUsername = casUsernameMapper.getMappedUsername(mapping, session, userSession, clientSession); | ||
} | ||
return mappedUsername; | ||
} | ||
} |
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