rack_facebook_connect 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ /live
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michael Bleigh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,103 @@
1
+ = Rack::FacebookConnect
2
+
3
+ This is a Rack middleware to provide as simple as possible Facebook Connect functionality to Rack apps. It is built to be as simple and
4
+ unobtrusive as possible and mimic the kinds of authentication flows more common with other SSO providers such as OpenID or OAuth.
5
+
6
+ == Installation
7
+
8
+ Rack::FacebookConnect is available as a RubyGem:
9
+
10
+ gem install rack_facebook_connect
11
+
12
+ == Application Setup
13
+
14
+ To use Rack::Facebook connect you will need to install it as a middleware in your Rack application. You will also need to set up a Facebook application for yourself with appropriate credentials in the Connect settings panel for your application.
15
+
16
+ === Rails 2.3-2.X
17
+
18
+ To use Rack::FacebookConnect in your Rails 2.X application you need to add it to your gems and middlewares. In <tt>environment.rb</tt> do the following:
19
+
20
+ config.gem 'rack_facebook_connect'
21
+ config.middleware.use 'Rack::FacebookConnect', 'your_api_key', 'your_api_secret'
22
+
23
+ === Rails 3
24
+
25
+ In Rails 3.0 you simply need to add the middleware to your Gemfile and the provided <tt>config.ru</tt> file before <tt>run YourApp::Application</tt>:
26
+
27
+ # in Gemfile
28
+ gem 'rack_facebook_connect'
29
+
30
+ # in config.ru
31
+ use Rack::FacebookConnect 'your_api_key', 'your_api_secret'
32
+
33
+ == Sinatra
34
+
35
+ In Sinatra you need to require the Rack::FacebookConnect file somehow (either through bundler or just <tt>require 'rack/facebook_connect'</tt> if you have RubyGems accessible) and then simply declare a "use" statement in your code before your DSL routes:
36
+
37
+ use Rack::FacebookConnect 'your_api_key', 'your_api_secret'
38
+
39
+ get '/'
40
+ #...
41
+ end
42
+
43
+ == Usage
44
+
45
+ Once you've installed Rack::FacebookConnect as a middleware it will take care of many things for you. Rack::FacebookConnect handles:
46
+
47
+ 1. Adding the required Javascript files to any HTML files rendered by your application to perform Facebook Connect actions.
48
+ 2. Providing the cross-domain receiver file for Facebook's Javascript to bridge to your application.
49
+ 3. A convenient method to perform an authentication 'callback' like with other systems.
50
+
51
+ Because of the Javascript Rack::FacebookConnect automatically injects into your site, you will be able to use XFBML, meaning that to provide a Facebook Connect login experience you simply need to add the following code to your application's HTML:
52
+
53
+ <fb:login-button onlogin='rack_fbconnect()'></fb:login-button>
54
+
55
+ This will render a Facebook Connect button that will prompt the user first to log into Facebook and then to grant access to your application for certain permissions. Once that is done, your user will be redirected to the callback URL.
56
+
57
+ === Callback
58
+
59
+ Rack::FacebookConnect doesn't make assumptions about how you want to handle your user system. Instead, you simply need to provide some route on your application that responds to the path <tt>/auth/facebook/callback</tt>. This URL will be the destination of the redirect performed after authentication. On this URL Rack::FacebookConnect will modify the request parameters with an <tt>:auth</tt> key that provides information about the logged in user. So in Rails you could do something like this:
60
+
61
+ # in routes.rb
62
+ map.connect '/auth/facebook/callback', :controller => 'sessions', :action => 'fb_connect'
63
+
64
+ # in app/controllers/sessions_controller.rb
65
+ class SessionsController < ApplicationController
66
+ def fb_connect
67
+ @user = User.find_by_facebook_uid(params[:auth][:user_id]) ||
68
+ User.create(:facebook_uid => params[:auth][:user_id],
69
+ :name => params[:auth][:info][:name],
70
+ :email => params[:auth][:info][:email])
71
+ session[:user_id] = @user.id
72
+ redirect_to root_path
73
+ end
74
+ end
75
+
76
+ Or if you're in Sinatra:
77
+
78
+ get '/auth/facebook/callback' do
79
+ @user = User.find_by_facebook_uid(params[:auth][:user_id]) ||
80
+ User.create(:facebook_uid => params[:auth][:user_id],
81
+ :name => params[:auth][:info][:name],
82
+ :email => params[:auth][:info][:email])
83
+ session[:user_id] = @user.id
84
+ redirect '/'
85
+ end
86
+
87
+ == Gotchas
88
+
89
+ * Facebook's privacy policy requires that any user information other that the UID be stored for no longer than 24 hours. This means to comply with this policy you will need to re-fetch user information daily by storing the user's session key and secret to make subsequent API calls.
90
+
91
+ == Note on Patches/Pull Requests
92
+
93
+ * Fork the project.
94
+ * Make your feature addition or bug fix.
95
+ * Add tests for it. This is important so I don't break it in a
96
+ future version unintentionally.
97
+ * Commit, do not mess with rakefile, version, or history.
98
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
99
+ * Send me a pull request. Bonus points for topic branches.
100
+
101
+ == Copyright
102
+
103
+ Copyright (c) 2010 Michael Bleigh. See LICENSE for details.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack_facebook_connect"
8
+ gem.summary = %Q{A Rack middleware for authentication via Facebook Connect.}
9
+ gem.description = %Q{A Rack middleware that tries to make FB Connect behave a little bit more like other identity providers such as OpenID and OAuth.}
10
+ gem.email = "michael@intridea.com"
11
+ gem.homepage = "http://github.com/intridea/rack_facebook_connect"
12
+ gem.authors = ["Michael Bleigh"]
13
+ gem.add_dependency 'mini_fb'
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "rack_facebook_connect #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,108 @@
1
+ require 'rack/facebook_connect/helpers'
2
+
3
+ module Rack
4
+ class FacebookConnect
5
+ include Helpers
6
+
7
+ # Initialize the Rack::FacebookConnect middleware. Requires
8
+ # a Facebook API key and secret and takes the following options:
9
+ #
10
+ # <tt>:permissions</tt> :: An array of Facebook extended permissions, defaults to <tt>%w(email offline_access)</tt>
11
+ # <tt>:get_info</tt> :: Boolean as to whether to fetch info about the user on login. Defaults to "true".
12
+ #
13
+ def initialize(app, api_key, api_secret, options = {})
14
+ @options = {
15
+ :permissions => %w(email offline_access),
16
+ :get_info => true
17
+ }.merge(options)
18
+
19
+ @app = app
20
+ @api_key = api_key
21
+ @api_secret = api_secret
22
+ end
23
+
24
+ def call(env)
25
+ dup._call(env)
26
+ end
27
+
28
+ def _call(env) #:nodoc:
29
+ @env = env
30
+ @base_url = (request.scheme.downcase == 'https' ? 'https://ssl.connect.facebook.com' : 'http://static.ak.connect.facebook.com')
31
+
32
+ case request.path
33
+ when "/auth/facebook/xd_receiver.html"
34
+ xd_receiver
35
+ when '/auth/facebook/callback'
36
+ perform_callback
37
+ else
38
+ inject_facebook
39
+ end
40
+ end
41
+
42
+ def xd_receiver #:nodoc:
43
+ xd = <<-HTML
44
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
45
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
46
+ <html xmlns="http://www.w3.org/1999/xhtml" >
47
+ <head>
48
+ <title>Cross-Domain Receiver Page</title>
49
+ </head>
50
+ <body>
51
+ <script src="#{@base_url}/js/api_lib/v0.4/XdCommReceiver.js" type="text/javascript"></script>
52
+ </body>
53
+ </html>
54
+ HTML
55
+
56
+ Rack::Response.new(xd).finish
57
+ end
58
+
59
+ def perform_callback #:nodoc:
60
+ request[:auth] = {
61
+ :provider => :facebook,
62
+ :user_id => request.cookies["#{@api_key}_user"],
63
+ :session => {
64
+ :key => request.cookies["#{@api_key}_session_key"],
65
+ :secret => request.cookies["#{@api_key}_ss"],
66
+ :expires => (Time.at(request.cookies["#{@api_key}_expires"].to_i) if request.cookies["#{@api_key}_expires"].to_i > 0)
67
+ }
68
+ }
69
+
70
+ if @options[:get_info]
71
+ require 'mini_fb'
72
+ request[:auth][:info] = MiniFB.call(@api_key, @api_secret, "Users.getInfo", 'session_key' => request[:auth][:session][:key], 'uids' => request[:auth][:user_id], 'fields' => MiniFB::User.all_fields)[0]
73
+ end
74
+
75
+ @app.call(@env)
76
+ end
77
+
78
+ def inject_facebook #:nodoc:
79
+ status, headers, responses = @app.call(@env)
80
+ responses = Array(responses) unless responses.respond_to?(:each)
81
+
82
+ if headers["Content-Type"] =~ %r{(text/html)|(application/xhtml+xml)}
83
+ resp = []
84
+ responses.each do |r|
85
+ r.sub! /(<html[^\/>]*)>/i, '\1 xmlns:fb=\"http://www.facebook.com/2008/fbml\">'
86
+ r.sub! /(<body[^\/>]*)>/i, '\1><script src="' + @base_url + '/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>'
87
+ r.sub! /<\/body>/i, <<-HTML
88
+ <script type="text/javascript">
89
+ FB.init("#{@api_key}", "/auth/facebook/xd_receiver.html");
90
+ function rack_fbconnect() {
91
+ FB.Connect.showPermissionDialog("#{Array(@options[:permissions]).join(',')}", function(perms) {
92
+ if (perms) {
93
+ window.location.href = '/auth/facebook/callback';
94
+ } else {
95
+ window.location.href = '/auth/facebook/callback?permissions=denied';
96
+ }
97
+ });
98
+ }
99
+ </script></body>
100
+ HTML
101
+ resp << r
102
+ end
103
+ end
104
+
105
+ Rack::Response.new(resp || responses, status, headers).finish
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,11 @@
1
+ require 'digest/md5'
2
+
3
+ module Rack
4
+ class FacebookConnect
5
+ module Helpers
6
+ def request #:nodoc:
7
+ @request ||= Rack::Request.new(@env)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ require 'rack/facebook_connect'
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rack_facebook_connect}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Bleigh"]
12
+ s.date = %q{2010-03-29}
13
+ s.description = %q{A Rack middleware that tries to make FB Connect behave a little bit more like other identity providers such as OpenID and OAuth.}
14
+ s.email = %q{michael@intridea.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rack/facebook_connect.rb",
27
+ "lib/rack/facebook_connect/helpers.rb",
28
+ "lib/rack_facebook_connect.rb",
29
+ "rack_facebook_connect.gemspec",
30
+ "spec/rack_facebook_connect_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/intridea/rack_facebook_connect}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{A Rack middleware for authentication via Facebook Connect.}
39
+ s.test_files = [
40
+ "spec/rack_facebook_connect_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<mini_fb>, [">= 0"])
50
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
51
+ else
52
+ s.add_dependency(%q<mini_fb>, [">= 0"])
53
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<mini_fb>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,72 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class TestApp < Sinatra::Base
4
+ get '/' do
5
+ '<html><head></head><body><fb:login-button onlogin="rack_fbconnect()"></fb:login-button></body></html>'
6
+ end
7
+
8
+ get '/not-html.json' do
9
+ content_type :xml
10
+ '<html><head></head><body><fb:login-button></fb:login-button></body></html>'
11
+ end
12
+ end
13
+
14
+ def app
15
+ Rack::Builder.new {
16
+ use Rack::FacebookConnect, *(@params || ['my_app_key', 'my_app_secret'])
17
+ run TestApp
18
+ }.to_app
19
+ end
20
+
21
+ describe Rack::FacebookConnect do
22
+ after { @app = nil }
23
+
24
+ context ' Javascript injection' do
25
+ it 'should inject the appropriate pieces' do
26
+ get '/'
27
+ last_response.body.should be_include('FB.init')
28
+ last_response.body.should be_include('FeatureLoader.js.php')
29
+ last_response.body.should be_include('xmlns:fb=\"http://www.facebook.com/2008/fbml\">')
30
+ end
31
+
32
+ it 'should use SSL when appropriate' do
33
+ get '/', {}, {'rack.url_scheme' => 'https', 'HTTPS' => 'on'}
34
+ last_response.body.should be_include('https://ssl.connect.facebook.com')
35
+ end
36
+
37
+ it 'should not use SSL when not appropriate' do
38
+ get '/'
39
+ last_response.body.should_not be_include('https://ssl.connect.facebook.com')
40
+ end
41
+
42
+ it 'should not inject on non HTML datatypes' do
43
+ get '/not-html.json'
44
+ last_response.body.should_not be_include('FB.init')
45
+ end
46
+ end
47
+
48
+ context ' /auth/facebook/xd_receiver' do
49
+ it 'should be at /auth/facebook/xd_receiver.html' do
50
+ get '/auth/facebook/xd_receiver.html'
51
+ last_response.body.should be_include('XdCommReceiver.js')
52
+ end
53
+
54
+ it 'should use SSL when appropriate' do
55
+ get '/auth/facebook/xd_receiver.html', {}, {'rack.url_scheme' => 'https', 'HTTPS' => 'on'}
56
+ last_response.body.should be_include('https://ssl.connect.facebook.com')
57
+ end
58
+
59
+ it 'should not use SSL when not appropriate' do
60
+ get '/auth/facebook/xd_receiver.html'
61
+ last_response.body.should_not be_include('https://ssl.connect.facebook.com')
62
+ end
63
+ end
64
+
65
+ context ' /auth/facebook/callback' do
66
+ it 'should call to MiniFB' do
67
+ require 'mini_fb'
68
+ MiniFB.should_receive(:call).and_return({})
69
+ get '/auth/facebook/callback'
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ --color
2
+ --backtrace
3
+ --format=specdoc
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+ require 'sinatra'
8
+ require 'rack/test'
9
+ require 'rack/facebook_connect'
10
+
11
+ include Rack::Test::Methods
12
+
13
+ Spec::Runner.configure do |config|
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_facebook_connect
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Michael Bleigh
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-29 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mini_fb
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: rspec
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 1
41
+ - 2
42
+ - 9
43
+ version: 1.2.9
44
+ type: :development
45
+ version_requirements: *id002
46
+ description: A Rack middleware that tries to make FB Connect behave a little bit more like other identity providers such as OpenID and OAuth.
47
+ email: michael@intridea.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - LICENSE
54
+ - README.rdoc
55
+ files:
56
+ - .document
57
+ - .gitignore
58
+ - LICENSE
59
+ - README.rdoc
60
+ - Rakefile
61
+ - VERSION
62
+ - lib/rack/facebook_connect.rb
63
+ - lib/rack/facebook_connect/helpers.rb
64
+ - lib/rack_facebook_connect.rb
65
+ - rack_facebook_connect.gemspec
66
+ - spec/rack_facebook_connect_spec.rb
67
+ - spec/spec.opts
68
+ - spec/spec_helper.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/intridea/rack_facebook_connect
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.6
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: A Rack middleware for authentication via Facebook Connect.
99
+ test_files:
100
+ - spec/rack_facebook_connect_spec.rb
101
+ - spec/spec_helper.rb