authpwn_rails 0.9.6 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +6 -0
- data/Gemfile +3 -2
- data/Gemfile.lock +38 -36
- data/README.rdoc +6 -11
- data/VERSION +1 -1
- data/authpwn_rails.gemspec +30 -22
- data/lib/authpwn_rails.rb +2 -2
- data/lib/authpwn_rails/credential_model.rb +38 -0
- data/lib/authpwn_rails/credentials.rb +10 -0
- data/lib/authpwn_rails/credentials/email.rb +30 -0
- data/lib/authpwn_rails/credentials/facebook.rb +77 -0
- data/lib/authpwn_rails/credentials/password.rb +63 -0
- data/lib/authpwn_rails/engine.rb +5 -7
- data/lib/authpwn_rails/facebook_session.rb +5 -5
- data/lib/authpwn_rails/generators/{session_generator.rb → all_generator.rb} +28 -9
- data/lib/authpwn_rails/generators/templates/001_create_users.rb +3 -11
- data/lib/authpwn_rails/generators/templates/002_create_credentials.rb +19 -0
- data/lib/authpwn_rails/generators/templates/credential.rb +16 -0
- data/lib/authpwn_rails/generators/templates/credentials.yml +34 -0
- data/lib/authpwn_rails/generators/templates/session/forbidden.html.erb +2 -2
- data/lib/authpwn_rails/generators/templates/session/home.html.erb +1 -1
- data/lib/authpwn_rails/generators/templates/session/new.html.erb +6 -6
- data/lib/authpwn_rails/generators/templates/session_controller.rb +1 -1
- data/lib/authpwn_rails/generators/templates/session_controller_test.rb +2 -2
- data/lib/authpwn_rails/generators/templates/user.rb +2 -2
- data/lib/authpwn_rails/generators/templates/users.yml +5 -8
- data/lib/authpwn_rails/session.rb +7 -7
- data/lib/authpwn_rails/session_controller.rb +15 -13
- data/lib/authpwn_rails/test_extensions.rb +6 -6
- data/lib/authpwn_rails/user_model.rb +23 -92
- data/test/email_credential_test.rb +50 -0
- data/test/facebook_controller_test.rb +7 -2
- data/test/facebook_credential_test.rb +74 -0
- data/test/helpers/db_setup.rb +4 -4
- data/test/helpers/fbgraph.rb +6 -2
- data/test/password_credential_test.rb +67 -0
- data/test/session_controller_api_test.rb +12 -12
- data/test/test_helper.rb +1 -0
- data/test/user_test.rb +11 -100
- metadata +41 -25
- data/lib/authpwn_rails/facebook_token_model.rb +0 -66
- data/lib/authpwn_rails/generators/facebook_generator.rb +0 -18
- data/lib/authpwn_rails/generators/templates/002_create_facebook_tokens.rb +0 -15
- data/lib/authpwn_rails/generators/templates/facebook_token.rb +0 -6
- data/lib/authpwn_rails/generators/templates/facebook_tokens.yml +0 -10
- data/lib/authpwn_rails/generators/users_generator.rb +0 -16
- data/test/facebook_token_test.rb +0 -28
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
gem "fbgraph_rails", ">= 0.2.2"
|
3
|
-
gem "rails", ">= 3.1.
|
3
|
+
gem "rails", ">= 3.1.3"
|
4
4
|
|
5
5
|
group :development do
|
6
6
|
gem "bundler", "~> 1.0.0"
|
7
|
+
gem "flexmock", "~> 0.9.0"
|
7
8
|
gem "jeweler", "~> 1.6.0"
|
8
|
-
gem "rcov", ">= 0"
|
9
|
+
gem "rcov", ">= 0", :platform => :mri
|
9
10
|
gem "sqlite3", ">= 1.3.3"
|
10
11
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
actionmailer (3.1.
|
5
|
-
actionpack (= 3.1.
|
4
|
+
actionmailer (3.1.3)
|
5
|
+
actionpack (= 3.1.3)
|
6
6
|
mail (~> 2.3.0)
|
7
|
-
actionpack (3.1.
|
8
|
-
activemodel (= 3.1.
|
9
|
-
activesupport (= 3.1.
|
7
|
+
actionpack (3.1.3)
|
8
|
+
activemodel (= 3.1.3)
|
9
|
+
activesupport (= 3.1.3)
|
10
10
|
builder (~> 3.0.0)
|
11
11
|
erubis (~> 2.7.0)
|
12
12
|
i18n (~> 0.6)
|
13
|
-
rack (~> 1.3.
|
13
|
+
rack (~> 1.3.5)
|
14
14
|
rack-cache (~> 1.1)
|
15
15
|
rack-mount (~> 0.8.2)
|
16
16
|
rack-test (~> 0.6.1)
|
17
|
-
sprockets (~> 2.0.
|
18
|
-
activemodel (3.1.
|
19
|
-
activesupport (= 3.1.
|
17
|
+
sprockets (~> 2.0.3)
|
18
|
+
activemodel (3.1.3)
|
19
|
+
activesupport (= 3.1.3)
|
20
20
|
builder (~> 3.0.0)
|
21
21
|
i18n (~> 0.6)
|
22
|
-
activerecord (3.1.
|
23
|
-
activemodel (= 3.1.
|
24
|
-
activesupport (= 3.1.
|
22
|
+
activerecord (3.1.3)
|
23
|
+
activemodel (= 3.1.3)
|
24
|
+
activesupport (= 3.1.3)
|
25
25
|
arel (~> 2.2.1)
|
26
26
|
tzinfo (~> 0.3.29)
|
27
|
-
activeresource (3.1.
|
28
|
-
activemodel (= 3.1.
|
29
|
-
activesupport (= 3.1.
|
30
|
-
activesupport (3.1.
|
27
|
+
activeresource (3.1.3)
|
28
|
+
activemodel (= 3.1.3)
|
29
|
+
activesupport (= 3.1.3)
|
30
|
+
activesupport (3.1.3)
|
31
31
|
multi_json (~> 1.0)
|
32
32
|
addressable (2.2.6)
|
33
33
|
arel (2.2.1)
|
@@ -37,7 +37,7 @@ GEM
|
|
37
37
|
addressable (~> 2.2.6)
|
38
38
|
multipart-post (~> 1.1.3)
|
39
39
|
rack (>= 1.1.0, < 2)
|
40
|
-
fbgraph (1.
|
40
|
+
fbgraph (1.9.0)
|
41
41
|
activesupport
|
42
42
|
faraday (>= 0.7.5)
|
43
43
|
hashie (>= 1.0.0)
|
@@ -51,8 +51,9 @@ GEM
|
|
51
51
|
json (>= 1.6.1)
|
52
52
|
oauth2 (>= 0.5.0)
|
53
53
|
rails (>= 3.1.0)
|
54
|
+
flexmock (0.9.0)
|
54
55
|
git (1.2.5)
|
55
|
-
hashie (1.
|
56
|
+
hashie (1.2.0)
|
56
57
|
hike (1.2.1)
|
57
58
|
i18n (0.6.0)
|
58
59
|
jeweler (1.6.4)
|
@@ -64,14 +65,14 @@ GEM
|
|
64
65
|
i18n (>= 0.4.0)
|
65
66
|
mime-types (~> 1.16)
|
66
67
|
treetop (~> 1.4.8)
|
67
|
-
mime-types (1.
|
68
|
+
mime-types (1.17.2)
|
68
69
|
multi_json (1.0.3)
|
69
70
|
multipart-post (1.1.3)
|
70
71
|
oauth2 (0.5.1)
|
71
72
|
faraday (~> 0.7.4)
|
72
73
|
multi_json (~> 1.0.3)
|
73
|
-
polyglot (0.3.
|
74
|
-
rack (1.3.
|
74
|
+
polyglot (0.3.3)
|
75
|
+
rack (1.3.5)
|
75
76
|
rack-cache (1.1)
|
76
77
|
rack (>= 0.4)
|
77
78
|
rack-mount (0.8.3)
|
@@ -80,28 +81,28 @@ GEM
|
|
80
81
|
rack
|
81
82
|
rack-test (0.6.1)
|
82
83
|
rack (>= 1.0)
|
83
|
-
rails (3.1.
|
84
|
-
actionmailer (= 3.1.
|
85
|
-
actionpack (= 3.1.
|
86
|
-
activerecord (= 3.1.
|
87
|
-
activeresource (= 3.1.
|
88
|
-
activesupport (= 3.1.
|
84
|
+
rails (3.1.3)
|
85
|
+
actionmailer (= 3.1.3)
|
86
|
+
actionpack (= 3.1.3)
|
87
|
+
activerecord (= 3.1.3)
|
88
|
+
activeresource (= 3.1.3)
|
89
|
+
activesupport (= 3.1.3)
|
89
90
|
bundler (~> 1.0)
|
90
|
-
railties (= 3.1.
|
91
|
-
railties (3.1.
|
92
|
-
actionpack (= 3.1.
|
93
|
-
activesupport (= 3.1.
|
91
|
+
railties (= 3.1.3)
|
92
|
+
railties (3.1.3)
|
93
|
+
actionpack (= 3.1.3)
|
94
|
+
activesupport (= 3.1.3)
|
94
95
|
rack-ssl (~> 1.3.2)
|
95
96
|
rake (>= 0.8.7)
|
96
97
|
rdoc (~> 3.4)
|
97
98
|
thor (~> 0.14.6)
|
98
|
-
rake (0.9.2)
|
99
|
+
rake (0.9.2.2)
|
99
100
|
rcov (0.9.11)
|
100
|
-
rdoc (3.
|
101
|
+
rdoc (3.11)
|
101
102
|
json (~> 1.4)
|
102
103
|
rest-client (1.6.7)
|
103
104
|
mime-types (>= 1.16)
|
104
|
-
sprockets (2.0.
|
105
|
+
sprockets (2.0.3)
|
105
106
|
hike (~> 1.2)
|
106
107
|
rack (~> 1.0)
|
107
108
|
tilt (~> 1.1, != 1.3.0)
|
@@ -111,7 +112,7 @@ GEM
|
|
111
112
|
treetop (1.4.10)
|
112
113
|
polyglot
|
113
114
|
polyglot (>= 0.3.1)
|
114
|
-
tzinfo (0.3.
|
115
|
+
tzinfo (0.3.31)
|
115
116
|
|
116
117
|
PLATFORMS
|
117
118
|
ruby
|
@@ -119,7 +120,8 @@ PLATFORMS
|
|
119
120
|
DEPENDENCIES
|
120
121
|
bundler (~> 1.0.0)
|
121
122
|
fbgraph_rails (>= 0.2.2)
|
123
|
+
flexmock (~> 0.9.0)
|
122
124
|
jeweler (~> 1.6.0)
|
123
|
-
rails (>= 3.1.
|
125
|
+
rails (>= 3.1.3)
|
124
126
|
rcov
|
125
127
|
sqlite3 (>= 1.3.3)
|
data/README.rdoc
CHANGED
@@ -4,17 +4,12 @@ User authentication for a Ruby on Rails 3 application. Works with Facebook.
|
|
4
4
|
|
5
5
|
== Integration
|
6
6
|
|
7
|
-
Scaffold user accounts, session controller views
|
8
|
-
rails g authpwn:
|
9
|
-
rails g authpwn:session
|
10
|
-
rails g authpwn:facebook
|
7
|
+
Scaffold user accounts, and session controller views.
|
8
|
+
rails g authpwn:all
|
11
9
|
|
12
10
|
Wire authentication into your ApplicationController.
|
13
11
|
authenticates_using_session
|
14
|
-
authenticates_using_facebook
|
15
|
-
|
16
|
-
Point the default route to the sessions controller in config/routes.rb
|
17
|
-
root :to => "session#show"
|
12
|
+
authenticates_using_facebook
|
18
13
|
|
19
14
|
Note: the code inside the models and controllers is tucked away in the plug-in.
|
20
15
|
The scaffold models and controllers are there as extension points. You will be
|
@@ -24,12 +19,12 @@ able to update the plug-in without regenerating the scaffolds.
|
|
24
19
|
|
25
20
|
* Fork the project.
|
26
21
|
* Make your feature addition or bug fix.
|
27
|
-
* Add tests for it. This is important so I don't break it in a
|
28
|
-
|
22
|
+
* Add tests for it. This is important so I don't break it in a future version
|
23
|
+
unintentionally.
|
29
24
|
* Commit, do not mess with rakefile, version, or history.
|
30
25
|
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
31
26
|
* Send me a pull request. Bonus points for topic branches.
|
32
27
|
|
33
28
|
== Copyright
|
34
29
|
|
35
|
-
Copyright (c) 2010 Victor Costan, released under the MIT license
|
30
|
+
Copyright (c) 2010-2012 Victor Costan, released under the MIT license
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.10.0
|
data/authpwn_rails.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
7
|
+
s.name = "authpwn_rails"
|
8
|
+
s.version = "0.10.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
11
|
+
s.authors = ["Victor Costan"]
|
12
|
+
s.date = "2011-11-21"
|
13
|
+
s.description = "Works with Facebook."
|
14
|
+
s.email = "victor@costan.us"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.rdoc"
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".project",
|
22
|
+
".travis.yml",
|
22
23
|
"Gemfile",
|
23
24
|
"Gemfile.lock",
|
24
25
|
"LICENSE",
|
@@ -28,15 +29,18 @@ Gem::Specification.new do |s|
|
|
28
29
|
"app/helpers/session_helper.rb",
|
29
30
|
"authpwn_rails.gemspec",
|
30
31
|
"lib/authpwn_rails.rb",
|
32
|
+
"lib/authpwn_rails/credential_model.rb",
|
33
|
+
"lib/authpwn_rails/credentials.rb",
|
34
|
+
"lib/authpwn_rails/credentials/email.rb",
|
35
|
+
"lib/authpwn_rails/credentials/facebook.rb",
|
36
|
+
"lib/authpwn_rails/credentials/password.rb",
|
31
37
|
"lib/authpwn_rails/engine.rb",
|
32
38
|
"lib/authpwn_rails/facebook_session.rb",
|
33
|
-
"lib/authpwn_rails/
|
34
|
-
"lib/authpwn_rails/generators/facebook_generator.rb",
|
35
|
-
"lib/authpwn_rails/generators/session_generator.rb",
|
39
|
+
"lib/authpwn_rails/generators/all_generator.rb",
|
36
40
|
"lib/authpwn_rails/generators/templates/001_create_users.rb",
|
37
|
-
"lib/authpwn_rails/generators/templates/
|
38
|
-
"lib/authpwn_rails/generators/templates/
|
39
|
-
"lib/authpwn_rails/generators/templates/
|
41
|
+
"lib/authpwn_rails/generators/templates/002_create_credentials.rb",
|
42
|
+
"lib/authpwn_rails/generators/templates/credential.rb",
|
43
|
+
"lib/authpwn_rails/generators/templates/credentials.yml",
|
40
44
|
"lib/authpwn_rails/generators/templates/session/forbidden.html.erb",
|
41
45
|
"lib/authpwn_rails/generators/templates/session/home.html.erb",
|
42
46
|
"lib/authpwn_rails/generators/templates/session/new.html.erb",
|
@@ -45,51 +49,55 @@ Gem::Specification.new do |s|
|
|
45
49
|
"lib/authpwn_rails/generators/templates/session_controller_test.rb",
|
46
50
|
"lib/authpwn_rails/generators/templates/user.rb",
|
47
51
|
"lib/authpwn_rails/generators/templates/users.yml",
|
48
|
-
"lib/authpwn_rails/generators/users_generator.rb",
|
49
52
|
"lib/authpwn_rails/session.rb",
|
50
53
|
"lib/authpwn_rails/session_controller.rb",
|
51
54
|
"lib/authpwn_rails/test_extensions.rb",
|
52
55
|
"lib/authpwn_rails/user_model.rb",
|
53
56
|
"test/cookie_controller_test.rb",
|
57
|
+
"test/email_credential_test.rb",
|
54
58
|
"test/facebook_controller_test.rb",
|
55
|
-
"test/
|
59
|
+
"test/facebook_credential_test.rb",
|
56
60
|
"test/helpers/application_controller.rb",
|
57
61
|
"test/helpers/db_setup.rb",
|
58
62
|
"test/helpers/fbgraph.rb",
|
59
63
|
"test/helpers/routes.rb",
|
60
64
|
"test/helpers/view_helpers.rb",
|
65
|
+
"test/password_credential_test.rb",
|
61
66
|
"test/session_controller_api_test.rb",
|
62
67
|
"test/test_helper.rb",
|
63
68
|
"test/user_test.rb"
|
64
69
|
]
|
65
|
-
s.homepage =
|
66
|
-
s.licenses = [
|
67
|
-
s.require_paths = [
|
68
|
-
s.rubygems_version =
|
69
|
-
s.summary =
|
70
|
+
s.homepage = "http://github.com/pwnall/authpwn_rails"
|
71
|
+
s.licenses = ["MIT"]
|
72
|
+
s.require_paths = ["lib"]
|
73
|
+
s.rubygems_version = "1.8.11"
|
74
|
+
s.summary = "User authentication for Rails 3 applications."
|
70
75
|
|
71
76
|
if s.respond_to? :specification_version then
|
72
77
|
s.specification_version = 3
|
73
78
|
|
74
79
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
75
80
|
s.add_runtime_dependency(%q<fbgraph_rails>, [">= 0.2.2"])
|
76
|
-
s.add_runtime_dependency(%q<rails>, [">= 3.1.
|
81
|
+
s.add_runtime_dependency(%q<rails>, [">= 3.1.3"])
|
77
82
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
83
|
+
s.add_development_dependency(%q<flexmock>, ["~> 0.9.0"])
|
78
84
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
|
79
85
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
80
86
|
s.add_development_dependency(%q<sqlite3>, [">= 1.3.3"])
|
81
87
|
else
|
82
88
|
s.add_dependency(%q<fbgraph_rails>, [">= 0.2.2"])
|
83
|
-
s.add_dependency(%q<rails>, [">= 3.1.
|
89
|
+
s.add_dependency(%q<rails>, [">= 3.1.3"])
|
84
90
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
91
|
+
s.add_dependency(%q<flexmock>, ["~> 0.9.0"])
|
85
92
|
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
86
93
|
s.add_dependency(%q<rcov>, [">= 0"])
|
87
94
|
s.add_dependency(%q<sqlite3>, [">= 1.3.3"])
|
88
95
|
end
|
89
96
|
else
|
90
97
|
s.add_dependency(%q<fbgraph_rails>, [">= 0.2.2"])
|
91
|
-
s.add_dependency(%q<rails>, [">= 3.1.
|
98
|
+
s.add_dependency(%q<rails>, [">= 3.1.3"])
|
92
99
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
100
|
+
s.add_dependency(%q<flexmock>, ["~> 0.9.0"])
|
93
101
|
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
94
102
|
s.add_dependency(%q<rcov>, [">= 0"])
|
95
103
|
s.add_dependency(%q<sqlite3>, [">= 1.3.3"])
|
data/lib/authpwn_rails.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# :nodoc: namespace
|
2
|
-
module
|
2
|
+
module Authpwn
|
3
3
|
end
|
4
4
|
|
5
|
+
require 'authpwn_rails/credential_model.rb'
|
5
6
|
require 'authpwn_rails/facebook_session.rb'
|
6
|
-
require 'authpwn_rails/facebook_token_model.rb'
|
7
7
|
require 'authpwn_rails/session.rb'
|
8
8
|
require 'authpwn_rails/session_controller.rb'
|
9
9
|
require 'authpwn_rails/test_extensions.rb'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
# :nodoc: namespace
|
4
|
+
module Authpwn
|
5
|
+
|
6
|
+
# Included by the model class that represents facebook tokens.
|
7
|
+
#
|
8
|
+
# Parts of the codebase assume the model will be named Credential.
|
9
|
+
module CredentialModel
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
# The user whose token this is.
|
14
|
+
belongs_to :user, :inverse_of => :credentials
|
15
|
+
validates :user, :presence => true
|
16
|
+
|
17
|
+
# Name that can be used to find the token.
|
18
|
+
validates :name, :length => { :in => 1..128, :allow_nil => true },
|
19
|
+
:uniqueness => { :scope => [:type], :allow_nil => true }
|
20
|
+
|
21
|
+
# Secret information associated with the token.
|
22
|
+
validates :key, :length => { :in => 1..2.kilobytes, :allow_nil => true }
|
23
|
+
end
|
24
|
+
|
25
|
+
# Included in the metaclass of models that call pwnauth_facebook_token_model.
|
26
|
+
module ClassMethods
|
27
|
+
|
28
|
+
end # module Authpwn::FacebookTokenModel::ClassMethods
|
29
|
+
|
30
|
+
|
31
|
+
# Included in models that include Authpwn::FacebookTokenModel.
|
32
|
+
module InstanceMethods
|
33
|
+
|
34
|
+
end # module Authpwn::FacebookTokenModel::InstanceMethods
|
35
|
+
|
36
|
+
end # namespace Authpwn::FacebookTokenModel
|
37
|
+
|
38
|
+
end # namespace Authpwn
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Loads sub-classes of the Credential model.
|
2
|
+
#
|
3
|
+
# We allow the Credential model to be defined in the Rails application, so the
|
4
|
+
# application can choose the storage model (ActiveRecord vs Mongoid etc.). This
|
5
|
+
# means that we have to load the classes that inherit from Credential after it's
|
6
|
+
# defined, which is long after the authpwn_rails engine is loaded.
|
7
|
+
|
8
|
+
require 'authpwn_rails/credentials/email.rb'
|
9
|
+
require 'authpwn_rails/credentials/facebook.rb'
|
10
|
+
require 'authpwn_rails/credentials/password.rb'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# :namespace
|
2
|
+
module Credentials
|
3
|
+
|
4
|
+
# Associates an e-mail address with the user account.
|
5
|
+
class Email < ::Credential
|
6
|
+
# The e-mail address.
|
7
|
+
alias_attribute :email, :name
|
8
|
+
validates :name, :format => /^[A-Za-z0-9.+_]+@[^@]*\.(\w+)$/,
|
9
|
+
:presence => true, :uniqueness => { :scope => [:type],
|
10
|
+
:message => 'This e-mail address is already claimed by an account' }
|
11
|
+
|
12
|
+
# '1' if the user proved ownership of the e-mail address.
|
13
|
+
alias_attribute :verified, :key
|
14
|
+
validates :verified, :presence => true
|
15
|
+
|
16
|
+
before_validation :set_verified_to_false, :on => :create
|
17
|
+
# :nodoc: by default, e-mail addresses are not verified
|
18
|
+
def set_verified_to_false
|
19
|
+
self.verified ||= '0' if self.key.nil?
|
20
|
+
end
|
21
|
+
end # class Credentials::Email
|
22
|
+
|
23
|
+
end # namespace Credentials
|
24
|
+
|
25
|
+
# :nodoc: adds e-mail integration to the user model
|
26
|
+
module Authpwn::UserModel::InstanceMethods
|
27
|
+
def email_credential
|
28
|
+
credentials.find { |c| c.instance_of?(Credentials::Email) }
|
29
|
+
end
|
30
|
+
end # module Authpwn::UserModel::InstanceMethods
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# :namespace
|
2
|
+
module Credentials
|
3
|
+
|
4
|
+
# Associates a Facebook account and OAuth2 token with an account.
|
5
|
+
class Facebook < ::Credential
|
6
|
+
# The Graph API object ID of the Facebook account.
|
7
|
+
alias_attribute :facebook_uid, :name
|
8
|
+
validates :name, :format => /^\d+$/, :presence => true,
|
9
|
+
:uniqueness => { :scope => [:type],
|
10
|
+
:message => 'Your Facebook user is already associated to an account' }
|
11
|
+
|
12
|
+
# A user can be associated to a single Facebook account.
|
13
|
+
validates :user_id, :uniqueness => {
|
14
|
+
:message => 'Your account is already associated to a Facebook user' }
|
15
|
+
|
16
|
+
# OAuth2 token issued by Facebook.
|
17
|
+
alias_attribute :access_token, :key
|
18
|
+
validates :key, :presence => true
|
19
|
+
|
20
|
+
# FBGraph client loaded with this access token.
|
21
|
+
def facebook_client
|
22
|
+
@client ||= FBGraphRails.fbclient(access_token)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Finds or creates the model containing a token.
|
26
|
+
#
|
27
|
+
# If a model for the same user exists, the model is updated with the given
|
28
|
+
# token. Otherwise, a new model will be created, together with a user.
|
29
|
+
def self.for(access_token)
|
30
|
+
uid = uid_from_token access_token
|
31
|
+
credential = self.where(:name => uid.to_str).first
|
32
|
+
if credential
|
33
|
+
credential.update_attributes! :key => access_token
|
34
|
+
else
|
35
|
+
User.transaction do
|
36
|
+
user = User.create!
|
37
|
+
credential = self.create! :name => uid, :key => access_token,
|
38
|
+
:user => user
|
39
|
+
end
|
40
|
+
end
|
41
|
+
credential
|
42
|
+
end
|
43
|
+
|
44
|
+
# Extracts the Facebook user ID from a OAuth2 token.
|
45
|
+
#
|
46
|
+
# This used to be a hack that pulled the UID out of an OAuth2 token. The new
|
47
|
+
# encrypted OAuth2 tokens don't have UIDs anymore, so this method is an
|
48
|
+
# interim hack for old code that still depends on it.
|
49
|
+
def self.uid_from_token(access_token)
|
50
|
+
FBGraphRails.fbclient(access_token).selection.me.info!.id.to_s
|
51
|
+
end
|
52
|
+
end # class Credentials::Facebook
|
53
|
+
|
54
|
+
end # namespace Credentials
|
55
|
+
|
56
|
+
# :nodoc: adds Facebook integration methods to the User model.
|
57
|
+
module Authpwn::UserModel::ClassMethods
|
58
|
+
# Fills out a new user's information based on a Facebook access token.
|
59
|
+
def create_with_facebook_token(token)
|
60
|
+
self.create! :email => "#{token.external_uid}@graph.facebook.com"
|
61
|
+
end
|
62
|
+
|
63
|
+
# The user that owns a given Facebook OAuth2 token.
|
64
|
+
#
|
65
|
+
# A new user will be created if the token doesn't belong to any user. This
|
66
|
+
# is the case for a new visitor.
|
67
|
+
def for_facebook_token(access_token)
|
68
|
+
Credentials::Facebook.for(access_token).user
|
69
|
+
end
|
70
|
+
end # module Authpwn::UserModel::ClassMethods
|
71
|
+
|
72
|
+
# :nodoc: adds Facebook integration methods to the User model.
|
73
|
+
module Authpwn::UserModel::InstanceMethods
|
74
|
+
def facebook_credential
|
75
|
+
credentials.find { |c| c.instance_of?(Credentials::Facebook) }
|
76
|
+
end
|
77
|
+
end # module Authpwn::UserModel::InstanceMethods
|