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

EqualsProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 8 1
A verify() 0 16 3
A create() 0 19 1
A remove() 0 12 1
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
16
/**
17
 * EqualsProvider
18
 *
19
 * @package  Application\Auth
20
 * @author   Anton Shevchuk
21
 */
22
class EqualsProvider
23
{
24
    /**
25
     * Authenticate user by login/pass
26
     *
27
     * @param string $username
28
     * @param string $password
29
     *
30
     * @throws AuthException
31
     * @throws Exception
32
     */
33
    public static function authenticate($username, $password)
34
    {
35
        $authRow = self::verify($username, $password);
36
        $user = UsersTable::findRow($authRow->userId);
37
38
        // try to login
39
        Table::tryLogin($user);
40
    }
41
42
    /**
43
     * Get Auth record by login/pass
44
     *
45
     * @param string $username
46
     * @param string $password
47
     *
48
     * @return Row
49
     * @throws AuthException
50
     * @throws Exception
51
     */
52
    public static function verify($username, $password) : Row
53
    {
54
        /* @var Row $authRow */
55
        $authRow = Table::findRowWhere(['foreignKey' => $username, 'provider' => Table::PROVIDER_EQUALS]);
56
57
        if (!$authRow) {
58
            throw new AuthException('User can\'t login with password');
59
        }
60
61
        // verify password
62
        if (!Table::verify($password, $authRow->token)) {
63
            throw new AuthException('Wrong password');
64
        }
65
66
        return $authRow;
67
    }
68
69
    /**
70
     * Create new Auth record for user
71
     *
72
     * @param UsersRow $user
73
     * @param string   $password
74
     *
75
     * @return Row
76
     * @throws AuthException
77
     * @throws Exception
78
     */
79
    public static function create($user, $password)
80
    {
81
        // remove old Auth record
82
        self::remove($user->id);
83
84
        // create new auth row
85
        $row = new Row();
86
        $row->userId = $user->id;
87
        $row->foreignKey = $user->login;
88
        $row->provider = Table::PROVIDER_EQUALS;
89
        $row->tokenType = Table::TYPE_ACCESS;
90
91
        // generate secret part is not required
92
        // encrypt password and save as token
93
        $row->token = Table::hash($password);
94
        $row->save();
95
96
        return $row;
97
    }
98
99
    /**
100
     * Remove Auth record
101
     *
102
     * @param integer $id
103
     *
104
     * @return void
105
     */
106
    public static function remove($id)
107
    {
108
        // clear previous generated Auth record
109
        // works with change password
110
        Table::delete(
111
            [
112
                'userId' => $id,
113
                'provider' => Table::PROVIDER_EQUALS,
114
                'tokenType' => Table::TYPE_ACCESS
115
            ]
116
        );
117
    }
118
}
119