jsmestad-frankie 0.4.1 → 0.4.2
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 +150 -145
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.2
|
data/frankie.gemspec
CHANGED
data/lib/frankie.rb
CHANGED
@@ -13,169 +13,174 @@ gem 'mmangino-facebooker'
|
|
13
13
|
require 'facebooker'
|
14
14
|
|
15
15
|
module Sinatra
|
16
|
-
|
17
16
|
module Frankie
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
17
|
+
|
18
|
+
module Loader
|
19
|
+
def load_facebook_config(file, env=:development)
|
20
|
+
if File.exist?(file)
|
21
|
+
yaml = YAML.load_file(file)[env.to_s]
|
22
|
+
ENV['FACEBOOK_API_KEY'] = yaml['api_key']
|
23
|
+
ENV['FACEBOOK_SECRET_KEY'] = yaml['secret_key']
|
24
|
+
ENV['FACEBOOKER_RELATIVE_URL_ROOT'] = yaml['canvas_page_name']
|
25
|
+
end
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
def facebook_session_parameters
|
32
|
-
{:fb_sig_session_key=>params["fb_sig_session_key"]}
|
33
|
-
end
|
34
|
-
|
35
|
-
def set_facebook_session
|
36
|
-
session_set = session_already_secured? || secure_with_token! || secure_with_facebook_params!
|
37
|
-
if session_set
|
38
|
-
capture_facebook_friends_if_available!
|
39
|
-
Facebooker::Session.current = facebook_session
|
29
|
+
module Helpers
|
30
|
+
def facebook_session
|
31
|
+
@facebook_session
|
40
32
|
end
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
def facebook_params
|
45
|
-
@facebook_params ||= verified_facebook_params
|
46
|
-
end
|
47
|
-
|
48
|
-
|
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
|
33
|
+
|
34
|
+
def facebook_session_parameters
|
35
|
+
{:fb_sig_session_key=>params["fb_sig_session_key"]}
|
59
36
|
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def secure_with_facebook_params!
|
63
|
-
return unless request_is_for_a_facebook_canvas?
|
64
37
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
38
|
+
def set_facebook_session
|
39
|
+
session_set = session_already_secured? || secure_with_token! || secure_with_facebook_params!
|
40
|
+
if session_set
|
41
|
+
capture_facebook_friends_if_available!
|
42
|
+
Facebooker::Session.current = facebook_session
|
43
|
+
end
|
44
|
+
session_set
|
69
45
|
end
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
46
|
+
|
47
|
+
def facebook_params
|
48
|
+
@facebook_params ||= verified_facebook_params
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def session_already_secured?
|
53
|
+
(@facebook_session = session[:facebook_session]) && session[:facebook_session].secured?
|
54
|
+
end
|
55
|
+
|
56
|
+
def secure_with_token!
|
57
|
+
if params['auth_token']
|
58
|
+
@facebook_session = new_facebook_session
|
59
|
+
@facebook_session.auth_token = params['auth_token']
|
60
|
+
@facebook_session.secure!
|
61
|
+
session[:facebook_session] = @facebook_session
|
86
62
|
end
|
87
63
|
end
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
78
|
+
end
|
79
|
+
|
80
|
+
def new_facebook_session
|
81
|
+
Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
|
82
|
+
end
|
83
|
+
|
84
|
+
def capture_facebook_friends_if_available!
|
85
|
+
return unless request_is_for_a_facebook_canvas?
|
86
|
+
if friends = facebook_params['friends']
|
87
|
+
facebook_session.user.friends = friends.map do |friend_uid|
|
88
|
+
Facebooker::User.new(friend_uid, facebook_session)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def blank?(value)
|
94
|
+
(value == '0' || value.nil? || value == '')
|
95
|
+
end
|
93
96
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
97
|
+
def verified_facebook_params
|
98
|
+
facebook_sig_params = params.inject({}) do |collection, pair|
|
99
|
+
collection[pair.first.sub(/^fb_sig_/, '')] = pair.last if pair.first[0,7] == 'fb_sig_'
|
100
|
+
collection
|
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
|
98
107
|
end
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
108
|
+
|
109
|
+
# 48.hours.ago in sinatra
|
110
|
+
def earliest_valid_session
|
111
|
+
now = Time.now
|
112
|
+
now -= (60 * 60 * 48)
|
113
|
+
now
|
103
114
|
end
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
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)},
|
115
|
+
|
116
|
+
def verify_signature(facebook_sig_params,expected_signature)
|
117
|
+
raw_string = facebook_sig_params.map{ |*args| args.join('=') }.sort.join
|
118
|
+
actual_sig = Digest::MD5.hexdigest([raw_string, Facebooker::Session.secret_key].join)
|
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)},
|
128
131
|
'expires' => lambda{|value| blank?(value) ? nil : Time.at(value.to_f)},
|
129
132
|
'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]
|
133
|
+
)
|
138
134
|
end
|
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?
|
151
|
-
facebook_params['added']
|
152
|
-
end
|
153
135
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
136
|
+
def do_redirect(*args)
|
137
|
+
if request_is_for_a_facebook_canvas?
|
138
|
+
fbml_redirect_tag(args)
|
139
|
+
else
|
140
|
+
redirect args[0]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def fbml_redirect_tag(url)
|
145
|
+
"<fb:redirect url=\"#{url}\" />"
|
146
|
+
end
|
147
|
+
|
148
|
+
def request_is_for_a_facebook_canvas?
|
149
|
+
return false if params["fb_sig_in_canvas"].nil?
|
150
|
+
params["fb_sig_in_canvas"] == "1"
|
151
|
+
end
|
152
|
+
|
153
|
+
def application_is_installed?
|
154
|
+
facebook_params['added']
|
155
|
+
end
|
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
|
171
|
+
|
172
|
+
def set_fbml_format
|
173
|
+
params['format']="fbml" if request_is_for_a_facebook_canvas?
|
174
|
+
end
|
172
175
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
176
|
+
def fb_url_for(url)
|
177
|
+
url = "" if url == "/"
|
178
|
+
url = URI.escape(url)
|
179
|
+
return url if !request_is_for_a_facebook_canvas?
|
180
|
+
"http://apps.facebook.com/#{ENV['FACEBOOKER_RELATIVE_URL_ROOT']}/#{url}"
|
181
|
+
end
|
178
182
|
end
|
179
183
|
end
|
180
|
-
register Frankie
|
184
|
+
register Frankie::Loader
|
185
|
+
helpers Frankie::Helpers
|
181
186
|
end
|