YRC   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A checkAccessHeader() 0 17 4
1
<?php
2
3
namespace yrc\components;
4
5
use Yii;
6
use yii\base\BaseObject;
7
8
/**
9
 * Yii2 Rest Component
10
 * @class YRC
11
 */
12
class YRC extends BaseObject
13
{
14
    /**
15
     * The access header
16
     * If set access to controller actions is granted if and only if the HTTP header value
17
     * identified by this parameters equals the $accessHeaderSecret property
18
     * @return mixed
19
     */
20
    public $accessHeader;
21
22
    /**
23
     * The access header secret value
24
     * @return mixed
25
     */
26
    public $accessHeaderSecret;
27
28
    /**
29
     * Helper method to check the access header
30
     * @return boolean
31
     */
32
    public function checkAccessHeader($request)
33
    {
34
        // Both the access header and access header secret must be set for this check to validate
35
        if ($this->accessHeader === null || $this->accessHeaderSecret === null) {
36
            return true;
37
        }
38
39
        // Fetch the access header from the request
40
        $header = $request->getHeaders()->get($this->accessHeader);
41
42
        // Allow if the header values match
43
        if (\hash_equals($this->accessHeaderSecret, $header)) {
44
            return true;
45
        }
46
        
47
        // Deny by default
48
        return false;
49
    }
50
}
51