model_token_auth 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 49db6e46e41db1431294c4880f1c1a51a5589191c2ae25bd01aababcf7ea2742
4
+ data.tar.gz: a02df76c08d5238272631117ad313e528f4f80f61988a091ebb8c6d277cf6325
5
+ SHA512:
6
+ metadata.gz: c454c6a9641ad6ddcfa7b9f365258716f5e8c6ccf2049265395b96df71a856974438d7592d0261ccf7e79eb194613c52b99c9f3aebe4b5ba3171afbc8436f88a
7
+ data.tar.gz: 3a873df663303028922811a5edf323e90465bfc97ec50c2f2ce3a871d5116b4304d6eb69c03f80f195b7e610e77f91af28dae6d5b975c5a476a21a0f88449426
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # ModelTokenAuth
2
+
3
+ Generates tokens in models and authentication in the controllers.
4
+
5
+ [![Build Status](https://travis-ci.org/armando1339/model_token_auth.svg?branch=develop)](https://travis-ci.org/armando1339/model_token_auth) [![Coverage Status](https://coveralls.io/repos/github/armando1339/model_token_auth/badge.svg?branch=develop)](https://coveralls.io/github/armando1339/model_token_auth?branch=develop)
6
+
7
+ ## Usage
8
+
9
+ This plugin handles Token API authentication for models. In models in which
10
+ `#acts_as_model_authenticable` method is called, the instances will be able generating
11
+ a token by calling the methods `#save`, `#create` and `#create!`of ActiveRecord. The
12
+ token is generated in an associated model called AccessToken.
13
+
14
+ The plugin also handles authentication in the controllers by inserting a generic
15
+ `#authenticate!` method in ActionController::Base or ActionController::API that
16
+ will verify the existence of the token and creates the `current_*` method by adding
17
+ a header `X-Auth-Token` in the request with a token as a value.
18
+
19
+ ### Models
20
+
21
+ Make models authenticatables.
22
+
23
+ ```ruby
24
+ # example
25
+
26
+ class Center < ActiveRecord::Base
27
+ acts_as_model_authenticable
28
+ end
29
+ ```
30
+
31
+ Then.
32
+
33
+ ```ruby
34
+ # create new record
35
+ center = Center.create!
36
+
37
+ # verify token existence
38
+ center.access_token
39
+
40
+ # => #<AccessToken:0x00007fca2b1ff430
41
+ # id: 31,
42
+ # token: "e897f48950a5c946cd22c4d0c41ad3d3",
43
+ # entity_type: "Center",
44
+ # entity_id: 26,
45
+ # created_at: Fri, 12 Apr 2019 16:42:52 UTC +00:00,
46
+ # updated_at: Fri, 12 Apr 2019 16:42:52 UTC +00:00>
47
+ ```
48
+
49
+ ### Controllers
50
+
51
+ Allow controllers to handle token authentication
52
+
53
+ ```ruby
54
+ # example ActionController::Base
55
+
56
+ class ApplicationController < ActionController::Base
57
+ acts_as_controller_authenticable
58
+ end
59
+
60
+ # for API only
61
+
62
+ class ApplicationController < ActionController::API
63
+ acts_as_controller_authenticable
64
+ end
65
+ ```
66
+
67
+ Then.
68
+
69
+ ```ruby
70
+ # open a scope for centers
71
+
72
+ module Centers
73
+ class DummiesController < ApplicationController
74
+ before_action :authenticate!
75
+
76
+ # using current_* generated
77
+ def show
78
+ @current = current_center
79
+ end
80
+
81
+ # another example
82
+ def index
83
+ @dummies = current_center.dummies
84
+ end
85
+
86
+ ...
87
+ end
88
+ end
89
+ ```
90
+
91
+ ## Installation
92
+
93
+ Add this line to your application's Gemfile:
94
+
95
+ ```ruby
96
+ gem 'model_token_auth', '~> 1.0.0'
97
+ ```
98
+
99
+ And then execute:
100
+
101
+ ```bash
102
+ $ bundle
103
+ ```
104
+
105
+ ## Contributing
106
+
107
+ Bug report or pull request are welcome.
108
+
109
+ Make a pull request:
110
+
111
+ - Fork it
112
+ - Create your feature branch (git checkout -b my-new-feature)
113
+ - Commit your changes (git commit -am 'Add some feature')
114
+ - Push to the branch (git push origin HEAD)
115
+ - Create new Pull Request
116
+
117
+ Please write tests if necessary.
118
+
119
+ ## License
120
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ModelTokenAuth'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+ require "rspec/core/rake_task"
19
+
20
+ RSpec::Core::RakeTask.new(:spec)
21
+ task default: :spec
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate access_token_migration Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,25 @@
1
+ class AccessTokenGenerator < Rails::Generators::Base
2
+ include Rails::Generators::Migration
3
+ source_root File.expand_path('templates', __dir__)
4
+
5
+ def migration
6
+ migration_template(
7
+ 'migration.rb',
8
+ 'db/migrate/create_access_tokens.rb'
9
+ )
10
+ end
11
+
12
+ def rails5_and_up?
13
+ Rails::VERSION::MAJOR >= 5
14
+ end
15
+
16
+ def migration_version
17
+ if rails5_and_up?
18
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
19
+ end
20
+ end
21
+
22
+ def self.next_migration_number(dir)
23
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ class CreateAccessTokens < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :access_tokens do |t|
4
+ t.string :token
5
+ t.references :entity, polymorphic: true, index: true
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ module ModelTokenAuth
2
+ module AccessTokensConfig
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ # => callbacks
8
+ after_initialize :assign_token, if: :new_record?
9
+
10
+ # => associations
11
+ belongs_to :entity, polymorphic: true, optional: true
12
+
13
+ # => validations
14
+ validates_presence_of :token
15
+ validates_uniqueness_of :token, scope: :entity_type, if: :entity_type?
16
+ end
17
+
18
+ private
19
+
20
+ def assign_token
21
+ 5.times.each do
22
+ send(:token=, SecureRandom.hex(16))
23
+
24
+ return true unless self.class.find_by_token(token)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ require 'model_token_auth/controllers_auth'
2
+
3
+ module ModelTokenAuth
4
+ module ActsAsControllersAuthenticable
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def acts_as_controller_authenticable
9
+ include ModelTokenAuth::ControllersAuth
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'model_token_auth/token_generator'
2
+
3
+ module ModelTokenAuth
4
+ module ActsAsModelAuthenticable
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def acts_as_model_authenticable
9
+ include ModelTokenAuth::TokenGenerator
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module ModelTokenAuth
2
+ module ControllersAuth
3
+ def authenticate!
4
+ token = request.headers['HTTP_X_AUTH_TOKEN']
5
+
6
+ if token.present?
7
+ token_ = AccessToken.find_by_token(token)
8
+
9
+ if token_&.entity_type?
10
+ authenticate_entity(token_)
11
+ else
12
+ head :unauthorized
13
+ end
14
+ else
15
+ head :bad_request
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def authenticate_entity(token)
22
+ return false if token.entity_type.nil?
23
+ entity = token.entity_type.tableize.singularize
24
+
25
+ self.class.send(:attr_reader, "current_#{entity}")
26
+ instance_variable_set("@current_#{entity}", token.entity)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module ModelTokenAuth
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,31 @@
1
+ module ModelTokenAuth
2
+ module TokenGenerator
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ # => callbacks
8
+ before_validation :build_access_token, on: :create
9
+
10
+ # => associations
11
+ has_one :access_token, as: :entity
12
+
13
+ # => validations
14
+ validates_associated :access_token
15
+ end
16
+
17
+ def access_token
18
+ if super.nil?
19
+ raise NoDefinedToken
20
+ else
21
+ super
22
+ end
23
+ end
24
+
25
+ class ::NoDefinedToken < Exception
26
+ def message
27
+ 'there is not defined token'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module ModelTokenAuth
2
+ VERSION = '1.0.1'
3
+ end
@@ -0,0 +1,22 @@
1
+ require "model_token_auth/railtie"
2
+ require 'model_token_auth/access_tokens_config'
3
+ require 'model_token_auth/acts_as_model_authenticable'
4
+ require 'model_token_auth/acts_as_controllers_authenticable'
5
+
6
+ module ModelTokenAuth; end
7
+
8
+ class AccessToken < ActiveRecord::Base
9
+ include ModelTokenAuth::AccessTokensConfig
10
+ end
11
+
12
+ ActiveRecord::Base.include(
13
+ ModelTokenAuth::ActsAsModelAuthenticable
14
+ )
15
+
16
+ ActionController::Base.include(
17
+ ModelTokenAuth::ActsAsControllersAuthenticable
18
+ ) if ActionController::Base
19
+
20
+ ActionController::API.include(
21
+ ModelTokenAuth::ActsAsControllersAuthenticable
22
+ ) if ActionController::API
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :model_token_auth do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model_token_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Armando Alejandre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda-matchers
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: shoulda-callback-matchers
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Generates tokens in models and authentication in the controllers.
112
+ email:
113
+ - armando1339@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - MIT-LICENSE
119
+ - README.md
120
+ - Rakefile
121
+ - lib/generators/access_token/USAGE
122
+ - lib/generators/access_token/access_token_generator.rb
123
+ - lib/generators/access_token/templates/migration.rb
124
+ - lib/model_token_auth.rb
125
+ - lib/model_token_auth/access_tokens_config.rb
126
+ - lib/model_token_auth/acts_as_controllers_authenticable.rb
127
+ - lib/model_token_auth/acts_as_model_authenticable.rb
128
+ - lib/model_token_auth/controllers_auth.rb
129
+ - lib/model_token_auth/railtie.rb
130
+ - lib/model_token_auth/token_generator.rb
131
+ - lib/model_token_auth/version.rb
132
+ - lib/tasks/auth_api_tasks.rake
133
+ homepage:
134
+ licenses:
135
+ - MIT
136
+ metadata:
137
+ allowed_push_host: https://rubygems.org
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.7.8
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Generates tokens in models and authentication in the controllers.
158
+ test_files: []