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

Add custom validation rule with translation. #30

Closed
wants to merge 6 commits into from

Conversation

bskl
Copy link
Contributor

@bskl bskl commented May 28, 2020

With custom validation rule we can easily to redirect form page when the pin code is invalid.

@DarkGhostHunter
Copy link
Owner

It will fail if the user is not an instance of 2FA. It also confirms the code instead of just validating it.

Instead, you could make a rule to just check if the code is valid. If the user is not 2FA, it should return false.

@bskl
Copy link
Contributor Author

bskl commented May 28, 2020

Ok, I'll make the changes soon.

@DarkGhostHunter
Copy link
Owner

DarkGhostHunter commented May 28, 2020

The idea is to make the rule enforceable anywhere. The rule should be called totp_code, since its the name of the implementation.

It should only succeed if the string is, well, a string, the user is an instance of TwoFactorAuthenticatable and the code is correct.

public function __construct(Authenticatable $user = null)
{
    $this->user = $user
}

public function passes()
{
    if ($this->user instanceof TwoFactorAuthenticatable) {
        // ... check if the code is a string and is correct.
    }

    return false;
}

Of course this would bypass safe devices. You could use this validation rule to set manually the device as "safe" in any part of your code:

public function setSafeDevice(Request $request, Authenticatable $user)
{
    $request->validate('required|totp_code');

    $user->addSafeDevice($request);

    session()->flash('message', "This device has been added as safe and the app won't ask for codes");
}

@bskl
Copy link
Contributor Author

bskl commented May 30, 2020

I made some updates for translations.

@DarkGhostHunter
Copy link
Owner

Closing pending inactivity.

@bskl
Copy link
Contributor Author

bskl commented Jun 7, 2020

Hi,
Was a different change necessary? I implemented it as you wrote in your last comment.

Copy link
Owner

@DarkGhostHunter DarkGhostHunter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all good.

src/Rules/TotpCode.php Show resolved Hide resolved
README.md Show resolved Hide resolved
src/Rules/TotpCode.php Show resolved Hide resolved
@DarkGhostHunter
Copy link
Owner

DarkGhostHunter commented Jun 8, 2020

Sorry about the latter, I didn't hit F5 enough times.

I arranged your code and made the implementation on 2.0. You rock.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants