Completed
Push — master ( 68369a...caa690 )
by Tim
07:45
created

Negotiate.php ➔ apache_request_headers()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 4
nop 0
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\AuthSource;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestData as TestData;
7
use \SimpleSAML\Module\monitor\TestSuite as TestSuite;
8
9
final class Negotiate extends \SimpleSAML\Module\monitor\TestCaseFactory
10
{
11
    /**
12
     * @var bool
13
     */
14
    private $xml = false;
15
16
    /**
17
     * @var string|null
18
     */
19
    private $keytab = null;
20
21
    /**
22
     * @var array
23
     */
24
    private $headers = array();
25
    
26
    /*
27
     * @param TestData $testData
28
     *
29
     * @return void
30
     */
31
    protected function initialize($testData)
32
    {
33
        $this->keytab = $testData->getInput('keytab');
34
        
35
        $query = \SimpleSAML\Utils\HTTP::getSelfURL();
36
        parse_str($query, $query);
37
38
        $this->xml = isSet($query['xml']) && ((bool)$query['xml'] === true);
39
        $this->headers = apache_request_headers();
40
41
        parent::initialize($testData);
42
    }
43
44
    /*
45
     * @return void
46
     */
47
    protected function invokeTest()
48
    {
49
        if ($this->xml === false) {
50
            $auth = new \KRB5NegotiateAuth($this->keytab);
51
            try {
52
                $reply = @$auth->doAuthentication();
53
            } catch (\Exception $e) {
54
                // Fallthru
55
                $this->setState(State::WARNING);
56
                $this->addMessage(State::WARNING, 'Authentication', 'Kerberos token validation', $e->getMessage());
57
                return;
58
            }
59
60
            if (!isSet($this->headers['Authorization']) || empty($this->headers['Authorization'])) {
61
                $this->setState(State::SKIPPED);
62
                $this->addMessage(State::SKIPPED, 'Authentication', 'Kerberos token validation', 'Unable to authenticate; no token provided');
63
            } else if ($reply) {
64
                $this->setState(State::OK);
65
                $this->addMessage(State::OK, 'Authentication', 'Kerberos token validation', 'Succesfully authenticated as ' . $auth->getAuthenticatedUser());
66
            } else {
67
                $this->setState(State::WARNING);
68
                $this->addMessage(State::WARNING, 'Authentication', 'Kerberos token validation', "Something went wrong");
69
            }
70
        } else {
71
            $this->setState(State::SKIPPED);
72
            $this->addMessage(State::SKIPPED, 'Authentication', 'Kerberos token validation', 'Unable to authenticate');
73
        }
74
    }
75
}
76
77
if (!function_exists('apache_request_headers')) {
78
    function apache_request_headers()
0 ignored issues
show
Coding Style introduced by
function apache_request_headers() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
apache_request_headers uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
79
    {
80
        $arh = array();
81
        $rx_http = '/\AHTTP_/';
0 ignored issues
show
Coding Style introduced by
$rx_http does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
82
        foreach ($_SERVER as $key => $val) {
83
            if (preg_match($rx_http, $key)) {
0 ignored issues
show
Coding Style introduced by
$rx_http does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
84
                $arh_key = preg_replace($rx_http, '', $key);
0 ignored issues
show
Coding Style introduced by
$arh_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
85
                $rx_matches = array();
0 ignored issues
show
Coding Style introduced by
$rx_matches does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Unused Code introduced by
$rx_matches is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
                // do some nasty string manipulations to restore the original letter case
87
                // this should work in most cases
88
                $rx_matches = explode('_', $arh_key);
0 ignored issues
show
Coding Style introduced by
$rx_matches does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
89
                if ((count($rx_matches) > 0) && (strlen($arh_key) > 2)) {
0 ignored issues
show
Coding Style introduced by
$rx_matches does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
90
                    foreach ($rx_matches as $ak_key => $ak_val) {
0 ignored issues
show
Coding Style introduced by
$rx_matches does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
91
                        $rx_matches[$ak_key] = ucfirst($ak_val);
0 ignored issues
show
Coding Style introduced by
$rx_matches does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
92
                    }
93
                    $arh_key = implode('-', $rx_matches);
0 ignored issues
show
Coding Style introduced by
$arh_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
94
                }
95
                $arh[$arh_key] = $val;
0 ignored issues
show
Coding Style introduced by
$arh_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
96
            }
97
        }
98
        return $arh;
99
    }
100
}
101