Failed Conditions
Pull Request — master (#322)
by Anton
05:37 queued 03:16
created

Cookie::authenticate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
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
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 AbstractToken
29
{
30
    public const PROVIDER = Table::PROVIDER_COOKIE;
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @return Row
36
     * @throws AuthException
37
     * @throws DbException
38
     */
39
    protected static function verify(?Row $authRow): void
40
    {
41
        if (!$authRow) {
42
            throw new AuthException('User can\'t login with cookies');
43
        }
44
45
        if (strtotime($authRow->expired) < time()) {
46
            self::remove($authRow->userId);
47
            throw new AuthException('Token has expired');
48
        }
49
50
        if ($authRow->token !== hash('md5', $authRow->token . $authRow->tokenSecret)) {
51
            throw new AuthException('Incorrect token');
52
        }
53
    }
54
55
56
    /**
57
     * {@inheritdoc}
58
     *
59
     * @return Row
60
     * @throws DbException
61
     * @throws InvalidPrimaryKeyException
62
     * @throws TableNotFoundException
63
     * @throws Exception
64
     */
65
    public static function create(User $user): Row
66
    {
67
        // remove old Auth record
68
        self::remove($user->id);
69
70
        $ttl = Auth::getInstance()->getOption('cookie', 'ttl');
71
72
        // create new auth row
73
        $authRow = new Row();
74
75
        $authRow->userId = $user->id;
76
        $authRow->foreignKey = $user->login;
77
        $authRow->provider = self::PROVIDER;
78
        $authRow->tokenType = Table::TYPE_ACCESS;
79
        $authRow->expired = gmdate('Y-m-d H:i:s', time() + $ttl);
80
        // generate secret part is not required
81
        // encrypt password and save as token
82
        $authRow->token = bin2hex(random_bytes(32));
83
84
        $authRow->save();
85
86
        // Not great, not terrible
87
        Response::setCookie('Auth-Token', $authRow->token, time() + $ttl);
88
89
        return $authRow;
90
    }
91
}
92