jsmestad-frankie 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/frankie.gemspec +1 -1
- data/lib/frankie.rb +121 -131
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/frankie.gemspec
CHANGED
data/lib/frankie.rb
CHANGED
@@ -12,9 +12,9 @@ require 'uri'
|
|
12
12
|
gem 'mmangino-facebooker'
|
13
13
|
require 'facebooker'
|
14
14
|
|
15
|
-
module
|
15
|
+
module Sinatra
|
16
16
|
|
17
|
-
module
|
17
|
+
module Frankie
|
18
18
|
def load_facebook_config(file, env=:development)
|
19
19
|
if File.exist?(file)
|
20
20
|
yaml = YAML.load_file(file)[env.to_s]
|
@@ -23,9 +23,7 @@ module Frankie
|
|
23
23
|
ENV['FACEBOOKER_RELATIVE_URL_ROOT'] = yaml['canvas_page_name']
|
24
24
|
end
|
25
25
|
end
|
26
|
-
end
|
27
26
|
|
28
|
-
module Helpers
|
29
27
|
def facebook_session
|
30
28
|
@facebook_session
|
31
29
|
end
|
@@ -47,145 +45,137 @@ module Frankie
|
|
47
45
|
@facebook_params ||= verified_facebook_params
|
48
46
|
end
|
49
47
|
|
50
|
-
private
|
51
48
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def secure_with_facebook_params!
|
66
|
-
return unless request_is_for_a_facebook_canvas?
|
67
|
-
|
68
|
-
if ['user', 'session_key'].all? {|element| facebook_params[element]}
|
69
|
-
@facebook_session = new_facebook_session
|
70
|
-
@facebook_session.secure_with!(facebook_params['session_key'], facebook_params['user'], facebook_params['expires'])
|
71
|
-
session[:facebook_session] = @facebook_session
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def create_new_facebook_session_and_redirect!
|
76
|
-
session[:facebook_session] = new_facebook_session
|
77
|
-
throw :halt, do_redirect(session[:facebook_session].login_url) unless @installation_required
|
49
|
+
def session_already_secured?
|
50
|
+
(@facebook_session = session[:facebook_session]) && session[:facebook_session].secured?
|
51
|
+
end
|
52
|
+
|
53
|
+
def secure_with_token!
|
54
|
+
if params['auth_token']
|
55
|
+
@facebook_session = new_facebook_session
|
56
|
+
@facebook_session.auth_token = params['auth_token']
|
57
|
+
@facebook_session.secure!
|
58
|
+
session[:facebook_session] = @facebook_session
|
78
59
|
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def secure_with_facebook_params!
|
63
|
+
return unless request_is_for_a_facebook_canvas?
|
79
64
|
|
80
|
-
|
81
|
-
|
65
|
+
if ['user', 'session_key'].all? {|element| facebook_params[element]}
|
66
|
+
@facebook_session = new_facebook_session
|
67
|
+
@facebook_session.secure_with!(facebook_params['session_key'], facebook_params['user'], facebook_params['expires'])
|
68
|
+
session[:facebook_session] = @facebook_session
|
82
69
|
end
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_new_facebook_session_and_redirect!
|
73
|
+
session[:facebook_session] = new_facebook_session
|
74
|
+
throw :halt, do_redirect(session[:facebook_session].login_url) unless @installation_required
|
75
|
+
end
|
76
|
+
|
77
|
+
def new_facebook_session
|
78
|
+
Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
|
79
|
+
end
|
80
|
+
|
81
|
+
def capture_facebook_friends_if_available!
|
82
|
+
return unless request_is_for_a_facebook_canvas?
|
83
|
+
if friends = facebook_params['friends']
|
84
|
+
facebook_session.user.friends = friends.map do |friend_uid|
|
85
|
+
Facebooker::User.new(friend_uid, facebook_session)
|
90
86
|
end
|
91
87
|
end
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
88
|
+
end
|
89
|
+
|
90
|
+
def blank?(value)
|
91
|
+
(value == '0' || value.nil? || value == '')
|
92
|
+
end
|
96
93
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
102
|
-
verify_signature(facebook_sig_params, params['fb_sig'])
|
103
|
-
facebook_sig_params.inject(Hash.new) do |collection, pair|
|
104
|
-
collection[pair.first] = facebook_parameter_conversions[pair.first].call(pair.last)
|
105
|
-
collection
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
# 48.hours.ago in sinatra
|
110
|
-
def earliest_valid_session
|
111
|
-
now = Time.now
|
112
|
-
now -= (60 * 60 * 48)
|
113
|
-
now
|
94
|
+
def verified_facebook_params
|
95
|
+
facebook_sig_params = params.inject({}) do |collection, pair|
|
96
|
+
collection[pair.first.sub(/^fb_sig_/, '')] = pair.last if pair.first[0,7] == 'fb_sig_'
|
97
|
+
collection
|
114
98
|
end
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
raise Facebooker::Session::IncorrectSignature if actual_sig != expected_signature
|
120
|
-
raise Facebooker::Session::SignatureTooOld if Time.at(facebook_sig_params['time'].to_f) < earliest_valid_session
|
121
|
-
true
|
122
|
-
end
|
123
|
-
|
124
|
-
def facebook_parameter_conversions
|
125
|
-
@facebook_parameter_conversions ||= Hash.new do |hash, key|
|
126
|
-
lambda{|value| value}
|
127
|
-
end.merge(
|
128
|
-
'time' => lambda{|value| Time.at(value.to_f)},
|
129
|
-
'in_canvas' => lambda{|value| !blank?(value)},
|
130
|
-
'added' => lambda{|value| !blank?(value)},
|
131
|
-
'expires' => lambda{|value| blank?(value) ? nil : Time.at(value.to_f)},
|
132
|
-
'friends' => lambda{|value| value.split(/,/)}
|
133
|
-
)
|
99
|
+
verify_signature(facebook_sig_params, params['fb_sig'])
|
100
|
+
facebook_sig_params.inject(Hash.new) do |collection, pair|
|
101
|
+
collection[pair.first] = facebook_parameter_conversions[pair.first].call(pair.last)
|
102
|
+
collection
|
134
103
|
end
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
104
|
+
end
|
105
|
+
|
106
|
+
# 48.hours.ago in sinatra
|
107
|
+
def earliest_valid_session
|
108
|
+
now = Time.now
|
109
|
+
now -= (60 * 60 * 48)
|
110
|
+
now
|
111
|
+
end
|
112
|
+
|
113
|
+
def verify_signature(facebook_sig_params,expected_signature)
|
114
|
+
raw_string = facebook_sig_params.map{ |*args| args.join('=') }.sort.join
|
115
|
+
actual_sig = Digest::MD5.hexdigest([raw_string, Facebooker::Session.secret_key].join)
|
116
|
+
raise Facebooker::Session::IncorrectSignature if actual_sig != expected_signature
|
117
|
+
raise Facebooker::Session::SignatureTooOld if Time.at(facebook_sig_params['time'].to_f) < earliest_valid_session
|
118
|
+
true
|
119
|
+
end
|
120
|
+
|
121
|
+
def facebook_parameter_conversions
|
122
|
+
@facebook_parameter_conversions ||= Hash.new do |hash, key|
|
123
|
+
lambda{|value| value}
|
124
|
+
end.merge(
|
125
|
+
'time' => lambda{|value| Time.at(value.to_f)},
|
126
|
+
'in_canvas' => lambda{|value| !blank?(value)},
|
127
|
+
'added' => lambda{|value| !blank?(value)},
|
128
|
+
'expires' => lambda{|value| blank?(value) ? nil : Time.at(value.to_f)},
|
129
|
+
'friends' => lambda{|value| value.split(/,/)}
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
133
|
+
def do_redirect(*args)
|
134
|
+
if request_is_for_a_facebook_canvas?
|
135
|
+
fbml_redirect_tag(args)
|
136
|
+
else
|
137
|
+
redirect args[0]
|
151
138
|
end
|
152
|
-
|
153
|
-
|
139
|
+
end
|
140
|
+
|
141
|
+
def fbml_redirect_tag(url)
|
142
|
+
"<fb:redirect url=\"#{url}\" />"
|
143
|
+
end
|
144
|
+
|
145
|
+
def request_is_for_a_facebook_canvas?
|
146
|
+
return false if params["fb_sig_in_canvas"].nil?
|
147
|
+
params["fb_sig_in_canvas"] == "1"
|
148
|
+
end
|
149
|
+
|
150
|
+
def application_is_installed?
|
154
151
|
facebook_params['added']
|
155
|
-
|
156
|
-
|
157
|
-
def ensure_authenticated_to_facebook
|
158
|
-
set_facebook_session || create_new_facebook_session_and_redirect!
|
159
|
-
end
|
160
|
-
|
161
|
-
def ensure_application_is_installed_by_facebook_user
|
162
|
-
@installation_required = true
|
163
|
-
authenticated_and_installed = ensure_authenticated_to_facebook && application_is_installed?
|
164
|
-
application_is_not_installed_by_facebook_user unless authenticated_and_installed
|
165
|
-
authenticated_and_installed
|
166
|
-
end
|
167
|
-
|
168
|
-
def application_is_not_installed_by_facebook_user
|
169
|
-
throw :halt, do_redirect(session[:facebook_session].install_url)
|
170
|
-
end
|
152
|
+
end
|
171
153
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
154
|
+
def ensure_authenticated_to_facebook
|
155
|
+
set_facebook_session || create_new_facebook_session_and_redirect!
|
156
|
+
end
|
157
|
+
|
158
|
+
def ensure_application_is_installed_by_facebook_user
|
159
|
+
@installation_required = true
|
160
|
+
authenticated_and_installed = ensure_authenticated_to_facebook && application_is_installed?
|
161
|
+
application_is_not_installed_by_facebook_user unless authenticated_and_installed
|
162
|
+
authenticated_and_installed
|
163
|
+
end
|
164
|
+
|
165
|
+
def application_is_not_installed_by_facebook_user
|
166
|
+
throw :halt, do_redirect(session[:facebook_session].install_url)
|
167
|
+
end
|
168
|
+
|
169
|
+
def set_fbml_format
|
170
|
+
params['format']="fbml" if request_is_for_a_facebook_canvas?
|
171
|
+
end
|
183
172
|
|
184
|
-
|
185
|
-
|
186
|
-
|
173
|
+
def fb_url_for(url)
|
174
|
+
url = "" if url == "/"
|
175
|
+
url = URI.escape(url)
|
176
|
+
return url if !request_is_for_a_facebook_canvas?
|
177
|
+
"http://apps.facebook.com/#{ENV['FACEBOOKER_RELATIVE_URL_ROOT']}/#{url}"
|
178
|
+
end
|
187
179
|
end
|
188
|
-
|
180
|
+
register Frankie
|
189
181
|
end
|
190
|
-
|
191
|
-
Sinatra::Base.register Frankie
|