I've created an own Request called ValidateSecretRequest for the google 2fa. But I only get empty attributes.
As additional information: I followed a tutorial from https://www.sitepoint.com/2fa-in-laravel-with-google-authenticator-get-secure/
After the authenticate() method the LoginController executes the postValidateToken() method. For testing I only implemented the request output.
LoginController
public function postValidateToken(ValidateSecretRequest $request) {
echo '<pre>'; print_r($request); echo '</pre>';
}
Request class
Firstly I supposed that there was a mistake with the extened Request? Actually it should be right in this way?
<?php
namespace App\Http\Requests;
use Cache;
use Crypt;
use Google2FA;
use App\User;
use Illuminate\Validation\Factory as ValidatonFactory;
use Illuminate\Http\Request;
class ValidateSecretRequest extends Request {
/**
*
* @var \App\User
*/
private $user;
public $totp;
/**
* Create a new FormRequest instance.
*
* @param \Illuminate\Validation\Factory $factory
* @return void
*/
public function __construct(ValidatonFactory $factory) {
$factory->extend(
'valid_token',
function ($attribute, $value, $parameters, $validator) {
$secret = Crypt::decrypt($this->user->google2fa_secret);
return Google2FA::verifyKey($secret, $value);
},
'Not a valid token'
);
$factory->extend(
'used_token',
function ($attribute, $value, $parameters, $validator) {
$key = $this->user->id . ':' . $value;
return !Cache::has($key);
},
'Cannot reuse token'
);
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() {
try {
$this->user = User::findOrFail(
session('2fa:user:id')
);
} catch (Exception $exc) {
return false;
}
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() {
return [
'totp' => 'bail|required|digits:6|valid_token|used_token',
];
}
}
But the function postValidateToken() returns an empty array.
App\Http\Requests\ValidateSecretRequest Object
(
[user:App\Http\Requests\ValidateSecretRequest:private] =>
[totp] =>
[json:protected] =>
[convertedFiles:protected] =>
[userResolver:protected] =>
[routeResolver:protected] =>
[attributes] =>
[request] =>
[query] =>
[server] =>
...
[session:protected] =>
[locale:protected] =>
[defaultLocale:protected] => en
[isHostValid:Symfony\Component\HttpFoundation\Request:private] => 1
[isForwardedValid:Symfony\Component\HttpFoundation\Request:private] => 1
)