-
-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: kingthorin <[email protected]> # Conflicts: # addOns/authhelper/CHANGELOG.md
- Loading branch information
1 parent
1845b96
commit a1cbcf7
Showing
12 changed files
with
1,030 additions
and
47 deletions.
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
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
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
112 changes: 112 additions & 0 deletions
112
...elper/src/main/java/org/zaproxy/addon/authhelper/client/ClientScriptBasedAuthHandler.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,112 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2025 The ZAP Development Team | ||
* | ||
* Licensed 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.zaproxy.addon.authhelper.client; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.zaproxy.addon.authhelper.AuthUtils; | ||
import org.zaproxy.addon.authhelper.client.ClientScriptBasedAuthenticationMethodType.ClientScriptBasedAuthenticationMethod; | ||
import org.zaproxy.addon.network.ExtensionNetwork; | ||
import org.zaproxy.addon.network.server.ServerInfo; | ||
import org.zaproxy.zap.authentication.AuthenticationMethod; | ||
import org.zaproxy.zap.extension.selenium.BrowserHook; | ||
import org.zaproxy.zap.extension.selenium.ExtensionSelenium; | ||
import org.zaproxy.zap.extension.selenium.SeleniumScriptUtils; | ||
import org.zaproxy.zap.extension.spiderAjax.AuthenticationHandler; | ||
import org.zaproxy.zap.model.Context; | ||
import org.zaproxy.zap.users.User; | ||
import org.zaproxy.zest.impl.ZestBasicRunner; | ||
|
||
public class ClientScriptBasedAuthHandler implements AuthenticationHandler { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(ClientScriptBasedAuthHandler.class); | ||
|
||
private BrowserHook browserHook; | ||
private static ZestBasicRunner zestRunner; | ||
|
||
@Override | ||
public boolean enableAuthentication(User user) { | ||
Context context = user.getContext(); | ||
if (context.getAuthenticationMethod() | ||
instanceof | ||
ClientScriptBasedAuthenticationMethodType.ClientScriptBasedAuthenticationMethod) { | ||
|
||
if (browserHook != null) { | ||
throw new IllegalStateException("BrowserHook already enabled"); | ||
} | ||
browserHook = new AuthenticationBrowserHook(context, user); | ||
|
||
AuthUtils.getExtension(ExtensionSelenium.class).registerBrowserHook(browserHook); | ||
|
||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean disableAuthentication(User user) { | ||
if (browserHook != null) { | ||
AuthUtils.getExtension(ExtensionSelenium.class).deregisterBrowserHook(browserHook); | ||
browserHook = null; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
static class AuthenticationBrowserHook implements BrowserHook { | ||
|
||
private ClientScriptBasedAuthenticationMethod csaMethod; | ||
private Context context; | ||
|
||
AuthenticationBrowserHook(Context context, User user) { | ||
this.context = context; | ||
AuthenticationMethod method = context.getAuthenticationMethod(); | ||
if (!(method instanceof ClientScriptBasedAuthenticationMethod)) { | ||
throw new IllegalStateException("Unsupported method " + method.getType().getName()); | ||
} | ||
csaMethod = (ClientScriptBasedAuthenticationMethod) method; | ||
} | ||
|
||
private static ZestBasicRunner getZestRunner() { | ||
if (zestRunner == null) { | ||
zestRunner = new ZestBasicRunner(); | ||
// Always proxy via ZAP | ||
ServerInfo mainProxyInfo = | ||
AuthUtils.getExtension(ExtensionNetwork.class).getMainProxyServerInfo(); | ||
zestRunner.setProxy(mainProxyInfo.getAddress(), mainProxyInfo.getPort()); | ||
} | ||
return zestRunner; | ||
} | ||
|
||
@Override | ||
public void browserLaunched(SeleniumScriptUtils ssUtils) { | ||
ZestBasicRunner runner = getZestRunner(); | ||
runner.addWebDriver("ClientScriptBasedAuthHandler", ssUtils.getWebDriver()); | ||
try { | ||
runner.run(csaMethod.getZestScript(), null); | ||
} catch (Exception e) { | ||
LOGGER.warn( | ||
"An error occurred while trying to execute the Client Script Authentication script: {}", | ||
e.getMessage()); | ||
LOGGER.debug(e); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.