DigestAuthenticator::parse()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 44
ccs 0
cts 27
cp 0
rs 9.568
cc 4
nc 6
nop 1
crap 20
1
<?php
2
3
/**
4
 * AppserverIo\Authenticator\DigestAuthentication
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/authenticator
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Authenticator;
22
23
use AppserverIo\Psr\HttpMessage\Protocol;
24
use AppserverIo\Psr\HttpMessage\RequestInterface;
25
26
/**
27
 * Authenticator implementation for Digest Authentication support.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/appserver-io/authenticator
33
 * @link      http://www.appserver.io
34
 */
35
class DigestAuthenticator extends BasicAuthenticator
36
{
37
38
    /**
39
     * Defines the auth type which should match the client request type definition
40
     *
41
     * @var string
42
     */
43
    const AUTH_TYPE = 'Digest';
44
45
    /**
46
     * Parses the request for the necessary, authentication adapter specific, login credentials.
47
     *
48
     * @param \AppserverIo\Psr\HttpMessage\RequestInterface $request The request with the content of authentication data sent by client
49
     *
50
     * @return void
51
     */
52
    protected function parse(RequestInterface $request)
53
    {
54
55
        // load the raw login credentials
56
        $rawAuthData = $request->getHeader(Protocol::HEADER_AUTHORIZATION);
57
58
        // init data and matches arrays
59
        $data = array();
60
        $matches = array();
61
62
        // define required data
63
        $requiredData = array(
64
            'realm' => 1,
65
            'nonce' => 1,
66
            'nc' => 1,
67
            'cnonce' => 1,
68
            'qop' => 1,
69
            'username' => 1,
70
            'uri' => 1,
71
            'response' => 1
72
        );
73
74
        // prepare key for parsing logic
75
        $key = implode('|', array_keys($requiredData));
76
77
        // parse header value
78
        preg_match_all('@(' . $key . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $rawAuthData, $matches, PREG_SET_ORDER);
79
80
        // iterate all found values for header value
81
        foreach ($matches as $match) {
82
            // check if match could be found
83
            if ($match[3]) {
84
                $data[$match[1]] = $match[3];
85
            } else {
86
                $data[$match[1]] = $match[4];
87
            }
88
89
            // unset required value because we got it processed
90
            unset($requiredData[$match[1]]);
91
        }
92
93
        // set if all required data was processed
94
        $data['method'] = $this->getRequestMethod();
0 ignored issues
show
Bug introduced by
The method getRequestMethod() does not exist on AppserverIo\Authenticator\DigestAuthenticator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        /** @scrutinizer ignore-call */ 
95
        $data['method'] = $this->getRequestMethod();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
        $this->authData = $requiredData ? false : $data;
0 ignored issues
show
Bug Best Practice introduced by
The property authData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
introduced by
$requiredData is a non-empty array, thus is always true.
Loading history...
96
    }
97
98
    /**
99
     * Returns the authentication header for response to set
100
     *
101
     * @return string
102
     */
103
    public function getAuthenticateHeader()
104
    {
105
        return $this->getType() . ' realm="' . $this->configData["realm"] . '",qop="auth",nonce="' . uniqid() . '",opaque="' . md5($this->configData["realm"]) . '"';
0 ignored issues
show
Bug introduced by
The method getType() does not exist on AppserverIo\Authenticator\DigestAuthenticator. Did you maybe mean getAuthType()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        return $this->/** @scrutinizer ignore-call */ getType() . ' realm="' . $this->configData["realm"] . '",qop="auth",nonce="' . uniqid() . '",opaque="' . md5($this->configData["realm"]) . '"';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
    }
107
}
108