<?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>nFriedly Web Dev Tech Blog &#187; twitter</title>
	<atom:link href="http://nfriedly.com/techblog/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://nfriedly.com/techblog</link>
	<description>Expert Advice on Website Development, Javascript, Ajax, and Security</description>
	<lastBuildDate>Tue, 20 Jul 2010 14:58:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How AJAX Security and Twitter callbacks work</title>
		<link>http://nfriedly.com/techblog/2009/06/javascript-security-ajax-json-and-twitter-callbacks/</link>
		<comments>http://nfriedly.com/techblog/2009/06/javascript-security-ajax-json-and-twitter-callbacks/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 14:40:33 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/2009/06/javascript-security-ajax-json-and-twitter-callbacks/</guid>
		<description><![CDATA[The twitter callback feature is nice &#8211; it makes it extremely easy to to add a twitter feed to a page. But to get the most benefit out of it, you really need to understand what it&#8217;s doing. We&#8217;re going to look at how AJAX security works, specifically the Same Origin Policy, how Twitter gets [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="Breaking javascript - the right way" src="http://www.sxc.hu/pic/m/s/si/simonok/323276_game_of_pool.jpg" alt="" width="141" height="188" />The twitter callback feature is nice &#8211; it makes it extremely easy to to add a twitter feed to a page. But to get the most benefit out of it, you really need to understand what it&#8217;s doing.</p>
<p>We&#8217;re going to look at how AJAX security works, specifically the Same Origin Policy, how Twitter gets around it, and the type of callback that twitter uses.</p>
<p>Note: the callback that twitter uses is entirely different from callback in the sense of passing a javascript function around as a variable. We&#8217;ll look at that in a future article.</p>
<p><span id="more-114"></span></p>
<h2>AJAX Security</h2>
<p>The XMLHTTPRequest Object, which is the javascript object used to make AJAX requests, has a &#8220;<a href="https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript">Same Origin Policy</a>&#8221; which basically means that javascript on site1.com cannot use AJAX to directly load data from site2.com. This is a security feature, as it makes XSS (Cross-Site Scripting) attacks more difficult.</p>
<p>Worth noting, if the website is at site1.com, no scripts can communicate with any other site, even if the script was loaded from site2.com.</p>
<h2>Work Arounds</h2>
<p>There are a number of workarounds including iframes, java applets, and flash, but here&#8217;s a couple of the more common methods.</p>
<h3><img src="http://www.sxc.hu/pic/m/c/cw/cwmgary/486891_all_lined_up.jpg" alt="Line em up!" class="alignright" />Proxying Requests</h3>
<p>The way proxying works is to have a file on your server that grabs the data from a remote server and passes it along. Then for javascript, the data appears to be coming from your server, even though it actually originated at a remote server. This is what the Fancy part of my <a href="http://nfriedly.com/demos/twitter">twitter demo</a> does.</p>
<p>We&#8217;ll look at using a proxy to get remote data in a future article.</p>
<h3>Remotely hosted javascript files</h3>
<p>Scripts stored on other websites can be included on a page. As long as the script doesn&#8217;t need to call home after the initial load, everything works great. This is how a basic twitter function works: you load a script from twitter&#8217;s website and it communicates with your site via the callback feature. This is what the Simple part of my <a href="http://nfriedly.com/demos/twitter">twitter demo</a> does.</p>
<p>Here is a very basic page that uses Twitter&#8217;s callback feature and a remotely loaded javascript file to show my twitter status &#8211; remote data &#8211; on my website, by interacting with local javascript.</p>
<pre class="brush: xml;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;  dir=&quot;ltr&quot; lang=&quot;en-US&quot;&gt;

&lt;head&gt;
&lt;title&gt;Simple Twitter Status&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;My Twitter Status:&lt;/h1&gt;

&lt;div id=&quot;twitter_status&quot;&gt;Loading...&lt;/div&gt;

&lt;!-- Put scripts down here for speed --&gt;

&lt;!-- this must come before we load the twitter script --&gt;
&lt;script type=&quot;text/javascript&quot;&gt;

function showStatus(json){

	json = json[0]; // we only care about the most recent status;

	var myDiv = document.getElementById('twitter_status');

	myDiv.innerHTML = '&lt;img src=&quot;'
		+ json.user.profile_image_url
		+ '&quot; style=&quot;float:left; margin:5px 10px 10px 0&quot;&gt;'
		+ json.text;
}
&lt;/script&gt;

&lt;!-- now load the twitter file --&gt;
&lt;script type=&quot;text/javascript&quot;
src=&quot;http://twitter.com/statuses/user_timeline/nfriedly.json?count=1&amp;amp;callback=showStatus&amp;amp;random=&lt;?php echo time(); ?&gt;&quot; /&gt;
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>You can see a live copy of this code at <a href="http://nfriedly.com/demos/twitter-extra-simple">http://nfriedly.com/demos/twitter-extra-simple</a>.</p>
<h2>Digging into Twitter&#8217;s callback method</h2>
<p>Below is a trimmed down example of what Twitter&#8217;s API sends back when we make the request in the example above.</p>
<pre class="brush: jscript;">
showStatus([{&quot;in_reply_to_screen_name&quot;:null,&quot;text&quot;:&quot; [ Lots of information that I'm omitting because it's not the point. ] &quot;]);
</pre>
<p>Now, don&#8217;t worry about the jazz in the middle, just look at that showStatus(); that&#8217;s wrapped around it. First of all, how does Twitter even know that we have a function named show status? Because we said so in the url to the file -see how we added <code>&amp;callback=showStatus</code>? That&#8217;s where the magic is.  (Ok, technically we said <code>&amp;amp;</code> not just <code>&amp;</code>, but that was just to pass XHTML validation. )</p>
<p><img class="alignright" title="The break!" src="http://www.sxc.hu/pic/m/l/lj/ljweb/490307_pool_break.jpg" alt="" width="300" height="168" /></p>
<h3>Cross-domain!</h3>
<p>There&#8217;s a second important thing going on here &#8211; javascript from two different domains are interacting with each other. This is allowed because of how the Same Origin Policy works &#8211; everything is restricted to the local domain, but that means that everything can work together on the same plane.</p>
<h3>It&#8217;s a beautiful thing</h3>
<p>I hope this gave you a little bit better understanding of how AJAX security works and how Twitter gets around it and is still able to interact with your site. In the future, I&#8217;ll have an article on how &#8220;traditional&#8221; callbacks work that will use jQuery and more AJAX to dive a bit deeper into the topic.</p>
<h2>Javascript Ninja for Hire</h2>
<p>I have <a href="http://nfriedly.com/portfolio">extensive experience</a> working with AJAX, Twitter, and related technologies. I&#8217;m just the man you need to make your next <a href="http://nfriedly.com/webdev">javascript development</a> project shine!</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2009/06/javascript-security-ajax-json-and-twitter-callbacks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
