Repository navigation
Conditional SPNEGO Authenticator #17064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Copyright 2023 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.authentication.authenticators.browser; | ||
|
|
||
| import org.jboss.logging.Logger; | ||
| import org.keycloak.authentication.AuthenticationFlowContext; | ||
| import org.keycloak.sessions.AuthenticationSessionModel; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * A {@link SpnegoAuthenticator} that can conditionally skip SPNEGO authentication. | ||
| * <p> | ||
| * This Keycloak authenticator allows SPNEGO authentication to be skipped by explicit client request and optionally by | ||
| * request IP. Clients can request SPNEGO authentication be skipped via the URL prompt=login present in the client | ||
| * request. A Whitelist Regex Pattern can optionally be configured, and if configured, it is compared against the | ||
| * <i>X-Forwarded-For</i> header on auth requests to check for a match. If no match then no SPNEGO is skipped. | ||
| * </p> | ||
| * <p> | ||
| * It is often desirable to treat Intranet (internal) users with the convenience of Kerberos Single Sign On, while | ||
| * preventing Internet (external) users from possibly seeing a confusing NTLM prompt dialog. It can also be useful to | ||
| * allow users to explicitly opt out of SPNEGO in order to provide a switch user functionality. | ||
| * </p> | ||
| * @author <a href="mailto:Ryan.Slominski@gmail.com">Ryan Slominski</a> | ||
| */ | ||
| public class ConditionalSpnegoAuthenticator extends SpnegoAuthenticator { | ||
|
|
||
| private static Logger logger = Logger.getLogger(ConditionalSpnegoAuthenticator.class); | ||
|
|
||
| public static final String WHITELIST_PATTERN = "XForwardedForWhitelistPattern"; | ||
|
|
||
| @Override | ||
| public void authenticate(AuthenticationFlowContext context) { | ||
| AuthenticationSessionModel session = context.getAuthenticationSession(); | ||
| Map<String, String> clientNotes = session.getClientNotes(); | ||
| if ("login".equals(clientNotes.get("prompt"))) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Few points to this:
Will make it possible to use this with context of SAML as well. It is possible that this will need to add method Also will likely require some checks for null userSession in the protocol implementations.
This will allow that condition can be evaluated for:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It sounds like we may need two new separate
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see, yes supporting both SAML and OIDC would be best. So instead of PromptLoginCondition perhaps we call it ReauthenticationCondition.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, 2 separate conditions can be better option. +1 for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like someone else is already working on XForwardedForCondition: #16453
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This sounds like something the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
+1, it does not right now, but it is generally useful for every condition though. |
||
| logger.debug("Skip SPNEGO because of prompt=login"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Will be nice to remove SPNEGO fro 8 m the de 10BC8 bug message (if we go with dedicated condition as I've proposed). Similar to other debug messages. |
||
| context.attempted(); | ||
| return; | ||
| } | ||
|
|
||
| Map<String, String> config = context.getAuthenticatorConfig().getConfig(); | ||
| String patternStr = config.get(WHITELIST_PATTERN); | ||
| String xForwardedFor = context.getHttpRequest().getHttpHeaders().getRequestHeaders().getFirst("X-Forwarded-For"); | ||
|
|
||
| if(!patternStr.isEmpty()) { | ||
| logger.debug("Matcher pattern: " + patternStr + " ,xForwardedFor: " + xForwardedFor); | ||
|
|
||
| if(xForwardedFor != null && !xForwardedFor.isEmpty()) { | ||
| if (!inWhitelist(patternStr, xForwardedFor)) { | ||
| logger.debug("Skip SPNEGO because X-Forwarded-For does not match configured pattern"); | ||
| context.attempted(); | ||
| return; | ||
| } | ||
| } else { | ||
| logger.debug("Skip SPNEGO because whitelist pattern defined, but no X-Forwarded-For set"); | ||
|
|
||
| context.attempted(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| super.authenticate(context); | ||
| } | ||
|
|
||
| public boolean inWhitelist(String patternStr, String xForwardedFor) { | ||
| Pattern pattern = Pattern.compile(patternStr); | ||
|
|
||
| return pattern.matcher(xForwardedFor).matches(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * Copyright 2023 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.authentication.authenticators.browser; | ||
|
|
||
| import org.keycloak.Config; | ||
| import org.keycloak.authentication.Authenticator; | ||
| import org.keycloak.authentication.AuthenticatorFactory; | ||
| import org.keycloak.models.AuthenticationExecutionModel; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.KeycloakSessionFactory; | ||
| import org.keycloak.models.UserCredentialModel; | ||
| import org.keycloak.provider.ProviderConfigProperty; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static org.keycloak.authentication.authenticators.browser.ConditionalSpnegoAuthenticator.WHITELIST_PATTERN; | ||
| import static org.keycloak.provider.ProviderConfigProperty.STRING_TYPE; | ||
|
|
||
| /** | ||
| * An {@link AuthenticatorFactory} for {@link ConditionalSpnegoAuthenticator}s. | ||
| * | ||
| * @author <a href="mailto:Ryan.Slominski@gmail.com">Ryan Slominski</a> | ||
| */ | ||
| public class ConditionalSpnegoAuthenticatorFactory implements AuthenticatorFactory { | ||
| public static final String PROVIDER_ID = "conditional-auth-spnego"; | ||
| static ConditionalSpnegoAuthenticator SINGLETON = new ConditionalSpnegoAuthenticator(); | ||
|
|
||
| @Override | ||
| public Authenticator create(KeycloakSession session) { | ||
| return SINGLETON; | ||
| } | ||
|
|
||
| @Override | ||
| public void init(Config.Scope config) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void postInit(KeycloakSessionFactory factory) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return PROVIDER_ID; | ||
| } | ||
|
|
||
| @Override | ||
| public String getReferenceCategory() { | ||
| return UserCredentialModel.KERBEROS; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isConfigurable() { | ||
| return true; | ||
| } | ||
|
|
||
| public static final AuthenticationExecutionModel.Requirement[] REQUIREMENT_CHOICES = { | ||
| AuthenticationExecutionModel.Requirement.ALTERNATIVE, | ||
| AuthenticationExecutionModel.Requirement.REQUIRED, | ||
| AuthenticationExecutionModel.Requirement.DISABLED}; | ||
|
|
||
| @Override | ||
| public AuthenticationExecutionModel.Requirement[] getRequirementChoices() { | ||
| return REQUIREMENT_CHOICES; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayType() { | ||
| return "Conditional SPNEGO"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getHelpText() { | ||
| return "Conditionally attempt SPNEGO based on existence of prompt=login and optionally a configured whitelist regex pattern to match against the first X-Forwarded-For"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<ProviderConfigProperty> getConfigProperties() { | ||
| ProviderConfigProperty whitelistPattern = new ProviderConfigProperty(); | ||
| whitelistPattern.setType(STRING_TYPE); | ||
| whitelistPattern.setName(WHITELIST_PATTERN); | ||
| whitelistPattern.setLabel("Whitelist Regex Pattern"); | ||
| whitelistPattern.setHelpText("Whitelist Regex pattern to match against X-Forwarded-For HTTP header. If pattern is empty SPNEGO is allowed, else only hosts matching the pattern have SPNEGO attempted."); | ||
| whitelistPattern.setDefaultValue(""); | ||
|
|
||
| return Arrays.asList(whitelistPattern); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isUserSetupAllowed() { | ||
| return false; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be better to implement this as separate condition instead of extension of
SpnegoAuthenticator.I know we have already
ConditionalFormAuthenticatorFactory, but that was introduced before we have conditions support for authentication flows. For the sake of better composing and ability to re-use the similar condition in other places too, the dedicated implementation ofConditionalAuthenticatoris much better option.I am not sure if it is easily possible to test with
X-Forwardedheader, but I think that we can at least test with protocol re-authentication (EG. check that withprompt=loginthe SPNEGO is not triggered, but withoutprompt=loginit is triggered).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll look into
ConditionalAuthenticator. Just looking at the docs it's a little confusing as it says:I think this might be trying to say:
Otherwise the case of some executions evaluate to true while others evaluate to false is left unexplained.
Also, a SPNEGO execution is usually configured as an
ALTERNATIVE, meaning if it fails no problem just continue on to the next authenticator.REQUIRED is defined in the docs as:
Does that mean that if we use a
ConditionalAuthenticatorit is not possible to conditonally attempt SPNEGO, and if attempted, but fails, continue on to the next authenticator? If possible, then maybe I'm confusing the definition of a REQUIRED subflow vs a REQUIRED execution?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right. I think the docs should be updated as you suggested (regarding if any ...)
Regarding conditional subflow: I think that what you want to achieve probably can be achieved with something like replacing default Kerberos authenticator with something like:
Note that I can be wrong as I did not test it by myself. This configuration is a bit cumbersome and we plan to improve authentication flows by adding "Authentication policies" in the future, which will allow to configure them in the more intuitive way.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that's a mess. It looks like the requirement type of
Conditionalunnecessarily complicates and de-normalizes. TheConditionalrequirement should probably be deprecated and instead simply adding an execution type of Conditional subflow is enough to indicate you mean conditional subflow. Having a separate requirement type is redundant and actually prevents you from indicating that the subflow is ALTERNATIVE. Intuitively the only difference between a regular subflow and a conditional subflow would be that the regular one has ordered executions and a conditional subflow has ordered conditions and then one execution at the end that is only executed if the gauntlet of conditions are all passed. It's actually not immediately clear how this is supposed to work. It looks like this was defined in #7897There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it looks like the way conditional subflows work is if their condition fails then they can short-circuit the conditional subflow as if the entire subflow execution is DISABLED. If their condition passes then execution is allowed to continue and any executions in the conditional flow behave as if they're in a regular flow and not in a subflow at all. If that's true then the setup would look like:
I don't think conditions have "requirements" - they just simply resolve to pass or fail. I haven't tested any of this either and it's worrying that it's not immediately clear how this is supposed to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea of a set of generic reusable conditions is good, but an ordered list of conditions should probably be an attribute of an execution, not a special subflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the confusion with the requirement on the conditions...
I think that for your configuration, the
conditional-subflowwill becomeREQUIREDin case that conditions evaluate to true. Hence it likely won't work correctly if you want it to use at same level with other things and achieve something like: cookie OR conditional-kerberos OR identity-provider-redirector OR forms-subflowHence why I think another "parent" alternative subflow might be needed to wrap the conditional-subflow... You can try and see.
I definitely agree that this stuff may require some more love to become more usable and intuitive to configure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a discussion about this here. I'm not really sure how to proceed. I'm leaning towards sticking with the current approach of extending SpnegoAuthenticator for now until the new way is improved. It seems like the IP based Condition someone else is already working on may solve this partially.