| 1: | <?php |
| 2: | |
| 3: | /* |
| 4: | * Library to use PortBilling events with PSR-14 event dispatch |
| 5: | */ |
| 6: | |
| 7: | namespace Porta\Psr14Event\Auth; |
| 8: | |
| 9: | use Porta\Psr14Event\EventException; |
| 10: | |
| 11: | /** |
| 12: | * Class to perform basic authentification |
| 13: | * |
| 14: | * Create an instance of class with password and username and then check Event |
| 15: | * for credentials: |
| 16: | * ``` |
| 17: | * (new AuthBasic('username','pass'))->authentificate($event) |
| 18: | * ``` |
| 19: | * |
| 20: | * @api |
| 21: | * @package Auth |
| 22: | */ |
| 23: | class AuthBasic extends Auth |
| 24: | { |
| 25: | |
| 26: | protected const AUTH_BASIC = 'Basic'; |
| 27: | |
| 28: | protected string $login; |
| 29: | protected string $password; |
| 30: | |
| 31: | /** |
| 32: | * Sets login and password for basic authentification |
| 33: | * |
| 34: | * @param string $login |
| 35: | * @param string $password |
| 36: | * @api |
| 37: | */ |
| 38: | public function __construct(string $login, string $password) |
| 39: | { |
| 40: | $this->login = $login; |
| 41: | $this->password = $password; |
| 42: | } |
| 43: | |
| 44: | /** |
| 45: | * @internal |
| 46: | */ |
| 47: | protected function check(): void |
| 48: | { |
| 49: | if (($this->authType != self::AUTH_BASIC) || |
| 50: | ($this->authValue != base64_encode($this->login . ':' . $this->password))) { |
| 51: | throw new EventException("Basic auth failed", 401); |
| 52: | } |
| 53: | } |
| 54: | } |
| 55: |