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