Completed
Pull Request — master (#295)
by Anton
11:59
created

TokenProvider::authenticate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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;
10
11
use Application\Exception;
12
use Application\Users\Row as UsersRow;
13
use Application\Users\Table as UsersTable;
14
use Bluz\Auth\AuthException;
15
use Bluz\Proxy\Auth;
16
17
/**
18
 * TokenProvider
19
 *
20
 * @package  Application\Auth
21
 * @author   Anton Shevchuk
22
 */
23
class TokenProvider
24
{
25
    /**
26
     * authenticate user by token
27
     *
28
     * @param string $token
29
     *
30
     * @throws \Bluz\Auth\AuthException
31
     * @throws \Application\Exception
32
     */
33
    public static function authenticate($token)
34
    {
35
        $authRow = self::verify($token);
36
        $user = UsersTable::findRow($authRow->userId);
37
38
        // try to login
39
        $user->tryLogin();
40
    }
41
42
    /**
43
     * authenticate user by token
44
     *
45
     * @param string $token
46
     *
47
     * @throws \Bluz\Auth\AuthException
48
     * @return Row
49
     */
50
    public static function verify($token)
51
    {
52
        if (!$authRow = Table::findRowWhere(['token' => $token, 'provider' => Table::PROVIDER_TOKEN])) {
53
            throw new AuthException('Invalid token');
54
        }
55
56
        if ($authRow->expired < gmdate('Y-m-d H:i:s')) {
57
            throw new AuthException('Token has expired');
58
        }
59
60
        return $authRow;
61
    }
62
63
    /**
64
     * Create new Auth record for user
65
     *
66
     * @param UsersRow $user
67
     *
68
     * @return Row
69
     * @throws Exception
70
     */
71
    public static function create($user)
72
    {
73
        // clear previous generated Auth record
74
        self::remove($user->id);
75
76
        $ttl = Auth::getInstance()->getOption('token', 'ttl');
77
78
        // new auth row
79
        $row = new Row();
80
        $row->userId = $user->id;
81
        $row->foreignKey = $user->login;
82
        $row->provider = Table::PROVIDER_TOKEN;
83
        $row->tokenType = Table::TYPE_ACCESS;
84
        $row->expired = gmdate('Y-m-d H:i:s', time() + $ttl);
85
        $row->token = bin2hex(random_bytes(32));
86
87
        $row->save();
88
89
        return $row;
90
    }
91
92
    /**
93
     * Remove Auth record
94
     *
95
     * @param integer $id
96
     *
97
     * @return void
98
     */
99
    public static function remove($id)
100
    {
101
        // clear previous generated Auth record
102
        // works with change password
103
        Table::delete(
104
            [
105
                'userId' => $id,
106
                'provider' => Table::PROVIDER_TOKEN,
107
                'tokenType' => Table::TYPE_ACCESS
108
            ]
109
        );
110
    }
111
}
112