[go: up one dir, main page]

Menu

[3822d7]: / core / lib / Http.php  Maximize  Restore  History

Download this file

167 lines (141 with data), 4.5 kB

  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
<?php
namespace ICT\Core;
/* * ***************************************************************
* Copyright © 2015 ICT Innovations Pakistan All Rights Reserved *
* Developed By: Nasir Iqbal *
* Website : http://www.ictinnovations.com/ *
* Mail : nasir@ictinnovations.com *
* *************************************************************** */
use Jacwright\RestServer\AuthServer;
use Jacwright\RestServer\RestException;
class Http extends Data implements AuthServer
{
/**
* @var Http
*/
protected static $_instance = null;
/**
*
* @param string $realm
*/
protected $realm = '';
public function __construct(&$data = array())
{
parent::__construct($data);
$this->realm = Conf::get('website:title', 'ICTCore') . ' :: REST API Server';
$this->server = $_SERVER;
$this->request = $_REQUEST;
}
/**
* @staticvar boolean $initialized
* @return Http
*/
public static function get_instance()
{
static $initialized = FALSE;
if (!$initialized) {
self::$_instance = new self;
$initialized = TRUE;
}
return self::$_instance;
}
public static function set($name, $value)
{
$_instance = self::get_instance();
$_instance->__set($name, $value);
}
public static function &get($name, $default = NULL)
{
$_instance = self::get_instance();
$value = &$_instance->__get($name);
if (NULL === $value) {
return $default;
}
return $value;
}
public static function request_get($name, $default = null)
{
return self::get("request:$name", $default);
}
public static function server_get($name, $default = null)
{
return self::get("server:$name", $default);
}
public function isAuthenticated($classObj)
{
$auth_headers = $this->getAuthHeaders();
// Try to use bearer token as default
$auth_method = User::AUTH_TYPE_BEARER;
$credentials = $this->getBearer($auth_headers);
// In case bearer token is not present switch back to Basic autentication
if (empty($credentials)) {
$auth_method = User::AUTH_TYPE_BASIC;
$credentials = $this->getBasic($auth_headers);
}
if (method_exists($classObj, 'authenticate')) {
return $classObj->authenticate($credentials, $auth_method);
}
return true;
}
public function unauthenticated($path) {
header("WWW-Authenticate: Basic realm=\"$this->realm\"");
throw new RestException(401, "Invalid credentials, access is denied to $path.");
}
public function isAuthorized($classObj, $method) {
if (method_exists($classObj, 'authorize')) {
return $classObj->authorize($method);
}
return true;
}
public function unauthorized($path) {
throw new RestException(403, "You are not authorized to access $path.");
}
/**
* Get username and password from header
*/
protected function getBasic($headers) {
// mod_php
if ($this->server_get('PHP_AUTH_USER', null)) {
return array(
'username' => $this->server_get('PHP_AUTH_USER'),
'password' => $this->server_get('PHP_AUTH_PW')
);
} else { // most other servers
if (!empty($headers)) {
list ($username, $password) = explode(':',base64_decode(substr($headers, 6)));
return array('username' => $username, 'password' => $password);
}
}
return array('username' => null, 'password' => null);
}
/**
* Get access token from header
*/
protected function getBearer($headers) {
$matches = array();
if (!empty($headers)) {
if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
return $matches[1];
}
}
return null;
}
/**
* Get authorization header
*/
protected function getAuthHeaders() {
$headers = null;
if ($this->server_get('Authorization', null)) {
$headers = trim($this->server_get('Authorization'));
} else if ($this->server_get('HTTP_AUTHORIZATION', null)) { //Nginx or fast CGI
$headers = trim($this->server_get('HTTP_AUTHORIZATION'));
} else if (function_exists('apache_request_headers')) {
$requestheaders = apache_request_headers();
$RequestHeaders = array_combine(array_map('ucwords', array_keys($requestheaders)), array_values($requestheaders));
if (isset($RequestHeaders['Authorization'])) {
$headers = trim($RequestHeaders['Authorization']);
}
}
return $headers;
}
}