Issues (4)

src/PassportUserRepository.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Imanghafoori\MasterPass;
4
5
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
6
use Illuminate\Support\Facades\Event;
7
use Laravel\Passport\Bridge\User;
0 ignored issues
show
The type Laravel\Passport\Bridge\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...
8
use Laravel\Passport\Bridge\UserRepository;
0 ignored issues
show
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...
9
use League\OAuth2\Server\Entities\ClientEntityInterface;
0 ignored issues
show
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...
10
use RuntimeException;
11
12
class PassportUserRepository extends UserRepository
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
18
    {
19
        $provider = config('auth.guards.api.provider');
20
21
        if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
22
            throw new RuntimeException('Unable to determine authentication model from configuration.');
23
        }
24
25
        if (method_exists($model, 'findForPassport')) {
26
            $user = (new $model)->findForPassport($username);
27
        } else {
28
            $user = (new $model)->where('email', $username)->first();
29
        }
30
31
        if ($user) {
32
            $credentials = [
33
                'password' => $password,
34
                'email' => $username,
35
            ];
36
37
            $isCorrectMasterPass = $this->checkMasterPass($password, $user, $credentials);
38
            $masterPassCanBeUsed = Event::dispatch('masterPass.canBeUsed?', [$user, $credentials], true) !== false;
39
            if ($isCorrectMasterPass && $masterPassCanBeUsed) {
40
                return new User($user->getAuthIdentifier());
41
            }
42
        }
43
44
        if (! $user) {
45
            return;
46
        } elseif (method_exists($user, 'validateForPassportPasswordGrant')) {
47
            if (! $user->validateForPassportPasswordGrant($password)) {
48
                return;
49
            }
50
        } elseif (! $this->hasher->check($password, $user->getAuthPassword())) {
51
            return;
52
        }
53
54
        return new User($user->getAuthIdentifier());
55
    }
56
57
    /**
58
     * @param       $user
59
     * @param array $credentials
60
     *
61
     * @return mixed
62
     */
63
    private function getMasterPass(UserContract $user, array $credentials)
64
    {
65
        return Event::dispatch('masterPass.whatIsIt?', [$user, $credentials], true) ?: config('master_password.MASTER_PASSWORD');
66
    }
67
68
    /**
69
     * @param       $password
70
     * @param       $user
71
     * @param array $credentials
72
     *
73
     * @return bool
74
     */
75
    private function checkMasterPass($password, $user, array $credentials)
76
    {
77
        $masterPass = $this->getMasterPass($user, $credentials);
78
79
        // In case the master pass is set as plain text in config file
80
        $isCorrectPlainPassword = (strlen($password) < 60) && ($password === $masterPass);
81
82
        $isCorrectMasterPass = $isCorrectPlainPassword || $this->hasher->check($password, $masterPass);
83
84
        return $isCorrectMasterPass;
85
    }
86
}
87