Skip to content

Commit

Permalink
Improve auth framework with authenticator type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Aug 18, 2024
1 parent 64f041d commit d9f9c0b
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException;
import org.wso2.carbon.identity.application.authentication.framework.exception.LogoutFailedException;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatorData;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.common.model.Property;

import java.io.Serializable;
Expand Down Expand Up @@ -171,4 +172,13 @@ default String getI18nKey() {
return StringUtils.EMPTY;
}

/**
* Get the authenticator type (LOCAL, FEDERATED or CUSTOM)
*
* @return Authenticator Type.
*/
default FrameworkConstants.AuthenticatorType getAuthenticatorType() {

return FrameworkConstants.AuthenticatorType.UNDEFINED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.wso2.carbon.consent.mgt.core.model.ReceiptPurposeInput;
import org.wso2.carbon.consent.mgt.core.model.ReceiptServiceInput;
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.config.ConfigurationFacade;
import org.wso2.carbon.identity.application.authentication.framework.config.model.AuthenticatorConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.ExternalIdPConfig;
Expand Down Expand Up @@ -93,6 +92,7 @@

import static org.wso2.carbon.identity.application.authentication.framework.handler.request.PostAuthnHandlerFlowStatus.SUCCESS_COMPLETED;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.ALLOW_LOGIN_TO_IDP;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.AuthenticatorType;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.Config.SEND_ONLY_LOCALLY_MAPPED_ROLES_OF_IDP;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.EMAIL_ADDRESS_CLAIM;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkErrorConstants.ErrorMessages.ERROR_WHILE_ENCRYPTING_TOTP_SECRET_KEY;
Expand Down Expand Up @@ -187,7 +187,9 @@ private PostAuthnHandlerFlowStatus handleResponseFlow(HttpServletRequest request
AuthenticatorConfig authenticatorConfig = stepConfig.getAuthenticatedAutenticator();
ApplicationAuthenticator authenticator = authenticatorConfig.getApplicationAuthenticator();

if (authenticator instanceof FederatedApplicationAuthenticator) {
if (AuthenticatorType.FEDERATED.equals(authenticator.getAuthenticatorType()) ||
(AuthenticatorType.CUSTOM.equals(authenticator.getAuthenticatorType())
&& stepConfig.getAuthenticatedUser().isFederatedUser())) {
String externalIdPConfigName = stepConfig.getAuthenticatedIdP();
ExternalIdPConfig externalIdPConfig = getExternalIdpConfig(externalIdPConfigName, context);
context.setExternalIdP(externalIdPConfig);
Expand Down Expand Up @@ -295,7 +297,9 @@ private PostAuthnHandlerFlowStatus handleRequestFlow(HttpServletRequest request,
}
ApplicationAuthenticator authenticator = authenticatorConfig.getApplicationAuthenticator();

if (authenticator instanceof FederatedApplicationAuthenticator) {
if (AuthenticatorType.FEDERATED.equals(authenticator.getAuthenticatorType()) ||
(AuthenticatorType.CUSTOM.equals(authenticator.getAuthenticatorType()) &&
stepConfig.getAuthenticatedUser().isFederatedUser())) {
String externalIdPConfigName = stepConfig.getAuthenticatedIdP();
ExternalIdPConfig externalIdPConfig = getExternalIdpConfig(externalIdPConfigName, context);
context.setExternalIdP(externalIdPConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.config.model.AuthenticatorConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig;
Expand Down Expand Up @@ -53,6 +52,7 @@
import javax.servlet.http.HttpServletResponse;

import static org.wso2.carbon.identity.application.authentication.framework.handler.request.PostAuthnHandlerFlowStatus.SUCCESS_COMPLETED;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.AuthenticatorType;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.USER_TENANT_DOMAIN;

/**
Expand Down Expand Up @@ -114,7 +114,9 @@ public PostAuthnHandlerFlowStatus handle(HttpServletRequest request, HttpServlet
}
ApplicationAuthenticator authenticator = authenticatorConfig.getApplicationAuthenticator();

if (authenticator instanceof FederatedApplicationAuthenticator) {
if (AuthenticatorType.FEDERATED.equals(authenticator.getAuthenticatorType()) ||
(AuthenticatorType.CUSTOM.equals(authenticator.getAuthenticatorType()) &&
stepConfig.getAuthenticatedUser().isFederatedUser())) {
if (stepConfig.isSubjectIdentifierStep()) {
if (log.isDebugEnabled()) {
log.debug(authenticator.getName() + " has been set up for subject identifier step.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationFlowHandler;
import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.config.ConfigurationFacade;
import org.wso2.carbon.identity.application.authentication.framework.config.model.ApplicationConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.AuthenticatorConfig;
Expand Down Expand Up @@ -60,8 +59,10 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.AuthenticatorType;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.CONFIG_ALLOW_SP_REQUESTED_FED_CLAIMS_ONLY;


/**
* Default implementation of step based sequence handler.
*/
Expand Down Expand Up @@ -289,7 +290,9 @@ protected void handlePostAuthentication(HttpServletRequest request,

stepCount++;

if (authenticator instanceof FederatedApplicationAuthenticator) {
if (AuthenticatorType.FEDERATED.equals(authenticator.getAuthenticatorType()) ||
(AuthenticatorType.CUSTOM.equals(authenticator.getAuthenticatorType()) &&
stepConfig.getAuthenticatedUser().isFederatedUser())) {

ExternalIdPConfig externalIdPConfig = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationFlowHandler;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus;
import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.LocalApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.config.ConfigurationFacade;
import org.wso2.carbon.identity.application.authentication.framework.config.builder.FileBasedConfigurationBuilder;
Expand Down Expand Up @@ -87,6 +86,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.AuthenticatorType;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.BASIC_AUTH_MECHANISM;
import static org.wso2.carbon.identity.base.IdentityConstants.FEDERATED_IDP_SESSION_ID;

Expand Down Expand Up @@ -713,7 +713,9 @@ protected void doAuthentication(HttpServletRequest request, HttpServletResponse
}

String idpName = FrameworkConstants.LOCAL_IDP_NAME;
if (context.getExternalIdP() != null && authenticator instanceof FederatedApplicationAuthenticator) {
if (context.getExternalIdP() != null &&
(AuthenticatorType.FEDERATED.equals(authenticator.getAuthenticatorType()) ||
AuthenticatorType.CUSTOM.equals(authenticator.getAuthenticatorType()))) {
idpName = context.getExternalIdP().getIdPName();
}
// Add Diagnostic Logs for the selected authenticator by the user.
Expand Down Expand Up @@ -771,7 +773,9 @@ protected void doAuthentication(HttpServletRequest request, HttpServletResponse
context.getSubject().setAccessingOrganization(userResidentOrganization);
}

if (authenticator instanceof FederatedApplicationAuthenticator) {
if (AuthenticatorType.FEDERATED.equals(authenticator.getAuthenticatorType()) ||
(AuthenticatorType.CUSTOM.equals(authenticator.getAuthenticatorType())
&& context.getSubject().isFederatedUser())) {

if (context.getSubject().getUserName() == null) {
// Set subject identifier as the default username for federated users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,4 +817,17 @@ public enum AuthenticatorMessageType {
INFO,
ERROR
}

/**
* Default application related constants.
*/
public enum AuthenticatorType {

LOCAL,
FEDERATED,
REQUEST_PATH,
FLOW_HANDLER,
CUSTOM,
UNDEFINED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationDataPublisher;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationFlowHandler;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus;
import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.cache.AuthenticationContextCache;
import org.wso2.carbon.identity.application.authentication.framework.cache.AuthenticationContextCacheEntry;
import org.wso2.carbon.identity.application.authentication.framework.cache.AuthenticationContextCacheKey;
Expand Down Expand Up @@ -201,6 +200,7 @@
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.Application.CONSOLE_APP_PATH;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.Application.MY_ACCOUNT_APP;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.Application.MY_ACCOUNT_APP_PATH;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.AuthenticatorType;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.CONTEXT_PROP_INVALID_EMAIL_USERNAME;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.Config.AUTHENTICATION_CONTEXT_EXPIRY_VALIDATION;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.Config.SKIP_LOCAL_USER_SEARCH_FOR_AUTHENTICATION_FLOW_HANDLERS;
Expand Down Expand Up @@ -3395,7 +3395,9 @@ public static boolean isJITProvisioningEnabled(AuthenticationContext context)
}
ApplicationAuthenticator authenticator = authenticatorConfig.getApplicationAuthenticator();

if (authenticator instanceof FederatedApplicationAuthenticator) {
if (AuthenticatorType.FEDERATED.equals(authenticator.getAuthenticatorType()) ||
(AuthenticatorType.CUSTOM.equals(authenticator.getAuthenticatorType())
&& stepConfig.getAuthenticatedUser().isFederatedUser())) {
ExternalIdPConfig externalIdPConfig;
String externalIdPConfigName = stepConfig.getAuthenticatedIdP();
externalIdPConfig = getExternalIdpConfig(externalIdPConfigName, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ private AuthenticationContext processAndGetAuthenticationContext(ServiceProvider

if (isFederated) {
applicationAuthenticator = mock(FederatedApplicationAuthenticator.class);
lenient().when(applicationAuthenticator.getAuthenticatorType())
.thenReturn(FrameworkConstants.AuthenticatorType.FEDERATED);
} else {
lenient().when(applicationAuthenticator.getAuthenticatorType())
.thenReturn(FrameworkConstants.AuthenticatorType.LOCAL);
}
lenient().when(applicationAuthenticator.getName()).thenReturn("Authenticator1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ private AuthenticationContext processAndGetAuthenticationContext(ServiceProvider

if (isFederated) {
applicationAuthenticator = mock(FederatedApplicationAuthenticator.class);
when(applicationAuthenticator.getAuthenticatorType())
.thenReturn(FrameworkConstants.AuthenticatorType.FEDERATED);
} else {
when(applicationAuthenticator.getAuthenticatorType())
.thenReturn(FrameworkConstants.AuthenticatorType.LOCAL);
}
when(applicationAuthenticator.getName()).thenReturn("Authenticator1");

Expand Down

0 comments on commit d9f9c0b

Please sign in to comment.