Skip to content

Commit

Permalink
4547: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alek-s-andr committed Apr 7, 2022
0 parents commit b384882
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Magenable

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
92 changes: 92 additions & 0 deletions Plugin/IsCaptchaEnabledPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Magenable\CaptchaBypass\Plugin;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\ReCaptchaUi\Model\IsCaptchaEnabled;
use Magento\Framework\HTTP\Header;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Store\Model\ScopeInterface;

class IsCaptchaEnabledPlugin
{
/**
* @var ScopeConfigInterface
*/
private ScopeConfigInterface $scopeConfig;

/**
* @var Header
*/
private Header $httpHeader;

/**
* @var RemoteAddress
*/
private RemoteAddress $remoteAddress;

/**
* @param ScopeConfigInterface $scopeConfig
* @param Header $httpHeader
* @param RemoteAddress $remoteAddress
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Header $httpHeader,
RemoteAddress $remoteAddress
) {

$this->httpHeader = $httpHeader;
$this->remoteAddress = $remoteAddress;
$this->scopeConfig = $scopeConfig;
}

public function afterIsCaptchaEnabledFor(
IsCaptchaEnabled $subject,
bool $result
) {
if (!$this->scopeConfig->getValue('magenable_captcha_bypass/general/enabled', ScopeInterface::SCOPE_STORE)) {
return $result;
}

$ipWhitelisted = $this->scopeConfig->getValue(
'magenable_captcha_bypass/general/ip_whitelist',
ScopeInterface::SCOPE_STORE
);
$userAgentWhitelisted = $this->scopeConfig->getValue(
'magenable_captcha_bypass/general/user_agent_whitelist',
ScopeInterface::SCOPE_STORE
);
if (!$ipWhitelisted && !$userAgentWhitelisted) {
return $result;
}

$checkIp = false;
if ($ipWhitelisted) {
$ipWhitelisted = explode(',', $ipWhitelisted);
$remoteIp = $this->remoteAddress->getRemoteAddress();
foreach ($ipWhitelisted as $ipWhite) {
if ($remoteIp === trim($ipWhite)) {
$checkIp = true;
break;
}
}
}

$checkUserAgent = false;
if ($userAgentWhitelisted) {
$userAgent = $this->httpHeader->getHttpUserAgent();
if ($userAgent === $userAgentWhitelisted) {
$checkUserAgent = true;
}
}

if ($checkIp || $checkUserAgent) {
return false;
}

return $result;
}
}
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Magenable Captcha Bypass

The extension allows you to disable recaptcha on the site for certain ip-addresses or a specific browser User-Agent, this can be useful in time during automatic testing of the site.

## Installation

### Composer:

Run the following command in Magento 2 root folder

```
composer require magenable/module-captcha-bypass
bin/magento module:enable Magenable_CaptchaBypass
bin/magento setup:upgrade
```
## Upgrade

### Composer:

Run the following command in Magento 2 root folder

```
composer update magenable/module-captcha-bypass
bin/magento setup:upgrade
```

## Extension Settings

- In the menu of admin panel select **Stores** -> **Settings** -> **Configuration**
- Select **Magenable Extensions** and click on **Captcha Bypass**
- In group **General** in field **Enabled** select **Yes**
- In field **Whitelisted IP addresses** specify one or few ip-addresses separated by comma
- Or in field **Whitelisted User-Agent** specify User-Agent
- If one of this condition (from previous two fields) will meet the recaptcha will be disabled.
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "magenable/module-captcha-bypass",
"description": "A Magento 2 captcha bypass module for testing purposes",
"require": {
"magento/framework": ">=102.0",
"magenable/module-base": "*"
},
"type": "magento2-module",
"version": "1.0.0",
"license": "MIT",
"authors": [
{
"name": "Magenable",
"homepage": "https://magenable.com.au"
}
],
"keywords": [
"magento 2 captcha bypass",
"captcha workaround",
"recaptcha bypass"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magenable\\CaptchaBypass\\": ""
}
}
}
16 changes: 16 additions & 0 deletions etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magenable_Base::magenable">
<resource id="Magenable_CaptchaBypass::CaptchaBypass" title="Captcha Bypass"
sortOrder="10">
<resource id="Magenable_CaptchaBypass::config" title="Configuration" sortOrder="10"/>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
30 changes: 30 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="magenable_captcha_bypass" translate="label" type="text" sortOrder="100" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Captcha Bypass</label>
<tab>magenable</tab>
<resource>Magenable_CaptchaBypass::config</resource>
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>General</label>
<field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1"
showInStore="1" canRestore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="ip_whitelist" sortOrder="2" type="text" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Whitelisted IP addresses</label>
<comment>Comma separated, no spaces.</comment>
</field>
<field id="user_agent_whitelist" sortOrder="3" type="text" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Whitelisted User-Agent</label>
</field>
</group>
</section>
</system>
</config>
11 changes: 11 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<magenable_captcha_bypass>
<general>
<enabled>0</enabled>
</general>
</magenable_captcha_bypass>
</default>
</config>
8 changes: 8 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface">
<plugin name="magenable_captcha_bypass_plugin_is_captcha_enabled"
type="Magenable\CaptchaBypass\Plugin\IsCaptchaEnabledPlugin" sortOrder="1" disabled="false"/>
</type>
</config>
10 changes: 10 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenable_CaptchaBypass">
<sequence>
<module name="Magenable_Base"/>
<module name="Magento_ReCaptchaUi"/>
</sequence>
</module>
</config>
9 changes: 9 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Magenable_CaptchaBypass',
__DIR__
);

0 comments on commit b384882

Please sign in to comment.