Sunday, January 13, 2008

Authenticating Users in PHP Web Services.

User authentication is very important in Web services. Web Services Security Username token profile is the widely used standard used in Web Services for client authentication. WSO2 WSF/PHP provides Username token based authentication really easy. You may have a MYSQL based user details with their passwords. By just providing a callback function as following It is really easy to authenticate users before invoking the business logic.

/* The business logic */

function echoFunction($inMessage) {

$returnMessage = new WSMessage($inMessage->str);

return $returnMessage;
}


/*Password Callback function
function get_my_password_function($username)
{
//logic to get password from any source (ex: using mysql database
// etc)

}

$operations = array("echoString" => "echoFunction");
$actions = array("http://php.axis2.org/samples/echoString" => "echoString");
$sec_array = array("useUsernameToken" => TRUE);

$policy = new WSPolicy(array("security"=>$sec_array));
$sec_token = new WSSecurityToken(array("passwordCallback" => "get_my_password_function",
"passwordType" => "Digest"));

$svr = new WSService(array("operations" => $operations,
"actions" => $actions,
"policy" => $policy,
"securityToken" => $sec_token));

$svr->reply();

?>

For more clarification see WSF/PHP user manual

No comments: