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

YRC   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 3
cbo 1
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserClass() 0 4 1
A getTokenClass() 0 4 1
A checkAccessHeader() 0 18 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 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