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 12, 2024
1 parent 64f041d commit 6bc4d9c
Show file tree
Hide file tree
Showing 11 changed files with 79 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 @@ -18,9 +18,17 @@

package org.wso2.carbon.identity.application.authentication.framework;

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

/**
* Handler in the authentication flow but the authentication flow with this handler will be rejected.
*/
public interface AuthenticationFlowHandler extends ApplicationAuthenticator {


@Override
default FrameworkConstants.AuthenticatorType getAuthenticatorType() {

return FrameworkConstants.AuthenticatorType.FLOW_HANDLER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@

package org.wso2.carbon.identity.application.authentication.framework;

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

/**
* Federated application authenticator.
*/
public interface FederatedApplicationAuthenticator extends ApplicationAuthenticator {

@Override
default FrameworkConstants.AuthenticatorType getAuthenticatorType() {

return FrameworkConstants.AuthenticatorType.FEDERATED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@

package org.wso2.carbon.identity.application.authentication.framework;

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

/**
* Local authenticator for applications.
*/
public interface LocalApplicationAuthenticator extends ApplicationAuthenticator {

@Override
default FrameworkConstants.AuthenticatorType getAuthenticatorType() {

return FrameworkConstants.AuthenticatorType.LOCAL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@

package org.wso2.carbon.identity.application.authentication.framework;

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

/**
* Request path application authenticator.
*/
public interface RequestPathApplicationAuthenticator extends ApplicationAuthenticator {

@Override
default FrameworkConstants.AuthenticatorType getAuthenticatorType() {

return FrameworkConstants.AuthenticatorType.REQUEST_PATH;
}
}
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 @@ -61,6 +60,8 @@
import javax.servlet.http.HttpServletResponse;

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


/**
* 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 @@ -88,6 +87,7 @@
import javax.servlet.http.HttpServletResponse;

import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.BASIC_AUTH_MECHANISM;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.AuthenticatorType;
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

0 comments on commit 6bc4d9c

Please sign in to comment.