Completed
Push — master ( da3b9f...2a95a7 )
by Charles
02:34
created

YRC::checkAccessHeader()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 7
nc 3
nop 1
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 user class
16
     * @var string
17
     */
18
    public $user;
19
20
    /**
21
     * The token class
22
     * @var string
23
     */
24
    public $token;
25
26
    /**
27
     * The access header
28
     * If set access to controller actions is granted if and only if the HTTP header value
29
     * identified by this parameters equals the $accessHeaderSecret property
30
     * @return mixed
31
     */
32
    public $accessHeader;
33
34
    /**
35
     * The access header secret value
36
     * @return mixed
37
     */
38
    public $accessHeaderSecret;
39
40
    /**
41
     * Helper method to get the user class
42
     * @return string
43
     */
44
    public function getUserClass()
45
    {
46
        return $this->user['class'];
47
    }
48
49
     /**
50
     * Helper method to get the token class
51
     * @return string
52
     */
53
    public function getTokenClass()
54
    {
55
        return $this->token['class'];
56
    }
57
58
    /**
59
     * Helper method to check the access header
60
     * @return boolean
61
     */
62
    public function checkAccessHeader($request)
63
    {
64
        // Both the access header and access header secret must be set for this check to validate
65
        if ($this->accessHeader === null || $this->accessHeaderSecret === null) {
66
            return true;
67
        }
68
69
        // Fetch the access header from the request
70
        $header = $request->getHeaders()->get($this->accessHeader);
71
72
        // Allow if the header values match
73
        if (\hash_equals($this->accessHeaderSecret, $header)) {
74
            return true;
75
        }
76
        
77
        // Deny by default
78
        return false;
79
    }
80
}
81