Skip to content

Commit

Permalink
Add new authenticator configs for user defined auth extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Nov 7, 2024
1 parent 020a81a commit 44bf32f
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 180 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,60 @@ 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(authPropertiesMap, Property.USERNAME.getName()),
getProperty(authPropertiesMap, Property.PASSWORD.getName())).build();
case BEARER:
return new Authentication.BearerAuthBuilder(
getProperty(authPropertiesMap, Property.ACCESS_TOKEN.getName())).build();
case API_KEY:
return new Authentication.APIKeyAuthBuilder(
getProperty(authPropertiesMap, Property.HEADER.getName()),
getProperty(authPropertiesMap, Property.VALUE.getName())).build();
case NONE:
return new Authentication.NoneAuthBuilder().build();
default:
throw new IllegalArgumentException();
}
}

private String getProperty(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 is not found in the authentication " +
"configuration.", propertyName));
}
}
}

This file was deleted.

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.
*/
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
Expand Up @@ -27,7 +27,7 @@ public class UserDefinedFederatedAuthenticatorConfig extends FederatedAuthentica

private static final String TAG_CUSTOM = "CUSTOM";

protected AuthenticatorEndpointConfiguration endpointConfig;
protected UserDefinedAuthenticatorEndpointConfig endpointConfig;

public UserDefinedFederatedAuthenticatorConfig() {

Expand All @@ -40,7 +40,7 @@ public UserDefinedFederatedAuthenticatorConfig() {
*
* @return DefinedByType
*/
public AuthenticatorEndpointConfiguration getEndpointConfig() {
public UserDefinedAuthenticatorEndpointConfig getEndpointConfig() {

return endpointConfig;
}
Expand All @@ -50,7 +50,7 @@ public AuthenticatorEndpointConfiguration getEndpointConfig() {
*
* @param endpointConfig The endpoint config of the User defined federated authenticator config.
*/
public void setEndpointConfig(AuthenticatorEndpointConfiguration endpointConfig) {
public void setEndpointConfig(UserDefinedAuthenticatorEndpointConfig endpointConfig) {

this.endpointConfig = endpointConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class UserDefinedLocalAuthenticatorConfig extends LocalAuthenticatorConfi
private static final String TAG_2FA = "2FA";
private static final String TAG_CUSTOM = "CUSTOM";

protected AuthenticatorEndpointConfiguration endpointConfig;
protected UserDefinedAuthenticatorEndpointConfig endpointConfig;

public UserDefinedLocalAuthenticatorConfig(AuthenticationType type) {

Expand All @@ -46,7 +46,7 @@ public UserDefinedLocalAuthenticatorConfig(AuthenticationType type) {
*
* @return DefinedByType
*/
public AuthenticatorEndpointConfiguration getEndpointConfig() {
public UserDefinedAuthenticatorEndpointConfig getEndpointConfig() {

return endpointConfig;
}
Expand All @@ -56,7 +56,7 @@ public AuthenticatorEndpointConfiguration getEndpointConfig() {
*
* @param endpointConfig The endpoint config of the User defined local authenticator config.
*/
public void setEndpointConfig(AuthenticatorEndpointConfiguration endpointConfig) {
public void setEndpointConfig(UserDefinedAuthenticatorEndpointConfig endpointConfig) {

this.endpointConfig = endpointConfig;
}
Expand Down
Loading

0 comments on commit 44bf32f

Please sign in to comment.