Skip to content

Commit 71dc2d1

Browse files
committed
improved role handling
1 parent 4aaf8b5 commit 71dc2d1

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/main/java/com/instipod/keycloakauthenticators/ConditionalRoleEnhancedAuthenticator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public boolean matchCondition(AuthenticationFlowContext context) {
2828
boolean check;
2929
if (not) {
3030
//does not have role
31-
check = !(AuthenticatorUtils.hasRole(context.getUser(), role));
31+
check = !(AuthenticatorUtils.hasRole(context, role));
3232
} else {
3333
//has role
34-
check = (AuthenticatorUtils.hasRole(context.getUser(), role));
34+
check = (AuthenticatorUtils.hasRole(context, role));
3535
}
3636

3737
if (AuthenticatorUtils.debuggingBuild)

src/main/java/com/instipod/keycloakauthenticators/ConditionalRoleEnhancedAuthenticatorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ConditionalRoleEnhancedAuthenticatorFactory implements org.keycloak
2020

2121
static {
2222
commonConfig = Collections.unmodifiableList(ProviderConfigurationBuilder.create()
23-
.property().name(CONDITIONAL_ROLE).label("Role").helpText("Role to check for (supports variables)").type(ProviderConfigProperty.STRING_TYPE).add()
23+
.property().name(CONDITIONAL_ROLE).label("Role Id").helpText("Role Id to check for (supports variables)").type(ProviderConfigProperty.STRING_TYPE).add()
2424
.property().name(CONDITIONAL_NOT).label("Not").helpText("If we should match on NOT having this role").type(ProviderConfigProperty.BOOLEAN_TYPE).add()
2525
.build()
2626
);

src/main/java/com/instipod/keycloakauthenticators/utils/AuthenticatorUtils.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jboss.logging.Logger;
55
import org.keycloak.authentication.AuthenticationFlowContext;
66
import org.keycloak.models.AuthenticatorConfigModel;
7+
import org.keycloak.models.GroupModel;
78
import org.keycloak.models.RoleModel;
89
import org.keycloak.models.UserModel;
910

@@ -20,6 +21,9 @@ public static String variableReplace(AuthenticationFlowContext context, String m
2021
user = context.getUser();
2122
} catch (Exception ex) { }
2223

24+
try {
25+
message = message.replace("%userid%", user.getId());
26+
} catch (Exception ex) { }
2327
try {
2428
message = message.replace("%username%", user.getUsername());
2529
} catch (Exception ex) { }
@@ -35,6 +39,9 @@ public static String variableReplace(AuthenticationFlowContext context, String m
3539
try {
3640
message = message.replace("%ipaddress%", context.getConnection().getRemoteAddr());
3741
} catch (Exception ex) { }
42+
try {
43+
message = message.replace("%clientid%", context.getAuthenticationSession().getClient().getId());
44+
} catch (Exception ex) { }
3845
try {
3946
message = message.replace("%clientname%", context.getAuthenticationSession().getClient().getName());
4047
} catch (Exception ex) { }
@@ -48,20 +55,24 @@ public static String variableReplace(AuthenticationFlowContext context, String m
4855
return message;
4956
}
5057

51-
public static boolean hasRole(UserModel user, String roleName) {
52-
Set<RoleModel> roles = user.getRoleMappings();
58+
public static boolean hasRole(AuthenticationFlowContext context, String roleId) {
59+
RoleModel role = context.getRealm().getRoleById(roleId);
5360

54-
for (RoleModel role : roles) {
55-
if (role.getName().equalsIgnoreCase(roleName)) {
56-
return true;
57-
}
61+
if (role == null) {
62+
Logger.getLogger(AuthenticatorUtils.class).warn("Could not find role by id " + roleId);
63+
return false;
5864
}
5965

60-
return false;
66+
if (context.getUser() == null) {
67+
return false;
68+
}
69+
70+
return (context.getUser().hasRole(role));
6171
}
6272

6373
public static boolean getConfigBoolean(AuthenticationFlowContext context, String configName) {
6474
AuthenticatorConfigModel authConfig = context.getAuthenticatorConfig();
75+
6576
if (authConfig!=null && authConfig.getConfig()!=null) {
6677
String booleanValue = authConfig.getConfig().get(configName);
6778

0 commit comments

Comments
 (0)