Completed
Push — master ( bb7666...882899 )
by Oscar
02:21
created

BasicAuthentication   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 81
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsername() 0 4 1
A __invoke() 0 15 3
A checkUserPassword() 0 8 3
A parseAuthorizationHeader() 0 13 3
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Middleware;
6
use Psr7Middlewares\Utils;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Middleware to create basic http authentication.
12
 */
13
class BasicAuthentication
14
{
15
    use Utils\AuthenticationTrait;
16
17
    const KEY = 'USERNAME';
18
19
    /**
20
     * Returns the username.
21
     *
22
     * @param ServerRequestInterface $request
23
     *
24
     * @return string|null
25
     */
26
    public static function getUsername(ServerRequestInterface $request)
27
    {
28
        return Middleware::getAttribute($request, self::KEY);
29
    }
30
31
    /**
32
     * Execute the middleware.
33
     *
34
     * @param ServerRequestInterface $request
35
     * @param ResponseInterface      $response
36
     * @param callable               $next
37
     *
38
     * @return ResponseInterface
39
     */
40
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
41
    {
42
        $authorization = self::parseAuthorizationHeader($request->getHeaderLine('Authorization'));
43
44
        if ($authorization && $this->checkUserPassword($authorization['username'], $authorization['password'])) {
45
            return $next(
46
                Middleware::setAttribute($request, self::KEY, $authorization['username']),
47
                $response
48
            );
49
        }
50
51
        return $response
52
            ->withStatus(401)
53
            ->withHeader('WWW-Authenticate', 'Basic realm="'.$this->realm.'"');
54
    }
55
56
    /**
57
     * Validate the user and password.
58
     *
59
     * @param string $username
60
     * @param string $password
61
     *
62
     * @return bool
63
     */
64
    private function checkUserPassword($username, $password)
65
    {
66
        if (!isset($this->users[$username]) || $this->users[$username] !== $password) {
67
            return false;
68
        }
69
70
        return true;
71
    }
72
73
    /**
74
     * Parses the authorization header for a basic authentication.
75
     *
76
     * @param string $header
77
     *
78
     * @return false|array
79
     */
80
    private static function parseAuthorizationHeader($header)
81
    {
82
        if (strpos($header, 'Basic') !== 0) {
83
            return false;
84
        }
85
86
        $header = explode(':', base64_decode(substr($header, 6)), 2);
87
88
        return [
89
            'username' => $header[0],
90
            'password' => isset($header[1]) ? $header[1] : null,
91
        ];
92
    }
93
}
94