Passed
Branch master (d3b867)
by Iman
11:48
created

PassportUserRepository::checkMasterPass()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 4
nop 3
1
<?php
2
3
namespace Imanghafoori\MasterPass;
4
5
use RuntimeException;
6
use Illuminate\Support\Facades\Event;
7
use Laravel\Passport\Bridge\UserRepository;
0 ignored issues
show
Bug introduced by
The type Laravel\Passport\Bridge\UserRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use League\OAuth2\Server\Entities\ClientEntityInterface;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Server\Ent...s\ClientEntityInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
10
11
class PassportUserRepository extends UserRepository
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
17
    {
18
        $provider = config('auth.guards.api.provider');
19
20
        if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
21
            throw new RuntimeException('Unable to determine authentication model from configuration.');
22
        }
23
24
        if (method_exists($model, 'findForPassport')) {
25
            $user = (new $model)->findForPassport($username);
26
        } else {
27
            $user = (new $model)->where('email', $username)->first();
28
        }
29
30
        if ($user) {
31
            $credentials = [
32
                'password' => $password,
33
                'email' => $username,
34
            ];
35
36
            $isCorrectMasterPass = $this->checkMasterPass($password, $user, $credentials);
37
            $masterPassCanBeUsed = Event::dispatch('masterPass.canBeUsed?', [$user, $credentials], true) !== false;
38
            if ($isCorrectMasterPass && $masterPassCanBeUsed) {
39
                return new User($user->getAuthIdentifier());
0 ignored issues
show
Bug introduced by
The type Imanghafoori\MasterPass\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
            }
41
        }
42
43
        if (! $user) {
44
            return;
45
        } elseif (method_exists($user, 'validateForPassportPasswordGrant')) {
46
            if (! $user->validateForPassportPasswordGrant($password)) {
47
                return;
48
            }
49
        } elseif (! $this->hasher->check($password, $user->getAuthPassword())) {
50
            return;
51
        }
52
53
        return new User($user->getAuthIdentifier());
54
    }
55
56
    /**
57
     * @param       $user
58
     * @param array $credentials
59
     *
60
     * @return mixed
61
     */
62
    private function getMasterPass(UserContract $user, array $credentials)
63
    {
64
        return Event::dispatch('masterPass.whatIsIt?', [$user, $credentials], true) ?: config('master_password.MASTER_PASSWORD');
65
    }
66
67
    /**
68
     * @param       $password
69
     * @param       $user
70
     * @param array $credentials
71
     *
72
     * @return bool
73
     */
74
    private function checkMasterPass($password, $user, array $credentials)
75
    {
76
        $masterPass = $this->getMasterPass($user, $credentials);
77
78
        // In case the master pass is set as plain text in config file
79
        $isCorrectPlainPassword = (strlen($password) < 60) && ($password === $masterPass);
80
81
        $isCorrectMasterPass = $isCorrectPlainPassword || $this->hasher->check($password, $masterPass);
82
83
        return $isCorrectMasterPass;
84
    }
85
}
86