carlosparamio-rack-facebook 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +29 -0
- data/Rakefile +0 -0
- data/lib/rack/facebook.rb +74 -0
- data/rack-facebook.gemspec +22 -0
- data/spec/rack-facebook_spec.rb +27 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +3 -0
- metadata +67 -0
data/README.markdown
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
This Rack middleware checks the signature of Facebook params, and
|
2
|
+
converts them to Ruby objects when appropiate. Also, it converts
|
3
|
+
the request method from the Facebook POST to the original HTTP
|
4
|
+
method used by the client.
|
5
|
+
|
6
|
+
If the signature is wrong, it returns a "404 Invalid Facebook Signature".
|
7
|
+
|
8
|
+
Optionally, it can take a block that receives the Rack environment
|
9
|
+
and returns a value that evaluates to true when we want the middleware to
|
10
|
+
be executed for the specific request.
|
11
|
+
|
12
|
+
# Usage
|
13
|
+
|
14
|
+
In your config.ru:
|
15
|
+
|
16
|
+
require 'rack/facebook'
|
17
|
+
use Rack::Facebook, "my_facebook_secret_key"
|
18
|
+
|
19
|
+
Using a block condition:
|
20
|
+
|
21
|
+
use Rack::Facebook, "my_facebook_secret_key" do |env|
|
22
|
+
env['REQUEST_URI'] =~ /^\/facebook_only/
|
23
|
+
end
|
24
|
+
|
25
|
+
# Credits
|
26
|
+
|
27
|
+
Carlos Paramio
|
28
|
+
|
29
|
+
[http://evolve.st/](http://evolve.st/)
|
data/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Rack
|
2
|
+
# This Rack middleware checks the signature of Facebook params, and
|
3
|
+
# converts them to Ruby objects when appropiate. Also, it converts
|
4
|
+
# the request method from the Facebook POST to the original HTTP
|
5
|
+
# method used by the client.
|
6
|
+
#
|
7
|
+
# If the signature is wrong, it returns a "404 Invalid Facebook Signature".
|
8
|
+
#
|
9
|
+
# Optionally, it can take a block that receives the Rack environment
|
10
|
+
# and returns a value that evaluates to true when we want the middleware to
|
11
|
+
# be executed for the specific request.
|
12
|
+
#
|
13
|
+
# == Usage
|
14
|
+
#
|
15
|
+
# In your config.ru:
|
16
|
+
#
|
17
|
+
# require 'rack/facebook'
|
18
|
+
# use Rack::Facebook, "my_facebook_secret_key"
|
19
|
+
#
|
20
|
+
# Using a block condition:
|
21
|
+
#
|
22
|
+
# use Rack::Facebook, "my_facebook_secret_key" do |env|
|
23
|
+
# env['REQUEST_URI'] =~ /^\/facebook_only/
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
class Facebook
|
27
|
+
def initialize(app, secret_key, &condition)
|
28
|
+
@app = app
|
29
|
+
@secret_key = secret_key
|
30
|
+
@condition = condition
|
31
|
+
end
|
32
|
+
|
33
|
+
def call(env)
|
34
|
+
if @condition.nil? || @condition.call(env)
|
35
|
+
req = Rack::Request.new(env)
|
36
|
+
fb_params = extract_fb_sig_params(req.POST)
|
37
|
+
unless signature_is_valid?(fb_params, req.POST['fb_sig'])
|
38
|
+
return [404, {"Content-Type" => "text/html"}, ["Invalid Facebook signature"]]
|
39
|
+
end
|
40
|
+
env['REQUEST_METHOD'] = fb_params["request_method"]
|
41
|
+
convert_parameters!(req.POST)
|
42
|
+
end
|
43
|
+
return @app.call(env)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def extract_fb_sig_params(params)
|
49
|
+
params.inject({}) do |collection, pair|
|
50
|
+
collection[pair.first.sub(/^fb_sig_/, '')] = pair.last if pair.first[0,7] == 'fb_sig_'
|
51
|
+
collection
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def signature_is_valid?(fb_params, actual_sig)
|
56
|
+
raw_string = fb_params.map{ |*args| args.join('=') }.sort.join
|
57
|
+
expected_signature = Digest::MD5.hexdigest([raw_string, @secret_key].join)
|
58
|
+
actual_sig == expected_signature
|
59
|
+
end
|
60
|
+
|
61
|
+
def convert_parameters!(params)
|
62
|
+
params.each do |key, value|
|
63
|
+
case key
|
64
|
+
when 'fb_sig_added', 'fb_sig_in_canvas', 'fb_sig_in_new_facebook', 'fb_sig_position_fix'
|
65
|
+
params[key] = value == "1"
|
66
|
+
when 'fb_sig_expires', 'fb_sig_profile_update_time', 'fb_sig_time'
|
67
|
+
params[key] = value == "0" ? nil : Time.at(value.to_f)
|
68
|
+
when 'fb_sig_friends'
|
69
|
+
params[key] = value.split(',')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "rack-facebook"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.date = "2009-01-09"
|
5
|
+
s.summary = "Rack middleware to verify and parse Facebook parameters"
|
6
|
+
s.email = "carlos@evolve.st"
|
7
|
+
s.homepage = "http://evolve.st/articles/13-rack-facebook-a-new-rack-middleware-to-parse-facebook-parameters"
|
8
|
+
s.description = "rack-facebook is a Rack middleware that checks the signature of Facebook params, and converts them to Ruby objects when appropiate. Also, it converts the request method from the Facebook POST to the original HTTP method used by the client."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Carlos Paramio"]
|
11
|
+
s.files = [
|
12
|
+
"README.markdown",
|
13
|
+
"Rakefile",
|
14
|
+
"rack-facebook.gemspec",
|
15
|
+
"lib/rack/facebook.rb"]
|
16
|
+
s.test_files = ["spec/spec.opts",
|
17
|
+
"spec/spec_helper.rb",
|
18
|
+
"spec/rack-facebook_spec.rb"]
|
19
|
+
s.rdoc_options = ["--main", "Rack::Facebook"]
|
20
|
+
#s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
21
|
+
s.add_dependency("rack", ["= 0.4.0"])
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
require 'rack/facebook'
|
3
|
+
|
4
|
+
describe Rack::Facebook do
|
5
|
+
describe 'without a block' do
|
6
|
+
describe 'when the fb_sig is not valid' do
|
7
|
+
it 'should return 404 Invalid Facebook signature'
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'when the fb_sig is valid' do
|
11
|
+
it 'should convert the facebook parameters to Ruby objects'
|
12
|
+
|
13
|
+
it 'should convert the request method from POST to the original client method'
|
14
|
+
|
15
|
+
it 'should run app'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'with a block' do
|
20
|
+
describe 'when the block returns a value that evaluates to true' do
|
21
|
+
it 'should execute the middleware'
|
22
|
+
end
|
23
|
+
describe 'when the block returns a value that evaluates to true' do
|
24
|
+
it 'should skip the middleware'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-Du -c -fs
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carlosparamio-rack-facebook
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carlos Paramio
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-09 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - "="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.4.0
|
23
|
+
version:
|
24
|
+
description: rack-facebook is a Rack middleware that checks the signature of Facebook params, and converts them to Ruby objects when appropiate. Also, it converts the request method from the Facebook POST to the original HTTP method used by the client.
|
25
|
+
email: carlos@evolve.st
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- README.markdown
|
34
|
+
- Rakefile
|
35
|
+
- rack-facebook.gemspec
|
36
|
+
- lib/rack/facebook.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://evolve.st/articles/13-rack-facebook-a-new-rack-middleware-to-parse-facebook-parameters
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --main
|
42
|
+
- Rack::Facebook
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: Rack middleware to verify and parse Facebook parameters
|
64
|
+
test_files:
|
65
|
+
- spec/spec.opts
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
- spec/rack-facebook_spec.rb
|