github github
  • Home
  • Pricing and Signup
  • Training
  • Gist
  • Blog
  • Login

diogok / restserver

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 6
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Tree: b1e641c

click here to add a description

click here to add a homepage

  • Switch Branches (1)
    • master
  • Switch Tags (0)
  • Branch List
Sending Request…

RESTful resource/url mapping and MVC, featuring Request an Response objects. — Read more

  Cancel

http://www.phpclasses.org/browse/package/5080.html

  Cancel
  • HTTP
  • Git Read-Only

This URL has Read+Write access

Fixed redundant methods RestRequest::getInput and getBody and RestResponse::addResponse appendResponse, now they chain call the newer method. Did not removed duplicated due to backward compatibility and stuff like that. This is my longest commit message so far... 
diogok (author)
Thu Aug 12 17:07:00 -0700 2010
commit  b1e641c05d5d507d5810
tree    4326a6d862cc927adfc9
parent  330617e443421d5647aa
restserver / RestAuthenticator.class.php RestAuthenticator.class.php
Txt 100755 243 lines (214 sloc) 6.763 kb
  • edit
  • raw
  • blame
  • history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php

/**
* Class RestAuthenticator
* Responsible for dealing with both Basic and Digest authentication
*/
class RestAuthenticator {

    private $rest ;
    
    private $user ;
    private $pwd ;
    private $authData ;
    private $isDigest =false;
    private $requireAuth =false;
    private $auth;
    private $realm;

    /**
* RestAuthenticator constructor
* @param RestServer $rest
*/
    public function __construct(RestServer $rest=null) {
        $this->rest = $rest ;

        if(isset($_SERVER['PHP_AUTH_DIGEST']))
            $this->authData = $_SERVER['PHP_AUTH_DIGEST'] ;

        if(isset($_SERVER['PHP_AUTH_USER']))
            $this->user = $_SERVER["PHP_AUTH_USER"];

        if(isset($_SERVER['PHP_AUTH_PW']))
            $this->pwd = $_SERVER["PHP_AUTH_PW"];

        if(isset($_SERVER["HTTP_AUTHORIZATION"])) {
            $base = base64_decode(substr($_SERVER["HTTP_AUTHORIZATION"],6));
            $arr = explode(":",$base);
            $this->user = $arr[0];
            $this->pwd = $arr[1];
        }
        
        if (!empty($this->authData) && ($data = $this->digestParse($this->authData)) && $data['username']) {
            $this->user = $data['username'] ;
            $this->pwd = $data['response'] ;
        }
    }
    
    /**
* Return internal RestServer
* Return RestServer used;
* @return RestServer
*/
    public function getRest() {
        return $this->rest;
    }
    
    /**
* Return user sent on BASIC Authentication
* @return string
*/
    public function getUser() {
        return $this->user;
    }

    /**
* Return password sent for Authentication
* @return string
*/
    public function getPassword() {
        return $this->pwd ;
    }
    
    /**
* Return if is using digest authentication
* @return bool
*/
    public function isDigest() {
        return $this->isDigest ;
    }

    /**
* Set if authentication should be Digest(true)
* @param bool $bool
* @param string $realm
* @return RestAuthenticator
*/
    public function forceDigest($bool=true,$realm=null) {
        if($realm != null) $this->setRealm($realm);
        $this->isDigest = $bool;
        if($bool) {$this->requireAuthentication(true);}
        return $this;
    }

    /**
* Get the http Realm name
* @return string $realm
*/
    public function getRealm() {
        return $this->realm;
    }

    /**
* Set the http Realm name
* @param string $realm
* @return RestAuthenticator
*/
    public function setRealm($realm) {
        $this->realm = $realm ;
        return $this;
    }

    /**
* Sets if authentication is required
* @param bool $isRequered
* @return RestAuthenticator
*/
    public function requireAuthentication($isRequired=true) {
        if($isRequired !== null) $this->requireAuth = $isRequired ;
        return $this ;
    }
    
