-
Notifications
You must be signed in to change notification settings - Fork 4
/
AbstractPolicy.php
101 lines (90 loc) · 2.47 KB
/
AbstractPolicy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Hydrat\Laravel2FA\Policies;
use Illuminate\Http\Request;
use Hydrat\Laravel2FA\Models\LoginAttempt;
use Hydrat\Laravel2FA\Contracts\TwoFactorPolicyContract;
use Hydrat\Laravel2FA\Contracts\TwoFactorAuthenticatableContract;
abstract class AbstractPolicy implements TwoFactorPolicyContract
{
/**
* The incomming request at login.
*
* @var \Illuminate\Http\Request
*/
protected $request = null;
/**
* The user that just loggued in.
*
* @var \Hydrat\Laravel2FA\Contracts\TwoFactorAuthenticatableContract
*/
protected $user = null;
/**
* The login attempt, with UID and IP address data.
*
* @var \Hydrat\Laravel2FA\Models\LoginAttempt
*/
protected $attempt = null;
/**
* The failing message to set dynalically during check.
*
* @var string
*/
protected $message = '';
/**
* The class constructor.
*
* @param \Illuminate\Http\Request $request
* @param \Hydrat\Laravel2FA\Contracts\TwoFactorPolicyContract $user
* @param \Hydrat\Laravel2FA\Models\LoginAttempt $attempt
*
* @return void
*/
public function __construct(Request $request, TwoFactorAuthenticatableContract $user, LoginAttempt $attempt)
{
$this->request = $request;
$this->user = $user;
$this->attempt = $attempt;
}
/**
* Check that the request passes the policy.
* If this return false, the 2FA Auth will be triggered.
*
* @return bool
*/
public function passes(): bool
{
return true;
}
/**
* The reason sent to the Notification and the frontend view,
* to tell the user why the 2FA check was triggered.
*
* @return string
*/
public function message(): string
{
return $this->message ?: '';
}
/**
* An action to perform on successful 2FA login.
* May be used to remember stuff for the next policy check.
*
* @return void
*/
public function onSucceed(): void
{
//
}
/**
* Get the given policy options from configuration file.
*
* @param string $policy The policy name as defined in the laravel-2fa.options.policies conf.
* @param mixed $default Default value if not set of null.
*
* @return mixed
*/
protected function config(string $policy, $default = null)
{
return config('laravel-2fa.options.policies.' . $policy, $default);
}
}