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

Cookie   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 91
ccs 0
cts 28
cp 0
c 1
b 0
f 0
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 9 1
A create() 0 25 1
A find() 0 3 1
A verify() 0 13 4
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
use Bluz\Proxy\Response;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Response 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...
21
use Exception;
22
23
/**
24
 * Cookie Provider
25
 *
26
 * @package  Application\Auth\Provider
27
 */
28
class Cookie extends AbstractProvider
29
{
30
    public const PROVIDER = Table::PROVIDER_COOKIE;
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @throws AuthException
36
     * @throws DbException
37
     * @throws InvalidPrimaryKeyException
38
     */
39
    public static function authenticate(string $token): Row
40
    {
41
        $authRow = self::find($token);
42
43
        self::verify($authRow);
44
45
        self::login($authRow);
46
47
        return $authRow;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @return Row
54
     * @throws DbException
55
     */
56
    protected static function find(string $token): ?Row
57
    {
58
        return Table::findRowWhere(['token' => $token, 'provider' => self::PROVIDER]);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @return Row
65
     * @throws AuthException
66
     * @throws DbException
67
     */
68
    protected static function verify(?Row $authRow): void
69
    {
70
        if (!$authRow) {
71
            throw new AuthException('User can\'t login with cookies');
72
        }
73
74
        if (strtotime($authRow->expired) < time()) {
75
            self::remove($authRow->userId);
76
            throw new AuthException('Token has expired');
77
        }
78
79
        if ($authRow->token !== hash('md5', $authRow->token . $authRow->tokenSecret)) {
80
            throw new AuthException('Incorrect token');
81
        }
82
    }
83
84
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @return Row
89
     * @throws DbException
90
     * @throws InvalidPrimaryKeyException
91
     * @throws TableNotFoundException
92
     * @throws Exception
93
     */
94
    public static function create(User $user): Row
95
    {
96
        // remove old Auth record
97
        self::remove($user->id);
98
99
        $ttl = Auth::getInstance()->getOption('cookie', 'ttl');
100
101
        // create new auth row
102
        $authRow = new Row();
103
104
        $authRow->userId = $user->id;
105
        $authRow->foreignKey = $user->login;
106
        $authRow->provider = self::PROVIDER;
107
        $authRow->tokenType = Table::TYPE_ACCESS;
108
        $authRow->expired = gmdate('Y-m-d H:i:s', time() + $ttl);
109
        // generate secret part is not required
110
        // encrypt password and save as token
111
        $authRow->token = bin2hex(random_bytes(32));
112
113
        $authRow->save();
114
115
        // Not great, not terrible
116
        Response::setCookie('Auth-Token', $authRow->token, time() + $ttl);
117
118
        return $authRow;
119
    }
120
}
121