Skip to content
This repository has been archived by the owner on Feb 7, 2021. It is now read-only.

use captcha and csrf in login form, including config switch #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions config/lmcuser.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ $settings = array(
*/
//'use_registration_form_captcha' => false,

/**
* Login Form Captcha
*
* Determines if a captcha should be utilized on the user login form.
* Default value is false.
*/
//'use_login_form_captcha' => false,

/**
* Login Form CSRF
*
* Determines if a csrf should be utilized on the user login form.
* Default value is true.
*/
//'use_login_form_csrf' => true,

/**
* Form Captcha Options
*
Expand Down
29 changes: 21 additions & 8 deletions src/LmcUser/Form/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,27 @@ public function __construct($name, AuthenticationOptionsInterface $options)
)
);

// @todo: Fix this
// 1) getValidator() is a protected method
// 2) i don't believe the login form is actually being validated by the login action
// (but keep in mind we don't want to show invalid username vs invalid password or
// anything like that, it should just say "login failed" without any additional info)
//$csrf = new Element\Csrf('csrf');
//$csrf->getValidator()->setTimeout($options->getLoginFormTimeout());
//$this->add($csrf);
if ($this->getAuthenticationOptions()->getUseLoginFormCsrf()) {
$this->add([
'type' => '\Laminas\Form\Element\Csrf',
'name' => 'security',
'options' => [
'csrf_options' => [
'timeout' => $this->getAuthenticationOptions()->getLoginFormTimeout()
]
]
]);
}
if ($this->getAuthenticationOptions()->getUseLoginFormCaptcha()) {
$this->add(array(
'name' => 'captcha',
'type' => 'Laminas\Form\Element\Captcha',
'options' => array(
'label' => 'Human check',
'captcha' => $this->getAuthenticationOptions()->getFormCaptchaOptions(),
),
));
}

$submitElement = new Element\Button('submit');
$submitElement
Expand Down
5 changes: 4 additions & 1 deletion src/LmcUser/Form/LoginFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public function __construct(AuthenticationOptionsInterface $options)
$identityParams = array(
'name' => 'identity',
'required' => true,
'validators' => array()
'validators' => array(),
'filters' => array(
array('name' => 'StringTrim'),
)
);

$identityFields = $options->getAuthIdentityFields();
Expand Down
45 changes: 45 additions & 0 deletions src/LmcUser/Options/AuthenticationOptionsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,49 @@ public function setAuthIdentityFields($authIdentityFields);
* @return array
*/
public function getAuthIdentityFields();

/**
* set use a captcha in login form
*
* @param bool $useRegistrationFormCaptcha
* @return ModuleOptions
*/
public function setUseLoginFormCaptcha($useLoginFormCaptcha);

/**
* get use a captcha in login form
*
* @return bool
*/
public function getUseLoginFormCaptcha();

/**
* set use a csrf in login form
*
* @param bool $useLoginFormCsrf
* @return ModuleOptions
*/
public function setUseLoginFormCsrf($useLoginFormCsrf);

/**
* get use a csrf in login form
*
* @return bool
*/
public function getUseLoginFormCsrf();

/**
* set form CAPTCHA options
*
* @param array $formCaptchaOptions
* @return ModuleOptions
*/
public function setFormCaptchaOptions($formCaptchaOptions);

/**
* get form CAPTCHA options
*
* @return array
*/
public function getFormCaptchaOptions();
}
54 changes: 54 additions & 0 deletions src/LmcUser/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ class ModuleOptions extends AbstractOptions implements
* @var bool
*/
protected $useRegistrationFormCaptcha = false;

/**
* @var bool
*/
protected $useLoginFormCaptcha = false;

/**
* @var bool
*/
protected $useLoginFormCsrf = true;

/**
* @var int
Expand Down Expand Up @@ -472,6 +482,50 @@ public function getUseRegistrationFormCaptcha()
{
return $this->useRegistrationFormCaptcha;
}

/**
* set use a captcha in login form
*
* @param bool $useLoginFormCaptcha
* @return ModuleOptions
*/
public function setUseLoginFormCaptcha($useLoginFormCaptcha)
{
$this->useLoginFormCaptcha = $useLoginFormCaptcha;
return $this;
}

/**
* get use a captcha in login form
*
* @return bool
*/
public function getUseLoginFormCaptcha()
{
return $this->useLoginFormCaptcha;
}

/**
* set use a csrf in login form
*
* @param bool $useRegistrationFormCaptcha
* @return ModuleOptions
*/
public function setUseLoginFormCsrf($useLoginFormCsrf)
{
$this->useLoginFormCsrf = $useLoginFormCsrf;
return $this;
}

/**
* get use a csrf in login form
*
* @return bool
*/
public function getUseLoginFormCsrf()
{
return $this->useLoginFormCsrf;
}

/**
* set user entity class name
Expand Down