AuthorizationGuard.php 2.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
<?php

namespace Gr\Gov\Minedu\Osteam\Slim;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

if (!function_exists('getallheaders'))
{
    function getallheaders()
    {
       $headers = '';
       foreach ($_SERVER as $name => $value)
       {
           if (substr($name, 0, 5) == 'HTTP_')
           {
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
           }
       }
       return $headers;
    }
} 

class AuthorizationGuard
{
    private $_username;
    private $_password;

    public function __construct($username, $password)
    {
        $this->_username = $username;
        $this->_password = $password;
    }

    /**
     * Check for authorization basic token
     *
     * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
     * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
     * @param  callable                                 $next     Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
    {

        $auth = true;
        $headers = getallheaders();
        if (array_key_exists('Authorization', $headers)) {
            $header = $headers['Authorization'];
            $auth_parts = [];
            if (preg_match('/^Basic (.+)$/', $header, $auth_parts) === 1) {
                if ($auth_parts[1] !== base64_encode("{$this->_username}:{$this->_password}")) {
                    $auth = [
                        "message" => "Error: Invalid Credentials"
                    ];
                }
            } else {
                $auth = [
                    "message" => "Error: Mallformed Authorization Header"
                ];
            }
        } else {
            $auth = [
                "message" => "Error: Missing Authorization Header"
            ];
        }

        if ($auth !== true) {
            return $response->withJson($auth, 401);
        } else {
            return $next($request, $response);
        }
    }
}