Passed
Push — 2.x ( 5d0da9...0013c1 )
by Terry
04:10 queued 22s
created

HttpAuthentication::set()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.5555
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Middleware;
24
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\MiddlewareInterface;
28
use Psr\Http\Server\RequestHandlerInterface;
29
use Shieldon\Psr7\Response;
30
use InvalidArgumentException;
31
use function password_verify;
32
use function strpos;
33
34
/**
35
 * A PSR-15 middleware that provides WWW-Authenticate protection.
36
 */
37
class HttpAuthentication implements MiddlewareInterface
38
{
39
    /**
40
     * 401 - Unauthorized.
41
     *
42
     * @var int
43
     */
44
    const HTTP_STATUS_CODE = 401;
45
46
    /**
47
     * The URL list that you want to protect.
48
     *
49
     * @var array
50
     */
51
    protected $protectedUrlList = [
52
        [
53
            // Begin-with URL 
54
            'url' => '/wp-amdin', 
55
56
            // Username
57
            'user' => 'wp_shieldon_user',
58
59
            // Password encrypted by `password_hash()` function.
60
            // In this case, the uncrypted string is `wp_shieldon_pass`.
61
            'pass' => '$2y$10$eA/S6rH3JDkYV9nrrUvuMOTh8Q/ts33DdCerbNAUpdwtSl3Xq9cQq'  
62
        ]
63
    ];
64
65
    /**
66
     * The text displays on prompted window.
67
     * Most modern browsers won't show this anymore. You can ignore that.
68
     *
69
     * @var string
70
     */
71
    protected $realm;
72
73
    /**
74
     * Constructor.
75
     * 
76
     * @param array  $protectedUrlList The list that want to be protected.
77
     * @param string $realm            The welcome message.
78
     *
79
     * @return void
80
     */
81
    public function __construct(array $protectedUrlList = [], string $realm = 'Welcome to area 51.')
82
    {
83
        $this->set($protectedUrlList);
84
        $this->realm = $realm;
85
    }
86
87
    /**
88
     * Invoker.
89
     *
90
     * @param ServerRequestInterface  $request The PSR-7 server request.
91
     * @param RequestHandlerInterface $handler The PSR-15 request handler.
92
     *
93
     * @return ResponseInterface
94
     */
95
    public function process(ServerRequestInterface  $request, RequestHandlerInterface $handler): ResponseInterface
96
    {
97
        $currentUrl = $request->getUri()->getPath();
98
        $serverParams = $request->getServerParams();
99
100
        foreach ($this->protectedUrlList as $urlInfo) {
101
102
            // If we have set the protection for current URL.
103
            if (0 === strpos($currentUrl, $urlInfo['url'])) {
104
105
                // Prompt a window to ask for username and password.
106
                if (
107
                    !isset($serverParams['PHP_AUTH_USER']) ||
108
                    !isset($serverParams['PHP_AUTH_PW'])
109
                ) {
110
                    $authenticate = 'Basic realm="' . $this->realm . '"';
111
                    return (new Response)->
112
                        withStatus(self::HTTP_STATUS_CODE)->
113
                        withHeader('WWW-Authenticate', $authenticate);
114
                }
115
116
                // Identify the username and password for current URL.
117
                if (
118
                    $urlInfo['user'] !== $serverParams['PHP_AUTH_USER'] ||
119
                    !password_verify($serverParams['PHP_AUTH_PW'], $urlInfo['pass'])
120
                ) {
121
                    unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
122
                    return (new Response)->withStatus(self::HTTP_STATUS_CODE);
123
                }
124
            }
125
        }
126
127
        return $handler->handle($request);
128
    }
129
130
    /**
131
     * Set up the URL list that you want to protect.
132
     * 
133
     * @param $protectedUrlList
134
     *
135
     * @return void
136
     */
137
    public function set(array $protectedUrlList = []): void
138
    {
139
        if (!empty($protectedUrlList)) {
140
            $count = count($protectedUrlList);
141
            $urlCount = count(array_column($protectedUrlList, 'url'));
142
            $userCount = count(array_column($protectedUrlList, 'user'));
143
            $passCount = count(array_column($protectedUrlList, 'pass'));
144
145
            if (
146
                $count !== $urlCount || 
147
                $count !== $userCount || 
148
                $count !== $passCount
149
            ) {
150
                throw new InvalidArgumentException(
151
                    'The columns in the array should be fit the specification. '
152
                );
153
            }
154
155
            $this->protectedUrlList = $protectedUrlList;
156
        }
157
    }
158
}
159
160