AbstractToken::find()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
abstract class AbstractToken extends AbstractProvider
27
{
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @throws AuthException
32
     * @throws DbException
33
     * @throws InvalidPrimaryKeyException
34
     */
35
    public static function authenticate(string $token): Row
36
    {
37
        $authRow = self::find($token);
38
39
        self::verify($authRow);
40
41
        self::login($authRow);
42
43
        return $authRow;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @return Row
50
     * @throws DbException
51
     */
52
    protected static function find(string $token): ?Row
53
    {
54
        return Table::findRowWhere(['token' => $token, 'provider' => self::PROVIDER]);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * @param Row|null $authRow
61
     * @return void
62
     * @throws AuthException
63
     */
64
    protected static function verify(?Row $authRow): void
65
    {
66
        if (!$authRow) {
67
            throw new AuthException('Invalid token');
68
        }
69
70
        if ($authRow->expired < gmdate('Y-m-d H:i:s')) {
71
            throw new AuthException('Token has expired');
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @return Row
79
     * @throws DbException
80
     * @throws InvalidPrimaryKeyException
81
     * @throws TableNotFoundException
82
     */
83
    public static function create(User $user): Row
84
    {
85
        // clear previous generated Auth record
86
        self::remove((int) $user->id);
87
88
        $ttl = Auth::getInstance()->getOption('token', 'ttl');
89
90
        // new auth row
91
        $row = new Row();
92
93
        $row->userId = $user->id;
94
        $row->foreignKey = $user->login;
95
        $row->provider = self::PROVIDER;
96
        $row->tokenType = Table::TYPE_ACCESS;
97
        $row->expired = gmdate('Y-m-d H:i:s', time() + $ttl);
98
        $row->token = bin2hex(random_bytes(32));
99
100
        $row->save();
101
102
        return $row;
103
    }
104
}
105