8000 Conditional SPNEGO Authenticator by slominskir · Pull Request #17064 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
@mposolda mposolda Feb 15, 2023 •
Copy link
Copy Markdown
Contributor

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 of ConditionalAuthenticator is much better option.

I am not sure if it is easily possible to test with X-Forwarded header, but I think that we can at least test with protocol re-authentication (EG. check that with prompt=login the SPNEGO is not triggered, but without prompt=login it is triggered).

Copy link
Copy Markdown
Contributor Author

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:

  • If all executions evaluate as true, the Conditional sub-flow acts as Required.
  • If all executions evaluate as false, the Conditional sub-flow acts as Disabled.

I think this might be trying to say:

  • If all executions evaluate as true, the Conditional sub-flow acts as Required.
  • If any execution evaluates as false, the Conditional sub-flow acts as Disabled.

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:

All Required elements in the flow must be successfully sequentially executed. The flow terminates if a required element fails.

Does that mean that if we use a ConditionalAuthenticator it 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?

Copy link
Copy Markdown
Contributor

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:

- kerberos-subflow1 ALTERNATIVE
-- nested-conditional-subflow CONDITIONAL
--- condition1-reauthentication REQUIRED
--- condition2-xforwarded-headers REQUIRED
--- Kerberos ALTERNATIVE

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.

@slominskir slominskir Feb 15, 2023 •
Copy link
Copy Markdown
Contributor Author

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 Conditional unnecessarily complicates and de-normalizes. The Conditional requirement 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 #7897

Copy link
Copy Markdown
Contributor Author

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:

- conditional-subflow CONDITIONAL
-- condition1-reauthentication
-- condition2-xforwarded-headers
-- Kerberos ALTERNATIVE

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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-subflow will become REQUIRED in 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-subflow

Hence 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.

Copy link
Copy Markdown
Contributor Author

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.


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"))) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few points to this:

  1. Is it possible to replace this with something like:
LoginProtocol protocol = context.getSession().getProvider(LoginProtocol.class, authSession.getProtocol());
if (protocol.requireReauthentication(context.getUserSession(), context.getAuthenticationSession());

Will make it possible to use this with context of SAML as well. It is possible that this will need to add method getUserSession() to AuthenticationFlowContext (UserSession might have been attached before by CookieAuthenticator, which is usually before SPNEGO).

Also will likely require some checks for null userSession in the protocol implementations.

  1. Is it possible to add option to the condition like Ignore when protocol requires re-authentication with the description similar to When this option is enabled, then condition is evaluated to false when protocol requires re-authentication. For example when parameter prompt=login for OIDC protocol is used or forceauthn for SAML protocol .

This will allow that condition can be evaluated for:

  • only protocol authentication
  • only x-forwarded header
  • both

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like we may need two new separate ConditionalAuthenticators:

  1. XForwardedForPatternMatcherCondition
  2. PromptLoginCondition

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, 2 separate conditions can be better option. +1 for ReauthenticationCondition (will be good to add option to it which allows to evaluate it to true in case that protocol re-authentication is used but also for the opposite case when protocol re-authentication is not used. As I think that for various cases, both conditions can be useful).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like someone else is already working on XForwardedForCondition: #16453

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but also for the opposite case when protocol re-authentication is not used

This sounds like something the ConditionalAuthenticator framework should support generically - i.e. a config option to apply not (!) on the boolean result that a condition provides.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like something the ConditionalAuthenticator framework should support generically - i.e. a config option to apply not (!) on the boolean result that a condition provides.

+1, it does not right now, but it is generally useful for every condition though.

logger.debug("Skip SPNEGO because of prompt=login");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Will be nice to remove SPNEGO from the debug 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);
10BC8
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 10BC8 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.
#

org.keycloak.authentication.authenticators.browser.ConditionalSpnegoAuthenticatorFactory
org.keycloak.authentication.authenticators.browser.CookieAuthenticatorFactory
org.keycloak.authentication.authenticators.browser.UsernamePasswordFormFactory
org.keycloak.authentication.authenticators.browser.UsernameFormFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private List<Map<String, Object>> expectedAuthProviders() {
? "Initiates the SPNEGO protocol. Most often used with Kerberos."
: "DISABLED. Please enable Kerberos feature and make sure Kerberos available in your platform. Initiates the SPNEGO protocol. Most often used with Kerberos.";
addProviderInfo(result, "auth-spnego", "Kerberos", kerberosHelpMessage);
addProviderInfo(result, "conditional-auth-spnego", "Conditional SPNEGO", "Conditionally attempt SPNEGO based on existence of prompt=login and optionally a configured whitelist regex pattern to match against the first X-Forwarded-For");
addProviderInfo(result, "auth-username-password-form", "Username Password Form",
"Validates a username and password from login form.");
addProviderInfo(result, "auth-x509-client-username-form", "X509/Validate Username Form",
Expand Down
0