1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Micro |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2015-2018 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Micro\Auth\Adapter\Basic; |
13
|
|
|
|
14
|
|
|
use Micro\Auth\Adapter\AbstractAdapter; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
|
17
|
|
|
abstract class AbstractBasic extends AbstractAdapter implements BasicInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Logger. |
21
|
|
|
* |
22
|
|
|
* @var LoggerInterface |
23
|
|
|
*/ |
24
|
|
|
protected $logger; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Attributes. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $attributes = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Init. |
35
|
|
|
* |
36
|
|
|
* @param LoggerInterface $logger |
37
|
|
|
*/ |
38
|
|
|
public function __construct(LoggerInterface $logger) |
39
|
|
|
{ |
40
|
|
|
$this->logger = $logger; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Authenticate. |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function authenticate(): bool |
49
|
|
|
{ |
50
|
|
|
if (!isset($_SERVER['HTTP_AUTHORIZATION'])) { |
51
|
|
|
$this->logger->debug('skip auth adapter ['.get_class($this).'], no http authorization header found', [ |
52
|
|
|
'category' => get_class($this), |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$header = $_SERVER['HTTP_AUTHORIZATION']; |
59
|
|
|
$parts = explode(' ', $header); |
60
|
|
|
|
61
|
|
|
if ('Basic' === $parts[0]) { |
62
|
|
|
$this->logger->debug('found http basic authorization header', [ |
63
|
|
|
'category' => get_class($this), |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$username = $_SERVER['PHP_AUTH_USER']; |
67
|
|
|
$password = $_SERVER['PHP_AUTH_PW']; |
68
|
|
|
|
69
|
|
|
return $this->plainAuth($username, $password); |
70
|
|
|
} |
71
|
|
|
$this->logger->warning('http authorization header contains no basic string or invalid authentication string', [ |
72
|
|
|
'category' => get_class($this), |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get attributes. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function getAttributes(): array |
84
|
|
|
{ |
85
|
|
|
return $this->attributes; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|