How to quickly set up a test for Twitter OAuth authentication from your local machine

Working with API’s such as Twitter from your local machine can be a pain. A problem that comes up is Twitter does not let you set your callback URL to hit your localhost. If you are working with OAuth on your local machine and want to test the user authorization flow, you are screwed. I will explain how to circumnavigate this issue on Mac OS X with Firefox. This tutorial assumes you have set up a Twitter oauth application and specified a callback url in the application settings. At the end of the tutorial, I will present a much faster way to accomplish this task.

Important Update:
As Mark Puig mentioned in the comments below, Twitter is currently allowing you to register URL’s like “http://127.0.0.1:8000/twitter_callback” as your Twitter callback URL (pretty sure this was not allowed before) . That being said, this article is still useful for those using API’s that do not allow you to register your local host as a callback and for those who want to learn more about hacking your DNS settings. And if Twitter restricts this in the future…

Step 1 (optional): Force Firefox to expire your DNS cache.
Expiring your DNS cache will force Firefox to take a fresh look at your /etc/hosts file on each request. Otherwise you may have to wait a minute forchanges to /etc/hosts to take effect. This step isn’t necessary but it will save you time if you edit /etc/hosts often.

  1. Type “about:config” into your browser address bar.
  2. When you get to the config options, right click and go to “New” -> “Integer”. Enter “network.dnsCacheExpiration” as the preference name and “0″ as the integer value.
  3. Repeat for Step 2 preference name “network.dnsCacheEntries” and the integer value “0″.

The integer value “0″ for dnsCacheExpiration is the number of seconds it will take for the DNS cache to expire. As a side note, you can increase the performance of Firefox by expiring the cache much less often (set dnsCacheExpiration to “3600″ for once per hour). Obviously you do not want to do this if you are messing around with your DNS settings such as in this example.

Step 2: Trick your browser
Open up /etc/hosts and add a line like the following:
127.0.0.1 xyz.com
where xyz.com is your real host.

If your Twitter callback URL has “www” like www.xyz.com, you must use www.xyz.com instead of xyz.com. This line forces your browser to resolve xyz.com to your localhost instead of visiting the actual website. If you completed step 1 you can type http://xyz.com into your browser address bar and watch it hit your localhost. If you skipped step 1, flush your browser’s DNS cache or wait a minute for the settings to pick up.

Step 3: Add directory and port redirection
Chances are your application’s twitter callback URL is not http://xyz.com, but rather something like http://xyz.com/twitter_callback …plus the oauth_token GET parameter. If this is true, you need to create an identical path on your localhost to a callback directory and forward to wherever you want. This is best explained by example.

My callback URL is http://xyz.com/twitter_callback and Twitter appends the oauth_token to the URL so it looks something like:
http://www.xyz.com/twitter_callback?oauth_token=pyOYM5tbvK71fvt…
Since my localhost points to “/Library/WebServer/Documents”, I created a directory “/Library/WebServer/Documents/twitter_callback/” which is where my browser will go when Twitter redirects me to the callback URL. Of course my browser will be looking for an index.php file, so I created one that looks like this:

<?php
	if($_SERVER['HTTP_HOST'] == 'xyz.com'){
                //$_SERVER['REQUEST_URI'] makes sure the oauth_token is appended 
		header("Location: http://localhost:3000".$_SERVER['REQUEST_URI']);
	}
?>

This file forwards the request to port 3000 which you cannot accomplish through DNS settings. Now I have the Twitter callback URL pointing to the proper directory in my Ruby On Rails application. Again, if your Twitter callback URL is of the form www.xyz.com, you must use www.xyz.com instead of xyz.com for the HTTP_HOST check.

