Failed Conditions
Pull Request — master (#321)
by Anton
23:35 queued 08:33
created

Equals::authenticate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
c 0
b 0
f 0
cc 1
crap 1
rs 10
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
declare(strict_types=1);
8
9
namespace Application\Auth\Provider;
10
11
use Application\Auth\Row;
12
use Application\Auth\Table;
13
use Application\Users\Table as UsersTable;
14
use Bluz\Auth\AuthException;
0 ignored issues
show
Bug introduced by
The type Bluz\Auth\AuthException 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...
15
16
/**
17
 * Equals Provider
18
 *
19
 * @package  Application\Auth\Provider
20
 */
21
class Equals extends AbstractProvider
22
{
23
    public const PROVIDER = Table::PROVIDER_EQUALS;
24
25
    /**
26
     * @param string $username
27
     * @param string $password
28
     *
29
     * @throws AuthException
30
     * @throws \Bluz\Db\Exception\DbException
31
     * @throws \Bluz\Db\Exception\InvalidPrimaryKeyException
32
     * @throws \Application\Exception
33
     */
34 2
    public static function authenticate($username, $password = ''): void
35
    {
36 2
        $authRow = self::verify($username, $password);
37 1
        $user = UsersTable::findRow($authRow->userId);
38
39
        // try to login
40 1
        Table::tryLogin($user);
41 1
    }
42
43
    /**
44
     * @param string $username
45
     * @param string $password
46
     *
47
     * @return Row
48
     * @throws AuthException
49
     * @throws \Application\Exception
50
     * @throws \Bluz\Db\Exception\DbException
51
     */
52 4
    public static function verify($username, $password = ''): Row
53
    {
54
        /* @var Row $authRow */
55 4
        $authRow = Table::findRowWhere(['foreignKey' => $username, 'provider' => Table::PROVIDER_EQUALS]);
56
57 4
        if (!$authRow) {
0 ignored issues
show
introduced by
$authRow is of type Application\Auth\Row, thus it always evaluated to true.
Loading history...
58
            throw new AuthException('User can\'t login with password');
59
        }
60
61
        // verify password
62 4
        if (!Table::verify($password, $authRow->token)) {
63 2
            throw new AuthException('Wrong password');
64
        }
65
66 2
        return $authRow;
67
    }
68
69
    /**
70
     * @param         $user
71
     * @param string $password
72
     *
73
     * @return Row
74
     * @throws \Application\Exception
75
     * @throws \Bluz\Db\Exception\DbException
76
     * @throws \Bluz\Db\Exception\InvalidPrimaryKeyException
77
     * @throws \Bluz\Db\Exception\TableNotFoundException
78
     */
79
    public static function create($user, $password = ''): Row
80
    {
81
        // remove old Auth record
82
        self::remove($user->id);
83
84
        // create new auth row
85
        $row = new Row();
86
        $row->userId = $user->id;
87
        $row->foreignKey = $user->login;
88
        $row->provider = Table::PROVIDER_EQUALS;
89
        $row->tokenType = Table::TYPE_ACCESS;
90
91
        // generate secret part is not required
92
        // encrypt password and save as token
93
        $row->token = Table::hash($password);
94
        $row->save();
95
96
        return $row;
97
    }
98
}
99