Skip to content

Commit

Permalink
Merge pull request #6108 from Thisara-Welmilla/add-user-defined-auth-…
Browse files Browse the repository at this point in the history
…config

Add new authenticator configs for user defined authentication extensions.
  • Loading branch information
Thisara-Welmilla authored Nov 8, 2024
2 parents a1adff7 + 0c77d92 commit 4a30de8
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,29 +437,11 @@ private EndpointConfig getActionEndpointConfigById(String actionUUID, Integer te

Authentication authentication = null;
if (actionEndpointProperties.containsKey(ActionMgtConstants.AUTHN_TYPE_ATTRIBUTE)) {
Authentication.Type authnType = Authentication.Type.valueOf(
actionEndpointProperties.get(ActionMgtConstants.AUTHN_TYPE_ATTRIBUTE));
switch (authnType) {
case BASIC:
authentication = new Authentication.BasicAuthBuilder(
actionEndpointProperties.get(Authentication.Property.USERNAME.getName()),
actionEndpointProperties.get(Authentication.Property.PASSWORD.getName())).build();
break;
case BEARER:
authentication = new Authentication.BearerAuthBuilder(
actionEndpointProperties.get(Authentication.Property.ACCESS_TOKEN.getName())).build();
break;
case API_KEY:
authentication = new Authentication.APIKeyAuthBuilder(
actionEndpointProperties.get(Authentication.Property.HEADER.getName()),
actionEndpointProperties.get(Authentication.Property.VALUE.getName())).build();
break;
case NONE:
authentication = new Authentication.NoneAuthBuilder().build();
break;
default:
break;
}
authentication = new Authentication.AuthenticationBuilder()
.type(Authentication.Type.valueOf(
actionEndpointProperties.get(ActionMgtConstants.AUTHN_TYPE_ATTRIBUTE)))
.properties(actionEndpointProperties)
.build();
} else {
throw ActionManagementUtil.handleServerException(
ActionMgtConstants.ErrorMessages.ERROR_NO_AUTHENTICATION_TYPE, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.carbon.identity.action.management.model;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.action.management.ActionSecretProcessor;
import org.wso2.carbon.identity.action.management.constant.ActionMgtConstants;
import org.wso2.carbon.identity.action.management.exception.ActionMgtException;
Expand All @@ -27,6 +28,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
* Authentication class which hold supported authentication types and their properties.
Expand Down Expand Up @@ -233,4 +236,62 @@ public Authentication build() {
return new Authentication(this);
}
}

/**
* This builder build endpoint by taking the authentication type and properties as input.
*/
public static class AuthenticationBuilder {

private Type authType;
private Map<String, String> authPropertiesMap;

public AuthenticationBuilder type(Type type) {

this.authType = type;
return this;
}

public AuthenticationBuilder properties(Map<String, String> authPropertiesMap) {

this.authPropertiesMap = authPropertiesMap;
return this;
}

public Authentication build() {

switch (authType) {
case BASIC:
return new Authentication.BasicAuthBuilder(
getProperty(Type.BASIC, authPropertiesMap, Property.USERNAME.getName()),
getProperty(Type.BASIC, authPropertiesMap, Property.PASSWORD.getName())).build();
case BEARER:
return new Authentication.BearerAuthBuilder(
getProperty(Type.BEARER, authPropertiesMap, Property.ACCESS_TOKEN.getName())).build();
case API_KEY:
return new Authentication.APIKeyAuthBuilder(
getProperty(Type.API_KEY, authPropertiesMap, Property.HEADER.getName()),
getProperty(Type.API_KEY, authPropertiesMap, Property.VALUE.getName())).build();
case NONE:
return new Authentication.NoneAuthBuilder().build();
default:
throw new IllegalArgumentException(String.format("An invalid authentication type '%s' is " +
"provided for the authentication configuration of the endpoint.", authType.name()));
}
}

private String getProperty(Authentication.Type authType, Map<String, String> actionEndpointProperties,
String propertyName) {

if (actionEndpointProperties != null && actionEndpointProperties.containsKey(propertyName)) {
String propValue = actionEndpointProperties.get(propertyName);
if (StringUtils.isNotBlank(propValue)) {
return propValue;
}
throw new IllegalArgumentException(String.format("The Property %s cannot be blank.", propertyName));
}

throw new NoSuchElementException(String.format("The property %s must be provided as an authentication " +
"property for the %s authentication type.", propertyName, authType.name()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.central.log.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.action.management</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public class FederatedAuthenticatorConfig implements Serializable {
@XmlElement(name = "DefinedBy")
protected DefinedByType definedByType;

public FederatedAuthenticatorConfig() {

definedByType = DefinedByType.SYSTEM;
}

public static FederatedAuthenticatorConfig build(OMElement federatedAuthenticatorConfigOM) {

if (federatedAuthenticatorConfigOM == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public class LocalAuthenticatorConfig implements Serializable {
@XmlElement(name = "DefinedBy")
protected DefinedByType definedByType;

public LocalAuthenticatorConfig() {

definedByType = DefinedByType.SYSTEM;
}

/*
* <LocalAuthenticatorConfig> <Name></Name> <DisplayName></DisplayName> <IsEnabled></IsEnabled>
* <Properties></Properties> </LocalAuthenticatorConfig>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2024, 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.application.common.model;

import org.wso2.carbon.identity.action.management.model.Authentication;
import org.wso2.carbon.identity.action.management.model.EndpointConfig;

import java.util.Map;

/**
* The authenticator endpoint configuration model for the user defined authenticator configurations.
*/
public class UserDefinedAuthenticatorEndpointConfig {

private final EndpointConfig endpointConfig;

private UserDefinedAuthenticatorEndpointConfig(UserDefinedAuthenticatorEndpointConfigBuilder builder) {

endpointConfig = builder.endpointConfig;
}

public EndpointConfig getEndpointConfig() {

return endpointConfig;
}

/**
* UserDefinedAuthenticatorEndpointConfig builder.
*/
public static class UserDefinedAuthenticatorEndpointConfigBuilder {

private String uri;
private String authenticationType;
private Map<String, String> authenticationProperties;
private EndpointConfig endpointConfig;

public UserDefinedAuthenticatorEndpointConfigBuilder() {
}

public UserDefinedAuthenticatorEndpointConfigBuilder uri(String uri) {

this.uri = uri;
return this;
}

public UserDefinedAuthenticatorEndpointConfigBuilder authenticationProperties(
Map<String, String> authentication) {

this.authenticationProperties = authentication;
return this;
}

public UserDefinedAuthenticatorEndpointConfigBuilder authenticationType(String authenticationType) {

this.authenticationType = authenticationType;
return this;
}

public UserDefinedAuthenticatorEndpointConfig build() {

EndpointConfig.EndpointConfigBuilder endpointConfigBuilder = new EndpointConfig.EndpointConfigBuilder();
endpointConfigBuilder.uri(uri);
endpointConfigBuilder.authentication(new Authentication.AuthenticationBuilder()
.type(Authentication.Type.valueOf(authenticationType))
.properties(authenticationProperties)
.build());
endpointConfig = endpointConfigBuilder.build();

return new UserDefinedAuthenticatorEndpointConfig(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024, 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.application.common.model;

import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;

/**
* The user defined federated authenticator configuration model.
*/
public class UserDefinedFederatedAuthenticatorConfig extends FederatedAuthenticatorConfig {

private static final String TAG_CUSTOM = "CUSTOM";

protected UserDefinedAuthenticatorEndpointConfig endpointConfig;

public UserDefinedFederatedAuthenticatorConfig() {

definedByType = DefinedByType.USER;
setTags(new String[]{TAG_CUSTOM});
}

/**
* Get the endpoint configurations of the User defined federated authenticator config.
*
* @return UserDefinedAuthenticatorEndpointConfig
*/
public UserDefinedAuthenticatorEndpointConfig getEndpointConfig() {

return endpointConfig;
}

/**
* Set the endpoint configurations of the User defined federated authenticator config.
*
* @param endpointConfig The endpoint config of the User defined federated authenticator config.
*/
public void setEndpointConfig(UserDefinedAuthenticatorEndpointConfig endpointConfig) {

this.endpointConfig = endpointConfig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024, 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.application.common.model;

import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.AuthenticationType;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;

/**
* The user defined local authenticator configuration model.
*/
public class UserDefinedLocalAuthenticatorConfig extends LocalAuthenticatorConfig {

private static final String TAG_2FA = "2FA";
private static final String TAG_CUSTOM = "CUSTOM";

protected UserDefinedAuthenticatorEndpointConfig endpointConfig;

public UserDefinedLocalAuthenticatorConfig(AuthenticationType type) {

definedByType = DefinedByType.USER;
if (AuthenticationType.VERIFICATION == type) {
setTags(new String[]{TAG_CUSTOM, TAG_2FA});
} else {
setTags(new String[]{TAG_CUSTOM});
}
}

/**
* Get the endpoint configurations of the User defined local authenticator config.
*
* @return UserDefinedAuthenticatorEndpointConfig
*/
public UserDefinedAuthenticatorEndpointConfig getEndpointConfig() {

return endpointConfig;
}

/**
* Set the endpoint configurations of the User defined local authenticator config.
*
* @param endpointConfig The endpoint config of the User defined local authenticator config.
*/
public void setEndpointConfig(UserDefinedAuthenticatorEndpointConfig endpointConfig) {

this.endpointConfig = endpointConfig;
}
}
Loading

0 comments on commit 4a30de8

Please sign in to comment.