omniauth-facebook 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of omniauth-facebook might be problematic. Click here for more details.

@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ .rspec
4
+ Gemfile.lock
5
+ pkg/*
6
+ .powenv
7
+ tmp
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - jruby
5
+ branches:
6
+ only:
7
+ - master
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem 'omniauth-oauth2', :git => 'git://github.com/intridea/omniauth-oauth2.git'
@@ -0,0 +1,30 @@
1
+ # OmniAuth Facebook  [![Build Status](http://travis-ci.org/mkdynamic/omniauth-facebook.png?branch=master)](http://travis-ci.org/mkdynamic/omniauth-facebook)
2
+
3
+ **Note:** This gem is designed to work with the unreleased OmniAuth 1.0 library. It will not be officially released on RubyGems.org until OmniAuth 1.0 is released.
4
+
5
+ This gem contains the Facebook strategy for OmniAuth.
6
+
7
+ ## Supported Flows
8
+
9
+ Supports the Server-side Flow as described in the the Facebook docs:
10
+ http://developers.facebook.com/docs/authentication
11
+
12
+ **Pending:** Supports the Client-side Flow via parsing out the verification code from the signed request cookie.
13
+
14
+ ## Ruby
15
+
16
+ Tested with the following Ruby versions:
17
+
18
+ - MRI 1.9.2
19
+ - MRI 1.8.7
20
+ - JRuby 1.6.4
21
+
22
+ ## License
23
+
24
+ Copyright (c) 2011 by Mark Dodwell
25
+
26
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
27
+
28
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
29
+
30
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ gem 'sinatra'
4
+ gem 'omniauth-facebook', :path => '../'
@@ -0,0 +1,27 @@
1
+ require 'bundler/setup'
2
+ require 'sinatra/base'
3
+ require 'omniauth-facebook'
4
+
5
+ class App < Sinatra::Base
6
+ get '/' do
7
+ redirect '/auth/facebook'
8
+ end
9
+
10
+ get '/auth/:provider/callback' do
11
+ content_type 'application/json'
12
+ MultiJson.encode(request.env)
13
+ end
14
+
15
+ get '/auth/failure' do
16
+ content_type 'application/json'
17
+ MultiJson.encode(request.env)
18
+ end
19
+ end
20
+
21
+ use Rack::Session::Cookie
22
+
23
+ use OmniAuth::Builder do
24
+ provider :facebook, ENV['APP_ID'], ENV['APP_SECRET'], :scope => 'email,read_stream'
25
+ end
26
+
27
+ run App.new
@@ -0,0 +1 @@
1
+ require 'omniauth/facebook'
@@ -0,0 +1,2 @@
1
+ require 'omniauth/facebook/version'
2
+ require 'omniauth/strategies/facebook'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Facebook
3
+ VERSION = "1.0.0.rc1"
4
+ end
5
+ end
@@ -0,0 +1,76 @@
1
+ require 'omniauth/strategies/oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Facebook < OmniAuth::Strategies::OAuth2
6
+ option :client_options, {
7
+ :site => 'https://graph.facebook.com',
8
+ :token_url => '/oauth/access_token'
9
+ }
10
+
11
+ option :token_params, {
12
+ :parse => :query
13
+ }
14
+
15
+ option :access_token_options, {
16
+ :header_format => 'OAuth %s',
17
+ :param_name => 'access_token'
18
+ }
19
+
20
+ uid { raw_info['id'] }
21
+
22
+ info do
23
+ prune!({
24
+ 'nickname' => raw_info['username'],
25
+ 'email' => raw_info['email'],
26
+ 'name' => raw_info['name'],
27
+ 'first_name' => raw_info['first_name'],
28
+ 'last_name' => raw_info['last_name'],
29
+ 'image' => "http://graph.facebook.com/#{uid}/picture?type=square",
30
+ 'description' => raw_info['bio'],
31
+ 'urls' => {
32
+ 'Facebook' => raw_info['link'],
33
+ 'Website' => raw_info['website']
34
+ },
35
+ 'location' => (raw_info['location'] || {})['name']
36
+ })
37
+ end
38
+
39
+ credentials do
40
+ prune!({
41
+ 'expires' => access_token.expires?,
42
+ 'expires_at' => access_token.expires_at
43
+ })
44
+ end
45
+
46
+ extra do
47
+ prune!({
48
+ 'raw_info' => raw_info
49
+ })
50
+ end
51
+
52
+ def raw_info
53
+ @raw_info ||= access_token.get('/me').parsed
54
+ end
55
+
56
+ def build_access_token
57
+ super.tap do |token|
58
+ token.options.merge!(access_token_options)
59
+ end
60
+ end
61
+
62
+ def access_token_options
63
+ options.access_token_options.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
64
+ end
65
+
66
+ private
67
+
68
+ def prune!(hash)
69
+ hash.delete_if do |_, value|
70
+ prune!(value) if value.is_a?(Hash)
71
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'omniauth/facebook/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'omniauth-facebook'
7
+ s.version = OmniAuth::Facebook::VERSION
8
+ s.authors = ['Mark Dodwell']
9
+ s.email = ['mark@mkdynamic.co.uk']
10
+ s.summary = 'Facebook strategy for OmniAuth'
11
+ s.homepage = 'https://github.com/mkdynamic/omniauth-facebook'
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
16
+ s.require_paths = ['lib']
17
+
18
+ s.add_runtime_dependency 'omniauth-oauth2', '~> 1.0.0.rc2'
19
+
20
+ s.add_development_dependency 'rspec', '~> 2.7.0'
21
+ s.add_development_dependency 'rake'
22
+ end
@@ -0,0 +1,245 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-facebook'
3
+
4
+ describe OmniAuth::Strategies::Facebook do
5
+ subject do
6
+ OmniAuth::Strategies::Facebook.new(nil, @options || {})
7
+ end
8
+
9
+ it_should_behave_like 'an oauth2 strategy'
10
+
11
+ describe '#client' do
12
+ it 'has correct Facebook site' do
13
+ subject.client.site.should eq('https://graph.facebook.com')
14
+ end
15
+
16
+ it 'has correct authorize url' do
17
+ subject.client.options[:authorize_url].should eq('/oauth/authorize')
18
+ end
19
+
20
+ it 'has correct token url' do
21
+ subject.client.options[:token_url].should eq('/oauth/access_token')
22
+ end
23
+ end
24
+
25
+ describe '#authorize_params' do
26
+ it 'is empty by default' do
27
+ subject.authorize_params.should be_empty
28
+ end
29
+ end
30
+
31
+ describe '#token_params' do
32
+ it 'has correct parse strategy' do
33
+ subject.token_params[:parse].should eq(:query)
34
+ end
35
+ end
36
+
37
+ describe '#access_token_options' do
38
+ it 'has correct param name by default' do
39
+ subject.access_token_options[:param_name].should eq('access_token')
40
+ end
41
+
42
+ it 'has correct header format by default' do
43
+ subject.access_token_options[:header_format].should eq('OAuth %s')
44
+ end
45
+ end
46
+
47
+ describe '#uid' do
48
+ before :each do
49
+ subject.stub(:raw_info) { { 'id' => '123' } }
50
+ end
51
+
52
+ it 'returns the id from raw_info' do
53
+ subject.uid.should eq('123')
54
+ end
55
+ end
56
+
57
+ describe '#info' do
58
+ before :each do
59
+ @raw_info ||= { 'name' => 'Fred Smith' }
60
+ subject.stub(:raw_info) { @raw_info }
61
+ end
62
+
63
+ context 'when optional data is not present in raw info' do
64
+ it 'has no email key' do
65
+ subject.info.should_not have_key('email')
66
+ end
67
+
68
+ it 'has no nickname key' do
69
+ subject.info.should_not have_key('nickname')
70
+ end
71
+
72
+ it 'has no first name key' do
73
+ subject.info.should_not have_key('first_name')
74
+ end
75
+
76
+ it 'has no last name key' do
77
+ subject.info.should_not have_key('last_name')
78
+ end
79
+
80
+ it 'has no location key' do
81
+ subject.info.should_not have_key('location')
82
+ end
83
+
84
+ it 'has no description key' do
85
+ subject.info.should_not have_key('description')
86
+ end
87
+
88
+ it 'has no urls' do
89
+ subject.info.should_not have_key('urls')
90
+ end
91
+ end
92
+
93
+ context 'when data is present in raw info' do
94
+ it 'returns the name' do
95
+ subject.info['name'].should eq('Fred Smith')
96
+ end
97
+
98
+ it 'returns the email' do
99
+ @raw_info['email'] = 'fred@smith.com'
100
+ subject.info['email'].should eq('fred@smith.com')
101
+ end
102
+
103
+ it 'returns the username as nickname' do
104
+ @raw_info['username'] = 'fredsmith'
105
+ subject.info['nickname'].should eq('fredsmith')
106
+ end
107
+
108
+ it 'returns the first name' do
109
+ @raw_info['first_name'] = 'Fred'
110
+ subject.info['first_name'].should eq('Fred')
111
+ end
112
+
113
+ it 'returns the last name' do
114
+ @raw_info['last_name'] = 'Smith'
115
+ subject.info['last_name'].should eq('Smith')
116
+ end
117
+
118
+ it 'returns the location name as location' do
119
+ @raw_info['location'] = { 'id' => '104022926303756', 'name' => 'Palo Alto, California' }
120
+ subject.info['location'].should eq('Palo Alto, California')
121
+ end
122
+
123
+ it 'returns bio as description' do
124
+ @raw_info['bio'] = 'I am great'
125
+ subject.info['description'].should eq('I am great')
126
+ end
127
+
128
+ it 'returns the square format facebook avatar url' do
129
+ @raw_info['id'] = '321'
130
+ subject.info['image'].should eq('http://graph.facebook.com/321/picture?type=square')
131
+ end
132
+
133
+ it 'returns the Facebook link as the Facebook url' do
134
+ @raw_info['link'] = 'http://www.facebook.com/fredsmith'
135
+ subject.info['urls'].should be_a(Hash)
136
+ subject.info['urls']['Facebook'].should eq('http://www.facebook.com/fredsmith')
137
+ end
138
+
139
+ it 'returns website url' do
140
+ @raw_info['website'] = 'https://my-wonderful-site.com'
141
+ subject.info['urls'].should be_a(Hash)
142
+ subject.info['urls']['Website'].should eq('https://my-wonderful-site.com')
143
+ end
144
+
145
+ it 'return both Facebook link and website urls' do
146
+ @raw_info['link'] = 'http://www.facebook.com/fredsmith'
147
+ @raw_info['website'] = 'https://my-wonderful-site.com'
148
+ subject.info['urls'].should be_a(Hash)
149
+ subject.info['urls']['Facebook'].should eq('http://www.facebook.com/fredsmith')
150
+ subject.info['urls']['Website'].should eq('https://my-wonderful-site.com')
151
+ end
152
+ end
153
+ end
154
+
155
+ describe '#raw_info' do
156
+ before :each do
157
+ @access_token = double('OAuth2::AccessToken')
158
+ subject.stub(:access_token) { @access_token }
159
+ end
160
+
161
+ it 'performs a GET to https://graph.facebook.com/me' do
162
+ @access_token.stub(:get) { double('OAuth2::Response').as_null_object }
163
+ @access_token.should_receive(:get).with('/me')
164
+ subject.raw_info
165
+ end
166
+
167
+ it 'returns a Hash' do
168
+ @access_token.stub(:get).with('/me') do
169
+ raw_response = double('Faraday::Response')
170
+ raw_response.stub(:body) { '{ "ohai": "thar" }' }
171
+ raw_response.stub(:status) { 200 }
172
+ raw_response.stub(:headers) { { 'Content-Type' => 'application/json' } }
173
+ OAuth2::Response.new(raw_response)
174
+ end
175
+ subject.raw_info.should be_a(Hash)
176
+ subject.raw_info['ohai'].should eq('thar')
177
+ end
178
+ end
179
+
180
+ describe '#credentials' do
181
+ before :each do
182
+ @access_token = double('OAuth2::AccessToken')
183
+ @access_token.stub(:token)
184
+ @access_token.stub(:expires?)
185
+ @access_token.stub(:expires_at)
186
+ @access_token.stub(:refresh_token)
187
+ subject.stub(:access_token) { @access_token }
188
+ end
189
+
190
+ it 'returns a Hash' do
191
+ subject.credentials.should be_a(Hash)
192
+ end
193
+
194
+ it 'returns the token' do
195
+ @access_token.stub(:token) { '123' }
196
+ subject.credentials['token'].should eq('123')
197
+ end
198
+
199
+ it 'returns the expiry status' do
200
+ @access_token.stub(:expires?) { true }
201
+ subject.credentials['expires'].should eq(true)
202
+
203
+ @access_token.stub(:expires?) { false }
204
+ subject.credentials['expires'].should eq(false)
205
+ end
206
+
207
+ it 'returns the refresh token and expiry time when expiring' do
208
+ ten_mins_from_now = (Time.now + 360).to_i
209
+ @access_token.stub(:expires?) { true }
210
+ @access_token.stub(:refresh_token) { '321' }
211
+ @access_token.stub(:expires_at) { ten_mins_from_now }
212
+ subject.credentials['refresh_token'].should eq('321')
213
+ subject.credentials['expires_at'].should eq(ten_mins_from_now)
214
+ end
215
+
216
+ it 'does not return the refresh token when it is nil and expiring' do
217
+ @access_token.stub(:expires?) { true }
218
+ @access_token.stub(:refresh_token) { nil }
219
+ subject.credentials['refresh_token'].should be_nil
220
+ subject.credentials.should_not have_key('refresh_token')
221
+ end
222
+
223
+ it 'does not return the refresh token when not expiring' do
224
+ @access_token.stub(:expires?) { false }
225
+ @access_token.stub(:refresh_token) { 'XXX' }
226
+ subject.credentials['refresh_token'].should be_nil
227
+ subject.credentials.should_not have_key('refresh_token')
228
+ end
229
+ end
230
+
231
+ describe '#extra' do
232
+ before :each do
233
+ @raw_info = { 'name' => 'Fred Smith' }
234
+ subject.stub(:raw_info) { @raw_info }
235
+ end
236
+
237
+ it 'returns a Hash' do
238
+ subject.extra.should be_a(Hash)
239
+ end
240
+
241
+ it 'contains raw info' do
242
+ subject.extra.should eq({ 'raw_info' => @raw_info })
243
+ end
244
+ end
245
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'rspec'
3
+ Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
4
+
5
+ RSpec.configure do |config|
6
+ end
@@ -0,0 +1,37 @@
1
+ # NOTE it would be useful if this lived in omniauth-oauth2 eventually
2
+ shared_examples 'an oauth2 strategy' do
3
+ describe '#client' do
4
+ it 'should be initialized with symbolized client_options' do
5
+ @options = { :client_options => { 'authorize_url' => 'https://example.com' } }
6
+ subject.client.options[:authorize_url].should == 'https://example.com'
7
+ end
8
+ end
9
+
10
+ describe '#authorize_params' do
11
+ it 'should include any authorize params passed in the :authorize_params option' do
12
+ @options = { :authorize_params => { :foo => 'bar', :baz => 'zip' } }
13
+ subject.authorize_params['foo'].should eq('bar')
14
+ subject.authorize_params['baz'].should eq('zip')
15
+ end
16
+
17
+ it 'should include top-level options that are marked as :authorize_options' do
18
+ @options = { :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
19
+ subject.authorize_params['scope'].should eq('bar')
20
+ subject.authorize_params['foo'].should eq('baz')
21
+ end
22
+ end
23
+
24
+ describe '#token_params' do
25
+ it 'should include any authorize params passed in the :authorize_params option' do
26
+ @options = { :token_params => { :foo => 'bar', :baz => 'zip' } }
27
+ subject.token_params['foo'].should eq('bar')
28
+ subject.token_params['baz'].should eq('zip')
29
+ end
30
+
31
+ it 'should include top-level options that are marked as :authorize_options' do
32
+ @options = { :token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
33
+ subject.token_params['scope'].should eq('bar')
34
+ subject.token_params['foo'].should eq('baz')
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-facebook
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Mark Dodwell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-29 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth-oauth2
16
+ requirement: &2153016200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0.rc2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2153016200
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2153015460 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.7.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2153015460
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &2153014400 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2153014400
47
+ description:
48
+ email:
49
+ - mark@mkdynamic.co.uk
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .travis.yml
56
+ - Gemfile
57
+ - README.md
58
+ - Rakefile
59
+ - example/Gemfile
60
+ - example/config.ru
61
+ - lib/omniauth-facebook.rb
62
+ - lib/omniauth/facebook.rb
63
+ - lib/omniauth/facebook/version.rb
64
+ - lib/omniauth/strategies/facebook.rb
65
+ - omniauth-facebook.gemspec
66
+ - spec/omniauth/strategies/facebook_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/support/shared_examples.rb
69
+ homepage: https://github.com/mkdynamic/omniauth-facebook
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>'
85
+ - !ruby/object:Gem::Version
86
+ version: 1.3.1
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.10
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Facebook strategy for OmniAuth
93
+ test_files:
94
+ - spec/omniauth/strategies/facebook_spec.rb
95
+ - spec/spec_helper.rb
96
+ - spec/support/shared_examples.rb
97
+ has_rdoc: