Changeset 1446 for branches/newtoken/wifidog/ws/classes/WifidogWS/V1.php
- Timestamp:
- 02/11/10 17:34:27 (2 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/newtoken/wifidog/ws/classes/WifidogWS/V1.php
r1427 r1446 43 43 * Web service V1 class 44 44 * 45 * Actions are: 45 * mandatory parameters: 46 * action: get|list|auth 47 * 48 * Each action has its own set of parameters: 49 * 46 50 * get: get some information concerning a given object, identified by its id 51 * parameters: object_class The class of the object to get 52 * object_id The id of the object 53 * fields The list of fields to fetch (absent: all the allowed fields) 54 * id_type (o) Not used yet 55 * 47 56 * list: get some informations concerning a list of objects 48 * auth: verify the users credential. 57 * parameters: object_class The class of objects to list 58 * fields The fields to list for each object 59 * parent_class (o) The class of the parent object (for the nodes of a network, the class would be network) 60 * parent_id (o) The id of the parent object 61 * 62 * auth: verify the users credential. And in part authenticate the user 63 * parameters: username The username to authenticate 64 * password The password 65 * gw_id (o) The gateway id if the request comes from a gateway 66 * gw_address (o) The gateway address as sent in the original request from gateway 67 * gw_port (o) The gateway port as sent from the original request from gateway 68 * from_ip (o) The ip of the user, as can be got from the $_SERVER['REMOTE_ADDR'] variable 69 * mac (o) The user mac as sent in the original request from gateway 49 70 * NOTE: This action DOES NOT authenticate the user on the gateway and hence, DOES NOT grant access to the internet. 50 71 * There is an authentication protocol that needs to be respected (http://dev.wifidog.org/wiki/doc/developer/WiFiDogProtocol_V1) 51 * An authentication token must be generated and the response redirects to the gateway's auth server that redirects to the portal page72 * However, this action will return the url that should be used as a next step of this protocol, so the calling system may do what it must 52 73 * 53 74 * @package WiFiDogAuthServer … … 125 146 } 126 147 148 protected function mapFields($objectClass, $infields = array()) { 149 $fields = array() ; 150 foreach($infields as $field) { 151 if (isset(self::$_allowedFields[$objectClass][$field])) 152 $fields[] = self::$_allowedFields[$objectClass][$field]; 153 else 154 $fields[] = "$field.forbidden"; 155 } 156 return $fields; 157 } 158 127 159 /** 128 160 * This function executes the action requested by the web service … … 132 164 protected function executeAction() { 133 165 if (!isset($this->_action)) { 134 throw new WSException("No action was specified. Please use GET parameter 'action=list|get|auth' to specify an action" );166 throw new WSException("No action was specified. Please use GET parameter 'action=list|get|auth' to specify an action", WSException::INVALID_PARAMETER); 135 167 } 136 168 switch($this->_action) { … … 151 183 case 'auth': 152 184 $gw_id = (isset($this->_params['gw_id']) ? $this->_params['gw_id']:null); 153 $gw_ip = (isset($this->_params['gw_ip']) ? $this->_params['gw_ip']:null); 185 $gw_address = (isset($this->_params['gw_address']) ? $this->_params['gw_address']:null); 186 $gw_port = (isset($this->_params['gw_port']) ? $this->_params['gw_port']:null); 187 $mac = (isset($this->_params['mac']) ? $this->_params['mac']:null); 188 $from = (isset($this->_params['from_ip']) ? $this->_params['from_ip']:null); 154 189 $username = (isset($this->_params['username']) ? $this->_params['username']:''); 155 190 $password = (isset($this->_params['password']) ? $this->_params['password']:''); 156 $this->executeAuth($username, $password, $gw_id, $gw_ ip);191 $this->executeAuth($username, $password, $gw_id, $gw_address, $mac, $gw_port, $from); 157 192 break; 158 193 default: 159 throw new WSException("Action {$this->_action} is not defined. Please use GET parameter 'action=list|get|auth' to specify an action" );194 throw new WSException("Action {$this->_action} is not defined. Please use GET parameter 'action=list|get|auth' to specify an action", WSException::INVALID_PARAMETER); 160 195 break; 161 196 } … … 171 206 * @return unknown_type 172 207 */ 173 protected function executeAuth($username = null, $password = null, $gw_id = null, $gw_ip = null ) {208 protected function executeAuth($username = null, $password = null, $gw_id = null, $gw_ip = null, $mac = null, $gw_port = null, $from = null) { 174 209 $this->_outputArr['auth'] = 0; 175 210 … … 180 215 181 216 if (!is_null($gw_id)) { 182 if (is_null($gw_ip) ) {183 throw new WSException("Missing information on the gateway. Must specify parameter 'gw_ip' if there is a gateway id.");217 if (is_null($gw_ip) || is_null($gw_port) || is_null($from)) { 218 throw new WSException("Missing information on the gateway. You must specify parameter 'gw_address' AND 'gw_port' AND 'from_ip' if the parameter 'gw_id' is specified.", WSException::INVALID_PARAMETER); 184 219 } 185 220 $node = Node::getObjectByGatewayId($gw_id); … … 187 222 $network = $node->getNetwork(); 188 223 } else { 189 throw new WSException("Node identified by $gw_id cannot be found" );224 throw new WSException("Node identified by $gw_id cannot be found", WSException::PROCESS_ERROR); 190 225 } 191 226 } else { … … 198 233 * If this is a splash-only node, then the user is automatically authenticated 199 234 */ 235 $token = null; 200 236 if (!empty($node) && $node->isSplashOnly()) { 201 237 $this->_outputArr['auth'] = 1; 202 238 $user = $network->getSplashOnlyUser(); 239 $token = Token::generateConnectionToken($mac, $network, $node, $user, $from); 240 if (!$token) throw new WSException("User authenticated but cannot generate connection token.", WSException::PROCESS_ERROR); 203 241 } else { 204 242 // Authenticate the user on the requested network … … 209 247 } else { 210 248 $this->_outputArr['auth'] = 1; 211 } 249 if (!is_null($node)) { 250 $token = Token::generateConnectionToken($mac, $network, $node, $user, $from); 251 252 if (!$token) throw new WSException("User authenticated but cannot generate connection token.", WSException::PROCESS_ERROR); 253 } 254 } 255 } 256 if ($this->_outputArr['auth'] == 1 && !is_null($token)) { 257 $this->_outputArr['forwardTo'] = "http://" . $gw_ip . ":" . $gw_port . "/wifidog/auth?token=" . $token; 212 258 } 213 259 } … … 222 268 protected function executeGet($objectClass, $objectId, $fields = array(), $idtype = null) { 223 269 if (is_null($objectClass)) { 224 throw new WSException("Missing parameter 'object_class' in the request." );270 throw new WSException("Missing parameter 'object_class' in the request.", WSException::INVALID_PARAMETER); 225 271 } 226 272 if (is_null($objectId)) { 227 throw new WSException("Missing parameter 'object_id' in the request." );273 throw new WSException("Missing parameter 'object_id' in the request.", WSException::INVALID_PARAMETER); 228 274 } 229 275 if (!in_array($objectClass,self::$_allowedObjectClass)) { 230 throw new WSException("Wrong object class '{$objectClass}' requested. Possible values are " . implode(', ', self::$_allowedObjectClass) );276 throw new WSException("Wrong object class '{$objectClass}' requested. Possible values are " . implode(', ', self::$_allowedObjectClass), WSException::INVALID_PARAMETER); 231 277 } 232 278 … … 247 293 // IF the object still is not found, then return an error 248 294 if (is_null($object)) { 249 throw new WSException("Object of class {$objectClass} with id {$objectId} not found" );295 throw new WSException("Object of class {$objectClass} with id {$objectId} not found", WSException::PROCESS_ERROR); 250 296 } 251 297 298 $fields = $this->mapFields($objectClass, $fields); 252 299 if (empty($fields)) { 253 300 $fields = array_keys(self::$_allowedFields[$objectClass]); … … 255 302 $allowedFields = self::$_allowedFields[$objectClass]; 256 303 304 $this->_outputArr = self::filterRet($object, $fields); 305 /* 257 306 foreach($fields as $field) { 258 307 if (isset($allowedFields[ucfirst(strtolower($field))])) { … … 268 317 } 269 318 } 270 319 */ 271 320 272 321 } … … 282 331 protected function executeList($objectClass, $fields = array(), $parentClass = null, $parentId = null) { 283 332 if (is_null($objectClass)) { 284 throw new WSException("Missing parameter 'object_class' in the request." );333 throw new WSException("Missing parameter 'object_class' in the request.", WSException::INVALID_PARAMETER); 285 334 } 286 335 if (!in_array($objectClass,self::$_allowedObjectClass)) { 287 throw new WSException("Wrong object class '{$objectClass}' requested. Possible values are " . implode(', ', self::$_allowedObjectClass) );336 throw new WSException("Wrong object class '{$objectClass}' requested. Possible values are " . implode(', ', self::$_allowedObjectClass), WSException::INVALID_PARAMETER); 288 337 } 289 338 … … 294 343 if (!is_null($parentId)) { 295 344 if (!in_array($parentClass,self::$_allowedObjectClass)) { 296 throw new WSException("Wrong parent class '{$parentClass}' specified. Possible values are " . implode(', ', self::$_allowedObjectClass) );345 throw new WSException("Wrong parent class '{$parentClass}' specified. Possible values are " . implode(', ', self::$_allowedObjectClass), WSException::INVALID_PARAMETER); 297 346 } 298 347 include_once('classes/'.$parentClass.'.php'); 299 348 $parentObject = call_user_func($parentClass.'::getObject', $parentId); 300 349 } else { 301 throw new WSException("If parent class is specified, must specify 'parent_id'" );350 throw new WSException("If parent class is specified, must specify 'parent_id'", WSException::INVALID_PARAMETER); 302 351 } 303 352 } … … 308 357 } 309 358 } 359 $fields = $this->mapFields($objectClass, $fields); 310 360 if (empty($fields)) { 311 361 $fields = self::$_allowedFields[$objectClass]; 312 } 362 } 313 363 314 364 $this->_outputArr = self::filterRet($objectList, $fields); … … 326 376 } 327 377 $filtered = array(); 378 328 379 foreach($retVals as $key => $value) { 329 380 // If the return is one object we filter, return only the allowed fields … … 339 390 $retFields = array(); 340 391 foreach ($fields as $field) { 341 $methodName = 'get'.$field; 342 if (method_exists($value, $methodName)) { 343 $retFields[$field] = self::filterRet($value->$methodName()); 344 } else { 345 $retFields[$field] = 'unknown'; 346 } 392 $forbiddenfield = explode(".", $field); 393 if (! (count($forbiddenfield) == 2)) { 394 $methodName = 'get'.$field; 395 if (method_exists($value, $methodName)) { 396 397 $retFields[$field] = self::filterRet($value->$methodName()); 398 } else { 399 $retFields[$field] = 'unknown'; 400 } 401 } else 402 $retFields[$forbiddenfield[0]] = 'Not allowed'; 347 403 } 348 404 $filtered[] = $retFields;
