Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| File | |
100.00% |
36 / 36 |
|
100.00% |
8 / 8 |
22 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRealm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDelimiter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setFilename | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| setRealm | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setDelimiter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| authenticate | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Auth; |
| 15 | |
| 16 | /** |
| 17 | * File auth class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Auth |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 4.0.2 |
| 25 | */ |
| 26 | class File extends AbstractAuth |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Auth file |
| 31 | * @var ?string |
| 32 | */ |
| 33 | protected ?string $filename = null; |
| 34 | |
| 35 | /** |
| 36 | * Auth realm |
| 37 | * @var ?string |
| 38 | */ |
| 39 | protected ?string $realm = null; |
| 40 | |
| 41 | /** |
| 42 | * Auth file delimiter |
| 43 | * @var string |
| 44 | */ |
| 45 | protected string $delimiter = ':'; |
| 46 | |
| 47 | /** |
| 48 | * Constructor |
| 49 | * |
| 50 | * Instantiate the File auth adapter object |
| 51 | * |
| 52 | * @param string $filename |
| 53 | * @param ?string $realm |
| 54 | * @param string $delimiter |
| 55 | * @throws Exception |
| 56 | */ |
| 57 | public function __construct(string $filename, ?string $realm = null, string $delimiter = ':') |
| 58 | { |
| 59 | $this->setFilename($filename); |
| 60 | if (!empty($realm)) { |
| 61 | $this->setRealm($realm); |
| 62 | } |
| 63 | if (!empty($delimiter)) { |
| 64 | $this->setDelimiter($delimiter); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the auth filename |
| 70 | * |
| 71 | * @return string |
| 72 | */ |
| 73 | public function getFilename(): string |
| 74 | { |
| 75 | return $this->filename; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the auth realm |
| 80 | * |
| 81 | * @return ?string |
| 82 | */ |
| 83 | public function getRealm(): ?string |
| 84 | { |
| 85 | return $this->realm; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the auth file delimiter |
| 90 | * |
| 91 | * @return string |
| 92 | */ |
| 93 | public function getDelimiter(): string |
| 94 | { |
| 95 | return $this->delimiter; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Set the auth filename |
| 100 | * |
| 101 | * @param string $filename |
| 102 | * @throws Exception |
| 103 | * @return File |
| 104 | */ |
| 105 | public function setFilename(string $filename): File |
| 106 | { |
| 107 | if (!file_exists($filename)) { |
| 108 | throw new Exception("The access file '" . $filename . "' does not exist."); |
| 109 | } |
| 110 | $this->filename = $filename; |
| 111 | return $this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set the auth realm |
| 116 | * |
| 117 | * @param string $realm |
| 118 | * @return File |
| 119 | */ |
| 120 | public function setRealm(string $realm): File |
| 121 | { |
| 122 | $this->realm = $realm; |
| 123 | return $this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Set the auth file delimiter |
| 128 | * |
| 129 | * @param string $delimiter |
| 130 | * @return File |
| 131 | */ |
| 132 | public function setDelimiter(string $delimiter): File |
| 133 | { |
| 134 | $this->delimiter = $delimiter; |
| 135 | return $this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Method to authenticate |
| 140 | * |
| 141 | * @param string $username |
| 142 | * @param string $password |
| 143 | * @return int |
| 144 | */ |
| 145 | public function authenticate(string $username, string $password): int |
| 146 | { |
| 147 | $this->setUsername($username); |
| 148 | $this->setPassword($password); |
| 149 | |
| 150 | $lines = file($this->filename); |
| 151 | $hash = null; |
| 152 | $this->result = 0; |
| 153 | |
| 154 | foreach ($lines as $line) { |
| 155 | $line = trim($line); |
| 156 | $user = explode($this->delimiter, $line); |
| 157 | if (isset($user[0]) && ($user[0] == $this->username)) { |
| 158 | if (($this->realm !== null) && (count($user) == 3)) { |
| 159 | if (($this->username == $user[0]) && ($user[1] == $this->realm)) { |
| 160 | $hash = $user[2]; |
| 161 | break; |
| 162 | } |
| 163 | } else if (count($user) == 2) { |
| 164 | if ($this->username == $user[0]) { |
| 165 | $hash = $user[1]; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (($this->password !== null) && ($hash !== null)) { |
| 173 | $this->result = (int)$this->verify($this->password, $hash); |
| 174 | } |
| 175 | |
| 176 | return $this->result; |
| 177 | } |
| 178 | |
| 179 | } |