<?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"
	>

<channel>
	<title>Jim Hoskins</title>
	<atom:link href="http://jimhoskins.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jimhoskins.com</link>
	<description>Yeah... it's awesome</description>
	<pubDate>Fri, 10 Oct 2008 04:38:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Vote or Die - JakeAndAmir.com</title>
		<link>http://jimhoskins.com/2008/10/10/vote-or-die-jakeandamircom/</link>
		<comments>http://jimhoskins.com/2008/10/10/vote-or-die-jakeandamircom/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 04:30:42 +0000</pubDate>
		<dc:creator>Jim Hoskins</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jimhoskins.com/?p=10</guid>
		<description><![CDATA[




	
	




From JakeAndAmir.com

oh&#8230; and the embed code has been totally Validified
]]></description>
			<content:encoded><![CDATA[<p>
<!--[if !IE]> -->
<object type="application/x-shockwave-flash" data="http://vimeo.com/moogaloop.swf?clip_id=1926280&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" width="580" height="437">
<!-- <![endif]-->
<!--[if IE]>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="580" height="437">
	<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=1926280&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />
<!--><!--http://Validifier.com-->
	<param name="allowfullscreen" value="true" />
	<param name="allowscriptaccess" value="always" />
</object>
<!-- <![endif]-->
<br />
<br/><br />
<a href="http://jakeandamir.com">From JakeAndAmir.com</a><br />
<br/><br />
oh&#8230; and the embed code has been totally <a href="http://validifier.com">Validified</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jimhoskins.com/2008/10/10/vote-or-die-jakeandamircom/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Ruby Percent Syntax (Percent Functions)</title>
		<link>http://jimhoskins.com/2008/10/07/ruby-percent-syntax-percent-functions/</link>
		<comments>http://jimhoskins.com/2008/10/07/ruby-percent-syntax-percent-functions/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 20:47:59 +0000</pubDate>
		<dc:creator>Jim Hoskins</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[Percent Functions]]></category>

		<category><![CDATA[Percent Literals]]></category>

		<category><![CDATA[Syntax]]></category>

		<guid isPermaLink="false">http://jimhoskins.com/?p=8</guid>
		<description><![CDATA[I wanted to post a quick guide to the special ruby syntax for literals that utilize the % (percent) symbol. Most beginners guides to ruby leave out an explanation of these forms of literals, but many ruby coders use them. When someone encounters them for the first time it is almost impossible to figure out [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to post a quick guide to the special ruby syntax for literals that utilize the % (percent) symbol. Most beginners guides to ruby leave out an explanation of these forms of literals, but many ruby coders use them. When someone encounters them for the first time it is almost impossible to figure out what they mean. (Try searching Google for &#8220;%w&#8221;)</p>
<p>Ruby has special syntax for making strings, arrays and system commands easier to write. They allow you to use different characters as delimiters so you can minimize escaping in your literals.</p>
<h3>The syntax</h3>
<p>The syntax for the % literals is a percent symbol (%) a letter which defines what kind of literal it is (Q, q, w, x, r) a delimiter,  the content, and the closing delimiter.</p>
<p>The delimiter can be any character, and is defined as whatever is immediately after the letter in the syntax. For example %Q!content! , the delimiter is the ! and it surrounds the content. There are special cases when the delimiter is { or (, the closing delimiter will be } or ) respectively.</p>
<h3>%Q and %q (Percent Q): Strings</h3>
<h4>%Q!Some String of &#8220;Characters&#8221;! &lt;==&gt; &#8221; Some String of \&#8221;Characters\&#8221; &#8220;</h4>
<p>%Q is the equivalent to a double-quoted ruby string. #{expression} evaluation works just like in double-quoted strings, even if you use %Q{} as your delimiter!</p>
<p>You can also leave off the Q and it will have the same functionality. I recommend leaving the Q in to be more clear.</p>
<h4>%q!Some String of &#8220;Characters&#8221;! &lt;==&gt; &#8216;Some String of Characters&#8217;</h4>
<p>The %q is just like %Q, but acts the same as a single-quoted string. Whatever is inside the delimiters is returned as a string.</p>
<p>You can remember %Q is for strings because it acts like <span style="text-decoration: underline;"><strong>Q</strong></span>uotes<strong>.</strong></p>
<p>More info here: <a title="Ruby Syntax: Strings" href="http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#string" target="_self">http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#string</a></p>
<h3>%W (Percent W): Arrays</h3>
<h4>%W(North South East West) &lt;==&gt; ["North", "South", "East", "West"]</h4>
<p>%W (and %w) allow you to create an Array of strings without using quotes and commas.</p>
<p>The delimiter rules are the same as strings, but typically parentheses are used. The content inside the delimiters are split by white-space, and put into an array. This is great if you have a hard coded list of single word strings.</p>
<p>You can remember %W is by thinking of it as a <span style="text-decoration: underline;"><strong>W</strong></span>hite-space divided Array.</p>
<p>More info here : <a title="Ruby Syntax: Arrays" href="http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#array">http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#array</a></p>
<h3>%x (Percent x): System Execution</h3>
<h4>%x{ ls /usr/local } &lt;==&gt; `ls /usr/local`</h4>
<p>%x allows you to call system commands, equivilent to wrapping the command in `s (grave accents). The benefit of the $x{} syntax is you don&#8217;t have to escape your accents in commands that use them.</p>
<p>You can remember to use X because it e<span style="text-decoration: underline;"><strong>X</strong></span>ecutes a command.</p>
<p>More info here: <a title="Ruby Syntax: Command Output" href="http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#command">http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#command</a></p>
<h3>%r (Percent r): Regular Expressions</h3>
<h4>%r{/usr/bin/} &lt;==&gt; /\/user\/bin\//</h4>
<p>%r is really handy for regular expressions that contain /s (forward slashes) which are the default delimiter for regular expressions and have to be escaped.</p>
<p>Remember to use %r with <span style="text-decoration: underline;"><strong>r</strong></span>egular expressions.</p>
<p>More info here: <a title="Ruby Syntax: Regular Expressions" href="http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#regexp">http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#regexp</a></p>
<p>I hope this information is helpful. Please leave a comment if this helped or if I left something out.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimhoskins.com/2008/10/07/ruby-percent-syntax-percent-functions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Announcing Validifier: Make Your Flash Embed Code Valid XHTML</title>
		<link>http://jimhoskins.com/2008/10/02/announcing-validifier-make-your-flash-embed-code-valid-xhtml/</link>
		<comments>http://jimhoskins.com/2008/10/02/announcing-validifier-make-your-flash-embed-code-valid-xhtml/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 02:56:55 +0000</pubDate>
		<dc:creator>Jim Hoskins</dc:creator>
		
		<category><![CDATA[Done21]]></category>

		<category><![CDATA[app]]></category>

		<category><![CDATA[Flash]]></category>

		<category><![CDATA[Valid]]></category>

		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://jimhoskins.com/?p=7</guid>
		<description><![CDATA[Are your web pages valid XHTML? Hopefully. But if you use any flash on your page, chances are, your page is not valid at all. Why is Flash crashing your validation parade? It&#8217;s the embed code that was generated. Most of the time people put Flash into a page, they use embed code created by [...]]]></description>
			<content:encoded><![CDATA[<p>Are your web pages valid XHTML? Hopefully. But if you use any flash on your page, chances are, your page is not valid at all. Why is Flash crashing your validation parade? It&#8217;s the embed code that was generated. Most of the time people put Flash into a page, they use embed code created by either their content provider or their authoring application. These codes are almost invariably invalid XHTML.</p>
<p>Here comes <a title="Validifier" href="http://validifier.com">Validifier</a> to save the day! <a title="Validifier.com" href="http://validifier.com">Validifier makes your Flash embed code Valid XHTML</a>! There are a lot of different reasons embed code fails validation, Validifier takes care of all of them. Just paste your current code into the box, click Validify! and use the code provided. It&#8217;s Valid, Compatible, and Free!</p>
<p>This is the first product released by <a title="Done21" href="http://done21.com">Done21</a>, check out <a title="Almost Done" href="http://almost.done21.com">Almost Done: The Done21 Blog</a> for more information on upcoming tools and product releases!</p>
]]></content:encoded>
			<wfw:commentRss>http://jimhoskins.com/2008/10/02/announcing-validifier-make-your-flash-embed-code-valid-xhtml/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CherryPy Digest and Basic Authentication Tutorial</title>
		<link>http://jimhoskins.com/2008/07/21/cherrypy-digest-and-basic-authentication/</link>
		<comments>http://jimhoskins.com/2008/07/21/cherrypy-digest-and-basic-authentication/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 21:30:57 +0000</pubDate>
		<dc:creator>Jim Hoskins</dc:creator>
		
		<category><![CDATA[Cherrypy]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[Authentication]]></category>

		<category><![CDATA[Basic]]></category>

		<category><![CDATA[Digest]]></category>

		<category><![CDATA[Feature]]></category>

		<guid isPermaLink="false">http://jimhoskins.com/?p=5</guid>
		<description><![CDATA[Many applications require the use of password based authentication. In many web applications, this is done using cookies/sessions, and the login mechanism is an HTML form that is submitted to the application. If you are trying to build a RESTful web service, or any service where this client may be automated, It becomes useful to [...]]]></description>
			<content:encoded><![CDATA[<p>Many applications require the use of password based authentication. In many web applications, this is done using cookies/sessions, and the login mechanism is an HTML form that is submitted to the application. If you are trying to build a <a title="Representational State Transfer" href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">RESTful</a> web service, or any service where this client may be automated, It becomes useful to utilize <a title="RFC 2617: HTTP Authentication" href="http://tools.ietf.org/html/rfc2617" target="_blank">HTTP authentication</a>.</p>
<p>It may not be immediately obvious from the <a title="CherryPy" href="http://www.cherrypy.org/" target="_blank">CherryPy</a> <a title="CherryPy Docs" href="http://www.cherrypy.org/wiki/TableOfContents" target="_blank">documentation</a>, but CherryPy includes all the tools needed to implement HTTP authentication, both Digest and Basic. You can see what the CherryPy website has documented about authentication at the <a title="CherryPy Builtin Tools Page" href="http://cherrypy.org/wiki/BuiltinTools" target="_blank">CherryPy Builtin Tools Page</a>. The best documentation for this is available in the book: <a href="http://www.amazon.com/gp/product/1904811841?ie=UTF8&amp;tag=jimhos-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1904811841">CherryPy Essentials: Rapid Python Web Application Development</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=jimhos-20&amp;l=as2&amp;o=1&amp;a=1904811841" border="0" alt="" width="1" height="1" /></p>
<h3>Basic vs Digest</h3>
<p>HTTP supports two types of authentication, <a title="Basic access authentication" href="http://en.wikipedia.org/wiki/Basic_access_authentication" target="_blank">basic</a> and <a title="Digest access authentication" href="http://en.wikipedia.org/wiki/Digest_access_authentication" target="_blank">digest</a>. To a user accessing a protected page through a web browser, both types look the same. The browser will prompt the user for a username and password. The difference is how the client (browser) sends the password to the server.</p>
<p>With basic authentication, the client sends the password in clear-text, so any malicious attacker who can see the request can see the password. With digest authentication, the client does not send the password, but rather a <a title="Message Digest" href="http://en.wikipedia.org/wiki/Hash_algorithm">digest</a> based on the password and other factors. The server will also compute the digest using the known correct password. If the digest sent by the client matches the one calculated by the server, authentication succeeds.</p>
<p>The catch with digest authentication is the users&#8217; passwords must be stored stored in clear-text on the server** in order to calculate the digest. On the flipside, basic authentication must send the password in clear-text through the network. If you are going to use basic authentication, it is recommended you use SSL to prevent the password from being sniffed.</p>
<p><span style="color: #999999;">** This is not entirely true, The digest algorithm allows you to store a partially digested password on the server side, however CherryPy has no easy way to specify this.</span></p>
<h3>Digest Authentication</h3>
<p>The following code creates a simple application with a public page and a secure page using digest authentication. The server configuration defines that the /secure url is to be protected with digest authentication (via the tools.digest_auth Tool available in CherryPy)</p>
<p><span id="more-5"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RootServer:
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;This is a public page!&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SecureServer:
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;This is a secure section&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    users = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;admin&quot;</span>: <span style="color: #483d8b;">&quot;secretPassword&quot;</span>,
             <span style="color: #483d8b;">&quot;editor&quot;</span>: <span style="color: #483d8b;">&quot;otherPassword&quot;</span><span style="color: black;">&#125;</span>
&nbsp;
    conf = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'/secure'</span>: <span style="color: black;">&#123;</span><span style="color: #483d8b;">'tools.digest_auth.on'</span>: <span style="color: #008000;">True</span>,
                        <span style="color: #483d8b;">'tools.digest_auth.realm'</span>: <span style="color: #483d8b;">'Some site'</span>,
                        <span style="color: #483d8b;">'tools.digest_auth.users'</span>: users<span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
    root = RootServer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">secure</span> = SecureServer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cherrypy.<span style="color: black;">quickstart</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">'/'</span>, config=conf<span style="color: black;">&#41;</span></pre></div></div>

<p>Run this code and visit <a title="Public Page" href="http://localhost:8080/" target="_blank">http://localhost:8080/</a> . Now visit <a title="Secure Page" href="http://localhost:8080/secure" target="_blank">http://localhost:8080/secure</a>. When your browser prompts you to do so, enter &#8220;admin&#8221; and &#8220;secretPassword&#8221; for the username and password respectively.</p>
<p>The /secure configuration section sets up the digest authentication with the following options:</p>
<ul>
<li><strong>&#8216;tools.digest_auth.on&#8217;</strong>: This boolean enables digest authentication for the given section (in this case /secure)</li>
<li><span style="color: #000000;"><strong>&#8216;tools.digest_auth.realm&#8217;</strong></span>: This string defines the realm to supply to the client.</li>
<li><strong>&#8216;tools.digest_auth.users&#8217;</strong>: This dictionary defines users and passwords ({user: password}). This can also take two other forms, please see below.</li>
</ul>
<p><span style="color: #999999;"><em>There is a bug in the CherryPy Digest Authentication tool that prevents authentication when the request is a post request with a body. See <a title="CherryPy Digest Auth POST Problem (and solution!)" href="http://jimhoskins.com/2008/07/18/cherrypy-digest-auth-post-problem-and-solution/" target="_self">Cherrypy Digest Auth POST Problem (and Solution!)</a></em></span></p>
<h3>Basic Authentication</h3>
<p>We are going to use the same code to implement basic authentication, but we will change the configuration variables.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RootServer:
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;This is a public page!&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SecureServer:
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;This is a secure section&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    users = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;admin&quot;</span>: <span style="color: #483d8b;">&quot;secretPassword&quot;</span>,
             <span style="color: #483d8b;">&quot;editor&quot;</span>: <span style="color: #483d8b;">&quot;otherPassword&quot;</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> clear_text<span style="color: black;">&#40;</span>passwd<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> passwd
&nbsp;
    conf = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'/secure'</span>: <span style="color: black;">&#123;</span><span style="color: #483d8b;">'tools.basic_auth.on'</span>: <span style="color: #008000;">True</span>,
                        <span style="color: #483d8b;">'tools.basic_auth.realm'</span>: <span style="color: #483d8b;">'Some site2'</span>,
                        <span style="color: #483d8b;">'tools.basic_auth.users'</span>: users,
                        <span style="color: #483d8b;">'tools.basic_auth.encrypt'</span>: clear_text<span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
    root = RootServer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">secure</span> = SecureServer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cherrypy.<span style="color: black;">quickstart</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">'/'</span>, config=conf<span style="color: black;">&#41;</span></pre></div></div>

<p>Run this code as before, and supply the same username and password. Can&#8217;t tell the difference? Remember, the difference is in how it is transmitted. Here are the options we supplied to enable basic authentication:</p>
<ul>
<li><strong>&#8216;tools.basic_auth.on&#8217;</strong>: This boolean enables digest authentication for the given section (in this case /secure)</li>
<li><span style="color: #000000;"><strong>&#8216;tools.basic_auth.realm&#8217;</strong></span>: This string defines the realm to supply to the client.</li>
<li><strong>&#8216;tools.basic_auth.users&#8217;</strong>: This dictionary defines users and passwords ({user: password}). This can also take two other forms, please see below.</li>
<li><strong>&#8216;tools.basic_auth.encrypt&#8217;</strong>: This takes a function that encrypts the user-supplied password to match a pre-encrypted password in the user dict. (In our case, the function clear_text we defined just passes the password through, allowing us to store password in clear-text for ease of demonstration). If you do not supply this option, it will be assumed the passwords are stored in MD5.</li>
</ul>
<h3>Defining Users</h3>
<p>You can supply either a dict or function to the &#8216;tools.******_auth.users&#8217; option to define your users. There are three ways to supply users: as a dict ( {user:password} ), as a function that returns a dict ( {user:password} ), or as a function that takes a username as an argument, and returns a password.</p>
<p><span style="color: #999999;">Remember that password could mean the clear-text password, or an encrypted password, depending on whether your using basic or digest, and the encryption option specified.</span></p>
<h4>Using a dictionary</h4>
<p>Simply supply a dictionary of {username: password} to the users option of the authentication configuration.</p>

<div class="wp_syntax"><div class="code"><pre class="python">users = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'user1'</span>: <span style="color: #483d8b;">'password1'</span>,
         <span style="color: #483d8b;">'user2'</span>: <span style="color: #483d8b;">'password2}
&nbsp;
conf = {'</span>/secure<span style="color: #483d8b;">': {'</span>tools.<span style="color: black;">digest_auth</span>.<span style="color: black;">on</span><span style="color: #483d8b;">': True,
                     '</span>tools.<span style="color: black;">digest_auth</span>.<span style="color: black;">realm</span><span style="color: #483d8b;">': '</span>Some <span style="color: #dc143c;">site</span><span style="color: #483d8b;">',
                     '</span>tools.<span style="color: black;">digest_auth</span>.<span style="color: black;">users</span><span style="color: #483d8b;">': users}}
</span</pre></div></div>

<h4>Using a function that returns a dictionary</h4>
<p>Supply a function that takes no arguments and returns a dictionary containing all usernames and passwords</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> get_users<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'user1'</span>: <span style="color: #483d8b;">'password1'</span>,
            <span style="color: #483d8b;">'user2'</span>: <span style="color: #483d8b;">'password2}
&nbsp;
conf = {'</span>/secure<span style="color: #483d8b;">': {'</span>tools.<span style="color: black;">digest_auth</span>.<span style="color: black;">on</span><span style="color: #483d8b;">': True,
                     '</span>tools.<span style="color: black;">digest_auth</span>.<span style="color: black;">realm</span><span style="color: #483d8b;">': '</span>Some <span style="color: #dc143c;">site</span><span style="color: #483d8b;">',
                     '</span>tools.<span style="color: black;">digest_auth</span>.<span style="color: black;">users</span><span style="color: #483d8b;">': get_users}}
</span</pre></div></div>

<h4>Using a Function Which Takes a Username (Most Useful)</h4>
<p>Many times, applications have so many users that it does not make sense to pass the list of all users around like the options above. I have not seen this feature documented anywhere, but the code allows it. You can specify a function that takes a username string as an argument, and returns the password for that user. This function could consult a database or other mechanism, and allows your application to add and remove user more easily.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> find_password<span style="color: black;">&#40;</span>username<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#INSERT PASSWORD LOOKUP LOGIC HERE</span>
    password = db.<span style="color: black;">find_password_for_user</span><span style="color: black;">&#40;</span>username<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> password
&nbsp;
conf = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'/secure'</span>: <span style="color: black;">&#123;</span><span style="color: #483d8b;">'tools.digest_auth.on'</span>: <span style="color: #008000;">True</span>,
                     <span style="color: #483d8b;">'tools.digest_auth.realm'</span>: <span style="color: #483d8b;">'Some site'</span>,
                     <span style="color: #483d8b;">'tools.digest_auth.users'</span>: find_password<span style="color: black;">&#125;</span><span style="color: black;">&#125;</span></pre></div></div>

<h3>Retrieving the Username</h3>
<p>Once authenticated your application will likely want to know which user is logged in. The username of the user who has successfully authenticated will be available at &#8216;cherrypy.request.login&#8217;. This variable wil be False if authentication failed.</p>
<h3>In Closing</h3>
<p>It may not be well documented, but the tools for basic and digest authentication are already built in to CherryPy. You just have to know how to use them, and now you do.</p>
<p>If there are any problems with the code or this post in general, leave me a comment and I will make it right.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimhoskins.com/2008/07/21/cherrypy-digest-and-basic-authentication/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Cherrypy Digest Auth POST problem (and solution!)</title>
		<link>http://jimhoskins.com/2008/07/18/cherrypy-digest-auth-post-problem-and-solution/</link>
		<comments>http://jimhoskins.com/2008/07/18/cherrypy-digest-auth-post-problem-and-solution/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 20:04:20 +0000</pubDate>
		<dc:creator>Jim Hoskins</dc:creator>
		
		<category><![CDATA[Cherrypy]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[Bug]]></category>

		<category><![CDATA[Digest]]></category>

		<category><![CDATA[Hack]]></category>

		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://jimhoskins.com/?p=4</guid>
		<description><![CDATA[When using Digest Authentication with Cherrypy, if a client tries to authenticate a session from a page with a POST body, the credentials will be rejected even if they are correct. This is due to how Cherrypy&#8217;s digest_auth tool handles the initial request and request body.
The Problem
In Cherrypy 3, when utlizing Digest Authentication (via tools.digest_auth), [...]]]></description>
			<content:encoded><![CDATA[<p>When using Digest Authentication with Cherrypy, if a client tries to authenticate a session from a page with a POST body, the credentials will be rejected even if they are correct. This is due to how Cherrypy&#8217;s digest_auth tool handles the initial request and request body.</p>
<h3>The Problem</h3>
<p>In <a title="Cherrypy" href="http://www.cherrypy.org/" target="_blank">Cherrypy</a> 3, when utlizing Digest Authentication (via <a title="Cherrypy: tools.digest_auth" href="http://www.cherrypy.org/wiki/BuiltinTools#tools.digest_auth" target="_blank">tools.digest_auth</a>), if the first request to a protected area comes from a POST request (with HTTP body), the client will not be able to authenticate properly.</p>
<p>Example: Posting from an HTML form to a protected action, the client will be prompted for credentials. After supplying a good username and password, Cherrypy will reject and the request and ask for credentials again.</p>
<h3>The Cause</h3>
<p>First a primer on <a title="Digest access authentication" href="http://en.wikipedia.org/wiki/Digest_access_authentication" target="_blank">Digest Authentication</a>:</p>
<h4>The Process</h4>
<ol>
<li>The client requests a protected url URL (eg: /secret)</li>
<li>The server tries to authenticate the request from the HTTP headers for the URL. There are not credentials, so this fails. The server replies with an HTTP Status code of <a title="HTTP Status: 401 Not Authorized" href="http://en.wikipedia.org/wiki/HTTP_401#4xx_Client_Error" target="_blank">401: Not Authorized</a></li>
<li>The client recieves this requests and gets a username and password (from an operator or database or other source)</li>
<li>The client requests the URL again, but adds the credentials to the &#8220;Authorization&#8221; HTTP Header.</li>
<li>The server tries again to authenticate. If the credentials are good, the resource at /secret is served. If authentication fails, The server responds with 401 &#8230;</li>
<li>rinse and repeat..</li>
</ol>
<p><span id="more-4"></span></p>
<h4>The Digest</h4>
<p>Unlike <a title="Basic access authentication" href="http://en.wikipedia.org/wiki/Basic_access_authentication" target="_self">HTTP Basic Authentication</a>, the password is not sent in the Authorization header in clear text. Instead, a specified Digest Algorithm is used to create a digest of the password. The client calculates and sends the digest, then the server calculates the digest. If they match, the authorization succeeds.</p>
<p>Among other factors, the inputs to the digest are the username, password, HTTP method, and path.</p>
<h4>The Initial Request</h4>
<p>When the client makes the initial request (and the request contains a body), the Digest Authentication Tool tries to authenticate, and fails (due to lack of the Authorization header). The tool raises a <a title="Cherrypy: HTTPError" href="http://www.cherrypy.org/wiki/ErrorsAndExceptions#HTTPError" target="_blank">cherrypy.HTTPError</a> with status code 401 to signal to the client that credentials are required.</p>
<h4>The Leftovers</h4>
<p>At the point the HTTPError is raised, request.process_body has not been called, so the request body (if there is one) will remain in the socket. When the next request comes from the client, the leftover body from the last request will be prepended to the new request.</p>
<p>So when the client sends  &#8220;POST /secret HTTP/1.1 &#8230;&#8221;, Cherrypy receives &#8220;bodyfromlastrequestPOST /secret HTTP/1.1 &#8230;&#8221; When the line is parsed, request.method is set to &#8220;bodyfromlastrequestPOST&#8221; instead of &#8220;POST&#8221;</p>
<p>Since the digest is created using the HTTP method, Cherrypy creates the wrong digest, and the digests don&#8217;t match. The authentication fails. The next time this cycle repeats, the body sent in this request will show up, and you won&#8217;t be able to log in.</p>
<h3>The Solution</h3>
<p>The way to fix this is to make sure the body is processed (or cleared from the socket). You can either modify the Cherrypy code, or monkey-patch the code from your application. I don&#8217;t like to modify library code because it becomes hard to track, especially when installing the application on multiple servers.</p>
<h4>Option A: Patch Cherrypy library files</h4>
<p>You can update the cherrypy library file: <a title="Cherrypy lib/auth.py" href="http://www.cherrypy.org/browser/tags/cherrypy-3.1.0/cherrypy/lib/auth.py#L62" target="_self"><strong>cherrypy/lib/auth.py</strong></a></p>

<div class="wp_syntax"><div class="code"><pre class="python"> <span style="color: #ff7700;font-weight:bold;">def</span> digest_auth<span style="color: black;">&#40;</span>realm, users<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;If auth fails, raise 401 with a digest authentication header.
&nbsp;
    realm: a string containing the authentication realm.
    users: a dict of the form: {username: password} or a callable returning a dict.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> check_auth<span style="color: black;">&#40;</span>users, realm=realm<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># inform the user-agent this path is protected</span>
    cherrypy.<span style="color: black;">response</span>.<span style="color: black;">headers</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'www-authenticate'</span><span style="color: black;">&#93;</span> = httpauth.<span style="color: black;">digestAuth</span><span style="color: black;">&#40;</span>realm<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Added: Process body to clean up requesy</span>
    cherrypy.<span style="color: black;">request</span>.<span style="color: black;">process_body</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">raise</span> cherrypy.<span style="color: black;">HTTPError</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">401</span>, <span style="color: #483d8b;">&quot;You are not authorized to access that resource&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The added call to cherrypy.request.process_body() will clean out the request body so the next request will not be fed the body from the last request</p>
<h4>Option B: Monkey Patch the code from your application (recommended)</h4>
<p>Run the following code before starting your server</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
<span style="color: #ff7700;font-weight:bold;">def</span> replace_digest_auth<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># alias old auth method</span>
    _old = cherrypy.<span style="color: black;">tools</span>.<span style="color: black;">digest_auth</span>.<span style="color: #008000;">callable</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args,<span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># New method to wrap the old method</span>
        <span style="color: #808080; font-style: italic;"># Catch HTTPErrors with status == 401 and clean up with process_body</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            _old<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> cherrypy.<span style="color: black;">HTTPError</span>, e:
           <span style="color: #ff7700;font-weight:bold;">if</span> e.<span style="color: black;">args</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> == <span style="color: #ff4500;">401</span>:
               cherrypy.<span style="color: black;">request</span>.<span style="color: black;">process_body</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
           <span style="color: #ff7700;font-weight:bold;">raise</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Replace the digest_auth callable with our new safe method</span>
    cherrypy.<span style="color: black;">tools</span>.<span style="color: black;">digest_auth</span>.<span style="color: #008000;">callable</span>=<span style="color: #dc143c;">new</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Do the replacement</span>
replace_digest_auth<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># Clean up file</span>
<span style="color: #ff7700;font-weight:bold;">del</span> replace_digest_auth</pre></div></div>

<p>What this code does is wrap the existing authorization method in a method that cleans up if authentication fails. Notice that though we catch the HTTPError we re-raise it so cherrypy can handle it appropriately.</p>
<h3>In Closing</h3>
<p>I would definitely consider this a Cherrypy bug, and I will look into whether a ticket has been filed for it. If you need it now, this will do. If you experience any problems, please let me know.</p>
<p>Leave a comment if you need more information or if you want more tips like this.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimhoskins.com/2008/07/18/cherrypy-digest-auth-post-problem-and-solution/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
