fbauth 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.mdown +3 -0
- data/init.rb +1 -0
- data/lib/facebook_auth.rb +72 -0
- data/lib/facebook_config.rb +20 -0
- data/lib/facebook_graph.rb +47 -0
- metadata +68 -0
data/README.mdown
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ActionView::Base.send :include, FbauthHelper
|
@@ -0,0 +1,72 @@
|
|
1
|
+
class FacebookAuth
|
2
|
+
|
3
|
+
attr_accessor :access_token, :expires, :uid
|
4
|
+
attr_accessor :user_data, :validation_error
|
5
|
+
|
6
|
+
def self.create parms
|
7
|
+
auth = self.new
|
8
|
+
auth.access_token = parms['access_token']
|
9
|
+
auth.uid = parms['uid']
|
10
|
+
auth.expires_epoch = parms['expires'].to_i unless parms['expires'].nil?
|
11
|
+
auth
|
12
|
+
end
|
13
|
+
|
14
|
+
def expires_epoch= epoch
|
15
|
+
# Need to convolve for SanFran TZ?
|
16
|
+
self.expires = Time.at(epoch)
|
17
|
+
end
|
18
|
+
|
19
|
+
def is_expired?
|
20
|
+
if self.expires.nil?
|
21
|
+
true
|
22
|
+
else
|
23
|
+
self.expires < Time.now
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def validate
|
28
|
+
valid = false
|
29
|
+
unless self.uid.nil? || self.access_token.nil?
|
30
|
+
self.user_data = FacebookGraph.call(self.uid, self.access_token)
|
31
|
+
if self.user_data.has_key? 'error'
|
32
|
+
self.validation_error = self.user_data['error'].inspect
|
33
|
+
self.user_data = nil
|
34
|
+
else
|
35
|
+
valid = true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
return valid
|
39
|
+
end
|
40
|
+
|
41
|
+
def session_data
|
42
|
+
return {
|
43
|
+
'access_token' => self.access_token,
|
44
|
+
'uid' => self.uid,
|
45
|
+
'expires' => self.expires.to_i
|
46
|
+
}.to_json
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
# User data typically looks like this:
|
52
|
+
#
|
53
|
+
# {
|
54
|
+
# "id"=>"849395216",
|
55
|
+
# "name"=>"Steven Vetzal",
|
56
|
+
# "first_name"=>"Steven"
|
57
|
+
# "last_name"=>"Vetzal",
|
58
|
+
# "gender"=>"male",
|
59
|
+
# "verified"=>true,
|
60
|
+
# "link"=>"http://www.facebook.com/svetzal",
|
61
|
+
# "timezone"=>-5,
|
62
|
+
# "locale"=>"en_US",
|
63
|
+
# "location"=>{
|
64
|
+
# "name"=>"Oshawa, Ontario",
|
65
|
+
# "id"=>"114418101908145"
|
66
|
+
# },
|
67
|
+
# "hometown"=>{
|
68
|
+
# "name"=>"Oshawa, Ontario",
|
69
|
+
# "id"=>"114418101908145"
|
70
|
+
# },
|
71
|
+
# "updated_time"=>"2010-04-28T12:57:20+0000",
|
72
|
+
# }
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class FacebookConfig
|
2
|
+
def self.[](key)
|
3
|
+
get_environment[key].value
|
4
|
+
end
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def self.get_environment
|
9
|
+
read_yaml[ENV["RAILS_ENV"]]
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.read_yaml
|
13
|
+
file = File.join(::RAILS_ROOT, 'config', 'facebook.yml')
|
14
|
+
YAML.parse(ERB.new(IO.read(file)).result)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.app_url
|
18
|
+
"http://apps.facebook.com/#{self['app_context']}/"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
class FacebookGraph
|
5
|
+
|
6
|
+
def self.call(path, access_token = nil, options = {})
|
7
|
+
params = []
|
8
|
+
options.keys.each do |k|
|
9
|
+
params << "#{k}=#{options[k]}" unless options[k].nil?
|
10
|
+
end
|
11
|
+
token = access_token ? CGI::escape(access_token) : nil
|
12
|
+
url = "https://graph.facebook.com/#{path}&access_token=#{token}"
|
13
|
+
url = "#{url}?#{params.join('&')}" unless params.empty?
|
14
|
+
uri = URI.parse(url)
|
15
|
+
http = Net::HTTP.new uri.host, uri.port
|
16
|
+
begin
|
17
|
+
http.use_ssl = (uri.scheme == "https")
|
18
|
+
req = Net::HTTP::Get.new(uri.path)
|
19
|
+
response = http.request(req)
|
20
|
+
json = JSON.parse(response.body)
|
21
|
+
ensure
|
22
|
+
http.finish if http.started?
|
23
|
+
end
|
24
|
+
json
|
25
|
+
end
|
26
|
+
|
27
|
+
# Available options:
|
28
|
+
# message, picture, link, name, caption, description
|
29
|
+
def self.publish_to_member_feed(uid, access_token, options)
|
30
|
+
token = access_token ? CGI::escape(access_token) : nil
|
31
|
+
if %w{staging production}.include? ENV['RAILS_ENV']
|
32
|
+
url = "https://graph.facebook.com/#{uid}/feed&access_token=#{token}"
|
33
|
+
uri = URI.parse(url)
|
34
|
+
http = Net::HTTP.new uri.host, uri.port
|
35
|
+
begin
|
36
|
+
http.use_ssl = (uri.scheme == "https")
|
37
|
+
req = Net::HTTP::Post.new(uri.path)
|
38
|
+
req.set_form_data(options)
|
39
|
+
response = http.request(req)
|
40
|
+
json = JSON.parse(response.body)
|
41
|
+
ensure
|
42
|
+
http.finish if http.started?
|
43
|
+
end
|
44
|
+
json
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fbauth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 0
|
9
|
+
version: 0.9.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Three Wise Men Inc.
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-16 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: This library simplifies life when it comes to authentication in a Facebook iFrame application
|
22
|
+
email: info @nospam@ threewisemen.ca
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.mdown
|
29
|
+
files:
|
30
|
+
- lib/facebook_auth.rb
|
31
|
+
- lib/facebook_config.rb
|
32
|
+
- lib/facebook_graph.rb
|
33
|
+
- init.rb
|
34
|
+
- README.mdown
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/ThreeWiseMen/fbauth
|
37
|
+
licenses:
|
38
|
+
- Simplified BSD License
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Authentication framework for Rails Facebook apps
|
67
|
+
test_files: []
|
68
|
+
|