Basic   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 130
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A realm() 0 6 1
A verify() 0 6 1
A deny() 0 6 1
B auth() 0 21 6
A getUser() 0 4 1
A getPassword() 0 4 1
1
<?php
2
3
namespace Uauth;
4
5
class Basic
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $user;
11
12
    /**
13
     * @var string
14
     */
15
    protected $password;
16
17
    /**
18
     * @var string
19
     */
20
    protected $realm;
21
22
    /**
23
     * @var callable
24
     */
25
    protected $verify;
26
27
    /**
28
     * @var callable
29
     */
30
    protected $deny;
31
32
    /**
33
     * Constructor.
34
     */
35
    public function __construct($realm = "Secured Area", array $allowedUsers = array())
36
    {
37
        $this->realm  = $realm;
38
        $this->verify = function ($user, $password) use ($allowedUsers) {
39
            return isset($allowedUsers[$user]) && $allowedUsers[$user] == $password;
40
        };
41
    }
42
43
    /**
44
     * Set realm
45
     * @param  string $realm
46
     *
47
     * @return Basic The current instance
48
     */
49
    public function realm($realm)
50
    {
51
        $this->realm = $realm;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Set the user verification system
58
     *
59
     * @param callable $verify
60
     *
61
     * @return Basic
62
     */
63
    public function verify(callable $verify)
64
    {
65
        $this->verify = $verify;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Set the callable executed on deny by the verification
72
     *
73
     * @param callable $deny
74
     *
75
     * @return Basic
76
     */
77
    public function deny(callable $deny)
78
    {
79
        $this->deny = $deny;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Process the basic auth
86
     *
87
     * @return Basic
88
     */
89
    public function auth()
90
    {
91
        $user     = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null;
92
        $password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null;
93
94
        if (is_null($user) || !(bool) call_user_func($this->verify, $user, $password)) {
95
            header(sprintf('WWW-Authenticate: Basic realm="%s"', $this->realm));
96
            header('HTTP/1.0 401 Unauthorized');
97
98
            if ($this->deny) {
99
                call_user_func($this->deny, $user);
100
            }
101
102
            exit;
103
        }
104
105
        $this->user     = $user;
106
        $this->password = $password;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Get the verified user
113
     *
114
     * Only available after the auth method
115
     *
116
     * @return string
117
     */
118
    public function getUser()
119
    {
120
        return $this->user;
121
    }
122
123
    /**
124
     * Get the password of the verified user
125
     *
126
     * Only available after the auth method
127
     *
128
     * @return string
129
     */
130
    public function getPassword()
131
    {
132
        return $this->password;
133
    }
134
}
135