Created
April 3, 2018 16:24
-
-
Save hardselius/9a29d1e5f4945c980a6e678b607eb699 to your computer and use it in GitHub Desktop.
Keycloak Required Action ordering
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
public abstract class AbstractPrioritizedRequiredAction implements RequiredActionProvider, RequiredActionFactory { | |
@Override | |
public void requiredActionChallenge(RequiredActionContext context) { | |
if (!ActionPriorityUtils.isNext(getId(), context.getUser().getRequiredActions(), context.getAuthenticationSession().getRequiredActions())) { | |
context.ignore(); | |
return; | |
} | |
requiredActionChallengeImpl(context); | |
} | |
public abstract void requiredActionChallengeImpl(RequiredActionContext context); | |
} |
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
public class ActionPriorityUtils { | |
private static final ImmutableMap<String, Integer> priorities = ImmutableMap.<String, Integer>builder() | |
.put(CustomAction1.PROVIDER_ID, 0) | |
.put(CustomAction2.PROVIDER_ID, 10) | |
.put(CustomAction3.PROVIDER_ID, 20) | |
.put(CustomAction4.PROVIDER_ID, 30) | |
.build(); | |
public static boolean isNext(String currentAction, Set<String> userRequiredActions, Set<String> clientRequiredActions) { | |
Integer priority = getPriority(currentAction); | |
if (userRequiredActions.contains(currentAction)) { | |
return priority <= getNextPriority(userRequiredActions); | |
} | |
return priority <= getNextPriority(clientRequiredActions); | |
} | |
private static Integer getNextPriority(Set<String> actions) { | |
return actions.stream() | |
.map(ActionPriorityUtils::getPriority) | |
.sorted() | |
.findFirst() | |
.orElse(Integer.MAX_VALUE); | |
} | |
private static Integer getPriority(String action) { | |
Integer priority = priorities.get(action); | |
return priority != null ? priority : -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment