authpwn_rails 0.16.2 → 0.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -6
- data/Gemfile +7 -8
- data/Gemfile.lock +97 -113
- data/Gemfile.rails4 +8 -9
- data/{Gemfile.rails3 → Gemfile.rails41} +6 -7
- data/Gemfile.rails42 +17 -0
- data/README.rdoc +1 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/app/models/credentials/email.rb +15 -37
- data/app/models/credentials/omni_auth_uid.rb +96 -0
- data/app/models/credentials/password.rb +0 -5
- data/app/models/tokens/base.rb +11 -38
- data/authpwn_rails.gemspec +35 -33
- data/lib/authpwn_rails/credential_model.rb +1 -5
- data/lib/authpwn_rails/generators/all_generator.rb +3 -1
- data/lib/authpwn_rails/generators/templates/001_create_users.rb +3 -3
- data/lib/authpwn_rails/generators/templates/003_create_credentials.rb +7 -7
- data/lib/authpwn_rails/generators/templates/credentials.yml +13 -13
- data/lib/authpwn_rails/generators/templates/omniauth_initializer.rb +13 -0
- data/lib/authpwn_rails/generators/templates/session_controller_test.rb +22 -0
- data/lib/authpwn_rails/generators/templates/session_mailer/email_verification_email.html.erb +3 -3
- data/lib/authpwn_rails/generators/templates/session_mailer/email_verification_email.text.erb +1 -1
- data/lib/authpwn_rails/generators/templates/session_mailer/reset_password_email.html.erb +3 -3
- data/lib/authpwn_rails/generators/templates/session_mailer/reset_password_email.text.erb +1 -1
- data/lib/authpwn_rails/generators/templates/session_mailer.rb +1 -1
- data/lib/authpwn_rails/generators/templates/session_mailer_test.rb +14 -4
- data/lib/authpwn_rails/generators/templates/user.rb +40 -5
- data/lib/authpwn_rails/http_basic.rb +6 -5
- data/lib/authpwn_rails/routes.rb +20 -7
- data/lib/authpwn_rails/session.rb +1 -1
- data/lib/authpwn_rails/session_controller.rb +48 -12
- data/lib/authpwn_rails/session_mailer.rb +13 -14
- data/lib/authpwn_rails/session_model.rb +4 -24
- data/lib/authpwn_rails/user_extensions/email_field.rb +5 -21
- data/lib/authpwn_rails/user_extensions/password_field.rb +0 -4
- data/lib/authpwn_rails/user_model.rb +46 -12
- data/lib/authpwn_rails.rb +0 -2
- data/test/cookie_controller_test.rb +1 -7
- data/test/credentials/omni_auth_uid_credential_test.rb +141 -0
- data/test/helpers/action_controller.rb +2 -8
- data/test/helpers/db_setup.rb +8 -16
- data/test/helpers/routes.rb +35 -30
- data/test/helpers/test_order.rb +3 -0
- data/test/http_basic_controller_test.rb +7 -18
- data/test/routes_test.rb +19 -10
- data/test/session_controller_api_test.rb +181 -30
- data/test/session_controller_test.rb +6 -0
- data/test/session_mailer_api_test.rb +18 -13
- data/test/session_mailer_test.rb +6 -0
- data/test/test_helper.rb +3 -3
- data/test/user_test.rb +54 -7
- metadata +65 -64
- data/app/models/credentials/facebook.rb +0 -63
- data/lib/authpwn_rails/facebook_session.rb +0 -33
- data/lib/authpwn_rails/user_extensions/facebook_fields.rb +0 -63
- data/test/credentials/facebook_credential_test.rb +0 -64
- data/test/facebook_controller_test.rb +0 -65
- data/test/user_extensions/facebook_fields_test.rb +0 -61
metadata
CHANGED
@@ -1,181 +1,181 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: authpwn_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.17.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Costan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: fbgraph_rails
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 0.2.2
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - '>='
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 0.2.2
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rails
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
|
-
- -
|
17
|
+
- - ">="
|
32
18
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
19
|
+
version: 4.0.9
|
34
20
|
type: :runtime
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
|
-
- -
|
24
|
+
- - ">="
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
26
|
+
version: 4.0.9
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: bundler
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
44
30
|
requirements:
|
45
|
-
- -
|
31
|
+
- - ">="
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version: 1.3
|
33
|
+
version: 1.7.3
|
48
34
|
type: :development
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
|
-
- -
|
38
|
+
- - ">="
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: 1.3
|
40
|
+
version: 1.7.3
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: mocha
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
|
-
- -
|
45
|
+
- - ">="
|
60
46
|
- !ruby/object:Gem::Version
|
61
47
|
version: 0.14.0
|
62
48
|
type: :development
|
63
49
|
prerelease: false
|
64
50
|
version_requirements: !ruby/object:Gem::Requirement
|
65
51
|
requirements:
|
66
|
-
- -
|
52
|
+
- - ">="
|
67
53
|
- !ruby/object:Gem::Version
|
68
54
|
version: 0.14.0
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: jeweler
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
72
58
|
requirements:
|
73
|
-
- -
|
59
|
+
- - ">="
|
74
60
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
61
|
+
version: 2.0.1
|
76
62
|
type: :development
|
77
63
|
prerelease: false
|
78
64
|
version_requirements: !ruby/object:Gem::Requirement
|
79
65
|
requirements:
|
80
|
-
- -
|
66
|
+
- - ">="
|
81
67
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
68
|
+
version: 2.0.1
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
70
|
name: simplecov
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
86
72
|
requirements:
|
87
|
-
- -
|
73
|
+
- - ">="
|
88
74
|
- !ruby/object:Gem::Version
|
89
75
|
version: '0'
|
90
76
|
type: :development
|
91
77
|
prerelease: false
|
92
78
|
version_requirements: !ruby/object:Gem::Requirement
|
93
79
|
requirements:
|
94
|
-
- -
|
80
|
+
- - ">="
|
95
81
|
- !ruby/object:Gem::Version
|
96
82
|
version: '0'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: mysql2
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
100
86
|
requirements:
|
101
|
-
- -
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.3.16
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.3.16
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: omniauth
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
103
|
+
version: 1.2.2
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
110
|
+
version: 1.2.2
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: pg
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: 0.17.
|
117
|
+
version: 0.17.1
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: 0.17.
|
124
|
+
version: 0.17.1
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: sqlite3
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
|
-
- -
|
129
|
+
- - ">="
|
130
130
|
- !ruby/object:Gem::Version
|
131
|
-
version: 1.3.
|
131
|
+
version: 1.3.9
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
|
-
- -
|
136
|
+
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
|
-
version: 1.3.
|
138
|
+
version: 1.3.9
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: rubysl
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
|
-
- -
|
143
|
+
- - ">="
|
144
144
|
- !ruby/object:Gem::Version
|
145
145
|
version: '0'
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
|
-
- -
|
150
|
+
- - ">="
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: rubysl-bundler
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
|
-
- -
|
157
|
+
- - ">="
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
160
|
type: :development
|
161
161
|
prerelease: false
|
162
162
|
version_requirements: !ruby/object:Gem::Requirement
|
163
163
|
requirements:
|
164
|
-
- -
|
164
|
+
- - ">="
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
167
|
- !ruby/object:Gem::Dependency
|
168
168
|
name: rubysl-rake
|
169
169
|
requirement: !ruby/object:Gem::Requirement
|
170
170
|
requirements:
|
171
|
-
- -
|
171
|
+
- - ">="
|
172
172
|
- !ruby/object:Gem::Version
|
173
173
|
version: '0'
|
174
174
|
type: :development
|
175
175
|
prerelease: false
|
176
176
|
version_requirements: !ruby/object:Gem::Requirement
|
177
177
|
requirements:
|
178
|
-
- -
|
178
|
+
- - ">="
|
179
179
|
- !ruby/object:Gem::Version
|
180
180
|
version: '0'
|
181
181
|
description: Works with Facebook.
|
@@ -186,20 +186,21 @@ extra_rdoc_files:
|
|
186
186
|
- LICENSE
|
187
187
|
- README.rdoc
|
188
188
|
files:
|
189
|
-
- .document
|
190
|
-
- .project
|
191
|
-
- .travis.yml
|
189
|
+
- ".document"
|
190
|
+
- ".project"
|
191
|
+
- ".travis.yml"
|
192
192
|
- Gemfile
|
193
193
|
- Gemfile.lock
|
194
|
-
- Gemfile.rails3
|
195
194
|
- Gemfile.rails4
|
195
|
+
- Gemfile.rails41
|
196
|
+
- Gemfile.rails42
|
196
197
|
- LICENSE
|
197
198
|
- README.rdoc
|
198
199
|
- Rakefile
|
199
200
|
- VERSION
|
200
201
|
- app/helpers/session_helper.rb
|
201
202
|
- app/models/credentials/email.rb
|
202
|
-
- app/models/credentials/
|
203
|
+
- app/models/credentials/omni_auth_uid.rb
|
203
204
|
- app/models/credentials/password.rb
|
204
205
|
- app/models/tokens/base.rb
|
205
206
|
- app/models/tokens/email_verification.rb
|
@@ -214,13 +215,13 @@ files:
|
|
214
215
|
- lib/authpwn_rails/current_user.rb
|
215
216
|
- lib/authpwn_rails/engine.rb
|
216
217
|
- lib/authpwn_rails/expires.rb
|
217
|
-
- lib/authpwn_rails/facebook_session.rb
|
218
218
|
- lib/authpwn_rails/generators/all_generator.rb
|
219
219
|
- lib/authpwn_rails/generators/templates/001_create_users.rb
|
220
220
|
- lib/authpwn_rails/generators/templates/003_create_credentials.rb
|
221
221
|
- lib/authpwn_rails/generators/templates/credential.rb
|
222
222
|
- lib/authpwn_rails/generators/templates/credentials.yml
|
223
223
|
- lib/authpwn_rails/generators/templates/initializer.rb
|
224
|
+
- lib/authpwn_rails/generators/templates/omniauth_initializer.rb
|
224
225
|
- lib/authpwn_rails/generators/templates/session.rb
|
225
226
|
- lib/authpwn_rails/generators/templates/session/forbidden.html.erb
|
226
227
|
- lib/authpwn_rails/generators/templates/session/home.html.erb
|
@@ -245,19 +246,17 @@ files:
|
|
245
246
|
- lib/authpwn_rails/session_model.rb
|
246
247
|
- lib/authpwn_rails/test_extensions.rb
|
247
248
|
- lib/authpwn_rails/user_extensions/email_field.rb
|
248
|
-
- lib/authpwn_rails/user_extensions/facebook_fields.rb
|
249
249
|
- lib/authpwn_rails/user_extensions/password_field.rb
|
250
250
|
- lib/authpwn_rails/user_model.rb
|
251
251
|
- test/cookie_controller_test.rb
|
252
252
|
- test/credentials/email_credential_test.rb
|
253
253
|
- test/credentials/email_verification_token_test.rb
|
254
|
-
- test/credentials/
|
254
|
+
- test/credentials/omni_auth_uid_credential_test.rb
|
255
255
|
- test/credentials/one_time_token_credential_test.rb
|
256
256
|
- test/credentials/password_credential_test.rb
|
257
257
|
- test/credentials/password_reset_token_test.rb
|
258
258
|
- test/credentials/session_uid_token_test.rb
|
259
259
|
- test/credentials/token_crendential_test.rb
|
260
|
-
- test/facebook_controller_test.rb
|
261
260
|
- test/fixtures/bare_session/forbidden.html.erb
|
262
261
|
- test/fixtures/bare_session/home.html.erb
|
263
262
|
- test/fixtures/bare_session/new.html.erb
|
@@ -273,17 +272,19 @@ files:
|
|
273
272
|
- test/helpers/rails.rb
|
274
273
|
- test/helpers/rails_undo.rb
|
275
274
|
- test/helpers/routes.rb
|
275
|
+
- test/helpers/test_order.rb
|
276
276
|
- test/helpers/view_helpers.rb
|
277
277
|
- test/http_basic_controller_test.rb
|
278
278
|
- test/initializer_test.rb
|
279
279
|
- test/routes_test.rb
|
280
280
|
- test/session_controller_api_test.rb
|
281
|
+
- test/session_controller_test.rb
|
281
282
|
- test/session_mailer_api_test.rb
|
283
|
+
- test/session_mailer_test.rb
|
282
284
|
- test/session_test.rb
|
283
285
|
- test/test_extensions_test.rb
|
284
286
|
- test/test_helper.rb
|
285
287
|
- test/user_extensions/email_field_test.rb
|
286
|
-
- test/user_extensions/facebook_fields_test.rb
|
287
288
|
- test/user_extensions/password_field_test.rb
|
288
289
|
- test/user_test.rb
|
289
290
|
homepage: http://github.com/pwnall/authpwn_rails
|
@@ -296,18 +297,18 @@ require_paths:
|
|
296
297
|
- lib
|
297
298
|
required_ruby_version: !ruby/object:Gem::Requirement
|
298
299
|
requirements:
|
299
|
-
- -
|
300
|
+
- - ">="
|
300
301
|
- !ruby/object:Gem::Version
|
301
302
|
version: '0'
|
302
303
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
303
304
|
requirements:
|
304
|
-
- -
|
305
|
+
- - ">="
|
305
306
|
- !ruby/object:Gem::Version
|
306
307
|
version: '0'
|
307
308
|
requirements: []
|
308
309
|
rubyforge_project:
|
309
|
-
rubygems_version: 2.
|
310
|
+
rubygems_version: 2.2.2
|
310
311
|
signing_key:
|
311
312
|
specification_version: 4
|
312
|
-
summary: User authentication for Rails
|
313
|
+
summary: User authentication for Rails 4 applications.
|
313
314
|
test_files: []
|
@@ -1,63 +0,0 @@
|
|
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: /\A\d+\Z/, 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.key = access_token
|
34
|
-
credential.save!
|
35
|
-
else
|
36
|
-
User.transaction do
|
37
|
-
user = User.create!
|
38
|
-
credential = self.new
|
39
|
-
user.credentials << credential
|
40
|
-
credential.facebook_uid = uid
|
41
|
-
credential.access_token = access_token
|
42
|
-
credential.save!
|
43
|
-
end
|
44
|
-
end
|
45
|
-
credential
|
46
|
-
end
|
47
|
-
|
48
|
-
# Extracts the Facebook user ID from a OAuth2 token.
|
49
|
-
#
|
50
|
-
# This used to be a hack that pulled the UID out of an OAuth2 token. The new
|
51
|
-
# encrypted OAuth2 tokens don't have UIDs anymore, so this method is an
|
52
|
-
# interim hack for old code that still depends on it.
|
53
|
-
def self.uid_from_token(access_token)
|
54
|
-
FBGraphRails.fbclient(access_token).selection.me.info![:id].to_s
|
55
|
-
end
|
56
|
-
|
57
|
-
if ActiveRecord::Base.respond_to? :mass_assignment_sanitizer=
|
58
|
-
# Forms should not be able to touch any attribute.
|
59
|
-
attr_accessible
|
60
|
-
end
|
61
|
-
end # class Credentials::Facebook
|
62
|
-
|
63
|
-
end # namespace Credentials
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'action_controller'
|
2
|
-
|
3
|
-
# :nodoc: add authenticates_using_facebook
|
4
|
-
class ActionController::Base
|
5
|
-
# Authenticates users via Facebook OAuth2, using fbgraph_rails.
|
6
|
-
#
|
7
|
-
# The User model class must implement for_facebook_token. The controller
|
8
|
-
# should obtain the Facebook token, using probes_facebook_access_token or
|
9
|
-
# requires_facebook_access_token.
|
10
|
-
def self.authenticates_using_facebook(options = {})
|
11
|
-
include Authpwn::FacebookControllerInstanceMethods
|
12
|
-
before_filter :authenticate_using_facebook_access_token, options
|
13
|
-
end
|
14
|
-
end # module Authpwn::FacebookExtensions::ControllerClassMethods
|
15
|
-
|
16
|
-
# :nodoc: namespace
|
17
|
-
module Authpwn
|
18
|
-
|
19
|
-
# Included in controllers that call authenticates_using_facebook.
|
20
|
-
module FacebookControllerInstanceMethods
|
21
|
-
def authenticate_using_facebook_access_token
|
22
|
-
return true if current_user
|
23
|
-
if access_token = current_facebook_access_token
|
24
|
-
self.current_user = User.for_facebook_token access_token
|
25
|
-
# NOTE: nixing the token from the session so the user won't be logged on
|
26
|
-
# immediately after logging off
|
27
|
-
self.current_facebook_access_token = nil
|
28
|
-
end
|
29
|
-
end
|
30
|
-
private :authenticate_using_facebook_access_token
|
31
|
-
end # module Authpwn::FacebookControllerInstanceMethods
|
32
|
-
|
33
|
-
end # namespace Authpwn
|
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'active_model'
|
2
|
-
require 'active_support'
|
3
|
-
|
4
|
-
# :nodoc: namespace
|
5
|
-
module Authpwn
|
6
|
-
|
7
|
-
# :nodoc: namespace
|
8
|
-
module UserExtensions
|
9
|
-
|
10
|
-
# Augments the User model with Facebook-related virtual attributes.
|
11
|
-
module FacebookFields
|
12
|
-
extend ActiveSupport::Concern
|
13
|
-
|
14
|
-
module ClassMethods
|
15
|
-
# The user that owns a given Facebook OAuth2 token.
|
16
|
-
#
|
17
|
-
# A new user will be created if the token doesn't belong to any user. This
|
18
|
-
# is the case for a new visitor.
|
19
|
-
def for_facebook_token(access_token)
|
20
|
-
Credentials::Facebook.for(access_token).user
|
21
|
-
end
|
22
|
-
|
23
|
-
# The user who has a certain e-mail, or nil if the e-mail is unclaimed.
|
24
|
-
def with_facebook_uid(facebook_uid)
|
25
|
-
credential = Credentials::Facebook.where(name: facebook_uid).
|
26
|
-
includes(:user).first
|
27
|
-
credential && credential.user
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
# Credentials::Facebook instance associated with this user.
|
32
|
-
def facebook_credential
|
33
|
-
credentials.find { |c| c.instance_of?(Credentials::Facebook) }
|
34
|
-
end
|
35
|
-
|
36
|
-
# FBGraph client loaded with this access token.
|
37
|
-
#
|
38
|
-
# Returns nil if this user has no Facebook credential.
|
39
|
-
def facebook_client
|
40
|
-
credential = self.facebook_credential
|
41
|
-
credential && credential.facebook_client
|
42
|
-
end
|
43
|
-
|
44
|
-
# The facebook user ID from the user's Facebook credential.
|
45
|
-
#
|
46
|
-
# Returns nil if this user has no Facebook credential.
|
47
|
-
def facebook_uid
|
48
|
-
credential = self.facebook_credential
|
49
|
-
credential && credential.facebook_uid
|
50
|
-
end
|
51
|
-
|
52
|
-
# The facebook OAuth2 access token from the user's Facebook credential.
|
53
|
-
#
|
54
|
-
# Returns nil if this user has no Facebook credential.
|
55
|
-
def facebook_access_token
|
56
|
-
credential = self.facebook_credential
|
57
|
-
credential && credential.access_token
|
58
|
-
end
|
59
|
-
end # module Authpwn::UserExtensions::FacebookFields
|
60
|
-
|
61
|
-
end # module Authpwn::UserExtensions
|
62
|
-
|
63
|
-
end # module Authpwn
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
|
3
|
-
class FacebookCredentialTest < ActiveSupport::TestCase
|
4
|
-
def setup
|
5
|
-
@code = 'AAAEj8jKX2a8BAA4kNheRhOs6SlECVcZCE9o5pPKMytOjjoiNAoZBGZAwuL4KrrxXWesfJRhzDZCJiqrcQG3UdjRRNtyMJQMZD'
|
6
|
-
@credential = Credentials::Facebook.new
|
7
|
-
@credential.facebook_uid = '1181310542'
|
8
|
-
@credential.key = 'AAAEj8jKX2a8BAOBMZCjxBe4dw7cRoD1JVxUgZAtB6ozJlR4Viazh6OAYcHB5kZAtUwgjpDy7a54ZA1DObLmBT9X99CLWYOj5Stqx8bHwnE7EzyBS1WxY'
|
9
|
-
@credential.user = users(:bill)
|
10
|
-
end
|
11
|
-
|
12
|
-
test 'setup' do
|
13
|
-
assert @credential.valid?
|
14
|
-
end
|
15
|
-
|
16
|
-
test 'key required' do
|
17
|
-
@credential.key = nil
|
18
|
-
assert !@credential.valid?
|
19
|
-
end
|
20
|
-
|
21
|
-
test 'user presence' do
|
22
|
-
@credential.user = nil
|
23
|
-
assert !@credential.valid?
|
24
|
-
end
|
25
|
-
|
26
|
-
test 'user uniqueness' do
|
27
|
-
@credential.user = users(:john)
|
28
|
-
assert !@credential.valid?
|
29
|
-
end
|
30
|
-
|
31
|
-
test 'facebook_uid uniqueness' do
|
32
|
-
@credential.facebook_uid = credentials(:jane_facebook).facebook_uid
|
33
|
-
assert !@credential.valid?
|
34
|
-
end
|
35
|
-
|
36
|
-
test "uid_from_token" do
|
37
|
-
assert_equal '1011950666', Credentials::Facebook.uid_from_token(@code)
|
38
|
-
end
|
39
|
-
|
40
|
-
test "for with existing access token" do
|
41
|
-
Credentials::Facebook.expects(:uid_from_token).with(@code).at_least_once.
|
42
|
-
returns(credentials(:jane_facebook).facebook_uid)
|
43
|
-
|
44
|
-
assert_equal credentials(:jane_facebook), Credentials::Facebook.for(@code),
|
45
|
-
'Wrong token'
|
46
|
-
assert_equal @code, credentials(:jane_facebook).reload.key,
|
47
|
-
'Token not refreshed'
|
48
|
-
end
|
49
|
-
|
50
|
-
test "for with new access token" do
|
51
|
-
credential = nil
|
52
|
-
Credentials::Facebook.expects(:uid_from_token).at_least_once.
|
53
|
-
with(@credential.key).returns('123456789')
|
54
|
-
assert_difference 'Credentials::Facebook.count', 1 do
|
55
|
-
credential = Credentials::Facebook.for @credential.key
|
56
|
-
end
|
57
|
-
assert_equal '123456789', credential.facebook_uid
|
58
|
-
assert_equal @credential.key, credential.key
|
59
|
-
assert !credential.new_record?, 'New credential not saved'
|
60
|
-
assert !credential.user.new_record?, "New credential's user not saved"
|
61
|
-
assert_operator credential.user.credentials, :include?, credential,
|
62
|
-
"New user's credentials does not include Facebook credential"
|
63
|
-
end
|
64
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
require File.expand_path('../test_helper', __FILE__)
|
2
|
-
|
3
|
-
# Mock controller used for testing session handling.
|
4
|
-
class FacebookController < ApplicationController
|
5
|
-
authenticates_using_session
|
6
|
-
probes_facebook_access_token
|
7
|
-
authenticates_using_facebook
|
8
|
-
|
9
|
-
def show
|
10
|
-
if current_user
|
11
|
-
render text: "User: #{current_user.id}"
|
12
|
-
else
|
13
|
-
render text: "No user"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class UserWithFb2 < User
|
19
|
-
include Authpwn::UserExtensions::FacebookFields
|
20
|
-
end
|
21
|
-
|
22
|
-
class FacebookControllerTest < ActionController::TestCase
|
23
|
-
setup do
|
24
|
-
@old_user_class = ::User
|
25
|
-
Object.send :remove_const, :User
|
26
|
-
::User = UserWithFb2
|
27
|
-
|
28
|
-
@user = users(:john)
|
29
|
-
@new_token = 'facebook:new_token|boom'
|
30
|
-
end
|
31
|
-
|
32
|
-
teardown do
|
33
|
-
Object.send :remove_const, :User
|
34
|
-
::User = @old_user_class
|
35
|
-
end
|
36
|
-
|
37
|
-
test "no facebook token" do
|
38
|
-
get :show
|
39
|
-
assert_response :success
|
40
|
-
assert_nil assigns(:current_user)
|
41
|
-
end
|
42
|
-
|
43
|
-
test "facebook token for existing user" do
|
44
|
-
Credentials::Facebook.expects(:uid_from_token).at_least_once.
|
45
|
-
with(credentials(:john_facebook).key).
|
46
|
-
returns(credentials(:john_facebook).facebook_uid)
|
47
|
-
set_session_current_facebook_token credentials(:john_facebook).key
|
48
|
-
get :show, {}
|
49
|
-
assert_response :success
|
50
|
-
assert_equal @user, assigns(:current_user)
|
51
|
-
end
|
52
|
-
|
53
|
-
test "new facebook token" do
|
54
|
-
set_session_current_facebook_token @new_token
|
55
|
-
Credentials::Facebook.expects(:uid_from_token).at_least_once.
|
56
|
-
with(@new_token).returns('12345678')
|
57
|
-
get :show, {}
|
58
|
-
assert_response :success
|
59
|
-
assert_not_equal @user, assigns(:current_user)
|
60
|
-
end
|
61
|
-
|
62
|
-
test "auth_controller? is false" do
|
63
|
-
assert_equal false, @controller.auth_controller?
|
64
|
-
end
|
65
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
|
3
|
-
class UserWithFb < User
|
4
|
-
include Authpwn::UserExtensions::FacebookFields
|
5
|
-
end
|
6
|
-
|
7
|
-
class FacebookFieldsTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
@user = UserWithFb.new
|
10
|
-
|
11
|
-
@john = UserWithFb.find_by_id(users(:john).id)
|
12
|
-
@jane = UserWithFb.find_by_id(users(:jane).id)
|
13
|
-
@bill = UserWithFb.find_by_id(users(:bill).id)
|
14
|
-
end
|
15
|
-
|
16
|
-
test 'setup' do
|
17
|
-
assert @user.valid?
|
18
|
-
end
|
19
|
-
|
20
|
-
test 'facebook_credential' do
|
21
|
-
assert_equal credentials(:john_facebook), @john.facebook_credential
|
22
|
-
assert_equal credentials(:jane_facebook), @jane.facebook_credential
|
23
|
-
assert_nil @bill.facebook_credential
|
24
|
-
end
|
25
|
-
|
26
|
-
test 'facebook_uid' do
|
27
|
-
assert_equal credentials(:john_facebook).facebook_uid, @john.facebook_uid
|
28
|
-
assert_equal credentials(:jane_facebook).facebook_uid, @jane.facebook_uid
|
29
|
-
assert_nil @bill.facebook_uid
|
30
|
-
end
|
31
|
-
|
32
|
-
test 'facebook_access_token' do
|
33
|
-
assert_equal credentials(:john_facebook).access_token,
|
34
|
-
@john.facebook_access_token
|
35
|
-
assert_equal credentials(:jane_facebook).access_token,
|
36
|
-
@jane.facebook_access_token
|
37
|
-
assert_nil @bill.facebook_access_token
|
38
|
-
end
|
39
|
-
|
40
|
-
test 'facebook_client' do
|
41
|
-
assert_equal credentials(:john_facebook).access_token,
|
42
|
-
@john.facebook_client.access_token
|
43
|
-
assert_nil @bill.facebook_client
|
44
|
-
end
|
45
|
-
|
46
|
-
test 'with_facebook_uid' do
|
47
|
-
assert_equal users(:john), UserWithFb.with_facebook_uid(
|
48
|
-
credentials(:john_facebook).facebook_uid)
|
49
|
-
assert_equal users(:jane), UserWithFb.with_facebook_uid(
|
50
|
-
credentials(:jane_facebook).facebook_uid)
|
51
|
-
assert_nil UserWithFb.with_facebook_uid('0000000')
|
52
|
-
end
|
53
|
-
|
54
|
-
test 'for_facebook_token' do
|
55
|
-
Credentials::Facebook.expects(:uid_from_token).at_least_once.
|
56
|
-
with(credentials(:john_facebook).key).
|
57
|
-
returns credentials(:john_facebook).facebook_uid
|
58
|
-
assert_equal users(:john),
|
59
|
-
UserWithFb.for_facebook_token(credentials(:john_facebook).access_token)
|
60
|
-
end
|
61
|
-
end
|