authpwn_rails 0.10.2 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.10.2
1
+ 0.10.3
@@ -36,9 +36,9 @@ class Facebook < ::Credential
36
36
  User.transaction do
37
37
  user = User.create!
38
38
  credential = self.new
39
+ user.credentials << credential
39
40
  credential.facebook_uid = uid
40
41
  credential.access_token = access_token
41
- credential.user = user
42
42
  credential.save!
43
43
  end
44
44
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "authpwn_rails"
8
- s.version = "0.10.2"
8
+ s.version = "0.10.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Victor Costan"]
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
53
53
  "lib/authpwn_rails/session_controller.rb",
54
54
  "lib/authpwn_rails/test_extensions.rb",
55
55
  "lib/authpwn_rails/user_extensions/email_field.rb",
56
+ "lib/authpwn_rails/user_extensions/facebook_fields.rb",
56
57
  "lib/authpwn_rails/user_extensions/password_field.rb",
57
58
  "lib/authpwn_rails/user_model.rb",
58
59
  "test/cookie_controller_test.rb",
@@ -60,6 +61,7 @@ Gem::Specification.new do |s|
60
61
  "test/email_field_test.rb",
61
62
  "test/facebook_controller_test.rb",
62
63
  "test/facebook_credential_test.rb",
64
+ "test/facebook_fields_test.rb",
63
65
  "test/helpers/application_controller.rb",
64
66
  "test/helpers/autoload_path.rb",
65
67
  "test/helpers/db_setup.rb",
@@ -20,12 +20,29 @@ Kernel.system "rake db:migrate:up VERSION=20100725000003 RAILS_ENV=#{Rails.env}"
20
20
  # Build up credentials.
21
21
  reload!
22
22
  User.all.each do |user|
23
- Credentials::Email.create! :user => user, :email => user.attributes['email']
23
+ unless /\@graph\.facebook\.com$/ =~ user.email
24
+ eml = Credentials::Email.new :email => user.attributes['email']
25
+ eml.user = user
26
+ eml.save!
27
+ end
24
28
 
25
- pwd = Credentials::Password.new :user => user
26
- pwd.password = pwd.password_confirmation = '_'
27
- pwd.key = user.password_salt + '|' + user.password_hash
28
- pwd.save!
29
+ if user.password_hash
30
+ pwd = Credentials::Password.new
31
+ pwd.user = user
32
+ pwd.password = pwd.password_confirmation = '_'
33
+ pwd.key = user.password_salt + '|' + user.password_hash
34
+ pwd.save!
35
+ end
36
+ end
37
+ class FacebookToken < ActiveRecord::Base
38
+ belongs_to :user
39
+ end
40
+ FacebookToken.all.each do |token|
41
+ cred = Credentials::Facebook.new
42
+ cred.user = token.user
43
+ cred.facebook_uid = token.external_uid
44
+ cred.key = token.access_token
45
+ cred.save!
29
46
  end
30
47
 
31
48
  # Update the columns in the User model.
@@ -57,5 +74,9 @@ class FinishUpgradingUserModel < ActiveRecord::Migration
57
74
  end
58
75
  end
59
76
  FinishUpgradingUserModel.migrate :up
77
+ class DropFacebookTokens < ActiveRecord::Migration
78
+ def change
79
+ drop_table :facebook_tokens
80
+ end
81
+ end
60
82
  reload!
61
-
data/lib/authpwn_rails.rb CHANGED
@@ -11,6 +11,7 @@ module Authpwn
11
11
  # Contains extensions to the User model.
12
12
  module UserExtensions
13
13
  autoload :EmailField, 'authpwn_rails/user_extensions/email_field.rb'
14
+ autoload :FacebookFields, 'authpwn_rails/user_extensions/facebook_fields.rb'
14
15
  autoload :PasswordField, 'authpwn_rails/user_extensions/password_field.rb'
15
16
  end
16
17
  end
@@ -6,6 +6,9 @@ class User < ActiveRecord::Base
6
6
  # include Authpwn::UserExtensions::EmailField
7
7
  # Virtual password attribute, with confirmation validation.
8
8
  # include Authpwn::UserExtensions::PasswordField
9
+ # Convenience Facebook accessors.
10
+ # include Authpwn::UserExtensions::FacebookFields
11
+
9
12
 
10
13
  # Add your extensions to the User class here.
11
14
  end
@@ -0,0 +1,65 @@
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
+ module InstanceMethods
32
+ # Credentials::Facebook instance associated with this user.
33
+ def facebook_credential
34
+ credentials.find { |c| c.instance_of?(Credentials::Facebook) }
35
+ end
36
+
37
+ # FBGraph client loaded with this access token.
38
+ #
39
+ # Returns nil if this user has no Facebook credential.
40
+ def facebook_client
41
+ credential = self.facebook_credential
42
+ credential && credential.facebook_client
43
+ end
44
+
45
+ # The facebook user ID from the user's Facebook credential.
46
+ #
47
+ # Returns nil if this user has no Facebook credential.
48
+ def facebook_uid
49
+ credential = self.facebook_credential
50
+ credential && credential.facebook_uid
51
+ end
52
+
53
+ # The facebook OAuth2 access token from the user's Facebook credential.
54
+ #
55
+ # Returns nil if this user has no Facebook credential.
56
+ def facebook_access_token
57
+ credential = self.facebook_credential
58
+ credential && credential.access_token
59
+ end
60
+ end
61
+ end # module Authpwn::UserExtensions::FacebookFields
62
+
63
+ end # module Authpwn::UserExtensions
64
+
65
+ end # module Authpwn
@@ -52,26 +52,6 @@ module UserModel
52
52
  self.exuid ||= (Time.now.to_f * 1_000_000).to_i
