Search in sources :

Example 51 with Authenticator

use of org.wso2.carbon.identity.api.server.authenticators.v1.model.Authenticator in project identity-conditional-auth-functions by wso2-extensions.

the class JsSequenceHandlerRunner method addSubjectAuthenticator.

public void addSubjectAuthenticator(String authenticatorName, String subject, Map<String, String> claims) {
    FrameworkServiceDataHolder.getInstance().getAuthenticators().removeIf(applicationAuthenticator -> applicationAuthenticator.getName().equals(authenticatorName));
    MockAuthenticator authenticator = new MockAuthenticator(authenticatorName, (SubjectCallback) context1 -> {
        AuthenticatedUser user = createLocalAuthenticatedUserFromSubjectIdentifier(subject);
        if (claims != null) {
            for (Map.Entry<String, String> entry : claims.entrySet()) {
                user.getUserAttributes().put(ClaimMapping.build(entry.getKey(), entry.getKey(), entry.getValue(), false), entry.getValue());
            }
        }
        return user;
    });
    FrameworkServiceDataHolder.getInstance().getAuthenticators().add(authenticator);
}
Also used : AsyncSequenceExecutor(org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.AsyncSequenceExecutor) Enumeration(java.util.Enumeration) ServletException(javax.servlet.ServletException) URL(java.net.URL) SequenceConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig) SubjectCallback(org.wso2.carbon.identity.conditional.auth.functions.test.utils.api.SubjectCallback) Locale(java.util.Locale) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) Mockito.doReturn(org.mockito.Mockito.doReturn) PrintWriter(java.io.PrintWriter) HttpSession(javax.servlet.http.HttpSession) Collection(java.util.Collection) RequestDispatcher(javax.servlet.RequestDispatcher) UUID(java.util.UUID) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) GraphBasedSequenceHandler(org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.GraphBasedSequenceHandler) JsFunctionRegistryImpl(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsFunctionRegistryImpl) Principal(java.security.Principal) MultitenantUtils(org.wso2.carbon.utils.multitenancy.MultitenantUtils) ServletResponse(javax.servlet.ServletResponse) LogFactory(org.apache.commons.logging.LogFactory) MockAuthenticator(org.wso2.carbon.identity.conditional.auth.functions.test.utils.api.MockAuthenticator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mockito.mock(org.mockito.Mockito.mock) AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) ServletInputStream(javax.servlet.ServletInputStream) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) JsGraphBuilderFactory(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) UIBasedConfigurationLoader(org.wso2.carbon.identity.application.authentication.framework.config.loader.UIBasedConfigurationLoader) ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) JsFunctionRegistry(org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry) Cookie(javax.servlet.http.Cookie) Hashtable(java.util.Hashtable) OMElement(org.apache.axiom.om.OMElement) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) IdentityProviderManager(org.wso2.carbon.idp.mgt.IdentityProviderManager) Field(java.lang.reflect.Field) File(java.io.File) FileBasedConfigurationBuilder(org.wso2.carbon.identity.application.authentication.framework.config.builder.FileBasedConfigurationBuilder) CacheBackedIdPMgtDAO(org.wso2.carbon.idp.mgt.dao.CacheBackedIdPMgtDAO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Part(javax.servlet.http.Part) FrameworkServiceDataHolder(org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) IdentityUtil(org.wso2.carbon.identity.core.util.IdentityUtil) FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) DispatcherType(javax.servlet.DispatcherType) Log(org.apache.commons.logging.Log) ServletContext(javax.servlet.ServletContext) BufferedReader(java.io.BufferedReader) JSExecutionSupervisor(org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JSExecutionSupervisor) Collections(java.util.Collections) InputStream(java.io.InputStream) MockAuthenticator(org.wso2.carbon.identity.conditional.auth.functions.test.utils.api.MockAuthenticator) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)

Example 52 with Authenticator

use of org.wso2.carbon.identity.api.server.authenticators.v1.model.Authenticator in project identity-api-server by wso2.

the class ServerIdpManagementService method createFederatedAuthenticator.

/**
 * Create API Federated Authenticator model using internal FederatedAuthenticatorConfig.
 *
 * @param authenticatorId  Federated Authenticator ID.
 * @param identityProvider Identity Provider information.
 * @return FederatedAuthenticator.
 */