Update:
There is actually a much easier way to accomplish this assuming you don’t need any URL parameters passed from the service to your application upon callback. You can use bit.ly, a URL shortening service. Just shorten the url “http://localhost:3000/twitter_callback” and register the shortened URL as the callback in your Twitter app. For this method, you have to create another Twitter OAuth app for development so that the callback URL’s can differ. Using bit.ly is faster and easier than the 3-step method above, but I will leave that method posted because it allows parameters to be passed back if necessary and it teaches you a little bit about FF and OS X.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | Tagged , , , , , , | 6 Comments

Defending Against Attacks With Rails

A couple weeks ago I presented on Rails security at a local Ruby On Rails meetup. I finally got around to posting the slides online.

The presentation covers topics including authentication, hashing, salting, key stretching, white listing, session hijacking, replay attacks, session fixation, cross-site request forgery, cross site scripting, sql injections, other injections, and some other Rails related security issues. Let me know if you have questions and please give me some feedback. I didn’t get too creative with the presentation because there was a lot of content to cover.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | Tagged , , , , , , , , , , , | 1 Comment

How to point your browser at websites on your server without registering a domain

I recently started managing my own server and found it difficult to point my browser somewhere on my system without going through DNS records and registering a domain name. That is sort of a pain, but here is a quick hack to make this super easy.

Let’s say you want to create an admin site for your server. This could be useful to view the output of phpinfo() or something like that. Just set up a virtual host like you normally would. For example:

<VirtualHost *:80>
     ServerAdmin youremail@email.com
     ServerName server-admin.net
     ServerAlias server-admin.net    
 
     DirectoryIndex index.php index.html
     DocumentRoot /var/www/server-admin
 
     <Directory "/var/www/server-admin">
         Options Indexes FollowSymLinks
         AllowOverride All
         Order allow,deny
         Allow from all
     </Directory>
</VirtualHost>

Then just add this to the bottom of your /etc/hosts file (assuming your OS is *nix):
your_servers_ip_address server-admin.net

Now you should be able to see that directory in your browser.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | Tagged , , , | Leave a comment

Text inputs with rounded corners using jQuery (without image)

I have yet to find a solution for making rounded inputs without an image. I hacked one together today because I know it will save me a lot of time later. While the solution is hackish, I like it better than going into photoshop and making an image every time I want to change the length of a text input.

The solution has two dependencies: jQuery and jQuery.corner. Download jQuery here and jQuery.corner here.

You can skip the explanation and go straight to the demo, or check out this quick explanation. Note that in an effort to get this done quickly, my code isn’t too smart and you may have to mess with some of the values depending on the size of your text inputs. I still think it beats the crap out of getting on photoshop for every change.

First you must include the necessary files:

<script src="./js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="./js/jquery.corner.js" type="text/javascript"></script>

and the roundInput function with the function calls:

/* input_id is the ID of the input element */
/* container_class will let you control the text input background color and padding */
/* border_class will let you control the border color */
function roundInput(input_id, container_class, border_class){
	var input = $('#'+input_id+'');
	var input_width = input.css("width"); //get the width of input
	var wrap_width = parseInt(input_width) + 10; //add 10 for padding
	wrapper = input.wrap("<div class='"+container_class+"'></div>").parent();
	wrapper.wrap("<div class='"+border_class+"' style='width: "+wrap_width+"px;'></div>"); //apply border
	wrapper.corner("round 8px").parent().css('padding', '2px').corner("round 10px"); //round box and border
}
 
/* round 2 inputs */
$(function(){
	roundInput('rounded_input1','rounded_container','rounded_border');
	roundInput('rounded_input2','rounded_container','rounded_border');
});

Obviously replace the above roundInput function calls with your own input id, container class, and border class. The CSS below allows you to control the border color, text input background color, and the space between the text input and the rounded corners.

<style>
/* Container class */
div.rounded_container{
        /* This background color should have the same background color as your text input */
        /* Changing this color will show you how the rounded input is laid out */
        background-color: #fff;
        /* This padding is between the text input and the rounded border on all sides */
        /* Decreasing the value may interfere with the rounded corners */
        padding: 4px;
}
/* Border class */		
div.rounded_border {  
	/* This is the color of your rounded border */
	background: #6666ff; 
}
input.rounded_input{
	border:0;
}
</style>

