-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from dhaura/DP-add-get-masked-content-function
Implement Method to Mask Content in Adaptive Authentication Scripts
- Loading branch information
Showing
7 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ava/org/wso2/carbon/identity/conditional/auth/functions/utils/GetMaskedValueFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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.conditional.auth.functions.utils; | ||
|
||
/** | ||
* Function to mask the given value. | ||
*/ | ||
@FunctionalInterface | ||
public interface GetMaskedValueFunction { | ||
|
||
/** | ||
* Masks the given value. | ||
* | ||
* @param value The value to be masked. | ||
* @return The masked value. | ||
*/ | ||
String getMaskedValue(String value); | ||
} |
35 changes: 35 additions & 0 deletions
35
...org/wso2/carbon/identity/conditional/auth/functions/utils/GetMaskedValueFunctionImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.conditional.auth.functions.utils; | ||
|
||
import org.graalvm.polyglot.HostAccess; | ||
import org.wso2.carbon.identity.central.log.mgt.utils.LoggerUtils; | ||
|
||
/** | ||
* Implementation of {@link GetMaskedValueFunction}. | ||
*/ | ||
public class GetMaskedValueFunctionImpl implements GetMaskedValueFunction { | ||
|
||
@Override | ||
@HostAccess.Export | ||
public String getMaskedValue(String value) { | ||
|
||
return LoggerUtils.getMaskedContent(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...wso2/carbon/identity/conditional/auth/functions/utils/GetMaskedValueFunctionImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.conditional.auth.functions.utils; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Parameters; | ||
import org.testng.annotations.Test; | ||
import org.testng.annotations.DataProvider; | ||
import org.wso2.carbon.CarbonConstants; | ||
import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig; | ||
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext; | ||
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder; | ||
import org.wso2.carbon.identity.application.common.model.ServiceProvider; | ||
import org.wso2.carbon.identity.central.log.mgt.utils.LoggerUtils; | ||
import org.wso2.carbon.identity.common.testng.WithCarbonHome; | ||
import org.wso2.carbon.identity.common.testng.WithH2Database; | ||
import org.wso2.carbon.identity.common.testng.WithRealmService; | ||
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsSequenceHandlerAbstractTest; | ||
import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException; | ||
|
||
import java.util.Collections; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Test class for GetMaskedValueFunctionImplTest. | ||
*/ | ||
@WithCarbonHome | ||
@WithH2Database(files = "dbscripts/h2.sql") | ||
@WithRealmService(injectToSingletons = {LoggerUtils.class, FrameworkServiceDataHolder.class}) | ||
public class GetMaskedValueFunctionImplTest extends JsSequenceHandlerAbstractTest { | ||
|
||
@BeforeClass | ||
@Parameters({"scriptEngine"}) | ||
public void setUp(String scriptEngine) throws Exception { | ||
|
||
super.setUp(scriptEngine); | ||
CarbonConstants.ENABLE_LEGACY_AUTHZ_RUNTIME = true; | ||
sequenceHandlerRunner.registerJsFunction("getMaskedValue", | ||
new GetMaskedValueFunctionImpl()); | ||
} | ||
|
||
@Test(dataProvider = "maskableValueProvider") | ||
public void testGetMaskedValue(boolean isLogMaskingEnabled, String username, String expectedMaskedValue) | ||
throws JsTestException { | ||
|
||
LoggerUtils.isLogMaskingEnable = isLogMaskingEnabled; | ||
sequenceHandlerRunner.addSubjectAuthenticator("BasicMockAuthenticator", username, Collections.emptyMap()); | ||
|
||
ServiceProvider sp = sequenceHandlerRunner.loadServiceProviderFromResource("get-masked-value-sp.xml", this); | ||
AuthenticationContext context = sequenceHandlerRunner.createAuthenticationContext(sp); | ||
SequenceConfig sequenceConfig = sequenceHandlerRunner.getSequenceConfig(context, sp); | ||
context.setSequenceConfig(sequenceConfig); | ||
context.initializeAnalyticsData(); | ||
|
||
HttpServletRequest req = sequenceHandlerRunner.createHttpServletRequest(); | ||
HttpServletResponse resp = sequenceHandlerRunner.createHttpServletResponse(); | ||
|
||
sequenceHandlerRunner.handle(req, resp, context, "test_domain"); | ||
|
||
Assert.assertEquals(context.getSelectedAcr(), expectedMaskedValue); | ||
} | ||
|
||
@DataProvider(name = "maskableValueProvider") | ||
public Object[][] maskableValueProvider() { | ||
|
||
/* | ||
The "getMaskedValue" method should always mask the passed in value | ||
irrespective of the server-wide 'isLogMaskingEnable' configuration. | ||
*/ | ||
return new Object[][]{ | ||
{true, "johndoe", "j*****e"}, | ||
{false, "johndoe", "j*****e"}, | ||
}; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...sources/org/wso2/carbon/identity/conditional/auth/functions/utils/get-masked-value-sp.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<!-- | ||
~ Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. | ||
~ | ||
~ 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. | ||
--> | ||
<ServiceProvider> | ||
<ApplicationID>1</ApplicationID> | ||
<ApplicationName>default</ApplicationName> | ||
<Description>Default Service Provider</Description> | ||
<LocalAndOutBoundAuthenticationConfig> | ||
<AuthenticationSteps> | ||
<AuthenticationStep> | ||
<StepOrder>1</StepOrder> | ||
<LocalAuthenticatorConfigs> | ||
<LocalAuthenticatorConfig> | ||
<Name>BasicMockAuthenticator</Name> | ||
<DisplayName>basicauth</DisplayName> | ||
<IsEnabled>true</IsEnabled> | ||
</LocalAuthenticatorConfig> | ||
</LocalAuthenticatorConfigs> | ||
<SubjectStep>true</SubjectStep> | ||
<AttributeStep>true</AttributeStep> | ||
</AuthenticationStep> | ||
</AuthenticationSteps> | ||
<AuthenticationScript type="application/javascript" enabled="true"><![CDATA[ | ||
var onLoginRequest = function(context) { | ||
executeStep(1, { | ||
onSuccess: function(context) { | ||
var username = context.currentKnownSubject.username; | ||
var maskedUsername = getMaskedValue(username); | ||
Log.info("Masked username of the logged user: " + maskedUsername); | ||
context.selectedAcr = maskedUsername; | ||
}, | ||
}); | ||
}; | ||
]]></AuthenticationScript> | ||
<AuthenticationType>flow</AuthenticationType> | ||
</LocalAndOutBoundAuthenticationConfig> | ||
<ClaimConfig> | ||
<AlwaysSendMappedLocalSubjectId>true</AlwaysSendMappedLocalSubjectId> | ||
</ClaimConfig> | ||
</ServiceProvider> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters