challah-facebook 0.1.1 → 0.1.2
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.
@@ -2,41 +2,33 @@ module Challah
|
|
2
2
|
module Facebook
|
3
3
|
module Interfaces
|
4
4
|
class Base
|
5
|
-
attr_reader :app_id, :app_secret
|
5
|
+
attr_reader :app_id, :app_secret, :permissions
|
6
6
|
|
7
|
-
def
|
8
|
-
|
7
|
+
def initialize(options = {})
|
8
|
+
@app_id = options.fetch(:app_id)
|
9
|
+
@app_secret = options.fetch(:app_secret)
|
10
|
+
@permissions = options.fetch(:permissions)
|
9
11
|
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.permissions
|
16
|
-
ENV['FACEBOOK_PERMISSIONS'].to_s.split(',')
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.user_fields
|
20
|
-
%w( first_name last_name email )
|
21
|
-
end
|
22
|
-
|
23
|
-
def initialize(app_id, app_secret)
|
24
|
-
@app_id = app_id
|
25
|
-
@app_secret = app_secret
|
26
|
-
end
|
13
|
+
# These methods should be implemented by the specific
|
14
|
+
# interface that inherits from Base
|
27
15
|
|
28
16
|
def self.get_access_token_for_oauth_code(code, callback_uri)
|
29
17
|
raise 'Not implemented: get_access_token_for_oauth_code'
|
30
18
|
end
|
31
19
|
|
32
|
-
def self.
|
33
|
-
raise 'Not implemented:
|
20
|
+
def self.get_extended_token(access_token)
|
21
|
+
raise 'Not implemented: get_extended_token'
|
34
22
|
end
|
35
23
|
|
36
24
|
def self.get_facebook_uid_from_access_token(access_token)
|
37
25
|
raise 'Not implemented: get_facebook_uid_from_access_token'
|
38
26
|
end
|
39
27
|
|
28
|
+
def self.get_user_info_from_access_token(access_token)
|
29
|
+
raise 'Not implemented: get_user_info_from_access_token'
|
30
|
+
end
|
31
|
+
|
40
32
|
def self.get_authorization_url(callback_uri, permissions = nil)
|
41
33
|
raise 'Not implemented: get_authorization_url'
|
42
34
|
end
|
@@ -7,13 +7,13 @@ module Challah
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def self.get_access_token_for_oauth_code(code, callback_uri)
|
10
|
-
client = new(
|
10
|
+
client = new(Facebook.options).auth(callback_uri).client
|
11
11
|
client.authorization_code = code
|
12
12
|
client.access_token!(:client_auth_body).to_s
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.get_access_token_from_cookies(cookies_hash)
|
16
|
-
fb_auth = new(
|
16
|
+
fb_auth = new(Facebook.options).auth
|
17
17
|
fb_auth.from_cookie(cookies_hash)
|
18
18
|
fb_auth.access_token.to_s
|
19
19
|
rescue
|
@@ -21,13 +21,13 @@ module Challah
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def self.get_extended_token(access_token)
|
24
|
-
fb_auth = new(
|
24
|
+
fb_auth = new(Facebook.options).auth
|
25
25
|
fb_auth.exchange_token!(access_token)
|
26
26
|
fb_auth.access_token.to_s
|
27
27
|
end
|
28
28
|
|
29
29
|
def self.get_facebook_uid_from_access_token(access_token)
|
30
|
-
fb_user =
|
30
|
+
fb_user = get_user_object_from_access_token(access_token)
|
31
31
|
|
32
32
|
if fb_user
|
33
33
|
return fb_user.identifier.to_s
|
@@ -41,18 +41,26 @@ module Challah
|
|
41
41
|
def self.get_user_info_from_access_token(access_token)
|
42
42
|
result = {}
|
43
43
|
|
44
|
-
fb_user =
|
44
|
+
fb_user = get_user_object_from_access_token(access_token)
|
45
45
|
|
46
|
-
|
47
|
-
|
46
|
+
Facebook.user_fields.each do |field|
|
47
|
+
if fb_user.respond_to?(field)
|
48
|
+
result[field] = fb_user.send(field)
|
49
|
+
else
|
50
|
+
result[field] = nil
|
51
|
+
end
|
48
52
|
end
|
49
53
|
|
50
54
|
result
|
51
55
|
end
|
52
56
|
|
57
|
+
def self.get_user_object_from_access_token(access_token)
|
58
|
+
::FbGraph::User.me(access_token).fetch
|
59
|
+
end
|
60
|
+
|
53
61
|
def self.get_authorization_url(callback_uri, permissions = nil)
|
54
|
-
scope =
|
55
|
-
client = new(
|
62
|
+
scope = Facebook.permissions if permissions.nil?
|
63
|
+
client = new(Facebook.options).auth(callback_uri).client
|
56
64
|
client.authorization_uri(scope: scope)
|
57
65
|
end
|
58
66
|
|
@@ -37,11 +37,11 @@ module Challah
|
|
37
37
|
|
38
38
|
begin
|
39
39
|
# Get extended token
|
40
|
-
extended_token =
|
40
|
+
extended_token = Facebook.interface.get_extended_token(token)
|
41
41
|
user.facebook_provider[:token] = extended_token
|
42
42
|
|
43
43
|
# Verify UID
|
44
|
-
test_uid =
|
44
|
+
test_uid = Facebook.interface.get_facebook_uid_from_access_token(extended_token)
|
45
45
|
|
46
46
|
# If the uid's match up, this is a valid token
|
47
47
|
return test_uid == uid
|
@@ -4,18 +4,25 @@ module Challah
|
|
4
4
|
def initialize(session)
|
5
5
|
@provider = session.provider? ? session.provider : nil
|
6
6
|
@token = session.token? ? session.token : nil
|
7
|
-
@uid = session.uid? ? session.uid : nil
|
7
|
+
@uid = session.uid? ? session.uid.to_s.strip : nil
|
8
8
|
end
|
9
9
|
|
10
10
|
def authenticate
|
11
11
|
return nil unless @provider == 'facebook'
|
12
12
|
return nil unless @token
|
13
13
|
|
14
|
-
|
15
|
-
auth = ::Authorization.where(provider: 'facebook', token: token, uid: @uid).first
|
14
|
+
auth = ::Authorization.where(provider: 'facebook', uid: @uid).first
|
16
15
|
|
17
16
|
if auth
|
18
|
-
|
17
|
+
token = Facebook.interface.get_extended_token(@token)
|
18
|
+
test_uid = Facebook.interface.get_facebook_uid_from_access_token(token).to_s.strip
|
19
|
+
|
20
|
+
# If the uid from the given token matches the provided uid, update the token
|
21
|
+
# and allow access
|
22
|
+
if test_uid and @uid == test_uid
|
23
|
+
auth.update_attribute(:token, token)
|
24
|
+
return auth.user
|
25
|
+
end
|
19
26
|
end
|
20
27
|
|
21
28
|
nil
|
data/lib/challah/facebook.rb
CHANGED
@@ -5,5 +5,35 @@ module Challah
|
|
5
5
|
autoload :Interface, 'challah/facebook/interface'
|
6
6
|
autoload :Provider, 'challah/facebook/provider'
|
7
7
|
autoload :Technique, 'challah/facebook/technique'
|
8
|
+
|
9
|
+
def self.options
|
10
|
+
@options ||= {
|
11
|
+
app_id: ENV['FACEBOOK_APP_ID'],
|
12
|
+
app_secret: ENV['FACEBOOK_SECRET'],
|
13
|
+
permissions: ENV['FACEBOOK_PERMISSIONS'].to_s.split(','),
|
14
|
+
user_fields: %w( first_name last_name email )
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
%w( app_id app_secret permissions user_fields ).each do |m|
|
19
|
+
class_eval "def self.#{m}; self.options[:#{m}]; end"
|
20
|
+
end
|
21
|
+
|
22
|
+
# The interface to use for all Facebook commands.
|
23
|
+
#
|
24
|
+
# Override this as needed. The Facebook interface should respond
|
25
|
+
# to all methods in Interfaces::Base
|
26
|
+
def self.interface
|
27
|
+
@interface ||= Interface
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.interface=(klass)
|
31
|
+
@interface = klass
|
32
|
+
end
|
33
|
+
|
34
|
+
# Delegate all other methods to Challah::Facebook to the interface
|
35
|
+
def self.method_missing(method, *args)
|
36
|
+
self.interface.send(method, *args)
|
37
|
+
end
|
8
38
|
end
|
9
39
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: challah-facebook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: challah
|
@@ -104,3 +104,4 @@ signing_key:
|
|
104
104
|
specification_version: 3
|
105
105
|
summary: Facebook authentication interface for Challah.
|
106
106
|
test_files: []
|
107
|
+
has_rdoc:
|