And finally the text inputs to round (replace these with your own):

<input id="rounded_input1" type="text" class="rounded_input" style="width: 286px;" />
<input id="rounded_input2" type="text" class="rounded_input" style="width: 286px;" />

This code has not been heavily tested, but it looks great on FF, IE7, Safari 5, and Chrome 5. I will post updated code if I decide to create something more flexible.

Enjoy the demo!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | Tagged , , , | 8 Comments

Simple Time Select for Ruby On Rails Just Got Simpler

I added a start_hour and end_hour feature to my simple time select. Here are the details from the README:

Simple time select also takes a start_hour and end_hour option to be specified in military format (between 0-23).

<%= time_select "event", "time", { :default => Time.now.change(:hour => 21), :simple_time_select => true, 
:minute_interval => 20, :time_separator => "", :start_hour => 10, :end_hour => 14 } %>

The start hour behaves as you would expect but the end_hour may not. If you specify the end_hour as 10, your time select will include 10:15, 10:30, 10:45. So the end_hour sets the last hour that the time select will include. Leave a comment if you want me to change this.

The code is hosted on github here: http://github.com/tamoyal/simple_time_select/tree/master

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | Tagged , , , | 7 Comments

jQuery Rounded Corners with Rounded Borders with Set Width

Dave Methvin wrote a plugin for jQuery to easily round corners. Then Mike Alsup improved on the plugin by adding more effects. Mike’s solution for adding a rounded border is great but he doesn’t give good copy and paste code. I had to dig through the source on the page to figure out some usage. He also misses (or I cannot find) an important piece – some of us want to specify the dimensions of our rounded box with rounded borders. Before using this code, you need to make sure you have downloaded jQuery and jquery.corner.js and obviously include them on your page.

Here is a simple explanation and example with code:

div.rounded_box{
    background-color: #ccdaff; 
    color: #333; 
    padding: 10px;
    border:0;
    margin: 0;
    zoom:1;
}
/* border with rounded corners */
div.rounded_border {  background: #6666ff; padding: 8px; }

That is the CSS for the rounded box and rounded border to go around the box. The only attributes you should need to change are anything color related and the padding (pretty obvious what they do but you can mess with the values after you get this example working).

Now you have 2 options for your HTML/Javascript set up (note that I have namespaced my jQuery functions to $j instead of $). The first option is probably a bit faster while the second option avoids an extra wrapper div in your code, allowing you to specify the width inline. Here is the first option:

<div style="width: your width here;">
     <div id="my_rounded_box_id1" class="rounded_box">
          <p>Content</p>
     </div>
</div>
 
<div style="width: your width here;">
     <div id="my_rounded_box_id2" class="rounded_box">
          <p>Content</p>
     </div>
</div>
     $j(function(){
         $j('div.rounded_box').wrap('<div class="rounded_border"></div>'); //apply border
	 $j('div.rounded_box').corner("round 8px").parent().css('padding', '2px').corner("round 10px"); //round box and border
     });

Here is the second option:

<div id="my_rounded_box_id1" class="rounded_box" style="width: your width here;">
     <p>Content</p>
</div>
 
<div id="my_rounded_box_id2" class="rounded_box" style="width: your width here;">
     <p>Content</p>
</div>
function roundDiv(id){
	var tmp_width = $j('#'+id+'').css("width"); //get the width of target el
	$j('#'+id+'').css("width",""); //clear width value (or else corners screws up)
	$j('#'+id+'').wrap('<div class="rounded_border" style="width: ' +tmp_width+ ';"></div>'); //apply border
	$j('#'+id+'').corner("round 8px").parent().css('padding', '2px').corner("round 10px"); //round box and border
}
 
$j(function(){
 	//round each box separately so that we can specify inline dimensions
	roundDiv('my_rounded_box_id1');
	roundDiv('my_rounded_box_id2');
});

Enjoy!

UPDATE: After some more testing, the second option seems to be more cross browser compatible. I highly recommend using that option.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | Tagged , , , | 25 Comments

Fix for broken drag and drop in Wordpress Sociable plugin

Drag and drop is broken in the Wordpress Sociable plugin which means you cannot easily reorder your bookmarking options. I have seen posts 2 weeks old asking for a fix for this and I am too impatient…so here it is.

Navigate to your sociable plugin directory and create a file called “sociable-admin.js”. Add the following code to the file:

jQuery(document).ready(function(){
  jQuery("#sociable_site_list").sortable({});
});
/* make checkbox action prettier */
function toggle_checkbox(id) {
	var checkbox = document.getElementById(id);
 
	checkbox.checked = !checkbox.checked;
	if (checkbox.checked)
		checkbox.parentNode.className = 'active';
	else
		checkbox.parentNode.className = 'inactive';
}

Save the file and reload your sociable admin panel. Drag and drop should work now.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | Tagged , , , , | 1 Comment

Truncate your Flex ComboBox text

Today I ran into a situation where I wanted my ComboBox drop down text to have a 9px font size and a 10px font size on the text input portion. This is a problem because your labels can easily get cut off.

Ugly:
ComboBox text cut off
I think this is prettier:
ComboBox text with ellipsis



Here is how to do it:

<mx:Script>
	<![CDATA[
            [Bindable] private var carriers:ArrayCollection = new ArrayCollection();
            private static const CARRIER_TEXT_MAX_WIDTH:int = 18;
 
            private function some_type_of_init_function():void{
                  carriers = ConfigurationData.getInstance().mobile_carriers;
            }
 
            private function truncatePrompt():void{
		if(carrier_select.text.length > CARRIER_TEXT_MAX_WIDTH){
		   carrier_select.text = carrier_select.text.slice(0,CARRIER_TEXT_MAX_WIDTH-3) + "...";
		}
	     }
	]]>
</mx:Script>
<mx:ComboBox id="carrier_select" rowCount="5" height="26" width="154" 
		dataProvider="{carriers}"
		change="truncatePrompt()" />

You will obviously have to change the CARRIER_TEXT_MAX_WIDTH to fit your needs. Note that calling this on the “change” event is not as pretty because there is a delay.
UPDATE:
Turns out for this to work properly you will have to call the function from the change event. Sorry for not testing this more before I posted.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | 13 Comments

The Art And Science Of Seductive Interactions

I will generally stay away from posting other people’s content on my blog because this is not a news site. But I really liked this presentation… (sorry if it is a bit slow loading, slideshare isn’t perfect)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in marketing | Leave a comment

Flex widget with rounded corners (code included)

It should be really easy to create the template for a widget with rounded corners in Flex. It is certainly not a difficult task, but it is not obvious. Rather than explain the things you need to pay attention to, I will just post the code below. After setting up a flex project with the following code, add and remove some lines to see what they do. Feel free to post any questions you have here.

Here is the application code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="absolute" width="450" height="220" backgroundAlpha="0">
 
	<mx:Style>
		Panel {
		   borderStyle: solid;
		   borderThickness: 0;
		   cornerRadius: 10;
		   borderColor: #cccccc;
		   backgroundColor: #eeeeee;
		   roundedBottomCorners: true;
		}
	</mx:Style>
 
	<mx:Panel id="application_container" width="100%" height="100%" 
		dropShadowEnabled="false">
 
	</mx:Panel>
 
</mx:Application>

In your wrapper file, make sure to set wmode to transparent. Here is how you do it with SWFObject (assuming your object is “so”):

so.addParam("wmode","transparent");
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

Posted in Software | Tagged , , , , | Leave a comment