trusty 0.1.1 → 0.2.0
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.
- checksums.yaml +4 -4
- data/lib/trusty/omniauth/model_mapper.rb +20 -3
- data/lib/trusty/omniauth/provider_mapper.rb +57 -45
- data/lib/trusty/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 766c8728e87db3008595dbae530b7774cfb4c31d
|
4
|
+
data.tar.gz: 403d9eceb6e0b4b858a21a1e7be1191f54a66b01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4ac7fa3d31cad32e091f70f32b4e7bf1ad9ca4887013aba809acbcd5d1cb1e1d0d8f085831b65a907ef2fa51e1b834b7a6a9ac718e784f0610fe915041bca20
|
7
|
+
data.tar.gz: 6a767f6575043251f26700a221bfcea788b75f52683385fda358749fda853ca8a71a926ea56f98456a44254d3d5fb8cf7da863b30d339f56c2c705d4c811d7ba
|
@@ -4,10 +4,15 @@ module Trusty
|
|
4
4
|
module Omniauth
|
5
5
|
class ModelMapper
|
6
6
|
include MappingHelpers
|
7
|
+
|
8
|
+
attr_reader :unique_identifiers, :required_criteria
|
7
9
|
|
8
10
|
def initialize(provider, options = {})
|
9
11
|
@provider = provider
|
10
12
|
@options = options
|
13
|
+
|
14
|
+
@unique_identifiers = @options.fetch(:unique_identifiers, []).map(&:to_s)
|
15
|
+
@required_criteria = stringify_keys @options.fetch(:required_criteria, {})
|
11
16
|
end
|
12
17
|
|
13
18
|
def model
|
@@ -15,11 +20,12 @@ module Trusty
|
|
15
20
|
end
|
16
21
|
|
17
22
|
def attribute_names
|
18
|
-
|
23
|
+
# Remove required_criteria so that existing attributes are skipped
|
24
|
+
@attribute_names ||= (@options[:attribute_names] || column_names).map(&:to_s) - required_criteria.keys
|
19
25
|
end
|
20
26
|
|
21
27
|
def attributes(*filter_attribute_names)
|
22
|
-
@attributes ||= @provider.attributes(*attribute_names).merge
|
28
|
+
@attributes ||= @provider.attributes(*attribute_names).merge(stringify_keys @options[:attributes]).merge(required_criteria)
|
23
29
|
|
24
30
|
if filter_attribute_names.any?
|
25
31
|
@attributes.slice(*filter_attribute_names)
|
@@ -29,9 +35,15 @@ module Trusty
|
|
29
35
|
end
|
30
36
|
|
31
37
|
def build_record(additional_attributes = {})
|
32
|
-
model.new(attributes.merge(additional_attributes), without_protection: true)
|
38
|
+
model.new(attributes.merge(required_criteria).merge(additional_attributes), without_protection: true)
|
33
39
|
end
|
34
40
|
|
41
|
+
def find_records(additional_criteria = {})
|
42
|
+
conditions = model.where( attributes(*unique_identifiers) )
|
43
|
+
conditions = conditions.where(additional_criteria) unless additional_criteria.empty?
|
44
|
+
conditions.where(required_criteria)
|
45
|
+
end
|
46
|
+
|
35
47
|
def update_record!(record)
|
36
48
|
if Rails::VERSION::MAJOR >= 4
|
37
49
|
record.update_attributes!(attributes)
|
@@ -49,6 +61,11 @@ module Trusty
|
|
49
61
|
[]
|
50
62
|
end
|
51
63
|
end
|
64
|
+
|
65
|
+
protected
|
66
|
+
def stringify_keys(original_hash)
|
67
|
+
original_hash.each_with_object({}){|(key, value), hash| hash[key.to_s] = value}
|
68
|
+
end
|
52
69
|
end
|
53
70
|
end
|
54
71
|
end
|
@@ -17,6 +17,8 @@ module Trusty
|
|
17
17
|
# - :identity_model = Identity model
|
18
18
|
# - :identity_attributes = Hash of attributes to merge into identity_attributes
|
19
19
|
# - :identity_attribute_names = Array of attribute names to copy from Omniauth data (default: Identity.column_names)
|
20
|
+
# - :identity_required_criteria = Hash of criteria to use to find identities, and also to merge into attributes
|
21
|
+
# - :unique_identifiers = Array of column names that identify a model uniquely with omniauth data
|
20
22
|
# - :skip_raw_info (default: false) = Boolean whether to exclude OmniAuth "extra" data in identity_attributes[:raw_info]
|
21
23
|
# - :skip_nils (default: true) = Boolean whether to remove attributes with nil values
|
22
24
|
def initialize(provider_attributes, options = {})
|
@@ -30,36 +32,66 @@ module Trusty
|
|
30
32
|
}.merge(options)
|
31
33
|
|
32
34
|
@provider_identity = ModelMapper.new(self,
|
33
|
-
:model
|
34
|
-
:attributes
|
35
|
-
:attribute_names
|
35
|
+
:model => @options[:identity_model] || Identity,
|
36
|
+
:attributes => @options[:identity_attributes],
|
37
|
+
:attribute_names => @options[:identity_attribute_names],
|
38
|
+
:unique_identifiers => @options[:unique_identifiers] || [:provider, :uid],
|
39
|
+
:required_criteria => @options[:identity_required_criteria]
|
36
40
|
)
|
37
41
|
@provider_user = ModelMapper.new(self,
|
38
|
-
:model
|
39
|
-
:attributes
|
40
|
-
:attribute_names
|
42
|
+
:model => @options[:user_model] || User,
|
43
|
+
:attributes => @options[:user_attributes],
|
44
|
+
:attribute_names => @options[:user_attribute_names],
|
45
|
+
:unique_identifiers => @options[:unique_identifiers] || [:email]
|
41
46
|
)
|
42
47
|
end
|
43
48
|
|
44
49
|
# Query existing
|
50
|
+
|
51
|
+
def find_identities_for_user(user)
|
52
|
+
@provider_identity.find_records(user_id: user.id)
|
53
|
+
end
|
45
54
|
|
46
|
-
|
47
|
-
|
48
|
-
@
|
49
|
-
|
50
|
-
|
55
|
+
# Matched identities based on omniauth unique identifiers (provider, uid)
|
56
|
+
def matched_identities
|
57
|
+
@matched_identities ||= @provider_identity.find_records
|
58
|
+
end
|
59
|
+
|
60
|
+
def identities_exist?
|
61
|
+
matched_identities.any?
|
62
|
+
end
|
63
|
+
|
64
|
+
def single_identity?
|
65
|
+
matched_identities.size == 1
|
66
|
+
end
|
67
|
+
|
68
|
+
def multiple_identities?
|
69
|
+
matched_identities.size > 1
|
51
70
|
end
|
52
71
|
|
53
|
-
|
54
|
-
|
72
|
+
# Matched users based on omniauth unique identifiers (email)
|
73
|
+
def matched_users
|
74
|
+
@matched_users ||= @provider_user.find_records
|
75
|
+
end
|
76
|
+
|
77
|
+
def users_exist?
|
78
|
+
matched_users.any?
|
79
|
+
end
|
80
|
+
|
81
|
+
def single_user?
|
82
|
+
matched_users.size == 1
|
83
|
+
end
|
84
|
+
|
85
|
+
def multiple_users?
|
86
|
+
matched_users.size > 1
|
55
87
|
end
|
56
88
|
|
57
|
-
def
|
58
|
-
|
89
|
+
def single_user
|
90
|
+
@single_user ||= matched_users.first if single_user?
|
59
91
|
end
|
60
92
|
|
61
|
-
def
|
62
|
-
|
93
|
+
def single_identity
|
94
|
+
@single_identity ||= matched_identities.first if single_identity?
|
63
95
|
end
|
64
96
|
|
65
97
|
# USER
|
@@ -75,16 +107,15 @@ module Trusty
|
|
75
107
|
end
|
76
108
|
|
77
109
|
def build_identity_for_user(user)
|
78
|
-
build_identity.tap do |identity|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
110
|
+
# build_identity.tap do |identity|
|
111
|
+
# # this assumes there is an inverse relationship automatically created
|
112
|
+
# # such as user.identities would now contain this identity for the user
|
113
|
+
# identity.user = user
|
114
|
+
# end
|
115
|
+
build_identity(user: user)
|
83
116
|
end
|
84
117
|
|
85
|
-
def
|
86
|
-
raise "Identity doesn't exist!" unless identity
|
87
|
-
|
118
|
+
def update_identity!(identity)
|
88
119
|
@provider_identity.update_record!(identity)
|
89
120
|
end
|
90
121
|
|
@@ -113,6 +144,7 @@ module Trusty
|
|
113
144
|
:phone => clean(info['phone']),
|
114
145
|
:image_url => info['image'],
|
115
146
|
:profile_url => info.fetch('urls', {})['public_profile'],
|
147
|
+
:token_type => clean(credentials['token_type']),
|
116
148
|
:token => clean(credentials['token']),
|
117
149
|
:secret => clean(credentials['secret']),
|
118
150
|
:refresh_token => clean(credentials['refresh_token']),
|
@@ -136,26 +168,6 @@ module Trusty
|
|
136
168
|
end
|
137
169
|
end
|
138
170
|
|
139
|
-
private
|
140
|
-
|
141
|
-
def find_user_by_identity
|
142
|
-
if defined?(Mongoid::Document) && @provider_user.model.include?(Mongoid::Document)
|
143
|
-
@provider_user.model.elem_match(:identities => @provider_identity.attributes(:provider, :uid)).first
|
144
|
-
else
|
145
|
-
@provider_user.model.where(
|
146
|
-
:id => @provider_identity.model.where( @provider_identity.attributes(:provider, :uid) ).select(:user_id)
|
147
|
-
).first
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
def find_user_by_email
|
152
|
-
@provider_user.model.where( @provider_identity.attributes(:email) ).first
|
153
|
-
end
|
154
|
-
|
155
|
-
def find_identity_by_user
|
156
|
-
user && user.identities.where( @provider_identity.attributes(:provider, :uid) ).first
|
157
|
-
end
|
158
|
-
|
159
171
|
end
|
160
172
|
end
|
161
173
|
end
|
data/lib/trusty/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trusty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Van Horn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|