53
53
  end
54
54
  end # module Authpwn::UserModel::InstanceMethods
55
-
56
55
  end # namespace Authpwn::UserModel
57
56
 
58
57
  end # namespace Authpwn
59
-
60
-
61
- # :nodoc: adds Facebook integration methods to the User model.
62
- module Authpwn::UserModel::ClassMethods
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
@@ -14,7 +14,6 @@ class EmailFieldTest < ActiveSupport::TestCase
14
14
  end
15
15
 
16
16
  test 'setup' do
17
- @user.save!
18
17
  assert @user.valid?
19
18
  end
20
19
 
@@ -15,11 +15,22 @@ class FacebookController < ApplicationController
15
15
  end
16
16
  end
17
17
 
18
+ class UserWithFb2 < User
19
+ include Authpwn::UserExtensions::FacebookFields
20
+ end
21
+
18
22
  class FacebookControllerTest < ActionController::TestCase
19
23
  setup do
24
+ @old_user_class = ::User
25
+ ::User = UserWithFb2
26
+
20
27
  @user = users(:john)
21
28
  @new_token = 'facebook:new_token|boom'
22
29
  end
30
+
31
+ teardown do
32
+ ::User = @old_user_class
33
+ end
23
34
 
24
35
  test "no facebook token" do
25
36
  get :show
@@ -57,19 +57,8 @@ class FacebookCredentialTest < ActiveSupport::TestCase
57
57
  assert_equal '123456789', credential.facebook_uid
58
58
  assert_equal @credential.key, credential.key
59
59
  assert !credential.new_record?, 'New credential not saved'
60
- assert !credential.user.new_record?, "New credential's user 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"
61
63
  end
62
-
63
- test 'User#facebook_credential' do
64
- user = users(:john)
65
- assert_equal credentials(:john_facebook), user.facebook_credential
66
- end
67
-
68
- test 'User#for_facebook_token' do
69
- flexmock(Credentials::Facebook).should_receive(:uid_from_token).
70
- with(credentials(:john_facebook).key).
71
- and_return(credentials(:john_facebook).facebook_uid)
72
- assert_equal users(:john),
73
- User.for_facebook_token(credentials(:john_facebook).key)
74
- end
75
64
  end
@@ -0,0 +1,61 @@
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
+ flexmock(Credentials::Facebook).should_receive(:uid_from_token).
56
+ with(credentials(:john_facebook).key).
57
+ and_return(credentials(:john_facebook).facebook_uid)
58
+ assert_equal users(:john),
59
+ UserWithFb.for_facebook_token(credentials(:john_facebook).access_token)
60
+ end
61
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authpwn_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.10.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fbgraph_rails
16
- requirement: &17655420 !ruby/object:Gem::Requirement
16
+ requirement: &24789920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.2.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *17655420
24
+ version_requirements: *24789920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &17653940 !ruby/object:Gem::Requirement
27
+ requirement: &24770680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.1.3
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *17653940
35
+ version_requirements: *24770680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &17652840 !ruby/object:Gem::Requirement
38
+ requirement: &24769460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *17652840
46
+ version_requirements: *24769460
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: flexmock
49
- requirement: &17599820 !ruby/object:Gem::Requirement
49
+ requirement: &24767220 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.9.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *17599820
57
+ version_requirements: *24767220
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: jeweler
60
- requirement: &17598940 !ruby/object:Gem::Requirement
60
+ requirement: &24766540 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.6.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *17598940
68
+ version_requirements: *24766540
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rcov
71
- requirement: &17598120 !ruby/object:Gem::Requirement
71
+ requirement: &24765740 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *17598120
79
+ version_requirements: *24765740
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: sqlite3
82
- requirement: &17597180 !ruby/object:Gem::Requirement
82
+ requirement: &24765080 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: 1.3.3
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *17597180
90
+ version_requirements: *24765080
91
91
  description: Works with Facebook.
92
92
  email: victor@costan.us
93
93
  executables: []
@@ -132,6 +132,7 @@ files:
132
132
  - lib/authpwn_rails/session_controller.rb
133
133
  - lib/authpwn_rails/test_extensions.rb
134
134
  - lib/authpwn_rails/user_extensions/email_field.rb
135
+ - lib/authpwn_rails/user_extensions/facebook_fields.rb
135
136
  - lib/authpwn_rails/user_extensions/password_field.rb
136
137
  - lib/authpwn_rails/user_model.rb
137
138
  - test/cookie_controller_test.rb
@@ -139,6 +140,7 @@ files:
139
140
  - test/email_field_test.rb
140
141
  - test/facebook_controller_test.rb
141
142
  - test/facebook_credential_test.rb
143
+ - test/facebook_fields_test.rb
142
144
  - test/helpers/application_controller.rb
143
145
  - test/helpers/autoload_path.rb
144
146
  - test/helpers/db_setup.rb
@@ -165,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
167
  version: '0'
166
168
  segments:
167
169
  - 0
168
- hash: -3179900965882251985
170
+ hash: -300984823103934206
169
171
  required_rubygems_version: !ruby/object:Gem::Requirement
170
172
  none: false
171
173
  requirements: