summaryrefslogtreecommitdiffstats
path: root/admin/survey/minify/vendor/intervention/httpauth/src/Intervention/Httpauth/DigestUser.php
blob: b274904c41f8f59f5d05beb996bbc624a04079ea (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

namespace Intervention\Httpauth;

class DigestUser implements UserInterface
{
    /**
     * Nonce to discourage cryptanalysis
     *
     * @var string
     */
    private $nonce;

    /**
     * nc
     * @var string
     */
    private $nc;

    /**
     * cnonce
     * @var string
     */
    private $cnonce;

    /**
     * quality of protection
     *
     * @var string
     */
    private $qop;

    /**
     * Name of user
     *
     * @var string
     */
    private $username;

    /**
     * Requested uri
     *
     * @var string
     */
    private $uri;

    /**
     * Response
     *
     * @var string
     */
    private $response;

    /**
     * Creates a new instance
     */
    public function __construct()
    {
        $this->parse();
    }

    /**
     * Checks for valid username & password
     *
     * @param  string  $name
     * @param  string  $password
     * @return boolean
     */
    public function isValid($name, $password, $realm)
    {
        $request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
        $u1 = md5(sprintf('%s:%s:%s', $this->username, $realm, $password));
        $u2 = md5(sprintf('%s:%s', $request_method, $this->uri));
        $response = md5(sprintf('%s:%s:%s:%s:%s:%s', $u1, $this->nonce, $this->nc, $this->cnonce, $this->qop, $u2));

        return ($response == $this->response) && ($name == $this->username);
    }

    /**
     * Parses the User Information from server variables

     * @return void
     */
    public function parse()
    {
        $digest = $this->getDigest();
        $user = array();
        $required = array('nonce' => 1, 'nc' => 1, 'cnonce' => 1, 'qop' => 1, 'username' => 1, 'uri' => 1, 'response' => 1);

        preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $digest, $matches, PREG_SET_ORDER);

        if (is_array($matches)) {
            foreach ($matches as $m) {
                $key = $m[1];
                $user[$key] = $m[2] ? $m[2] : $m[3];
                unset($required[$key]);
            }

            if (count($required) == 0) {
                $this->nonce = $user['nonce'];
                $this->nc = $user['nc'];
                $this->cnonce = $user['cnonce'];
                $this->qop = $user['qop'];
                $this->username = $user['username'];
                $this->uri = $user['uri'];
                $this->response = $user['response'];
            }
        }
    }

    /**
     * Fetch digest data from environment information
     *
     * @return string
     */
    public function getDigest()
    {
        $digest = null;

        if (isset($_SERVER['PHP_AUTH_DIGEST'])) {

            $digest = $_SERVER['PHP_AUTH_DIGEST'];

        } elseif (isset($_SERVER['HTTP_AUTHORIZATION'])) {

            if (strpos(strtolower($_SERVER['HTTP_AUTHORIZATION']), 'digest') === 0) {

                $digest = substr($_SERVER['HTTP_AUTHORIZATION'], 7);
            }
        }

        return $digest;
    }
}