1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Auth\Method; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Yiisoft\Auth\AuthenticationMethodInterface; |
10
|
|
|
use Yiisoft\Auth\IdentityInterface; |
11
|
|
|
use Yiisoft\Auth\IdentityRepositoryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* HttpBasic supports the HTTP Basic authentication method. |
15
|
|
|
* |
16
|
|
|
* In case authentication does not work like expected, make sure your web server passes |
17
|
|
|
* username and password to `$request->getServerParams()['PHP_AUTH_USER']` and `$request->getServerParams()['PHP_AUTH_PW']` |
18
|
|
|
* variables. If you are using Apache with PHP-CGI, you might need to add this line to your `.htaccess` file: |
19
|
|
|
* |
20
|
|
|
* ``` |
21
|
|
|
* RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] |
22
|
|
|
* ``` |
23
|
|
|
*/ |
24
|
|
|
final class HttpBasic implements AuthenticationMethodInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string the HTTP authentication realm |
28
|
|
|
*/ |
29
|
|
|
private string $realm = 'api'; |
30
|
|
|
/** |
31
|
|
|
* @var callable a PHP callable that will authenticate the user with the HTTP basic auth information. |
32
|
|
|
* The callable receives a username and a password as its parameters. It should return an identity object |
33
|
|
|
* that matches the username and password. Null should be returned if there is no such identity. |
34
|
|
|
* The callable will be called only if current user is not authenticated. |
35
|
|
|
* |
36
|
|
|
* If this property is not set, the username information will be considered as an access token |
37
|
|
|
* while the password information will be ignored. The {@see \Yiisoft\Auth\IdentityRepositoryInterface::findIdentityByToken()} |
38
|
|
|
* method will be called to authenticate an identity. |
39
|
|
|
*/ |
40
|
|
|
private $authenticationCallback; |
41
|
|
|
|
42
|
|
|
private IdentityRepositoryInterface $identityRepository; |
43
|
|
|
|
44
|
|
|
public function __construct(IdentityRepositoryInterface $identityRepository) |
45
|
|
|
{ |
46
|
|
|
$this->identityRepository = $identityRepository; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function authenticate(ServerRequestInterface $request): ?IdentityInterface |
50
|
|
|
{ |
51
|
|
|
[$username, $password] = $this->getAuthenticationCredentials($request); |
52
|
|
|
|
53
|
|
|
if ($this->authenticationCallback) { |
54
|
|
|
if ($username !== null || $password !== null) { |
55
|
|
|
return \call_user_func($this->authenticationCallback, $username, $password); |
56
|
|
|
} |
57
|
|
|
} elseif ($username !== null) { |
58
|
|
|
return $this->identityRepository->findIdentityByToken($username, get_class($this)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function challenge(ResponseInterface $response): ResponseInterface |
65
|
|
|
{ |
66
|
|
|
return $response->withHeader('WWW-Authenticate', "Basic realm=\"{$this->realm}\""); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function withAuthenticationCallback(callable $authenticationCallback): self |
70
|
|
|
{ |
71
|
|
|
$new = clone $this; |
72
|
|
|
$new->authenticationCallback = $authenticationCallback; |
73
|
|
|
return $new; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function withRealm(string $realm): self |
77
|
|
|
{ |
78
|
|
|
$new = clone $this; |
79
|
|
|
$new->realm = $realm; |
80
|
|
|
return $new; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Obtains authentication credentials from request. |
85
|
|
|
* |
86
|
|
|
* @param ServerRequestInterface $request |
87
|
|
|
* @return array ['username', 'password'] array. |
88
|
|
|
*/ |
89
|
|
|
private function getAuthenticationCredentials(ServerRequestInterface $request): array |
90
|
|
|
{ |
91
|
|
|
$username = $request->getServerParams()['PHP_AUTH_USER'] ?? null; |
92
|
|
|
$password = $request->getServerParams()['PHP_AUTH_PW'] ?? null; |
93
|
|
|
if ($username !== null || $password !== null) { |
94
|
|
|
return [$username, $password]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* |
98
|
|
|
* Apache with php-cgi does not pass HTTP Basic authentication to PHP by default. |
99
|
|
|
* To make it work, add the following line to to your .htaccess file: |
100
|
|
|
* |
101
|
|
|
* RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
102
|
|
|
*/ |
103
|
|
|
$headers = $request->getHeader('Authorization'); |
104
|
|
|
$authToken = !empty($headers) |
105
|
|
|
? \reset($headers) |
106
|
|
|
: $request->getServerParams()['REDIRECT_HTTP_AUTHORIZATION'] ?? null; |
107
|
|
|
if ($authToken !== null && strncasecmp($authToken, 'basic', 5) === 0) { |
108
|
|
|
$parts = array_map(static function ($value) { |
109
|
|
|
return $value === '' ? null : $value; |
110
|
|
|
}, explode(':', base64_decode(mb_substr($authToken, 6)), 2)); |
111
|
|
|
|
112
|
|
|
if (\count($parts) < 2) { |
113
|
|
|
return [$parts[0], null]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $parts; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return [null, null]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|