Passed
Push — develop ( b02495...7b73ef )
by Anton
01:57
created

Token   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 52.17%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 79
ccs 12
cts 23
cp 0.5217
c 0
b 0
f 0
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 9 1
A find() 0 3 1
A verify() 0 8 3
A create() 0 20 1
1
<?php
2
3
/**
4
 * @copyright Bluz PHP Team
5
 * @link      https://github.com/bluzphp/skeleton
6
 */
7
8
declare(strict_types=1);
9
10
namespace Application\Auth\Provider;
11
12
use Application\Auth\Row;
13
use Application\Auth\Table;
14
use Application\Users\Row as User;
15
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...
16
use Bluz\Db\Exception\DbException;
0 ignored issues
show
Bug introduced by
The type Bluz\Db\Exception\DbException 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...
17
use Bluz\Db\Exception\InvalidPrimaryKeyException;
0 ignored issues
show
Bug introduced by
The type Bluz\Db\Exception\InvalidPrimaryKeyException 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...
18
use Bluz\Db\Exception\TableNotFoundException;
0 ignored issues
show
Bug introduced by
The type Bluz\Db\Exception\TableNotFoundException 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...
19
use Bluz\Proxy\Auth;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Auth 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...
20
21
/**
22
 * Token Provider
23
 *
24
 * @package  Application\Auth\Provider
25
 */
26
class Token extends AbstractProvider
27
{
28
    public const PROVIDER = Table::PROVIDER_TOKEN;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @throws AuthException
34
     * @throws DbException
35
     * @throws InvalidPrimaryKeyException
36
     */
37
    public static function authenticate(string $token): Row
38
    {
39
        $authRow = self::find($token);
40
41
        self::verify($authRow);
42
43
        self::login($authRow);
44
45
        return $authRow;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @return Row
52
     * @throws DbException
53
     */
54
    protected static function find(string $token): ?Row
55
    {
56
        return Table::findRowWhere(['token' => $token, 'provider' => self::PROVIDER]);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @param Row|null $authRow
63
     * @return void
64
     * @throws AuthException
65
     */
66
    protected static function verify(?Row $authRow): void
67
    {
68
        if (!$authRow) {
69
            throw new AuthException('Invalid token');
70 1
        }
71
72
        if ($authRow->expired < gmdate('Y-m-d H:i:s')) {
73 1
            throw new AuthException('Token has expired');
74
        }
75 1
    }
76
77
    /**
78 1
     * {@inheritdoc}
79 1
     *
80 1
     * @return Row
81 1
     * @throws DbException
82 1
     * @throws InvalidPrimaryKeyException
83 1
     * @throws TableNotFoundException
84 1
     */
85
    public static function create(User $user): Row
86 1
    {
87
        // clear previous generated Auth record
88 1
        self::remove((int) $user->id);
89
90
        $ttl = Auth::getInstance()->getOption('token', 'ttl');
91
92
        // new auth row
93
        $row = new Row();
94
95
        $row->userId = $user->id;
96
        $row->foreignKey = $user->login;
97
        $row->provider = self::PROVIDER;
98
        $row->tokenType = Table::TYPE_ACCESS;
99
        $row->expired = gmdate('Y-m-d H:i:s', time() + $ttl);
100
        $row->token = bin2hex(random_bytes(32));
101
102
        $row->save();
103
104
        return $row;
105
    }
106
}
107