summaryrefslogtreecommitdiffstats
path: root/src/Auth.php
blob: 69b0952064907c684eb1fda12e2b11146ef44322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

namespace Jasny;

use Jasny\Auth\User;
use Jasny\Auth\Middleware;

/**
 * Authentication and access control
 * 
 * <code>
 * class Auth extends Jasny\Auth
 * {
 *     use Jasny\Auth\ByLevel;
 *     use Jasny\Auth\Sessions;
 * 
 *     protected $levels = [
 *       'user' => 1,
 *       'admin' => 10
 *     ];
 * 
 *     public function fetchUserById($id)
 *     {
 *         ...
 *     }
 * 
 *     public function fetchUserByUsername($username)
 *     {
 *         ...
 *     }
 * }
 * </code>
 */
abstract class Auth
{
    /**
     * Current authenticated user
     * @var User|false
     */
    protected $user;
    
    
    /**
     * Persist the current user id across requests
     * 
     * @return void
     */
    abstract protected function persistCurrentUser();
    
    /**
     * Get current authenticated user id
     * 
     * @return mixed
     */
    abstract protected function getCurrentUserId();
    
    /**
     * Fetch a user by ID
     * 
     * @param int|string $id
     * @return User|null
     */
    abstract public function fetchUserById($id);

    /**
     * Fetch a user by username
     * 
     * @param string $username
     * @return User|null
     */
    abstract public function fetchUserByUsername($username);
    
    
    /**
     * Get current authenticated user
     * 
     * @return User|null
     */
    public function user()
    {
        if (!isset($this->user)) {
            $uid = $this->getCurrentUserId();
            $this->user = $uid ? ($this->fetchUserById($uid) ?: false) : false;
        }
        
        return $this->user ?: null;
    }
    
    /**
     * Set the current user
     * 
     * @param User $user
     * @return User|null
     */
    public function setUser(User $user)
    {
        if ($user->onLogin() === false) {
            return null;
        }
        
        $this->user = $user;
        $this->persistCurrentUser();
        
        return $this->user;
    }
    
    
    /**
     * Hash a password
     * 
     * @param string $password
     * @return string
     */
    public function hashPassword($password)
    {
        if (!is_string($password) || $password === '') {
            throw new \InvalidArgumentException("Password should be a (non-empty) string");
        }
        
        return password_hash($password, PASSWORD_BCRYPT);
    }
    
    /**
     * Fetch user and verify credentials.
     * 
     * @param User|null $user
     * @param string    $password
     * @return boolean
     */
    public function verifyCredentials($user, $password)
    {
        return isset($user) && password_verify($password, $user->getHashedPassword());
    }
    
    /**
     * Login with username and password
     * 
     * @param string $username
     * @param string $password
     * @return User|null
     */
    public function login($username, $password)
    {
        $user = $this->fetchUserByUsername($username);

        if (!isset($user) || !$this->verifyCredentials($user, $password)) {
            return null;
        }
        
        return $this->setUser($user);
    }
    
    /**
     * Logout
     */
    public function logout()
    {
        $user = $this->user();
        
        if (!$user) {
            return;
        }
        
        $user->onLogout();
        
        $this->user = false;
        $this->persistCurrentUser();
    }
    
    
    /**
     * Create auth middleware interface for access control.
     *
     * @param callable $getRequiredRole
     * @return Middleware
     */
    public function asMiddleware($getRequiredRole)
    {
        return new Middleware($this, $getRequiredRole);
    }
}