    /**
* Checks if authenticated is required
* @return bool $auth;
*/
    public function isAuthenticationRequired() {
        return $this->requireAuth ;
    }

    /**
* Checks if is authenticated
* @return bool $auth;
*/
    public function isAuthenticated() {
        return $this->auth ;
    }

    /**
* Sets authentication status
* @param bool $auth Status
* @return RestServer
*/
    public function setAuthenticated($bool) {
        $this->auth = $bool;
        return $this ;
    }
    
    /**
* Test if user is authenticated, and set proper headers if not
* @return bool
*/
    public function tryAuthenticate() {
        if($this->isAuthenticationRequired() === false) return true;
        if($this->isAuthenticated() == false) {
            $this->getRest()->getResponse()->cleanHeader();
            $this->getRest()->getResponse()->addHeader("HTTP/1.1 401 Unauthorized");
            if($this->isDigest()) {
                $this->getRest()->getResponse()->addHeader('WWW-Authenticate: Digest ' . $this->digestHeader());
            } else {
                $this->getRest()->getResponse()->addHeader('WWW-Authenticate: Basic realm="'.$this->getRealm().'"');
            }
            $this->getRest()->getResponse()->setResponse("Unauthorized");
            return false ;
        }
        return true ;
    }
    
    /**
* Test authentication against password for given username in Digest
* @param string $user
* @param string $password
* @return RestAuthenticator
*/
    public function validate($user,$password) {
        if($this->isDigest()) {
            $data = $this->digestParse($this->authData);
            $A1 = md5($this->getUser() . ':' . $this->getRealm() . ':' . $password);
            $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$_SERVER['REQUEST_URI']);
            $response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
            if($this->getUser() === $user && $this->getPassword() === $response) {
                $this->pwd = $password ;
                $this->setAuthenticated(true);
            } else {
                $this->setAuthenticated(false);
            }
        } else {
            if($this->getUser() === $user && $this->getPassword() === $password) {
                $this->setAuthenticated(true);
            } else {
                $this->setAuthenticated(false);
            }
        }
        return $this;
    }
    
    /**
* Parse the digest auth message
* @param string $message
* @return mixed
*/
    private function digestParse($txt) {
        $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
        $data = array();
        $parts = explode(",",$txt);
        foreach($parts as $part) {
            $div = strpos($part,"=");
            $name = trim(substr($part,0,$div));
            $value = trim(substr($part,$div + 1));
            if($value[0] == "\"") {
                $value = substr($value,1, strlen($value) - 2);
            }
            unset($needed_parts[$name]);
            $data[$name] = $value;
        }

        return $needed_parts ? false : $data;
    }
    
    /**
* Digest header
* @return string
*/
    private function digestHeader() {
        $op = array(
            'realm' => $this->getRealm(),
            'domain' => '/',
            'qop' => 'auth',
            'algorithm' => 'MD5',
            'nonce' => uniqid(),
            'opaque' => md5($this->getRealm()),
            );
        $str = 'realm="'.$op['realm'].'",';
        $str .= 'qop="'.$op['qop'].'",';
        $str .= 'nonce="'.$op['nonce'].'",';
        $str .= 'opaque="'.$op['opaque'].'"';
        return $str;
    }

    
}

?>

Dedicated Server Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
  • Blog
  • Support
  • Training
  • Job Board
  • Shop
  • Contact
  • API
  • Status
  • © 2010 GitHub Inc. All rights reserved.
  • Terms of Service
  • Privacy
  • Security
  • English
  • Deutsch
  • Français
  • 日本語
  • Português (BR)
  • 中文
  • See all available languages

Your current locale selection: English. Choose another?

  • English
  • Afrikaans
  • Català
  • Čeština
  • Deutsch
  • Español
  • Français
  • Hrvatski
  • Indonesia
  • Italiano
  • 日本語
  • Nederlands
  • Norsk
  • Polski
  • Português (BR)
  • Српски
  • Svenska
  • 中文