note 108157 added to soapserver.soapserver

From: Date: Tue, 03 Apr 2012 11:22:00 +0000
Subject: note 108157 added to soapserver.soapserver
Groups: php.notes 
Request: Send a blank email to php-notes+get-186947@lists.php.net to get a copy of this message
Here's my solution to make SOAP-headers based authentication. 1). First of all we define the decorator class for our service class: <?php class SOAP_Service_Secure { protected $class_name = ''; protected $authenticated = false; // ----- public function __construct($class_name) { $this->class_name = $class_name; } public function AuthHeader($Header) { if($Header->username == 'foo' && $Header->password == 'bar') $this->authenticated = true; } public function __call($method_name, $arguments) { if(!method_exists($this->class_name, $method_name)) throw new Exception('method not found'); $this->checkAuth(); return call_user_func_array(array($this->class_name, $method_name), $arguments); } // ----- protected function checkAuth() { if(!$this->authenticated) HTML_Output::error(403); } } ?> 2). Then we pass an instance of it to the SoapServer object. <?php $Service = new SOAP_Service_Secure('My_Service'); $Server = new SoapServer('my-service.wsdl'); $Server->setObject($Service); $Server->handle(); ?> 3). Implementing a client: <?php class AuthHeader { public $username; public $password; } // ----- $Client = new SoapClient('http://example.com/my-service.wsdl', array( 'exceptions' => true )); // ----- $AuthHeader = new AuthHeader(); $AuthHeader->username = 'foo'; $AuthHeader->password = 'bar'; $Headers[] = new SoapHeader('http://example.com', 'AuthHeader', $AuthHeader); $Client->__setSoapHeaders($Headers); // ----- $Result = $Client->someMethod(); ?> As you can see SoapServer uses our decorator class to answer client requests. If request contains "AuthHeader" SOAP-header then our wrapper checks the credentials and sets $authenticated parameter to TRUE (on success). Then SoapServer calls requested method on our wrapper, we are intercepting it with __call(), checking authentication and if everything is allright we are calling real method on the "My_Service" class. ---- Server IP: 195.54.192.44 Probable Submitter: 78.107.255.44 ---- Manual Page -- http://www.php.net/manual/en/soapserver.soapserver.php Edit -- https://master.php.net/note/edit/108157 Del: integrated -- https://master.php.net/note/delete/108157/integrated Del: useless -- https://master.php.net/note/delete/108157/useless Del: bad code -- https://master.php.net/note/delete/108157/bad+code Del: spam -- https://master.php.net/note/delete/108157/spam Del: non-english -- https://master.php.net/note/delete/108157/non-english Del: in docs -- https://master.php.net/note/delete/108157/in+docs Del: other reasons-- https://master.php.net/note/delete/108157 Reject -- https://master.php.net/note/reject/108157 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186947) next »