HttpBasicAuthenticator::unauthorizedChallenge()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12
 * @link          http://cakephp.org CakePHP(tm) Project
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
16
declare(strict_types=1);
17
18
namespace Phauthentic\Authentication\Authenticator;
19
20
use ArrayAccess;
21
use Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException;
22
use Phauthentic\Authentication\Identifier\IdentifierInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
25
/**
26
 * HttpBasic Authenticator
27
 *
28
 * Provides Basic HTTP authentication support.
29
 */
30
class HttpBasicAuthenticator extends AbstractAuthenticator implements StatelessInterface
31
{
32
    use CredentialFieldsTrait;
33
34
    /**
35
     * Realm
36
     *
37
     * @var string|null
38
     */
39
    protected $realm;
40
41
    /**
42
     * Sets the realm
43
     *
44
     * @param string|null $realm Realm
45 56
     * @return $this
46
     */
47 56
    public function setRealm(?string $realm): self
48
    {
49 56
        $this->realm = $realm;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Authenticate a user using HTTP auth. Will use the configured User model and attempt a
56
     * login using HTTP auth.
57
     *
58
     * @param \Psr\Http\Message\ServerRequestInterface $request The request to authenticate with.
59 28
     * @return \Phauthentic\Authentication\Authenticator\ResultInterface
60
     */
61 28
    public function authenticate(ServerRequestInterface $request): ResultInterface
62
    {
63 28
        $user = $this->getUser($request);
64 20
65
        if (empty($user)) {
66
            return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
67 8
        }
68
69
        return new Result($user, Result::SUCCESS);
70
    }
71
72
    /**
73
     * Checks for the user and password in the server request params
74
     *
75
     * @param array<string, mixed> $serverParams Server params from \Psr\Http\Message\ServerRequestInterface::getServerParams()
76 28
     * @return bool
77
     */
78 28
    protected function checkServerParams(array $serverParams): bool
79 20
    {
80 16
        return !isset($serverParams['PHP_AUTH_USER'], $serverParams['PHP_AUTH_PW'])
81 16
            || !is_string($serverParams['PHP_AUTH_USER'])
82 16
            || $serverParams['PHP_AUTH_USER'] === ''
83 28
            || !is_string($serverParams['PHP_AUTH_PW'])
84
            || $serverParams['PHP_AUTH_PW'] === '';
85
    }
86
87
    /**
88
     * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
89
     *
90
     * @param \Psr\Http\Message\ServerRequestInterface $request Request object.
91
     * @return \ArrayAccess|null User entity or null on failure.
92 28
     */
93
    public function getUser(ServerRequestInterface $request): ?ArrayAccess
94 28
    {
95 28
        $serverParams = $request->getServerParams();
96 12
        if ($this->checkServerParams($serverParams)) {
97
            return null;
98
        }
99 16
100 16
        return $this->identifier->identify([
101 16
            IdentifierInterface::CREDENTIAL_USERNAME => $serverParams['PHP_AUTH_USER'],
102
            IdentifierInterface::CREDENTIAL_PASSWORD => $serverParams['PHP_AUTH_PW'],
103
        ]);
104
    }
105
106
    /**
107
     * Create a challenge exception for basic auth challenge.
108
     *
109
     * @param \Psr\Http\Message\ServerRequestInterface $request A request object.
110
     * @return void
111
     * @throws \Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException
112 20
     */
113
    public function unauthorizedChallenge(ServerRequestInterface $request): void
114 20
    {
115
        throw new UnauthorizedException($this->loginHeaders($request), '');
116
    }
117
118
    /**
119
     * Generate the login headers
120
     *
121
     * @param \Psr\Http\Message\ServerRequestInterface $request Request object.
122
     * @return array<string, string> Headers for logging in.
123 8
     */
124
    protected function loginHeaders(ServerRequestInterface $request): array
125 8
    {
126 8
        $server = $request->getServerParams();
127
        $realm = $this->realm ?: $server['SERVER_NAME'];
128 8
129
        return ['WWW-Authenticate' => sprintf('Basic realm="%s"', $realm)];
130
    }
131
}
132