jm81auth 0.1.1 → 0.1.6
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 +5 -5
- data/lib/jm81auth.rb +1 -0
- data/lib/jm81auth/models/auth_method.rb +21 -5
- data/lib/jm81auth/models/auth_token.rb +25 -6
- data/lib/jm81auth/models/user.rb +27 -8
- data/lib/jm81auth/oauth/base.rb +2 -0
- data/lib/jm81auth/oauth/google.rb +17 -0
- data/lib/jm81auth/version.rb +1 -1
- data/spec/jm81auth/models/user_spec.rb +3 -0
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c5fe75d8315211ad54368bfd0b3ce2147245ca9a8b4f400e2a4e0a500f0fa2f6
|
4
|
+
data.tar.gz: ebc173d360db322cab2f87d8574cd3f6b47831691a46f5ec1814772b9a4afade
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7b59ffafb5e770ce82d0601cd64789b8d89dff737d09ce71c866efaad86bea28e01220a978883688d056bb856ec49caf1b3ab0355569abd9d592a8b74f5172d
|
7
|
+
data.tar.gz: 727abd69e14c142df3a1fb0b87d85ee4a4814ed8a258dd21cff995a901a7c98e5feaf5df53c3b02f8d2e68d1109b1f680f03bd38b802bc46a6217dec1171f939
|
data/lib/jm81auth.rb
CHANGED
@@ -5,16 +5,32 @@ module Jm81auth
|
|
5
5
|
base.extend ClassMethods
|
6
6
|
|
7
7
|
base.class_eval do
|
8
|
-
|
9
|
-
|
8
|
+
if respond_to? :belongs_to
|
9
|
+
belongs_to :user
|
10
|
+
has_many :auth_tokens
|
11
|
+
else
|
12
|
+
many_to_one :user
|
13
|
+
one_to_many :auth_tokens
|
14
|
+
end
|
10
15
|
end
|
11
16
|
end
|
12
17
|
|
13
|
-
# Create AuthToken, setting user and last_used_at
|
18
|
+
# Create AuthToken, setting user and last_used_at, and optionally
|
19
|
+
# access_token from the Oauth provider.
|
14
20
|
#
|
15
21
|
# @return [AuthToken]
|
16
|
-
def create_token
|
17
|
-
|
22
|
+
def create_token access_token: nil
|
23
|
+
auth_token = ::AuthToken.new
|
24
|
+
auth_token.auth_method = self
|
25
|
+
auth_token.user = user
|
26
|
+
auth_token.last_used_at = Time.now.utc
|
27
|
+
|
28
|
+
if auth_token.respond_to? :access_token=
|
29
|
+
auth_token.access_token = access_token
|
30
|
+
end
|
31
|
+
|
32
|
+
auth_token.save
|
33
|
+
auth_token
|
18
34
|
end
|
19
35
|
|
20
36
|
module ClassMethods
|
@@ -5,10 +5,15 @@ module Jm81auth
|
|
5
5
|
base.extend ClassMethods
|
6
6
|
|
7
7
|
base.class_eval do
|
8
|
-
|
8
|
+
if respond_to? :belongs_to
|
9
|
+
belongs_to :auth_method, required: false
|
10
|
+
belongs_to :user, required: false
|
11
|
+
else
|
12
|
+
plugin :timestamps
|
9
13
|
|
10
|
-
|
11
|
-
|
14
|
+
many_to_one :auth_method
|
15
|
+
many_to_one :user
|
16
|
+
end
|
12
17
|
end
|
13
18
|
end
|
14
19
|
|
@@ -17,7 +22,13 @@ module Jm81auth
|
|
17
22
|
|
18
23
|
# Set #closed_at. Called, for example, when logging out.
|
19
24
|
def close!
|
20
|
-
|
25
|
+
unless self.closed_at
|
26
|
+
if respond_to? :update_attributes!
|
27
|
+
self.update_attributes! closed_at: Time.now
|
28
|
+
else
|
29
|
+
self.update closed_at: Time.now
|
30
|
+
end
|
31
|
+
end
|
21
32
|
end
|
22
33
|
|
23
34
|
# @return [String]
|
@@ -54,7 +65,11 @@ module Jm81auth
|
|
54
65
|
token, config.jwt_secret, config.jwt_algorithm
|
55
66
|
).first
|
56
67
|
|
57
|
-
|
68
|
+
if self.respond_to? :[]
|
69
|
+
auth_token = self[payload['auth_token_id']]
|
70
|
+
else
|
71
|
+
auth_token = find payload['auth_token_id']
|
72
|
+
end
|
58
73
|
|
59
74
|
if payload['auth_token_id'].nil?
|
60
75
|
raise DecodeError, "auth_token_id missing: #{payload}"
|
@@ -84,7 +99,11 @@ module Jm81auth
|
|
84
99
|
auth_token = decode token
|
85
100
|
|
86
101
|
if auth_token && !auth_token.expired?
|
87
|
-
|
102
|
+
if respond_to? :update_attributes!
|
103
|
+
auth_token.update_attributes! last_used_at: Time.now
|
104
|
+
else
|
105
|
+
auth_token.update last_used_at: Time.now
|
106
|
+
end
|
88
107
|
auth_token
|
89
108
|
else
|
90
109
|
nil
|
data/lib/jm81auth/models/user.rb
CHANGED
@@ -7,8 +7,13 @@ module Jm81auth
|
|
7
7
|
base.extend ClassMethods
|
8
8
|
|
9
9
|
base.class_eval do
|
10
|
-
|
11
|
-
|
10
|
+
if respond_to? :has_many
|
11
|
+
has_many :auth_methods
|
12
|
+
has_many :auth_tokens
|
13
|
+
else
|
14
|
+
one_to_many :auth_methods
|
15
|
+
one_to_many :auth_tokens
|
16
|
+
end
|
12
17
|
end
|
13
18
|
end
|
14
19
|
|
@@ -40,15 +45,29 @@ module Jm81auth
|
|
40
45
|
method = ::AuthMethod.by_provider_data oauth.provider_data
|
41
46
|
|
42
47
|
if !method
|
43
|
-
user = find_by_email(oauth.email) ||
|
44
|
-
email: oauth.email.downcase,
|
45
|
-
display_name: oauth.display_name
|
46
|
-
)
|
48
|
+
user = find_by_email(oauth.email) || create_from_oauth(oauth)
|
47
49
|
|
48
|
-
|
50
|
+
if user.respond_to? :add_auth_method
|
51
|
+
method = user.add_auth_method oauth.provider_data
|
52
|
+
else
|
53
|
+
method = user.auth_methods.create! oauth.provider_data
|
54
|
+
end
|
49
55
|
end
|
50
56
|
|
51
|
-
method.create_token
|
57
|
+
method.create_token access_token: oauth.access_token
|
58
|
+
end
|
59
|
+
|
60
|
+
# Create User based on oauth data.
|
61
|
+
#
|
62
|
+
# @param oauth [OAuth::Base]
|
63
|
+
# OAuth login object, include #provider_data (Hash with provider_name
|
64
|
+
# and provider_id), #email and #display_name.
|
65
|
+
# @return [User]
|
66
|
+
def create_from_oauth oauth
|
67
|
+
create(
|
68
|
+
email: oauth.email.downcase,
|
69
|
+
display_name: oauth.display_name
|
70
|
+
)
|
52
71
|
end
|
53
72
|
end
|
54
73
|
end
|
data/lib/jm81auth/oauth/base.rb
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
module Jm81auth
|
5
5
|
module OAuth
|
6
6
|
class Base
|
7
|
+
attr_reader :access_token
|
8
|
+
|
7
9
|
# Setup @params from params param (Har, har). Also, set @access_token,
|
8
10
|
# either from params Hash, or by calling #get_access_token. @params is the
|
9
11
|
# expected params needed by #get_access_token.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Jm81auth
|
2
|
+
module OAuth
|
3
|
+
class Google < Base
|
4
|
+
ACCESS_TOKEN_URL = 'https://oauth2.googleapis.com/token'
|
5
|
+
DATA_URL = 'https://www.googleapis.com/oauth2/v3/userinfo'
|
6
|
+
|
7
|
+
def get_access_token
|
8
|
+
response = client.post(
|
9
|
+
ACCESS_TOKEN_URL, @params.merge(grant_type: 'authorization_code')
|
10
|
+
)
|
11
|
+
|
12
|
+
JSON.parse(response.body)['access_token']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
data/lib/jm81auth/version.rb
CHANGED
@@ -105,6 +105,7 @@ RSpec.describe User, type: :model do
|
|
105
105
|
context 'existing AuthMethod' do
|
106
106
|
before(:each) do
|
107
107
|
expect(oauth).to_not receive(:email)
|
108
|
+
expect(oauth).to receive(:access_token)
|
108
109
|
|
109
110
|
expect(oauth).to receive(:provider_data) do
|
110
111
|
{ provider_name: 'github', provider_id: 5 }
|
@@ -133,6 +134,8 @@ RSpec.describe User, type: :model do
|
|
133
134
|
|
134
135
|
context 'no existing AuthMethod' do
|
135
136
|
before(:each) do
|
137
|
+
expect(oauth).to receive(:access_token)
|
138
|
+
|
136
139
|
expect(oauth).to receive(:provider_data).twice do
|
137
140
|
{ provider_name: 'github', provider_id: 10 }
|
138
141
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jm81auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jared Morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/jm81auth/oauth/base.rb
|
127
127
|
- lib/jm81auth/oauth/facebook.rb
|
128
128
|
- lib/jm81auth/oauth/github.rb
|
129
|
+
- lib/jm81auth/oauth/google.rb
|
129
130
|
- lib/jm81auth/version.rb
|
130
131
|
- spec/factories/auth_methods.rb
|
131
132
|
- spec/factories/auth_tokens.rb
|
@@ -154,17 +155,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
155
|
- !ruby/object:Gem::Version
|
155
156
|
version: '0'
|
156
157
|
requirements: []
|
157
|
-
|
158
|
-
rubygems_version: 2.2.2
|
158
|
+
rubygems_version: 3.0.3
|
159
159
|
signing_key:
|
160
160
|
specification_version: 4
|
161
161
|
summary: An authentication library for Rails API
|
162
162
|
test_files:
|
163
|
-
- spec/
|
164
|
-
- spec/
|
165
|
-
- spec/factories/auth_methods.rb
|
163
|
+
- spec/spec_helper.rb
|
164
|
+
- spec/jm81auth/oauth/base_spec.rb
|
166
165
|
- spec/jm81auth/models/auth_token_spec.rb
|
167
|
-
- spec/jm81auth/models/user_spec.rb
|
168
166
|
- spec/jm81auth/models/auth_method_spec.rb
|
169
|
-
- spec/jm81auth/
|
170
|
-
- spec/
|
167
|
+
- spec/jm81auth/models/user_spec.rb
|
168
|
+
- spec/factories/auth_methods.rb
|
169
|
+
- spec/factories/users.rb
|
170
|
+
- spec/factories/auth_tokens.rb
|