model_token_auth 1.0.3 → 1.1.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/README.md +118 -18
- data/lib/model_token_auth/controllers_auth.rb +8 -2
- data/lib/model_token_auth/version.rb +1 -1
- metadata +21 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b3168a54dc4b80feef92939e9c1ec3445fd0598bdd6394933650c3b615087a0
|
4
|
+
data.tar.gz: 585c716460869964bef679d24c117bfadc5345a084bbf11feaa50b859cf4002d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c664b48be846dc4b15ad2b3ccc92ddaf0b10cdcfadf75c3b080ef970ef0f86e23e973ea704eac5d68872ef2fcdd30851935367669cd1fbba2f18510b7709914
|
7
|
+
data.tar.gz: 182fb2d03f4741e75e3953b0c8e00bb49e8482c4082df69c44921a951bdad8038e02849bbfeb5b55720efda1d4514c9fbbe8ce9bc1d856324a7e4946dbaa8920
|
data/README.md
CHANGED
@@ -1,8 +1,36 @@
|
|
1
1
|
# ModelTokenAuth
|
2
2
|
|
3
|
-
|
3
|
+
Plugin RoR that generates tokens in models and authentication in the controllers.
|
4
4
|
|
5
|
-
[](https://travis-ci.org/Kinedu/model_token_auth) [](https://coveralls.io/github/armando1339/model_token_auth?branch=develop)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'model_token_auth'
|
13
|
+
```
|
14
|
+
|
15
|
+
Then execute:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
In the root directory:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
$ rails generate access_token
|
25
|
+
```
|
26
|
+
|
27
|
+
Last command will generate a migration, then:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
$ rake db:migrate
|
31
|
+
```
|
32
|
+
|
33
|
+
Once the migration finishes, the models will be ready to be authenticated.
|
6
34
|
|
7
35
|
## Usage
|
8
36
|
|
@@ -15,7 +43,8 @@ The plugin also handles authentication in the controllers by inserting a generic
|
|
15
43
|
`#authenticate!` method in ActionController::Base or ActionController::API that
|
16
44
|
will verify the existence of the token and creates the `current_*` method.
|
17
45
|
|
18
|
-
To authenticate some token it'll need adding the header `
|
46
|
+
To authenticate some token it'll need adding the standard header `Authorization` in the request
|
47
|
+
with a token as a value.
|
19
48
|
|
20
49
|
### Models
|
21
50
|
|
@@ -89,33 +118,103 @@ module Centers
|
|
89
118
|
end
|
90
119
|
```
|
91
120
|
|
92
|
-
##
|
121
|
+
## Rspec
|
93
122
|
|
94
|
-
|
123
|
+
To facilitate the testing of the controllers:
|
124
|
+
|
125
|
+
1.- Add a new folder with the name of ```support``` in the root of ```spec```.
|
126
|
+
|
127
|
+
2.- Create a ruby file with the name of model_token_auth_helper (```model_token_auth_helper.rb```).
|
128
|
+
|
129
|
+
3.- Copy the following code:
|
95
130
|
|
96
131
|
```ruby
|
97
|
-
|
98
|
-
```
|
132
|
+
# => model_token_auth_helper.rb
|
99
133
|
|
100
|
-
|
134
|
+
module ModelTokenAuthHelper
|
101
135
|
|
102
|
-
|
103
|
-
|
136
|
+
# => skip authentication but still creates the
|
137
|
+
# instance variable #current_<model-name>.
|
138
|
+
#
|
139
|
+
def skip_authentication!(access_token)
|
140
|
+
subject.class.skip_before_action(:authenticate!, raise: false)
|
141
|
+
subject.send(:authenticate_entity, access_token)
|
142
|
+
end
|
143
|
+
|
144
|
+
# => add the 'Authorization' header with the
|
145
|
+
# value of the token that is passed to it,
|
146
|
+
# giving it the necessary format.
|
147
|
+
#
|
148
|
+
def add_authorization_header(token)
|
149
|
+
request.env['HTTP_AUTHORIZATION'] =
|
150
|
+
ActionController::HttpAuthentication::Token.encode_credentials(token)
|
151
|
+
end
|
152
|
+
end
|
104
153
|
```
|
105
154
|
|
106
|
-
|
155
|
+
4.- Require ModelTokenAuthHelper at top of the file ```spec_helper.rb``` and add the module to
|
156
|
+
the RSpec configuration:
|
107
157
|
|
108
|
-
```
|
109
|
-
|
110
|
-
```
|
158
|
+
```ruby
|
159
|
+
# => spec_helper.rb
|
111
160
|
|
112
|
-
|
161
|
+
require_relative 'support/model_token_auth_helper'
|
113
162
|
|
114
|
-
|
115
|
-
|
163
|
+
RSpec.configure do |config|
|
164
|
+
...
|
165
|
+
|
166
|
+
# => *
|
167
|
+
config.include ModelTokenAuthHelper, type: :controller
|
168
|
+
end
|
116
169
|
```
|
117
170
|
|
118
|
-
|
171
|
+
### ModelTokenAuthHelper Usage
|
172
|
+
|
173
|
+
Here is described how to use the methods of the ModelTokenAuthHelper module that we created
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
require 'rails_helper'
|
177
|
+
|
178
|
+
RSpec.describe DummiesController do
|
179
|
+
describe 'controllers authentication' do
|
180
|
+
|
181
|
+
# => EXAMPLE 1
|
182
|
+
context '#authenticate!' do
|
183
|
+
before do
|
184
|
+
|
185
|
+
# You can use this method to add the
|
186
|
+
# necessary header and allow the control
|
187
|
+
# to do its magic
|
188
|
+
#
|
189
|
+
add_authorization_header(dummy.access_token.token)
|
190
|
+
|
191
|
+
get :index, format: :json
|
192
|
+
end
|
193
|
+
|
194
|
+
it { expect(response).to be_successful }
|
195
|
+
|
196
|
+
it { expect(subject.respond_to? :current_center).to be_truthy }
|
197
|
+
end
|
198
|
+
|
199
|
+
# => EXAMPLE 2
|
200
|
+
context 'skip #authenticate!' do
|
201
|
+
before do
|
202
|
+
# You can use this method to skip
|
203
|
+
# the #authenticate! method but still
|
204
|
+
# have access to the current_* variable
|
205
|
+
#
|
206
|
+
skip_authentication!(dummy.access_token)
|
207
|
+
|
208
|
+
get :index, format: :json
|
209
|
+
end
|
210
|
+
|
211
|
+
it { expect(response).to be_successful }
|
212
|
+
|
213
|
+
it { expect(subject.respond_to? :current_center).to be_truthy }
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
```
|
119
218
|
|
120
219
|
## Contributing
|
121
220
|
|
@@ -132,4 +231,5 @@ Make a pull request:
|
|
132
231
|
Please write tests if necessary.
|
133
232
|
|
134
233
|
## License
|
234
|
+
|
135
235
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module ModelTokenAuth
|
2
2
|
module ControllersAuth
|
3
3
|
def authenticate!
|
4
|
-
token = request.headers['
|
4
|
+
token = request.headers['HTTP_AUTHORIZATION']
|
5
5
|
|
6
6
|
if token.present?
|
7
|
-
token_ = AccessToken.find_by_token(token)
|
7
|
+
token_ = AccessToken.find_by_token(parses_the_token(token))
|
8
8
|
|
9
9
|
if token_&.entity_type?
|
10
10
|
authenticate_entity(token_)
|
@@ -18,6 +18,7 @@ module ModelTokenAuth
|
|
18
18
|
|
19
19
|
private
|
20
20
|
|
21
|
+
# => *
|
21
22
|
def authenticate_entity(token)
|
22
23
|
return false if token.entity_type.nil?
|
23
24
|
entity = token.entity_type.tableize.singularize
|
@@ -25,5 +26,10 @@ module ModelTokenAuth
|
|
25
26
|
self.class.send(:attr_reader, "current_#{entity}")
|
26
27
|
instance_variable_set("@current_#{entity}", token.entity)
|
27
28
|
end
|
29
|
+
|
30
|
+
# => *
|
31
|
+
def parses_the_token(token)
|
32
|
+
token.split.last.remove('token=').parameterize
|
33
|
+
end
|
28
34
|
end
|
29
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_token_auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Armando Alejandre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -108,7 +108,22 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
-
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Plugin RoR that generates tokens in models and authentication in the
|
126
|
+
controllers.
|
112
127
|
email:
|
113
128
|
- armando1339@gmail.com
|
114
129
|
executables: []
|
@@ -130,10 +145,11 @@ files:
|
|
130
145
|
- lib/model_token_auth/token_generator.rb
|
131
146
|
- lib/model_token_auth/version.rb
|
132
147
|
- lib/tasks/auth_api_tasks.rake
|
133
|
-
homepage: https://
|
148
|
+
homepage: https://www.kinedu.com/
|
134
149
|
licenses:
|
135
150
|
- MIT
|
136
151
|
metadata:
|
152
|
+
source_code_uri: https://github.com/Kinedu/model_token_auth
|
137
153
|
allowed_push_host: https://rubygems.org
|
138
154
|
post_install_message:
|
139
155
|
rdoc_options: []
|
@@ -154,5 +170,5 @@ rubyforge_project:
|
|
154
170
|
rubygems_version: 2.7.8
|
155
171
|
signing_key:
|
156
172
|
specification_version: 4
|
157
|
-
summary:
|
173
|
+
summary: Plugin RoR that generates tokens in models and authentication in the controllers.
|
158
174
|
test_files: []
|