<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dimuthu's Blog &#187; policy</title>
	<atom:link href="http://www.dimuthu.org/tag/policy/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 21 Dec 2011 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>Sending Encrypted Binary Messages With PHP Web Services</title>
		<link>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 17:51:04 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[Attachment]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[MIME]]></category>
		<category><![CDATA[mtom]]></category>
		<category><![CDATA[policy]]></category>
		<category><![CDATA[secuirty token]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=797</guid>
		<description><![CDATA[Web services has made the communication between heterogeneous environments (say PHP with .NET  or Java) a reality. It has defines standards for communicate not only with texts but also with binaries. And more importantly you can keep these communication confidential using encrypted messages according to your requirement. In this post, we will look at how [...]]]></description>
			<content:encoded><![CDATA[<p>Web services has made the communication between heterogeneous environments (say PHP with .NET  or Java) a reality. It has defines standards for communicate not only with texts but also with binaries. And more importantly you can keep these communication confidential using encrypted messages according to your requirement. In this post, we will look at how we can implement such a system with PHP in one side.</p>
<p>In web services we can send/receive binary messages in two basic forms.</p>
<ol>
<li>Setting the binary inside the SOAP message. &#8211; Binary should be converted to base64 to make sure the SOAP body contains only texts. Since base64 converted data span longer than the binary data, we call this form as non-optimized way of sending binaries.</li>
<li>Setting the binary outside the SOAP message &#8211; Binary would be sent as a MIME part in the message. And some element inside SOAP body keeps a reference to the binary using the MIME id. MTOM is a standard for referencing the MIME from inside the SOAP body. Since the binary is encoded, this will keep the message optimum with the binaries.</li>
</ol>
<p>In <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> you can use any of these methods as you prefer. Lets forget about the encryption for now. We will check how we can send binaries in both of the above mentioned forms.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// first the request xml. Note tht xop:Include element that is referring the attachment with the id "myid1".</span>
<span style="color: #0000ff;">$reqPayloadString</span> = &lt;&lt;&lt;XML
&lt;ns1:upload xmlns:ns1=<span style="color: #ff0000;">"http://wso2.org/wsfphp/samples/mtom"</span>&gt;
               &lt;ns1:fileName&gt;test.jpg&lt;/ns1:fileName&gt;
               &lt;ns1:image xmlmime:contentType=<span style="color: #ff0000;">"image/jpeg"</span> xmlns:xmlmime=<span style="color: #ff0000;">"http://www.w3.org/2004/06/xmlmime"</span>&gt;
                  &lt;xop:<span style="color: #b1b100;">Include</span> xmlns:xop=<span style="color: #ff0000;">"http://www.w3.org/2004/08/xop/include"</span> href=<span style="color: #ff0000;">"cid:myid1"</span>&gt;&lt;/xop:Include&gt;
               &lt;/ns1:image&gt;
&lt;/ns1:upload&gt;
XML;

try <span style="color: #66cc66;">{</span>
    <span style="color: #0000ff;">$f</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"my_binary_file.jpg"</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// here in the attachments option we define the binaries</span>
    <span style="font-style: italic; color: #808080;">// corresponding to the id defined in the above XML</span>
    <span style="color: #0000ff;">$reqMessage</span> = <span style="font-weight: bold; color: #000000;">new</span> WSMessage<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqPayloadString</span>,
                                <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"to"</span> =&gt; <span style="color: #ff0000;">"http://localhost/simple_upload_service.php"</span>,
                                      <span style="color: #ff0000;">"action"</span> =&gt; <span style="color: #ff0000;">"http://wso2.org/upload"</span>,
                                      <span style="color: #ff0000;">"attachments"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"myid1"</span> =&gt; <span style="color: #0000ff;">$f</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// creating the WSClient</span>
    <span style="font-style: italic; color: #808080;">// here the option useMTOM will decide whether the</span>
    <span style="font-style: italic; color: #808080;">// attachment is set MTOM or base64</span>
    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// sending the message and retrieving the response</span>
    <span style="color: #0000ff;">$resMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqMessage</span><span style="color: #66cc66;">)</span>;

    <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Response = %s <span style="font-weight: bold; color: #000099;">\\</span>n"</span>, <span style="color: #0000ff;">$resMessage</span>-&gt;<span style="color: #006600;">str</span><span style="color: #66cc66;">)</span>;

<span style="color: #66cc66;">}</span> catch <span style="color: #66cc66;">(</span>Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$e</span> instanceof WSFault<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Soap Fault: %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>, <span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">Reason</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Message = %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>,<span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span></pre>
<p>As mentioned in the inline comment we can choose the preferred form of sending binary using the &#8220;useMTOM&#8221; option. if it is true, the binary is set as a MTOM, (referencing from the body) or if it is set false, the binary will be set as a base64 binary within the SOAP body.<br />
To encrypt the message you only need to write few additional lines. First you define your policy that you need to encrypt this message using a WSPolicy object. Then the security token including the service public key and your private key. You need to give these two option as a constructor argument in WSClient. Here is that little additional code you need to write to add the encryption.</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">// loading the keys</span>
    <span style="color: #0000ff;">$rec_cert</span> = ws_get_cert_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"receiving_server.cert"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #0000ff;">$pvt_key</span> = ws_get_key_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"my_private_key.pem"</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// here we defines the policies and create WSPolicy object</span>
    <span style="color: #0000ff;">$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"encrypt"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                       <span style="color: #ff0000;">"algorithmSuite"</span> =&gt; <span style="color: #ff0000;">"Basic256Rsa15"</span>,
                       <span style="color: #ff0000;">"securityTokenReference"</span> =&gt; <span style="color: #ff0000;">"IssuerSerial"</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span> =&gt; <span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// defining Security Tokens</span>
    <span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"privateKey"</span> =&gt; <span style="color: #0000ff;">$pvt_key</span>,
                                           <span style="color: #ff0000;">"receiverCertificate"</span> =&gt; <span style="color: #0000ff;">$rec_cert</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// modifing WSClient with adding WSPolicy and WSSecurityToken object</span>
    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
                                 <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>You can implement the receiving side of the message similar to the sending side that we just described above. The most important thing is it doesn&#8217;t need to be written in PHP. It can be a Java code or .NET code.If you already have web services that use encrypted binary messaging, the above php code can be use out of the box to communicate with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WS-SecurityPolicy With PHP</title>
		<link>http://www.dimuthu.org/blog/2008/11/19/ws-securitypolicy-with-php/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/19/ws-securitypolicy-with-php/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 16:20:43 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[DataServices]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[policy]]></category>
		<category><![CDATA[ws-policy]]></category>
		<category><![CDATA[ws-security]]></category>
		<category><![CDATA[ws-security policy]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=647</guid>
		<description><![CDATA[WS-SecurityPolicy specification defines standards for defining security policies for your web service. WSF/PHP allows you to declare your security policies according to these standards. You can take one of following approaches to associate policies to your web service or client. PHP Array to represent your policies Policy file compliant with WS-Security Policy. Declaring policies inline [...]]]></description>
			<content:encoded><![CDATA[<p>WS-SecurityPolicy specification defines standards for defining security policies for your web service.<a href="http://wso2.org/projects/wsf/php"> WSF/PHP</a> allows you to declare your security policies according to these standards.</p>
<p>You can take one of following approaches to associate policies to your web service or client.</p>
<ul>
<li>PHP Array to represent your policies</li>
<li>Policy file compliant with WS-Security Policy.</li>
<li>Declaring policies inline with the WSDL.</li>
</ul>
<p><strong>Declaring Policies with a PHP Array</strong></p>
<p>This is a WSF/PHP specific API to declare policies for a web service. You don&#8217;t need to learn WS-Security Policy to write policies with this approach. You can set whether you want to use encryption, signing or usernameToken in a PHP array and create a WSPolicy object using it.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// here is the security array to declare your policies in simple manner</span><span style="color: #0000ff;">
$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"encrypt"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
 <span style="color: #ff0000;">"algorithmSuite"</span> =&gt; <span style="color: #ff0000;">"Basic256Rsa15"</span>,
 <span style="color: #ff0000;">"securityTokenReference"</span> =&gt; <span style="color: #ff0000;">"IssuerSerial"</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// creating WSPolicy instance using the policy array</span><span style="color: #0000ff;">
$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span>=&gt; <span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>You can use this policy object to create a service along with a WSSecurityToken which contain the user parameters like the server private key and the client certificate.</p>
<pre class="php"><span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
 <span style="color: #ff0000;">"privateKey"</span> =&gt; <span style="color: #0000ff;">$server_pvt_key</span>,
 <span style="color: #ff0000;">"receiverCertificate"</span> =&gt; <span style="color: #0000ff;">$client_pub_key</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span> = <span style="font-weight: bold; color: #000000;">new</span> WSService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"actions"</span> =&gt; <span style="color: #0000ff;">$actions</span>,
 <span style="color: #ff0000;">"operations"</span> =&gt; <span style="color: #0000ff;">$operations</span>,
 <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
 <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>; <span style="font-style: italic; color: #808080;">// here is the policy object you just created</span><span style="color: #0000ff;">

$svr</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
<p>You can invoke this service just by writing a simple web service client. There also you need to provide the policies declared in the service, so the client can build his request to validate with server policies. You will be using a similar WSPolicy object to set these policies at the client side too, as show in the below code segment.</p>
<pre class="php"> <span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
 <span style="color: #ff0000;">  "privateKey"</span> =&gt; <span style="color: #0000ff;">$pvt_key</span>,
 <span style="color: #ff0000;">  "receiverCertificate"</span> =&gt; <span style="color: #0000ff;">$rec_cert</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

 <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
    <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>, <span style="font-style: italic; color: #808080;">/* the policy object */</span>
    <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

 <span style="color: #0000ff;">$resMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqMessage</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>Declaring Policies with a Policy File</strong></p>
<p>You can set your policies in the server or client side using a policy file compliant with WS-Security Policy specification. You have to take this approach if your policy requirements are too complicated, like you want to sign only some parts of the message or you want to encrypt some soap headers.</p>
<p>Similar to the above method, here too you will use the WSPolicy object to set your policies. But unlike the above where you give the policies as a PHP array , here you can just give the policy file as an argument to the WSPolicy constructor.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// creating the WSPolicy instance from a policy file</span><span style="color: #0000ff;">
$policy_xml</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"policy.xml"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$policy_xml</span><span style="color: #66cc66;">)</span>;</pre>
<p><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fsecurity%2Fsigning%2Fpolicy_file_based%2Fpolicy.xml">Here </a>is an example of a complete policy file written according to the WS-Security Policy standards. And you can find a quick guide on WS-Security Policy from the article  <a href="http://wso2.org/library/3132">Understanding WS-Security Policy Language</a> written by <a href="http://nandana83.blogspot.com/">Nandana</a>, a key leader of Apache Rampart project.</p>
<p><strong>Declaring Policies inline in a WSDL</strong></p>
<p>We use WSDL to describe our web services. WSDL has the information about the service endpoint, the transport protocols (e.g. http), messaging protocols (e.g. SOAP) and the message schemas and many others about the service. You can attach your policies inside a WSDL.</p>
<p><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2FCalendar%2FCalendar.wsdl">Here</a> is an example of a WSDL with inline policies. The difference in this approach is you can set your policies separately for each messages or each operations or each endpoints of your service. The following segment of a WSDL shows how you refer to different policies which are declared in the early part of the WSDL.</p>
<pre class="xml">     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"CalendarSOAP12Binding"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"ns1:CalendarPortType"</span><span style="font-weight: bold; color: black;">&gt;
</span></span>       <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- Endpoint policies are declared here.
          these are common to all messages transferring
          through this protocols (i.e. SOAP12, http)--&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsp:PolicyReference</span> <span style="color: #000066;">URI</span>=<span style="color: #ff0000;">"#transport_binding_policy"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">  &lt;soap12:binding</span> <span style="color: #000066;">transport</span>=<span style="color: #ff0000;">"http://schemas.xmlsoap.org/soap/http"</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"document"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"login"</span><span style="font-weight: bold; color: black;">&gt;
          </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">"urn:login"</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"document"</span><span style="font-weight: bold; color: black;">/&gt;
          </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:input<span style="font-weight: bold; color: black;">&gt;
             </span></span></span> <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- policy specific to the 'login' operation --&gt;</span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsp:PolicyReference</span> <span style="color: #000066;">URI</span>=<span style="color: #ff0000;">"#username_token_policy"</span><span style="font-weight: bold; color: black;">/&gt;
             </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;
          </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
           <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:output<span style="font-weight: bold; color: black;">&gt;
         </span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:operation<span style="font-weight: bold; color: black;">&gt;
        </span></span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"register"</span><span style="font-weight: bold; color: black;">&gt;
           </span></span> <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- no specific policies are set for the 'register' operation</span></span><span style="color: #009900;"><span style="font-style: italic; color: #808080;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">
          </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;"> &lt;soap12:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">"urn:register"</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"document"</span><span style="font-weight: bold; color: black;">/&gt;
         </span></span> </span></span>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;
          </span></span>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:operation<span style="font-weight: bold; color: black;">&gt;</span></span></span>
           ....
       <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:binding<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>This is the binding section of a WSDL where we bind messaging protocol and transport protocols with a service endpoint. Here we have &#8220;login&#8221; and &#8220;register&#8221; operations. Note that we are referring to &#8220;transport_binding_policy&#8221; from the parent level of each operation elements. That means these policies are common to all the operation in that binding. And inside the &#8220;login&#8221; operation we are referring to &#8220;username_token_policy&#8221;, so in order to invoke this operation, you have to send username token headers. And &#8220;register&#8221; doesn&#8217;t require any operation specific policies allowing users to register without any prior authentications.</p>
<p>You can select any of the above mentioned approach to define policies of your web service or to invoke a web service that support WS-Policy. If your policy requirements are simple, it will be easy to use the array based approach. If your policy requirements are complex or you have a good understanding of WS-Policy  and WS-Security Policy you can rely on the policy file based approach or defining policy inline with WSDL. And the former 2 methods will give you a nice separation of the logic code and security configurations. The selection is yours:)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/19/ws-securitypolicy-with-php/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>PHP Data Services With WS-Security</title>
		<link>http://www.dimuthu.org/blog/2008/09/16/php-data-services-with-ws-security/</link>
		<comments>http://www.dimuthu.org/blog/2008/09/16/php-data-services-with-ws-security/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 16:22:02 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[DataServices]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[Authentication. Username Token]]></category>
		<category><![CDATA[data services]]></category>
		<category><![CDATA[DataService]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[policy]]></category>
		<category><![CDATA[securityToken]]></category>
		<category><![CDATA[ws-security]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=277</guid>
		<description><![CDATA[Earlier I wrote a blog about how to make your wordpress blog a web service using the WSF/PHP Data Services library. I will expand that post to demonstrate the use of WS-Security features with WSF/PHP. This time it is a Tag Search service for my wordpress blog. Check the &#8216;Tag Search&#8217; Data Services Demo from [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier I wrote a blog about <a href="http://www.dimuthu.org/blog/2008/08/11/make-your-wordpress-blog-a-web-service-in-few-step/">how to make your wordpress blog a web service</a> using the <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> Data Services library. I will expand that post to demonstrate the use of WS-Security features with WSF/PHP.</p>
<p>This time it is a Tag Search service for my wordpress blog. Check the &#8216;Tag Search&#8217; Data Services Demo from <a href="http://ws.dimuthu.org/">http://ws.dimuthu.org/</a>. The only difference is here you are authenticated before accessing the service using the username tokens as specified in WS-Security.</p>
<p>Just look at the WSSecurity constructor in the <a href="http://ws.dimuthu.org/source.php?src=tag.search.service">Data Service Demo Code</a>. You can observe 4 new parameters passed in to it. (In addition to the &#8220;config&#8221; and &#8220;operations&#8221; options)</p>
<ul>
<li>policy &#8211; This is where you specify the policy governed by the service.  Here you can either use the WS-Policy compliant policy file or just a simple PHP array that contain the required security token informations.
<pre><span style="color: #0000ff;">$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useUsernameToken"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span>=&gt;<span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
</li>
<li> securityToken: You specify the user parameters like how you handle the authentication and the encoding type in this option.
<pre><span style="color: #0000ff;">$security_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"passwordCallback"</span> =&gt; <span style="color: #ff0000;">"password_callback_function"</span>,
                                       <span style="color: #ff0000;">"passwordType"</span> =&gt; <span style="color: #ff0000;">"Digest"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">/* callback function
 * @param string $username username of the client request
 * @return string $password password for that username
 */</span>
<span style="font-weight: bold; color: #000000;">function</span> password_callback_function<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
    <span style="font-style: italic; color: #808080;">// In the real word I should authenticate users from database.</span>
    <span style="font-style: italic; color: #808080;">// for this demo I have a simple if-else block</span>

    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$username</span> == <span style="color: #ff0000;">"visitor"</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
        <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">"visitor123"</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">"notavistor"</span>;
<span style="color: #66cc66;">}</span></pre>
<p>Note that here you specify a callback function to the security token parameter. Inside this function you retrieve the password for the user (mostly from the database) and return. WSF/PHP will authenticate the user from these information.</li>
<li> useWSA : You need to set this option in order to generate the WS-Addressing parameters (like action) for your WSDL. WS-Addressing is required to run web services with WS-Security in WSF/PHP.</li>
<li> actions: You should provide a map of action to service operations in order to get the WS Addressing information generated with your WSDL.
<pre><span style="color: #0000ff;">$actions</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"http://ws.dimuthu.org/blog/getPosts"</span> =&gt; <span style="color: #ff0000;">"getPosts"</span><span style="color: #66cc66;">)</span>;</pre>
<p>Just have a look at how these information are rendered in the generated WSDL, <a href="http://ws.dimuthu.org/blog/WordpressTagSearchService.php?wsdl">http://ws.dimuthu.org/blog/WordpressTagSearchService.php?wsdl</a>. (Note the wsaw:action attribute in the messages inside the portType element.</li>
</ul>
<p>After you deploy the service, it is very easy to generate a client with the WSDL. If you write clients in PHP you can use the wsdl2php tool shipping with WSF/PHP.  The code for my demo client can be found in <a href="http://ws.dimuthu.org/source.php?src=tag.search.client">http://ws.dimuthu.org/source.php?src=tag.search.client</a>. (There I have hard coded the username and password just for the demo purpose)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/09/16/php-data-services-with-ws-security/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Web Services Security in WSF/PHP 1.3.2</title>
		<link>http://www.dimuthu.org/blog/2008/08/18/web-services-security-in-wsfphp-132/</link>
		<comments>http://www.dimuthu.org/blog/2008/08/18/web-services-security-in-wsfphp-132/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 06:32:06 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[1.3.2]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[policy]]></category>
		<category><![CDATA[security token]]></category>
		<category><![CDATA[signing]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[usernameToken]]></category>
		<category><![CDATA[ws-security]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=181</guid>
		<description><![CDATA[With WSF/PHP 1.3.2 you can use following basic features in WS-Security. Feature Purpose Array based Security Policy Options ($sec_policies) Security Token Options ($sec_token_options) UsernameToken Authentication array(&#8220;useUsernameToken&#8221; =&#62; TRUE) array(&#8220;user&#8221; =&#62; &#8220;your_username&#8221;, &#8220;password&#8221; =&#62; &#8220;your_password&#8221;, &#8220;passwordType&#8221; =&#62; &#8220;Digest&#8221;); //Digest/Plain Timestamp Avoid Interception,Replay Attack (use with signing) array(&#8220;includeTimeStamp&#8221; =&#62; TRUE); array(&#8220;ttl&#8221; =&#62; 100) Signing Non-Repudiation, Verify Server/Clients [...]]]></description>
			<content:encoded><![CDATA[<p>With <a href="http://wso2.org/projects/wsf/php">WSF/PHP 1.3.2</a> you can use following basic features in WS-Security.</p>
<table border="1">
<tbody>
<tr>
<td><strong>Feature</strong></td>
<td><strong>Purpose</strong></td>
<td><strong>Array based Security Policy Options ($sec_policies)</strong></td>
<td><strong>Security Token Options ($sec_token_options)</strong></td>
</tr>
<tr>
<td>UsernameToken</td>
<td>Authentication</td>
<td>array(&#8220;useUsernameToken&#8221; =&gt; TRUE)</td>
<td>array(&#8220;user&#8221; =&gt; &#8220;your_username&#8221;,<br />
&#8220;password&#8221; =&gt; &#8220;your_password&#8221;,<br />
&#8220;passwordType&#8221; =&gt; &#8220;Digest&#8221;); //Digest/Plain</td>
</tr>
<tr>
<td>Timestamp</td>
<td>Avoid Interception,Replay Attack (use with signing)</td>
<td>array(&#8220;includeTimeStamp&#8221; =&gt; TRUE);</td>
<td>array(&#8220;ttl&#8221; =&gt; 100)</td>
</tr>
<tr>
<td>Signing</td>
<td>Non-Repudiation, Verify Server/Clients identity</td>
<td>array(&#8220;sign&#8221; =&gt; TRUE,<br />
&#8220;algorithmSuite&#8221; =&gt; &#8220;Basic256Rsa15&#8243;,<br />
&#8220;securityTokenReference&#8221; =&gt; &#8220;KeyIdentifier&#8221;)</td>
<td>array(&#8220;privateKey&#8221; =&gt; $pvt_key,<br />
certificate&#8221; =&gt; $cert)</td>
</tr>
<tr>
<td>Encryption</td>
<td>privacy</td>
<td>array(&#8220;encrypt&#8221; =&gt; TRUE,<br />
&#8220;algorithmSuite&#8221; =&gt; &#8220;Basic256Rsa15&#8243;,<br />
&#8220;securityTokenReference&#8221; =&gt; &#8220;IssuerSerial&#8221;);</td>
<td>array(&#8220;privateKey&#8221; =&gt; $pvt_key,<br />
&#8220;receiverCertificate&#8221; =&gt; $pub_key))</td>
</tr>
</tbody>
</table>
<p>You can build the WSPolicy and WSSecurityToken with an any mix of above features. For some scenarios you may only need timestamp with signing where as some other critical scenarios you want signing, encryption, username token and timestamp.</p>
<p>Here is how you build the WSSPolicy and WSSecurityToken classes using the above mentioned $sec_policies and $sec_token_options.</p>
<pre><span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span>=&gt; <span style="color: #0000ff;">$sec_policies</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sec_token_options</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span> = <span style="font-weight: bold; color: #000000;">new</span> WSService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
                           <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span>,
                            <span style="color: #ff0000;">"actions"</span> =&gt; <span style="color: #0000ff;">$your_actions</span>,
                           <span style="color: #ff0000;">"operations"</span> =&gt; <span style="color: #0000ff;">$your_operations</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
<p>Similarly you can use the WSPolicy and WSSecurity with WSClient for the client side security. See the samples <a href="http://labs.wso2.org/wsf/php/samples/security/">WS-Security demos</a> and <a href="http://labs.wso2.org/wsf/php/source_page_frame.php?src=samples/security">WS-Security sources</a>.</p>
<p>This blog is about some of the security features shipped with WSF/PHP 1.3.2. With the next release of WSF/PHP you will have more features related to WS-Security like WS-SecureConversations, WS-Trust and use of KeyStores for encryption and signing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/08/18/web-services-security-in-wsfphp-132/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

