<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>willnorris.com &#187; xmlrpc</title>
	<atom:link href="http://willnorris.com/tag/xmlrpc/feed" rel="self" type="application/rss+xml" />
	<link>http://willnorris.com</link>
	<description>there&#039;s more to life than this</description>
	<lastBuildDate>Tue, 15 May 2012 21:57:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-beta3-20574</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Authentication in WordPress 2.8</title>
		<link>http://willnorris.com/2009/03/authentication-in-wordpress-28</link>
		<comments>http://willnorris.com/2009/03/authentication-in-wordpress-28#comments</comments>
		<pubDate>Tue, 10 Mar 2009 20:50:07 +0000</pubDate>
		<dc:creator>Will Norris</dc:creator>
				<category><![CDATA[identity]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[diso]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[openid]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[xmlrpc]]></category>

		<guid isPermaLink="false">http://willnorris.com/?p=508</guid>
		<description><![CDATA[Use Case I&#8217;ve spent a lot of time working with the WordPress authentication system. I took over the OpenID plugin for WordPress two years ago, and was hired by Vidoop last May to work on the DiSo Project full time. Last summer, Matt Mullenweg invited me to talk at WordCamp SF 2008 about OAuth. As [...]]]></description>
			<content:encoded><![CDATA[<h2>Use Case</h2>

<p>I&#8217;ve spent a <strong>lot</strong> of time working with the WordPress authentication system.  I took over the <a href="http://wordpress.org/extend/plugins/openid/">OpenID plugin</a> for WordPress two years ago, and <a href="http://willnorris.com/2008/05/why-im-going-to-vidoop">was hired by Vidoop</a> last May to work on the <a href="http://diso-project.org/">DiSo Project</a> full time.  Last summer, Matt Mullenweg invited me to talk at <a href="http://2008.sf.wordcamp.org/">WordCamp SF 2008</a> about OAuth.  As you can see in my <a href="http://www.slideshare.net/willnorris/wordpress-oauth-presentation">slidedeck</a>, it was a lot of smoke and mirrors at that point&#8230; we didn&#8217;t have OAuth in WordPress, although it was on the roadmap for 2.7.</p>

<p>We&#8217;ve had an OAuth plugin for a little while that <a href="http://singpolyma.net/">Stephen Paul Weber</a> wrote, but it wasn&#8217;t until a couple of months ago that I finally sat down to polish it up.  The first use-case we wanted to tackled was XML-RPC, so I got to work with <a href="http://josephscott.org/">Joseph Scott</a>.  Having OAuth authentication with XML-RPC would allow for blog clients like MarsEdit or the WordPress iPhone app to communicate with your blog without having to share your WordPress password.</p>

<p><span id="more-508"></span></p>

<h2>Problem</h2>

<p>It wasn&#8217;t very long before we butted up against my biggest complaint about the WordPress authentication system &#8212; it is very &#8220;username/password&#8221; centric.  There are places in the authentication code where it bails out prematurely if the username or password are missing.  This isn&#8217;t a problem if your plugin simply wants to authenticate the user against a different password store like LDAP; in fact that works quite well.</p>

<p>The problem is that there are a number of authentication systems widely deployed in the wild (SAML, OpenID, OAuth, etc) that do not fit the standard model of username and password.  You can look at the OpenID plugin to see some of the more <em>interesting</em> things that need to be done in order to make it work on the various versions of WordPress.  However, when it came to hooking OAuth into the WordPress XML-RPC endpoint, there simply was no way to hack around it&#8230; we had to change some of the underlying assumptions.</p>

<p>It&#8217;s worth noting one additional requirement we had.  Because the <code>wp_authenticate()</code> function, which does most of the heavy lifting for WordPress authentication, resides in <code>pluggable.php</code> it is possible for a plugin to replace the function entirely and authenticate the user however they want.  The problem with this solution is that many authentication mechanisms don&#8217;t know if they should be invoked without examining the request.  If <code>wp_authenticate</code> is replaced, and then the plugin determines it shouldn&#8217;t intervene, then it&#8217;s already too late.  There is no way to pass the function call back to the standard version of <code>wp_authenticate()</code>.  This is actually the case for all functions in <code>pluggable.php</code>.  One possible solution is to create wrapper functions for everything, which I initially <a href="https://core.trac.wordpress.org/ticket/8833">advocated</a>.  Instead, <a href="http://peter.westwood.name/">Peter Westwood</a> came up with a better solution using a well-established model, which we ended up using for the new authentication system.</p>

<h2>Solution</h2>

<p>It took far more planning than actual coding, but we finally developed a solution that breaks the dependence on a username and password, but maintains backward compatibility with existing plugins that hook into the authentication code. WordPress 2.8 includes a new filter called <em>authenticate</em> which is passed three parameters: a mixed value (either a <code>WP_User</code> object, a <code>WP_Error</code> object, or <code>null</code>), along with the username and password (either or both of which may be <code>null</code>).  All of the standard WordPress authentication logic has been moved into two functions that implement this filter, both with relatively low priority.</p>

<ul>
<li><p><code>wp_authenticate_username_password()</code> (priority 20) includes the standard logic for authenticating using a standard username and password.  It still calls the <code>wp_authenticate_user</code> filter, so plugins that rely on that should be fine.  This function also performs the check for an empty username or password.</p></li>
<li><p><code>wp_authenticate_cookie()</code> (priority 30) is only added into the filter chain when the user is authenticating via <code>wp-login.php</code> and does the normal checking for the WordPress authentication cookie.</p></li>
</ul>

<p>Both of these functions first check to see if the first parameter passed in is a valid <code>WP_User</code> object, and immediately stop if it is.  This allows plugins to add their own functions into the filter chain which populate the <code>WP_User</code> object using whatever means they see fit.  WordPress still takes care of setting the authentication cookie when appropriate, so plugins need only worry with authenticating the user and returning a valid <code>WP_User</code> object.</p>

<p>So what will this look like for plugins?  Well, the OAuth plugin for WordPress isn&#8217;t finished yet, but the function below should be pretty close to the final version.  While there is of course a lot more code for actually implementing OAuth, this is the only hook  into the WordPress authentication system needed to make it work.  Note that this function doesn&#8217;t care about the username and password parameters that are available from the <code>authenticate</code> hook&#8230; other plugins may use them.</p>

<pre><code>add_filter('authenticate', 'oauth_authenticate');

/**
 * If the current request was signed using a valid OAuth access token, verify 
 * the request and return the associated user.
 *
 * @param WP_User|WP_Error|null $user authenticated user
 * @return WP_User|WP_Error|null OAuth authenticated user, if request was signed
 */
function oauth_authenticate($user) {
    if (Auth_OAuth_Signer::requestIsSigned()) {
        $oauth_server = oauth_server();
        $user_id = $oauth_server-&gt;verify();
        if ($user_id !== false) {
            $user = new WP_User($user_id);
        }
    }   

    return $user;
}
</code></pre>

<h2>For Plugin Authors</h2>

<p>If you&#8217;re currently hooking into the WordPress authentication system, <strong>especially</strong> if you&#8217;re providing a custom implementation of <code>wp_authenticate()</code>, take a look at the new <code>authenticate</code> hook.  If you are relying on the <code>wp_authenticate</code> action hook, you should also look closely to see if the new hook will do what you need.  We left the <code>wp_authenticate</code> hook in place for now, but I&#8217;m pretty sure it&#8217;s no longer necessary and will likely be removed in future releases.  If you are using the <code>wp_authenticate_user</code> hook exclusively, then you&#8217;re probably fine, but it&#8217;s probably still a good idea to take a look at the new stuff.</p>

<h2>So, OAuth in WordPress?</h2>

<p>We made additional changes to the WordPress XML-RPC code to make it use the new authentication system appropriately, so it is now possible to hook OAuth into WordPress without any core modifications.  We do in fact have a basic <a href="http://diso.googlecode.com/svn/wordpress/oauth/trunk/">OAuth plugin</a> that works with the trunk version of WordPress.  However, I don&#8217;t think I&#8217;m going to push to have the OAuth code included in WordPress 2.8 for two reasons:</p>

<ul>
<li><p>the OAuth libraries are in flux right now.  There have been two main PHP libraries that people have used for OAuth, both with their own strengths and weaknesses.  I&#8217;m currently working with the <a href="http://groups.google.com/group/oauth-php">oauth-php community</a> to combine these libraries  using the best parts from each, and a new clean architecture.  This effort can be found <a href="http://github.com/willnorris/oauth-php/">on github</a>.  (This library also requires PHP5 which is a deal breaker for WordPress&#8230; not sure how we&#8217;ll manage that.)</p></li>
<li><p>Because OAuth has the potential to be such an important part of how third party clients interact with a WordPress blog, I want to make sure we get this right.  Personally, I&#8217;d feel much more comfortable getting some real world experience with this code in a slightly more constrained environment by releasing it as a plugin first.  Once we&#8217;ve done that and are comfortable with how it integrates into WordPress (plugin API, admin interface, database schema, etc), I&#8217;m all for making it a core part of WordPress.</p></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://willnorris.com/2009/03/authentication-in-wordpress-28/feed</wfw:commentRss>
		<slash:comments>68</slash:comments>
		</item>
	</channel>
</rss>

