Skip to content
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

Add support function to filter authenticators #148

Merged
merged 3 commits into from
Nov 23, 2023
Merged
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,84 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
~
~ WSO2 LLC. licenses this file to you 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.wso2.carbon.identity.conditional.auth.functions</groupId>
<artifactId>identity-conditional-auth-functions</artifactId>
<version>1.2.37-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>org.wso2.carbon.identity.conditional.auth.functions.utils</artifactId>
<name>Conditional Authentication - Util Functions</name>
<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>commons-lang.wso2</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.authentication.framework</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Private-Package>
org.wso2.carbon.identity.conditional.auth.functions.utils.internal
</Private-Package>
<Export-Package>
!org.wso2.carbon.identity.conditional.auth.functions.utils.internal,
org.wso2.carbon.identity.conditional.auth.functions.utils.*
</Export-Package>
<Import-Package>
org.apache.commons.lang,
org.osgi.framework; version="${org.osgi.framework.imp.pkg.version.range}",
org.osgi.service.component; version="${org.osgi.service.imp.pkg.version.range}",
org.wso2.carbon.identity.application.authentication.framework; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.util; version="${carbon.identity.package.import.version.range}",
</Import-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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.wso2.carbon.identity.conditional.auth.functions.utils;

import java.util.List;
import java.util.Map;

/**
* Function to exclude a defined authenticator from the provided options list.
*/
@FunctionalInterface
public interface FilterAuthenticatorsFunction {

Map<String, Map<String, String>> filterAuthenticators(List<Map<String, String>> authenticatorOptions,
String excludeAuthenticator);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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.wso2.carbon.identity.conditional.auth.functions.utils;

import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Implementation of the {@link FilterAuthenticatorsFunction}.
*/
public class FilterAuthenticatorsFunctionImpl implements FilterAuthenticatorsFunction {
@Override
public Map<String, Map<String, String>> filterAuthenticators(List<Map<String, String>> authenticatorOptions,
String excludeAuthenticator) {

Map<String, Map<String, String>> result = new HashMap<>();
int index = 0;

if (authenticatorOptions != null) {
for (Map<String, String> option : authenticatorOptions) {
String idp = option.get(FrameworkConstants.JSAttributes.IDP);
String authenticator = option.get(FrameworkConstants.JSAttributes.AUTHENTICATOR);

if (!StringUtils.equals(excludeAuthenticator, authenticator)) {
Map<String, String> idpMap = new HashMap<>();
if (FrameworkConstants.LOCAL_IDP_NAME.equals(idp)) {
idpMap.put(FrameworkConstants.JSAttributes.AUTHENTICATOR, authenticator);
} else {
idpMap.put(FrameworkConstants.JSAttributes.IDP, idp);
}
result.put(String.valueOf(index++), idpMap);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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.wso2.carbon.identity.conditional.auth.functions.utils.internal;

import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry;
import org.wso2.carbon.identity.conditional.auth.functions.utils.FilterAuthenticatorsFunction;
import org.wso2.carbon.identity.conditional.auth.functions.utils.FilterAuthenticatorsFunctionImpl;

/**
* OSGi declarative services component which handles registration and de-registration of utils related
* conditional auth functions.
*/
@Component(
name = "identity.conditional.auth.functions.utils.component",
immediate = true
)
public class UtilsFunctionServiceComponent {

@Activate
protected void activate(ComponentContext ctxt) {

FilterAuthenticatorsFunction filterAuthenticatorsFunctionImpl = new FilterAuthenticatorsFunctionImpl();
JsFunctionRegistry jsFunctionRegistry = UtilsFunctionServiceHolder.getInstance().getJsFunctionRegistry();
jsFunctionRegistry.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "filterAuthenticators",
filterAuthenticatorsFunctionImpl);
}

@Deactivate
protected void deactivate(ComponentContext ctxt) {

JsFunctionRegistry jsFunctionRegistry = UtilsFunctionServiceHolder.getInstance().getJsFunctionRegistry();
if (jsFunctionRegistry != null) {
jsFunctionRegistry.deRegister(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "filterAuthenticators");
}
}

@Reference(
service = JsFunctionRegistry.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetJsFunctionRegistry"
)
public void setJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) {

UtilsFunctionServiceHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistry);
}

public void unsetJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) {

UtilsFunctionServiceHolder.getInstance().setJsFunctionRegistry(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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.wso2.carbon.identity.conditional.auth.functions.utils.internal;

import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry;

/**
* Class to hold services discovered via OSGI on this component.
*/
public class UtilsFunctionServiceHolder {

private static UtilsFunctionServiceHolder instance = new UtilsFunctionServiceHolder();

private JsFunctionRegistry jsFunctionRegistry;

public static UtilsFunctionServiceHolder getInstance() {

return instance;
}
private UtilsFunctionServiceHolder(){
}

public JsFunctionRegistry getJsFunctionRegistry() {

return jsFunctionRegistry;
}

public void setJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) {

this.jsFunctionRegistry = jsFunctionRegistry;
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<module>components/org.wso2.carbon.identity.conditional.auth.functions.session</module>
<module>components/org.wso2.carbon.identity.conditional.auth.functions.jwt.decode</module>
<module>features/org.wso2.carbon.identity.conditional.auth.functions.server.feature</module>
<module>components/org.wso2.carbon.identity.conditional.auth.functions.utils</module>

</modules>

Expand Down