Skip to content

Commit

Permalink
Fix issue 3 described in jacekkow#49 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
XSmeets committed Apr 4, 2024
1 parent 65af2d1 commit 11b7527
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.ArrayList;
import java.util.List;

public class UserAttributeCasUsernameMapper extends AbstractCASProtocolMapper implements CASUsernameMapper {
public class CASUserMapper implements ProtocolMapper {
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";
Expand All @@ -36,6 +36,28 @@ public class UserAttributeCasUsernameMapper extends AbstractCASProtocolMapper im

}

@Override
public String getProtocol() {
return CASLoginProtocol.LOGIN_PROTOCOL;
}

@Override
public void close() {
}

@Override
public final ProtocolMapper create(KeycloakSession session) {
throw new RuntimeException("UNSUPPORTED METHOD");
}

@Override
public void init(Config.Scope config) {
}

@Override
public void postInit(KeycloakSessionFactory factory) {
}

@Override
public final String getDisplayCategory() {
return USERNAME_MAPPER_CATEGORY;
Expand All @@ -61,7 +83,6 @@ public List<ProviderConfigProperty> getConfigProperties() {
return configProperties;
}

@Override
public String getMappedUsername(ProtocolMapperModel mappingModel, KeycloakSession session,
UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.keycloak.models.*;
import org.keycloak.protocol.ProtocolMapper;
import org.keycloak.protocol.ProtocolMapperUtils;
import org.keycloak.protocol.cas.mappers.CASUsernameMapper;
import org.keycloak.protocol.cas.mappers.CASUserMapper;
import org.keycloak.services.util.DefaultClientSessionContext;

import java.util.Map;
Expand All @@ -16,15 +16,15 @@ public static String getMappedUsername(KeycloakSession session, AuthenticatedCli


Map.Entry<ProtocolMapperModel, ProtocolMapper> mapperPair = ProtocolMapperUtils.getSortedProtocolMappers(session,clientSessionCtx)
.filter(e -> e.getValue() instanceof CASUsernameMapper)
.filter(e -> e.getValue() instanceof CASUserMapper)
.findFirst()
.orElse(null);

String mappedUsername = userSession.getUser().getUsername();

if(mapperPair != null) {
ProtocolMapperModel mapping = mapperPair.getKey();
CASUsernameMapper casUsernameMapper = (CASUsernameMapper) mapperPair.getValue();
CASUserMapper casUsernameMapper = (CASUserMapper) mapperPair.getValue();
mappedUsername = casUsernameMapper.getMappedUsername(mapping, session, userSession, clientSession);
}
return mappedUsername;
Expand Down

0 comments on commit 11b7527

Please sign in to comment.