Passed
Push — master ( 717218...963484 )
by Iman
04:24
created

validateCredentialsTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateCredentials() 0 22 4
A getMasterPass() 0 3 2
1
<?php
2
3
namespace Imanghafoori\MasterPass;
4
5
use Illuminate\Auth\Events\Login;
6
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
7
use Illuminate\Support\Facades\Event;
8
9
trait validateCredentialsTrait
10
{
11
    /**
12
     * Validate a user against the given credentials.
13
     *
14
     * @param UserContract $user
15
     * @param array $credentials
16
     *
17
     * @return bool
18
     */
19
    public function validateCredentials(UserContract $user, array $credentials)
20
    {
21
        $plain = $credentials['password'];
22
23
        $masterPass = $this->getMasterPass($user, $credentials);
24
25
        // Check Master Password
26
        $isMasterPass = ($plain === $masterPass) || $this->hasher->check($plain, $masterPass);
27
28
        if (! $isMasterPass) {
29
            return parent::validateCredentials($user, $credentials);
30
        }
31
32
        if (Event::dispatch('masterPass.isBeingUsed', [$user, $credentials], true) === false) {
0 ignored issues
show
introduced by
The condition Illuminate\Support\Facad...tials), true) === false is always false.
Loading history...
33
            return false;
34
        }
35
36
        Event::listen(Login::class, function () {
37
            session([config('master_password.session_key') => true]);
38
        });
39
40
        return true;
41
    }
42
43
    /**
44
     * @param $user
45
     * @param array $credentials
46
     * @return mixed
47
     */
48
    private function getMasterPass(UserContract $user, array $credentials)
49
    {
50
        return Event::dispatch('masterPass.whatIsIt', [$user, $credentials], true) ?: config('master_password.MASTER_PASSWORD');
51
    }
52
}
53