koala 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ v0.9.0
2
+ -- Added parse_signed_request to handle Facebook's new authentication scheme
3
+ -- note: creates dependency on OpenSSL (OpenSSL::HMAC) for decryption
4
+ -- Added GraphCollection class to provide paging support for GraphAPI get_connections and search methods (thanks to jagthedrummer)
5
+ -- Added get_page method to easily fetch pages of results from GraphCollections
6
+ -- Exchanging sessions for tokens now works properly when provided invalid/expired session keys
7
+ -- You can now include a :typhoeus_options key in TyphoeusService#make_request's options hash to control the Typhoeus call (for example, to set :disable_ssl_peer_verification => true)
8
+ -- All paths provided to HTTP services start with leading / to improve compatibility with stubbing libraries
9
+ -- If Facebook returns nil for search or get_connections requests, Koala now returns nil rather than throwing an exception
10
+
1
11
  v0.8.0
2
12
  -- Breaking interface changes
3
13
  -- Removed string overloading for the methods, per 0.7.3, which caused Marshaling issues
data/Manifest CHANGED
@@ -2,6 +2,20 @@ CHANGELOG
2
2
  LICENSE
3
3
  Manifest
4
4
  Rakefile
5
+ examples/oauth_playground/Capfile
6
+ examples/oauth_playground/LICENSE
7
+ examples/oauth_playground/Rakefile
8
+ examples/oauth_playground/config.ru
9
+ examples/oauth_playground/config/deploy.rb
10
+ examples/oauth_playground/config/facebook.yml
11
+ examples/oauth_playground/lib/load_facebook.rb
12
+ examples/oauth_playground/lib/oauth_playground.rb
13
+ examples/oauth_playground/readme.md
14
+ examples/oauth_playground/spec/oauth_playground_spec.rb
15
+ examples/oauth_playground/spec/spec_helper.rb
16
+ examples/oauth_playground/tmp/restart.txt
17
+ examples/oauth_playground/views/index.erb
18
+ examples/oauth_playground/views/layout.erb
5
19
  init.rb
6
20
  koala.gemspec
7
21
  lib/koala.rb
@@ -16,6 +30,7 @@ spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb
16
30
  spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb
17
31
  spec/koala/graph_api/graph_api_no_access_token_tests.rb
18
32
  spec/koala/graph_api/graph_api_with_access_token_tests.rb
33
+ spec/koala/graph_api/graph_collection_tests.rb
19
34
  spec/koala/live_testing_data_helper.rb
20
35
  spec/koala/net_http_service_tests.rb
21
36
  spec/koala/oauth/oauth_tests.rb
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake'
4
4
  require 'echoe'
5
5
 
6
6
  # gem management
7
- Echoe.new('koala', '0.8.0') do |p|
7
+ Echoe.new('koala', '0.9.0') do |p|
8
8
  p.summary = "A lightweight, flexible library for Facebook with support for the Graph API, the old REST API, realtime updates, and OAuth validation."
9
9
  p.description = "Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, as well as support for realtime updates and OAuth and Facebook Connect authentication. Koala is fully tested and supports Net::HTTP and Typhoeus connections out of the box and can accept custom modules for other services."
10
10
  p.url = "http://github.com/arsduo/koala"
@@ -0,0 +1,2 @@
1
+ load 'deploy' if respond_to?(:namespace) # cap2 differentiator
2
+ load 'config/deploy' # remove this line to skip loading any of the default tasks
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Alex Koppel
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ namespace :oauth_playground do
2
+
3
+
4
+ end
@@ -0,0 +1,27 @@
1
+ # gems
2
+ require 'sinatra'
3
+ require 'logger'
4
+ require 'yaml'
5
+
6
+ # app files
7
+ require 'koala'
8
+ require File.join(File.dirname(__FILE__), 'lib', 'load_facebook.rb')
9
+ require File.join(File.dirname(__FILE__), 'lib', 'oauth_playground.rb')
10
+
11
+ # LOGGING
12
+ # set up the logfile
13
+ Dir.mkdir('log') unless File.exists?('log')
14
+ log_filename = File.join(File.dirname(__FILE__), "log", "sinatra.log")
15
+ log = File.new(log_filename, "a+")
16
+
17
+ # log requests
18
+ use Rack::CommonLogger, log
19
+ # log application-generated code
20
+ LOGGER = Logger.new(log_filename)
21
+ # log output to stdout and stderr as well
22
+ $stdout.reopen(log)
23
+ $stderr.reopen(log)
24
+
25
+ # activate the app
26
+ disable :run
27
+ run OAuthPlayground
@@ -0,0 +1,39 @@
1
+ set :application, "oauth_playground"
2
+ set :repository, "git://github.com/arsduo/oauth_playground.git"
3
+ set :domain, "oauth.twoalex.com"
4
+ set :deploy_to, "$HOME/rails_apps/#{application}/"
5
+
6
+ # authentication
7
+ set :scm, "git"
8
+ set :user, "alexkm"
9
+ set :use_sudo, false
10
+ ssh_options[:forward_agent] = true
11
+
12
+ # web server
13
+ role :web, "oauth.twoalex.com" # Your HTTP server, Apache/etc
14
+ role :app, "oauth.twoalex.com" # This may be the same as your `Web` server
15
+ role :db, "oauth.twoalex.com", :primary => true # This is where Rails migrations will run
16
+
17
+
18
+ # other git-related commands
19
+ set :branch, "master"
20
+ default_run_options[:pty] = true
21
+ # cache the repository locally to speed updates
22
+ set :repository_cache, "git_cache"
23
+ set :deploy_via, :remote_cache
24
+
25
+
26
+ # passenger-specific deploy tasks
27
+ namespace :deploy do
28
+ task :start do
29
+ run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
30
+ end
31
+
32
+ task :stop do
33
+ # nothing
34
+ end
35
+
36
+ task :restart, :roles => :app, :except => { :no_release => true } do
37
+ run "touch #{File.join(current_path,'tmp','restart.txt')}"
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ development:
2
+ api_key: 171e3563d4fee42e0ba27450838bba32
3
+ secret_key: c81302ccef57cbdd2e68b2229e54cd2f
4
+ app_id: 119347844754245
5
+
6
+ test:
7
+ api_key:
8
+ secret_key:
9
+
10
+ production:
11
+ api_key: 25e1cec0df2b3bfa781da3ed78da3a1e
12
+ secret_key: e45e55a333eec232d4206d2703de1307
13
+ app_id: 119908831367602
@@ -0,0 +1,3 @@
1
+ # load Facebook info for this environment
2
+ FACEBOOK_INFO = YAML.load_file(File.join(File.dirname(__FILE__), "..", "config", "facebook.yml"))[ENV["RACK_ENV"]]
3
+
@@ -0,0 +1,187 @@
1
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
2
+
3
+ require 'rubygems'
4
+ require 'sinatra'
5
+ require 'erb'
6
+
7
+ class OAuthPlayground < Sinatra::Application
8
+
9
+ set :root, APP_ROOT
10
+
11
+ include Koala
12
+
13
+ layout :layout
14
+
15
+ get "/" do
16
+ @app_data = FACEBOOK_INFO.merge("callback_url" => "#{request.scheme}://#{request.host}/")
17
+ @oauth = Facebook::OAuth.new(@app_data["app_id"], @app_data["secret_key"], @app_data["callback_url"])
18
+
19
+ # get authentication info
20
+ set_facebook_cookies
21
+ set_oauth_data
22
+ set_access_token
23
+
24
+ unless (@permissions = params[:permissions]) && @permissions.length > 0
25
+ @active_permissions = (get_active_permissions || {}).inject([]) do |active, perm|
26
+ # collect our active permissions
27
+ active << perm[0].to_sym if perm[1] == 1
28
+ active
29
+ end
30
+ @fetched_permissions = true
31
+ else
32
+ @active_permissions = @permissions.collect {|p| p.to_sym}
33
+ end
34
+
35
+ @available_permissions = [
36
+ {:name => "User Activity", :perms => ACTIVITY_PERMISSIONS},
37
+ {:name => "User Info", :perms => USER_PERMISSIONS},
38
+ {:name => "Friend Info", :perms => FRIEND_PERMISSIONS}
39
+ ]
40
+
41
+ erb :index
42
+ end
43
+
44
+ get "/subscriptions" do
45
+ # validate that this is a valid response
46
+ # it will automatically render the result of the verification
47
+ # e.g. either the challenge phrase or false
48
+ subscription = Facebook::RealtimeUpdates.meet_challenge(params) do |verification_token|
49
+ token_parts = verification_token.split("|")
50
+ expected = Digest::MD5.hexdigest("#{token_parts.first}~koala")
51
+ logger.info "expected: #{expected}"
52
+ logger.info "got: #{token_parts.last}"
53
+ # determine if this is a valid token -- that is, if the send part is a properly encoding of the first
54
+ expected == token_parts.last
55
+ end
56
+ end
57
+
58
+ helpers do
59
+ def logger
60
+ LOGGER
61
+ end
62
+ end
63
+
64
+ # helpers
65
+
66
+ # set up our understanding of the user's session
67
+
68
+ def set_access_token
69
+ # get the access token from wherever we can
70
+ @access_token ||= (set_oauth_data && @oauth_access_token) || (set_facebook_cookies && @cookie_access_token)
71
+ end
72
+
73
+ def set_oauth_data
74
+ unless @oauth_access_token
75
+ if (@code = params[:code]) && @raw_access_response = @oauth.send(:fetch_token_string, {:code => @code, :redirect_uri => @app_data["callback_url"]})
76
+ parsed = @oauth.send(:parse_access_token, @raw_access_response)
77
+ @oauth_access_token = parsed["access_token"]
78
+ @expiration = parsed["expires"] || "Does not expire (offline)"
79
+ end
80
+ end
81
+
82
+ @oauth_access_token
83
+ end
84
+
85
+ def set_facebook_cookies
86
+ unless @facebook_cookies
87
+ if @facebook_cookies = @oauth.get_user_from_cookie(request.cookies)
88
+ @cookie_access_token = @facebook_cookies["access_token"]
89
+ end
90
+ end
91
+
92
+ @facebook_cookies
93
+ end
94
+
95
+ def set_uid
96
+ # get the OAuth data, including fetching the access token, if available and necessary
97
+ # e.g. if we have an OAuth token and no cookie data
98
+ unless @uid
99
+ if @facebook_cookies
100
+ @uid = @facebook_cookies["uid"]
101
+ elsif token = set_access_token
102
+ # we have to fetch the info
103
+ @graph = Facebook::GraphAPI.new(token)
104
+ result = @graph.get_object("me")
105
+ @uid = result["id"]
106
+ end
107
+ end
108
+ @uid
109
+ end
110
+
111
+ # fetch the active permissions about the user
112
+ def get_active_permissions
113
+ set_access_token
114
+ if @access_token && !@permissions && set_uid
115
+ # if we don't have permissions set but have an access token
116
+ # grab the user's info
117
+ @rest = Facebook::RestAPI.new(@access_token)
118
+ result = @rest.fql_query("select #{all_permissions.join(",")} from permissions where uid = #{@uid.to_s}")
119
+ result.first
120
+ end
121
+ end
122
+
123
+ # list of permissions
124
+
125
+ def all_permissions
126
+ ACTIVITY_PERMISSIONS + USER_PERMISSIONS + FRIEND_PERMISSIONS
127
+ end
128
+
129
+ ACTIVITY_PERMISSIONS = [
130
+ :publish_stream, # Enables your application to post content, comments, and likes to a user's stream and to the streams of the user's friends, without prompting the user each time.
131
+ :create_event, # Enables your application to create and modify events on the user's behalf
132
+ :rsvp_event, # Enables your application to RSVP to events on the user's behalf
133
+ :sms, # Enables your application to send messages to the user and respond to messages from the user via text message
134
+ :offline_access # Enables your application to perform authorized requests on behalf of the user at any time. By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user when the are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.
135
+ ]
136
+
137
+ USER_PERMISSIONS = [
138
+ :email, # Provides access to the user's primary email address in the email property. Do not spam users. Your use of email must comply both with Facebook policies and with the CAN-SPAM Act.
139
+ :read_insights, # Provides read access to the Insights data for pages, applications, and domains the user owns.
140
+ :read_stream, # Provides access to all the posts in the user's News Feed and enables your application to perform searches against the user's News Feed
141
+ :user_about_me, # Provides access to the "About Me" section of the profile in the about property
142
+ :user_activities, # Provides access to the user's list of activities as the activities connection
143
+ :user_birthday, # Provides access to the full birthday with year as the birthday_date property
144
+ :user_education_history, # Provides access to education history as the education property
145
+ :user_events, # Provides access to the list of events the user is attending as the events connection
146
+ :user_groups, # Provides access to the list of groups the user is a member of as the groups connection
147
+ :user_hometown, # Provides access to the user's hometown in the hometown property
148
+ :user_interests, # Provides access to the user's list of interests as the interests connection
149
+ :user_likes, # Provides access to the list of all of the pages the user has liked as the likes connection
150
+ :user_location, # Provides access to the user's current location as the current_location property
151
+ :user_notes, # Provides access to the user's notes as the notes connection
152
+ :user_online_presence, # Provides access to the user's online/offline presence
153
+ :user_photo_video_tags, # Provides access to the photos the user has been tagged in as the photos connection
154
+ :user_photos, # Provides access to the photos the user has uploaded
155
+ :user_relationships, # Provides access to the user's family and personal relationships and relationship status
156
+ :user_religion_politics, # Provides access to the user's religious and political affiliations
157
+ :user_status, # Provides access to the user's most recent status message
158
+ :user_videos, # Provides access to the videos the user has uploaded
159
+ :user_website, # Provides access to the user's web site URL
160
+ :user_work_history # Provides access to work history as the work property
161
+ ]
162
+
163
+ FRIEND_PERMISSIONS = [
164
+ :read_friendlists, # Provides read access to the user's friend lists
165
+ :read_requests, # Provides read access to the user's friend requests
166
+ :friends_about_me, # Provides access to the "About Me" section of the profile in the about property
167
+ :friends_activities, # Provides access to the user's list of activities as the activities connection
168
+ :friends_birthday, # Provides access to the full birthday with year as the birthday_date property
169
+ :friends_education_history, # Provides access to education history as the education property
170
+ :friends_events, # Provides access to the list of events the user is attending as the events connection
171
+ :friends_groups, # Provides access to the list of groups the user is a member of as the groups connection
172
+ :friends_hometown, # Provides access to the user's hometown in the hometown property
173
+ :friends_interests, # Provides access to the user's list of interests as the interests connection
174
+ :friends_likes, # Provides access to the list of all of the pages the user has liked as the likes connection
175
+ :friends_location, # Provides access to the user's current location as the current_location property
176
+ :friends_notes, # Provides access to the user's notes as the notes connection
177
+ :friends_online_presence, # Provides access to the user's online/offline presence
178
+ :friends_photo_video_tags, # Provides access to the photos the user has been tagged in as the photos connection
179
+ :friends_photos, # Provides access to the photos the user has uploaded
180
+ :friends_relationships, # Provides access to the user's family and personal relationships and relationship status
181
+ :friends_religion_politics, # Provides access to the user's religious and political affiliations
182
+ :friends_status, # Provides access to the user's most recent status message
183
+ :friends_videos, # Provides access to the videos the user has uploaded
184
+ :friends_website, # Provides access to the user's web site URL
185
+ :friends_work_history # Provides access to work history as the work property
186
+ ]
187
+ end
@@ -0,0 +1,8 @@
1
+ A simple OAuth Playground chock full of the info you need to test your OAuth-based Facebook application.
2
+
3
+ To Do's
4
+ =======
5
+
6
+ * Extend the permissions controls to cover all available permissions
7
+ * Make expiration dates human-readable
8
+ * Let people plug in their own app (updating the app's connect properties through setAppProperties)
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'ruby-debug'
3
+
4
+ describe 'OAuthPlayground' do
5
+ before :each do
6
+ @hydra = Typhoeus::Hydra.hydra
7
+ end
8
+
9
+ after :each do
10
+ @hydra.clear_stubs
11
+ end
12
+
13
+ it 'should load the index' do
14
+ get '/'
15
+ last_response.should be_ok
16
+ end
17
+
18
+ =begin
19
+ # unfortunately, this fails when you pass the get method a param named code!
20
+ # fixing this will require some mucking around in Rack::Test
21
+
22
+ it "should make a request to Facebook's OAuth server when passed a code" do
23
+ test_string = Regexp.new("The time is #{Time.now.to_i}")
24
+
25
+ # stub out the request and make sure it's returned
26
+ @hydra.stub("https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token", "get").and_return(test_string)
27
+
28
+ get "/", {"code" => "foo_bar"}
29
+
30
+ # make sure the body includes the request string
31
+ last_response.body.should =~ test_string
32
+ end
33
+ =end
34
+ end
35
+
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'rack/test'
4
+ require 'typhoeus'
5
+ require 'koala'
6
+
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'oauth_playground.rb')
8
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'load_facebook.rb')
9
+
10
+ OAuthPlayground.set(
11
+ :environment => :test,
12
+ :run => false,
13
+ :raise_errors => true,
14
+ :logging => false
15
+ )
16
+
17
+ module TestHelper
18
+
19
+ def app
20
+ # change to your app class if using the 'classy' style
21
+ OAuthPlayground
22
+ end
23
+
24
+ def body
25
+ last_response.body
26
+ end
27
+
28
+ def status
29
+ last_response.status
30
+ end
31
+
32
+ include Rack::Test::Methods
33
+
34
+ end
35
+
36
+ include TestHelper