tok_access 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -15
- data/lib/generators/tok_access/model_generator.rb +1 -1
- data/lib/tok_access/tok_authenticable.rb +21 -21
- data/lib/tok_access/tok_config.rb +2 -2
- data/lib/tok_access/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fafa5abd9d9c931e83776fcfa62a40ee0ba2f038
|
4
|
+
data.tar.gz: 915d606a864bd9aa6448e8f61929903bc9c38dad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93314be37f8cd700b7d6d105ee2299d062c667d94cae280bfe17533eae1afd5046779120beaea9f6553354eb3c47751d4a729d26ab1c3089d95cff26ef665217
|
7
|
+
data.tar.gz: 945908665bf875065a1d8f403e3ccb9f42ac504a899c296a921be287e7f9a460740214fe6ba24e21a6834595bdf1f6cb708bdf30046cada3c107f177da07bf79
|
data/README.md
CHANGED
@@ -3,9 +3,9 @@ Handle authentication of your users using tokens.
|
|
3
3
|
Every 'tokified' user will have a association named
|
4
4
|
toks.
|
5
5
|
|
6
|
-
A tok is an object that consist of two attributes: token,
|
6
|
+
A tok is an object that consist of two attributes: token, device_token.
|
7
7
|
|
8
|
-
You should use the
|
8
|
+
You should use the device_token to identify the user devices
|
9
9
|
in which the user has logged in and the token to authenticate the
|
10
10
|
user request.
|
11
11
|
|
@@ -24,7 +24,7 @@ TokAccess use bcrypt has_secure_password and has_secure_token methods to handle
|
|
24
24
|
# if the user is saved, an associated tok is created
|
25
25
|
# and you can access that tok using the following methods
|
26
26
|
token = user.get_token
|
27
|
-
|
27
|
+
device_token = user.get_device_token
|
28
28
|
# return the tokens to the front-end.
|
29
29
|
else
|
30
30
|
# code if the user can't be saved
|
@@ -50,8 +50,8 @@ TokAccess use bcrypt has_secure_password and has_secure_token methods to handle
|
|
50
50
|
current_user ? current_user.get_token : nil
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
54
|
-
current_user ? current_user.
|
53
|
+
def current_device_token
|
54
|
+
current_user ? current_user.get_device_token : nil
|
55
55
|
end
|
56
56
|
|
57
57
|
# After 15 minutes, the next user request will refresh the tokens.
|
@@ -76,7 +76,7 @@ TokAccess use bcrypt has_secure_password and has_secure_token methods to handle
|
|
76
76
|
# if the user is authenticated successfully, a new tok is created
|
77
77
|
# and you can access that tok using the following methods
|
78
78
|
token = user.get_token
|
79
|
-
|
79
|
+
device_token = user.get_device_token
|
80
80
|
# then you should return to the front-end the tokens.
|
81
81
|
else
|
82
82
|
# code if the user authentication fails
|
@@ -88,22 +88,22 @@ TokAccess use bcrypt has_secure_password and has_secure_token methods to handle
|
|
88
88
|
|
89
89
|
```ruby
|
90
90
|
# somewhere in a controller that handle the users sign in
|
91
|
-
# let's say that you are sending a
|
92
|
-
#
|
91
|
+
# let's say that you are sending a device_token in a header named
|
92
|
+
# APP_device_TOKEN
|
93
93
|
|
94
94
|
def sign_in
|
95
95
|
user = User.find_by(email: params[:email])
|
96
|
-
if user.tok_auth(params[:password], request.headers["
|
96
|
+
if user.tok_auth(params[:password], request.headers["HTTP_APP_device_TOKEN"])
|
97
97
|
# if the user is authenticated successfully and the tok related to the
|
98
|
-
# given
|
99
|
-
# and you can access that tok using the get_token and
|
98
|
+
# given device_token is found, the token and device_token are regenerated
|
99
|
+
# and you can access that tok using the get_token and get_device_token
|
100
100
|
# methods.
|
101
101
|
# if the user is authenticated successfully and the tok related to the
|
102
|
-
# given
|
103
|
-
# that tok using the get_token and
|
102
|
+
# given device_token wasn't found, a new tok is created and you can access
|
103
|
+
# that tok using the get_token and get_device_token
|
104
104
|
# methods.
|
105
105
|
token = user.get_token
|
106
|
-
|
106
|
+
device_token = user.get_device_token
|
107
107
|
# then you should return to the front-end the tokens.
|
108
108
|
else
|
109
109
|
# code if the user authentication fails
|
@@ -124,7 +124,7 @@ TokAccess use bcrypt has_secure_password and has_secure_token methods to handle
|
|
124
124
|
Add this line to your application's Gemfile:
|
125
125
|
|
126
126
|
```ruby
|
127
|
-
gem 'tok_access'
|
127
|
+
gem 'tok_access', '~> 1.1.0'
|
128
128
|
```
|
129
129
|
|
130
130
|
And then execute:
|
@@ -29,7 +29,7 @@ module TokAccess
|
|
29
29
|
tok_model_class_name = "#{table_name.singularize}_tok".camelize
|
30
30
|
if behavior == :invoke
|
31
31
|
if !File.exist?(Rails.root.join("app", "models", "#{tok_model_table_name}.rb"))
|
32
|
-
invoke "active_record:model", [tok_model_class_name, "token:string","
|
32
|
+
invoke "active_record:model", [tok_model_class_name, "token:string","device_token:string", "object_id:integer"]
|
33
33
|
end
|
34
34
|
inject_into_class(Rails.root.join("app", "models", "#{table_name.singularize}.rb"), Object.const_get(table_name.singularize.camelize)) do
|
35
35
|
%Q{\ttokify\n}
|
@@ -18,10 +18,10 @@ module TokAccess
|
|
18
18
|
|
19
19
|
# Return nil if you haven't authenticated the object
|
20
20
|
# using the method #tok_auth or the method #tok_auth failed the
|
21
|
-
# authentication process. Otherwise, return the
|
21
|
+
# authentication process. Otherwise, return the device_token attribute
|
22
22
|
# of one of the associated object toks.
|
23
|
-
def
|
24
|
-
@
|
23
|
+
def get_device_token
|
24
|
+
@_device_token
|
25
25
|
end
|
26
26
|
|
27
27
|
# Return nil if you haven't use the method TokAccess.validate_access.
|
@@ -35,29 +35,29 @@ module TokAccess
|
|
35
35
|
|
36
36
|
# Authenticate the object with the given password .
|
37
37
|
# If the authentication process is successful return the object and the
|
38
|
-
# methods #get_tok and #
|
38
|
+
# methods #get_tok and #get_device_tok will return the tokens
|
39
39
|
# Return nil if the authentication process fails
|
40
|
-
# You can pass as a parameter a
|
41
|
-
# match the
|
42
|
-
# pass the
|
40
|
+
# You can pass as a parameter a device_token. If the object has a tok that
|
41
|
+
# match the device_token given, will regenerate the tok. If you do not
|
42
|
+
# pass the device_token parameter, the method will create a new tok
|
43
43
|
# associated with the object. If the object.toks.size is equal to the
|
44
44
|
# toks_limit, the method will destroy one of the toks and create a new one
|
45
|
-
def tok_auth(password,
|
45
|
+
def tok_auth(password, device_token = nil )
|
46
46
|
if self.authenticate(password)
|
47
|
-
generate_access_toks(
|
47
|
+
generate_access_toks(device_token)
|
48
48
|
return self
|
49
49
|
end
|
50
50
|
nil
|
51
51
|
end
|
52
52
|
|
53
53
|
# If the object is associated to the tok given the method will return
|
54
|
-
# the object and the methods #get_tok and #
|
54
|
+
# the object and the methods #get_tok and #get_device_tok will
|
55
55
|
# return the tokens. Otherwise return nil
|
56
56
|
def provide_access(tok)
|
57
57
|
if self.toks.find_by(id: tok.id)
|
58
58
|
refresh tok
|
59
59
|
set_token tok.token
|
60
|
-
|
60
|
+
set_device_token tok.device_token
|
61
61
|
return self
|
62
62
|
end
|
63
63
|
nil
|
@@ -76,27 +76,27 @@ module TokAccess
|
|
76
76
|
@_token = token
|
77
77
|
end
|
78
78
|
|
79
|
-
def
|
80
|
-
@
|
79
|
+
def set_device_token(token)
|
80
|
+
@_device_token = token
|
81
81
|
end
|
82
82
|
|
83
83
|
def setup_access_tok
|
84
84
|
self.toks.build()
|
85
85
|
end
|
86
86
|
|
87
|
-
def generate_access_toks(
|
88
|
-
if !
|
87
|
+
def generate_access_toks(device_token = nil)
|
88
|
+
if !device_token
|
89
89
|
self.toks.order(updated_at: :asc).first.destroy if self.toks.count == TokAccess.config.tokens_limit
|
90
90
|
tok = self.toks.create
|
91
91
|
else
|
92
|
-
tok = self.toks.find_by(
|
92
|
+
tok = self.toks.find_by(device_token: device_token)
|
93
93
|
tok.regenerate_token if tok
|
94
|
-
tok.
|
94
|
+
tok.regenerate_device_token if tok
|
95
95
|
end
|
96
96
|
if tok
|
97
97
|
@refreshed = true
|
98
98
|
set_token tok.token
|
99
|
-
|
99
|
+
set_device_token tok.device_token
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
@@ -107,20 +107,20 @@ module TokAccess
|
|
107
107
|
# object: The object to authenticate
|
108
108
|
# password: The password to authenticate the object with
|
109
109
|
# if the object is authenticatred successfully, returns the object with
|
110
|
-
# => the get_token and
|
110
|
+
# => the get_token and get_device_token methods returning the access
|
111
111
|
# => tokens.
|
112
112
|
# otherwise return nil
|
113
113
|
def tok_authentication(object, password)
|
114
114
|
return object.tok_auth(password)
|
115
115
|
end
|
116
116
|
|
117
|
-
# tokens: hash with a
|
117
|
+
# tokens: hash with a device_token key or token key
|
118
118
|
# if any of the tokens is found. Provide access to the related
|
119
119
|
# object calling the method provide_access
|
120
120
|
# otherwise return nil
|
121
121
|
def validate_access(tokens = {})
|
122
122
|
toks_class = Object.const_get("#{self.name.camelize}Tok")
|
123
|
-
tok = toks_class.find_by(
|
123
|
+
tok = toks_class.find_by(device_token: tokens[:device_token]) if tokens[:device_token]
|
124
124
|
tok = toks_class.find_by(token: tokens[:token]) if tokens[:token] and !tok
|
125
125
|
if tok
|
126
126
|
return tok._tok_object.provide_access(tok)
|
@@ -43,8 +43,8 @@ module TokAccess
|
|
43
43
|
class_name: "#{self.to_s.gsub(/Tok\z/,'')}",
|
44
44
|
foreign_key: "object_id" if association
|
45
45
|
has_secure_token
|
46
|
-
has_secure_token :
|
47
|
-
validates :token, :
|
46
|
+
has_secure_token :device_token
|
47
|
+
validates :token, :device_token, presence: true, on: :update
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
data/lib/tok_access/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tok_access
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yonga9121
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.5.1
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Handle authentication of your users using tokens.
|