Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractAuth
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
8 / 8
12
100.00% covered (success)
100.00%
1 / 1
 getResult
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAuthenticated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 needsRehash
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPassword
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUsername
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setPassword
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 verify
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 authenticate
n/a
0 / 0
n/a
0 / 0
0
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Auth;
16
17/**
18 * Abstract auth class
19 *
20 * @category   Pop
21 * @package    Pop\Auth
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27abstract class AbstractAuth implements AuthInterface
28{
29
30    /**
31     * Constant for auth result
32     * @var int
33     */
34    const NOT_VALID = 0;
35    const VALID     = 1;
36
37    /**
38     * Authentication result
39     * @var int
40     */
41    protected int $result = 0;
42
43    /**
44     * Whether the last verified hash should be rehashed
45     * @var bool
46     */
47    protected bool $needsRehash = false;
48
49    /**
50     * Authentication username
51     * @var ?string
52     */
53    protected ?string $username = null;
54
55    /**
56     * Authentication password
57     * @var ?string
58     */
59    protected ?string $password = null;
60
61    /**
62     * Get the authentication result
63     *
64     * @return int
65     */
66    public function getResult(): int
67    {
68        return $this->result;
69    }
70
71    /**
72     * Determine if the authentication attempt was successful
73     *
74     * @return bool
75     */
76    public function isAuthenticated(): bool
77    {
78        return ($this->result == 1);
79    }
80
81    /**
82     * Determine if the last verified hash should be rehashed
83     *
84     * @return bool
85     */
86    public function needsRehash(): bool
87    {
88        return $this->needsRehash;
89    }
90
91    /**
92     * Get the username
93     *
94     * @return ?string
95     */
96    public function getUsername(): ?string
97    {
98        return $this->username;
99    }
100
101    /**
102     * Get the password
103     *
104     * @return ?string
105     */
106    public function getPassword(): ?string
107    {
108        return $this->password;
109    }
110
111    /**
112     * Set the username
113     *
114     * @param  string $username
115     * @return AbstractAuth
116     */
117    public function setUsername(string $username): AbstractAuth
118    {
119        $this->username = $username;
120        return $this;
121    }
122
123    /**
124     * Set the password
125     *
126     * @param  string $password
127     * @return AbstractAuth
128     */
129    public function setPassword(string $password): AbstractAuth
130    {
131        $this->password = $password;
132        return $this;
133    }
134
135    /**
136     * Method to verify a password against a hash
137     *
138     * @param  string $password
139     * @param  string $hash
140     * @return bool
141     */
142    public function verify(string $password, string $hash): bool
143    {
144        $info       = password_get_info($hash);
145        $isUnhashed = (($info['algo'] == 0) && ($info['algoName'] == 'unknown'));
146        $result     = $isUnhashed ? hash_equals($hash, $password) : password_verify($password, $hash);
147
148        $this->needsRehash = $result && ($isUnhashed || password_needs_rehash($hash, PASSWORD_DEFAULT));
149
150        return $result;
151    }
152
153    /**
154     * Method to authenticate
155     *
156     * @param  string  $credential
157     * @param  ?string $secondary
158     * @return int
159     */
160    abstract public function authenticate(string $credential, ?string $secondary = null): int;
161
162}