private FederatedAuthenticator createFederatedAuthenticator(String authenticatorId, IdentityProvider identityProvider) {
    FederatedAuthenticatorConfig[] authConfigs = identityProvider.getFederatedAuthenticatorConfigs();
    if (ArrayUtils.isEmpty(authConfigs)) {
        return null;
    }
    FederatedAuthenticatorConfig config = null;
    boolean isDefaultAuthenticator = false;
    String authenticatorName = base64URLDecode(authenticatorId);
    for (FederatedAuthenticatorConfig authConfig : authConfigs) {
        if (StringUtils.equals(authConfig.getName(), authenticatorName)) {
            config = authConfig;
        }
    }
    if (identityProvider.getDefaultAuthenticatorConfig() != null && StringUtils.equals(identityProvider.getDefaultAuthenticatorConfig().getName(), authenticatorName)) {
        isDefaultAuthenticator = true;
    }
    FederatedAuthenticator federatedAuthenticator = new FederatedAuthenticator();
    if (config != null) {
        federatedAuthenticator.setAuthenticatorId(authenticatorId);
        federatedAuthenticator.setName(config.getName());
        federatedAuthenticator.setIsEnabled(config.isEnabled());
        federatedAuthenticator.setIsDefault(isDefaultAuthenticator);
        FederatedAuthenticatorConfig federatedAuthenticatorConfig = ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(config.getName());
        if (federatedAuthenticatorConfig != null) {
            String[] tags = federatedAuthenticatorConfig.getTags();
            if (ArrayUtils.isNotEmpty(tags)) {
                federatedAuthenticator.setTags(Arrays.asList(tags));
            }
        }
        List<org.wso2.carbon.identity.api.server.idp.v1.model.Property> properties = Arrays.stream(config.getProperties()).map(propertyToExternal).collect(Collectors.toList());
        federatedAuthenticator.setProperties(properties);
    }
    return federatedAuthenticator;
}
Also used : FederatedAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig) FederatedAuthenticator(org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticator) MetaFederatedAuthenticator(org.wso2.carbon.identity.api.server.idp.v1.model.MetaFederatedAuthenticator) IdentityProviderProperty(org.wso2.carbon.identity.application.common.model.IdentityProviderProperty) Property(org.wso2.carbon.identity.application.common.model.Property) SubProperty(org.wso2.carbon.identity.application.common.model.SubProperty) MetaProperty(org.wso2.carbon.identity.api.server.idp.v1.model.MetaProperty)

Example 53 with Authenticator

use of org.wso2.carbon.identity.api.server.authenticators.v1.model.Authenticator in project identity-api-server by wso2.

the class ServerIdpManagementService method getFederatedAuthenticators.

/**
 * Returns configured federated authenticators of a specific identity provider.
 *
 * @param idpId Identity provider resource ID.
 * @return FederatedAuthenticatorListResponse Federated authenticator list.
 */
public FederatedAuthenticatorListResponse getFederatedAuthenticators(String idpId) {
    FederatedAuthenticatorListResponse listResponse;
    try {
        IdentityProvider idP = IdentityProviderServiceHolder.getIdentityProviderManager().getIdPByResourceId(idpId, ContextLoader.getTenantDomainFromContext(), true);
        if (idP == null) {
            throw handleException(Response.Status.NOT_FOUND, Constants.ErrorMessage.ERROR_CODE_IDP_NOT_FOUND, idpId);
        }
        listResponse = new FederatedAuthenticatorListResponse();
        FederatedAuthenticatorConfig[] fedAuthConfigs = idP.getFederatedAuthenticatorConfigs();
        if (fedAuthConfigs != null) {
            List<FederatedAuthenticatorListItem> fedAuthList = new ArrayList<>();
            String defaultAuthenticator = null;
            for (FederatedAuthenticatorConfig config : fedAuthConfigs) {
                String fedAuthId = base64URLEncode(config.getName());
                FederatedAuthenticatorListItem listItem = new FederatedAuthenticatorListItem();
                listItem.setAuthenticatorId(fedAuthId);
                listItem.setName(config.getName());
                listItem.setIsEnabled(config.isEnabled());
                FederatedAuthenticatorConfig federatedAuthenticatorConfig = ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(config.getName());
                if (federatedAuthenticatorConfig != null) {
                    String[] tags = federatedAuthenticatorConfig.getTags();
                    if (ArrayUtils.isNotEmpty(tags)) {
                        listItem.setTags(Arrays.asList(tags));
                    }
                }
                listItem.setSelf(ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + IDP_PATH_COMPONENT + "/%s/federated-authenticators/%s", idpId, fedAuthId)).toString());
                fedAuthList.add(listItem);
                if (idP.getDefaultAuthenticatorConfig() != null) {
                    defaultAuthenticator = base64URLEncode(idP.getDefaultAuthenticatorConfig().getName());
                }
            }
            listResponse.setDefaultAuthenticatorId(defaultAuthenticator);
            listResponse.setAuthenticators(fedAuthList);
        }
    } catch (IdentityProviderManagementException e) {
        throw handleIdPException(e, Constants.ErrorMessage.ERROR_CODE_ERROR_RETRIEVING_IDP_AUTHENTICATORS, idpId);
    }
    return listResponse;
}
Also used : FederatedAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig) ArrayList(java.util.ArrayList) MetaFederatedAuthenticatorListItem(org.wso2.carbon.identity.api.server.idp.v1.model.MetaFederatedAuthenticatorListItem) FederatedAuthenticatorListItem(org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorListItem) IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) FederatedAuthenticatorListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorListResponse) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException)

