Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.33% |
4 / 12 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| AddBasicAuthContextBuilder | |
33.33% |
4 / 12 |
|
0.00% |
0 / 1 |
16.67 | |
0.00% |
0 / 1 |
| process | |
33.33% |
4 / 12 |
|
0.00% |
0 / 1 |
16.67 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\BasicAuth; |
| 3 | |
| 4 | use Apie\Common\LoginService; |
| 5 | use Apie\Core\Context\ApieContext; |
| 6 | use Apie\Core\ContextBuilders\ContextBuilderInterface; |
| 7 | use Apie\Core\ContextConstants; |
| 8 | use Psr\Http\Message\ServerRequestInterface; |
| 9 | |
| 10 | class AddBasicAuthContextBuilder implements ContextBuilderInterface |
| 11 | { |
| 12 | public function process(ApieContext $context): ApieContext |
| 13 | { |
| 14 | $request = $context->getContext(ServerRequestInterface::class, false); |
| 15 | $loginService = $context->getContext(LoginService::class, false); |
| 16 | if ($request instanceof ServerRequestInterface && $loginService instanceof LoginService) { |
| 17 | $authHeader = $request->getHeaderLine('Authorization'); |
| 18 | |
| 19 | if (str_starts_with($authHeader, 'Basic ')) { |
| 20 | $encodedCredentials = substr($authHeader, 6); |
| 21 | $decoded = base64_decode($encodedCredentials, true); |
| 22 | |
| 23 | if ($decoded !== false && str_contains($decoded, ':')) { |
| 24 | [$username, $password] = explode(':', $decoded, 2); |
| 25 | $user = $loginService->authorize($username, $password); |
| 26 | $context = $context->withContext(ContextConstants::AUTHENTICATED_USER, $user); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | return $context; |
| 32 | } |
| 33 | } |