Skip to content
Browse files

a simple twitter app on heroku

  • Loading branch information...
1 parent 4c705ed commit db11f36327fb56bdef28850b8e7ed35a1c0d7cc3 @atmos committed May 10, 2010
Showing with 75 additions and 4 deletions.
  1. +31 −2 Rakefile
  2. +39 −1 lib/app.js
  3. +5 −1 server.js
View
33 Rakefile
@@ -1,8 +1,10 @@
# install all the shit you need to host this
require 'fileutils'
-express_version = ENV['EXPRESS_VERSION'] || '0.11.0'
-vendor_directory = File.join(File.dirname(__FILE__), 'vendor')
+express_version = ENV['EXPRESS_VERSION'] || '0.11.0'
+node_oauth_version = ENV['NODE_OAUTH_VERSION'] || 'v0.0.7'
+express_auth_version = ENV['EXPRESS_AUTH_VERSION'] || 'v0.0.3'
+vendor_directory = File.join(File.dirname(__FILE__), 'vendor')
desc "Package express to vendor/ for deployment"
task :setup do |t|
@@ -29,6 +31,33 @@ task :setup do |t|
FileUtils.rm_rf(file)
end
end
+
+ if File.directory?('express-auth')
+ puts "You already have a copy of express-auth"
+ else
+ # express auth for twitter auth
+ system("git clone git://github.com/ciaranj/express-auth.git")
+ Dir.chdir("express-auth") do
+ system("git checkout #{express_auth_version}")
+ system("rm -rf .git")
+ end
+ Dir['express-auth/*'].each do |file|
+ FileUtils.rm_rf(file) unless file =~ %r!express-auth/lib!
+ end
+ end
+
+ if File.directory?('oauth')
+ puts "You already have a copy of oauth"
+ else
+ system("git clone git://github.com/ciaranj/node-oauth.git oauth")
+ Dir.chdir("oauth") do
+ system("git checkout #{node_oauth_version}")
+ system("rm -rf .git")
+ end
+ Dir['oauth/*'].each do |file|
+ FileUtils.rm_rf(file) unless file =~ %r!oauth/lib!
+ end
+ end
end
end
task :default => :setup
View
40 lib/app.js
@@ -1,12 +1,50 @@
-// Example Express App on Heroku - Copyright Corey Donohoe <atmos@atmos.org> (MIT Licensed)
+// Example Twitter Express App on Heroku - Copyright Corey Donohoe <atmos@atmos.org> (MIT Licensed)
+var OAuth = require('oauth').OAuth
+var StrategyDefinition = require('express/plugins/strategyDefinition').StrategyDefinition;
+
+var twitterKeys = { consumerKey: process.env["TWITTER_CONSUMER_KEY"], consumerSecret: process.env["TWITTER_CONSUMER_SECRET"] }
+
+var anonStrategy = new StrategyDefinition(Anonymous),
+ twitterStrategy = new StrategyDefinition(Twitter, twitterKeys)
+
+
configure(function() {
set("root", __dirname)
use(Logger)
+ use(Cookie)
use(Static, { path: require("path").join(__dirname, "..", "public") })
+ use(Session, { lifetime: (150).seconds, reapInterval: (10).seconds })
+ use(Auth, { strategies: { "anon": anonStrategy, "twitter": twitterStrategy } })
enable("show exceptions")
})
+get ('/auth/twitter', function() {
+ var self = this
+ self.authenticate(['twitter'], function(error, authenticated) {
+ if( authenticated ) {
+ var oa= new OAuth("http://twitter.com/oauth/request_token",
+ "http://twitter.com/oauth/access_token",
+ twitterConsumerKey,
+ twitterConsumerSecret,
+ "1.0",
+ null,
+ "HMAC-SHA1");
+ oa.getProtectedResource("http://twitter.com/statuses/user_timeline.xml", "GET", self.session.auth["oauth_token"], self.session.auth["oauth_token_secret"], function (error, data) {
+ sys.p('got protected resource ')
+ self.respond(200, "<html><h1>Hello! Twitter authenticated user ("+self.session.auth.user.username+")</h1>"+data+ "</html>")
+ });
+ } else {
+ self.respond(200, "<html><h1>Twitter authentication failed :( </h1></html>")
+ }
+ })
+})
+
+get ('/logout', function() {
+ this.logout();
+ this.redirect("/")
+})
+
get("/", function() {
this.render("index.html.haml", {
locals: {
View
6 server.js
@@ -1,11 +1,15 @@
-// Example Express App on Heroku - Copyright Corey Donohoe <atmos@atmos.org> (MIT Licensed)
+// Example Twitter Express App on Heroku - Copyright Corey Donohoe <atmos@atmos.org> (MIT Licensed)
// add the vendored express to the require path
require.paths.unshift("vendor/express/lib")
+require.paths.unshift("vendor/express-auth/lib")
+require.paths.unshift("vendor/oauth/lib")
+
// require express and its plugins
require("express")
require("express/plugins")
+Object.merge(global, require('express/plugins/auth'))
//require the actual express app
require ("./lib/app")

0 comments on commit db11f36

Please sign in to comment.
Something went wrong with that request. Please try again.