Example 54 with Authenticator

use of org.wso2.carbon.identity.api.server.authenticators.v1.model.Authenticator in project identity-api-server by wso2.

the class ServerIdpManagementService method updateFederatedAuthenticators.

/**
 * Update federated authenticator of and IDP.
 *
 * @param idpId                Identity Provider resource ID.
 * @param authenticatorRequest Federated Authenticators Request.
 * @return FederatedAuthenticatorListResponse.
 */
public FederatedAuthenticatorListResponse updateFederatedAuthenticators(String idpId, FederatedAuthenticatorRequest authenticatorRequest) {
    try {
        IdentityProvider idp = IdentityProviderServiceHolder.getIdentityProviderManager().getIdPByResourceId(idpId, ContextLoader.getTenantDomainFromContext(), true);
        if (idp == null) {
            throw handleException(Response.Status.NOT_FOUND, Constants.ErrorMessage.ERROR_CODE_IDP_NOT_FOUND, idpId);
        }
        // Need to create a clone, since modifying the fields of the original object, will modify the cached
        // IDP object.
        IdentityProvider idpToUpdate = createIdPClone(idp);
        updateFederatedAuthenticatorConfig(idpToUpdate, authenticatorRequest);
        IdentityProvider updatedIdp = IdentityProviderServiceHolder.getIdentityProviderManager().updateIdPByResourceId(idpId, idpToUpdate, ContextLoader.getTenantDomainFromContext());
        return createFederatedAuthenticatorResponse(updatedIdp);
    } catch (IdentityProviderManagementException e) {
        throw handleIdPException(e, Constants.ErrorMessage.ERROR_CODE_ERROR_UPDATING_IDP, null);
    }
}
Also used : IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException)

Example 55 with Authenticator

use of org.wso2.carbon.identity.api.server.authenticators.v1.model.Authenticator in project identity-api-server by wso2.

the class ServiceProviderToApiModel method buildAuthStep.

private AuthenticationStepModel buildAuthStep(AuthenticationStep authenticationStep) {
    AuthenticationStepModel authStep = new AuthenticationStepModel();
    authStep.setId(authenticationStep.getStepOrder());
    arrayToStream(authenticationStep.getFederatedIdentityProviders()).forEach(y -> authStep.addOptionsItem(new Authenticator().idp(y.getIdentityProviderName()).authenticator(y.getDefaultAuthenticatorConfig().getName())));
    arrayToStream(authenticationStep.getLocalAuthenticatorConfigs()).forEach(y -> authStep.addOptionsItem(new Authenticator().idp(FrameworkConstants.LOCAL_IDP_NAME).authenticator(y.getName())));
    return authStep;
}
Also used : AuthenticationStepModel(org.wso2.carbon.identity.api.server.application.management.v1.AuthenticationStepModel) Authenticator(org.wso2.carbon.identity.api.server.application.management.v1.Authenticator)

Aggregations

FederatedAuthenticatorConfig (org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig)27 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)25 Test (org.testng.annotations.Test)23 IdentityProviderManagementException (org.wso2.carbon.idp.mgt.IdentityProviderManagementException)23 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)22 AuthenticatorConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.AuthenticatorConfig)22 ApplicationAuthenticator (org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator)19 StepConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig)19 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)16 FrameworkException (org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException)15 LocalAuthenticatorConfig (org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig)15 ISIntegrationTest (org.wso2.identity.integration.common.utils.ISIntegrationTest)15 IOException (java.io.IOException)12 Map (java.util.Map)12 FederatedApplicationAuthenticator (org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator)12 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)11 RequestPathAuthenticatorConfig (org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig)11 Property (org.wso2.carbon.identity.application.common.model.Property)10 HttpResponse (org.apache.http.HttpResponse)8