authentication-rails 0.1.0

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: 0cf9399c991ec6e657022001d20212800d6b6fd19855b3e35f42b0206118fa68
4
+ data.tar.gz: 1e832ccfc7dc0139e9967a18d4cb59e9d042ffc4619a21729634bdb7bd82d847
5
+ SHA512:
6
+ metadata.gz: bf2887f5defec22445171f0f4f4bdcc06b96bbe2b085ae87bf197cdcb94073aa16479c566ee59aa263ee18436089805c440c6c009bb0de1173aa5346a357b42b
7
+ data.tar.gz: 8ce4008bfe813b5dc940bf5c90f260672865171870827e8094c82039fded0f999692fd81fb00baf4453fbda5746c670f24f36a78c1eb349854aa27f304e9d97a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Julien Dargelos
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 @@
1
+ # Authentication Rails
data/Rakefile ADDED
@@ -0,0 +1,33 @@
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 = 'Authentication::Rails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+
33
+ task default: :test
@@ -0,0 +1,101 @@
1
+ module Authentication
2
+ module Attributes
3
+ extend ActiveSupport::Concern
4
+
5
+ DEFAULT_ID_ATTRIBUTE = :id
6
+ DEFAULT_PUBLIC_ATTRIBUTE = :email
7
+ DEFAULT_PRIVATE_ATTRIBUTE = :password
8
+
9
+ included do
10
+ attr_accessor :public_attribute, :private_attribute
11
+
12
+ def id
13
+ return nil if model.blank?
14
+ model.send self.class.id_attribute
15
+ end
16
+
17
+ def id=(v)
18
+ return self.model = nil if v.nil?
19
+ self.model = self.class.authenticated_model.find_by self.class.id_attribute => v
20
+ end
21
+
22
+ def model
23
+ return nil if public_attribute.blank?
24
+ self.class.authenticated_model.find_by self.class.public_attribute => public_attribute
25
+ end
26
+
27
+ def model=(v)
28
+ self.public_attribute = v.try self.class.public_attribute
29
+ end
30
+ end
31
+
32
+ class_methods do
33
+ attr_reader :authenticated_model
34
+
35
+ def has_model(name)
36
+ alias_accessor :model, :authenticated_model_name do
37
+ @authenticated_model = name.present? ? name.to_s.classify.constantize : nil
38
+ end
39
+ end
40
+
41
+ def has_id(attribute)
42
+ alias_accessor :id, :id_attribute do
43
+ @id_attribute = attribute.to_s.to_sym.presence
44
+ end
45
+ end
46
+
47
+ def has_public(attribute)
48
+ alias_accessor :public_attribute do
49
+ @public_attribute = attribute.to_s.to_sym.presence
50
+ end
51
+ end
52
+
53
+ def has_private(attribute)
54
+ alias_accessor :private_attribute do
55
+ @private_attribute = attribute.to_s.to_sym.presence
56
+ end
57
+ end
58
+
59
+ def authenticated_model_name
60
+ authenticated_model.present? ? authenticated_model.to_s.underscore.gsub('/', '_').to_sym : :model
61
+ end
62
+
63
+ def id_attribute
64
+ @id_attribute || DEFAULT_ID_ATTRIBUTE
65
+ end
66
+
67
+ def public_attribute
68
+ @public_attribute || DEFAULT_PUBLIC_ATTRIBUTE
69
+ end
70
+
71
+ def private_attribute
72
+ @private_attribute || DEFAULT_PRIVATE_ATTRIBUTE
73
+ end
74
+
75
+ def columns
76
+ [
77
+ Struct.new(:name, :type).new(public_attribute, :string),
78
+ Struct.new(:name, :type).new(private_attribute, :string)
79
+ ]
80
+ end
81
+
82
+ protected
83
+
84
+ def alias_accessor(name, name_method = nil)
85
+ method_name = send name_method || name
86
+ if method_name.present? && method_name.to_s != name.to_s
87
+ remove_method method_name if respond_to? method_name
88
+ remove_method "#{method_name}=" if respond_to? "#{method_name}="
89
+ end
90
+
91
+ yield
92
+
93
+ method_name = send name_method || name
94
+ if method_name.to_s != name.to_s
95
+ alias_method method_name, name
96
+ alias_method "#{method_name}=", "#{name}="
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,10 @@
1
+ module Authentication
2
+ class Base
3
+ include ActiveModel::Model
4
+
5
+ include Validations
6
+ include Attributes
7
+ include Storage
8
+ include Comparisons
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module Authentication
2
+ module Comparisons
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def ==(authentication)
7
+ authentication.is_a?(self.class) && id.present? && id == authentication.id
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ require __dir__ + '/core_ext/action_controller'
@@ -0,0 +1,54 @@
1
+ ActionController::Base.class_eval do
2
+ before_action :initialize_authentication
3
+
4
+ class << self
5
+ protected
6
+
7
+ def authenticates(model = nil, options = {}, &hook)
8
+ authentication = authentication_class_for model, options
9
+
10
+ define_method authentication_hook_name_for(:success, nil, class_name: authentication) do
11
+ instance_eval &hook unless authentication.current.exists?
12
+ end
13
+ end
14
+
15
+ def unauthenticates(model = nil, options = {}, &hook)
16
+ authentication = authentication_class_for model, options
17
+
18
+ define_method authentication_hook_name_for(:fail, nil, class_name: authentication) do
19
+ instance_eval &hook if authentication.current.exists?
20
+ end
21
+ end
22
+
23
+ def authenticates!(model = nil, options = {})
24
+ before_action authentication_hook_name_for(:success, model, options), options.except(:class_name)
25
+ end
26
+
27
+ def doesnt_authenticate!(model = nil, options = {})
28
+ skip_before_action authentication_hook_name_for(:success, model, options), options.except(:class_name)
29
+ end
30
+
31
+ def unauthenticates!(model = nil, options = {})
32
+ before_action authentication_hook_name_for(:fail, model, options), options.except(:class_name)
33
+ end
34
+
35
+ def doesnt_unauthenticate!(model = nil, options = {})
36
+ bskip_efore_action authentication_hook_name_for(:fail, model, options), options.except(:class_name)
37
+ end
38
+
39
+ def authentication_class_for(model, options)
40
+ (options[:class_name].presence || "#{model.to_s.classify}::Authentication").to_s.constantize
41
+ end
42
+
43
+ def authentication_hook_name_for(type, model, options)
44
+ authentication = authentication_class_for model, options
45
+ "#{authentication.to_s.underscore.gsub '/', '_'}_#{type}_hook".to_sym
46
+ end
47
+ end
48
+
49
+ protected
50
+
51
+ def initialize_authentication
52
+ Authentication::Base.store = session
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module Authentication
2
+ class Engine < ::Rails::Engine
3
+
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/railtie'
2
+
3
+ module Authentication
4
+ module Rails
5
+ require __dir__ + '/engine'
6
+ require __dir__ + '/core_ext'
7
+
8
+ require __dir__ + '/validations'
9
+ require __dir__ + '/attributes'
10
+ require __dir__ + '/storage'
11
+ require __dir__ + '/comparisons'
12
+
13
+ require __dir__ + '/base'
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ module Authentication
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Authentication
2
+ module Rails
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ module Authentication
2
+ module Storage
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def save
7
+ if validate
8
+ self.class.current = self
9
+ return true
10
+ else
11
+ return false
12
+ end
13
+ end
14
+
15
+ def destroy
16
+ self.class.current = nil
17
+ end
18
+
19
+ def persisted?
20
+ self.class.current == self
21
+ end
22
+ alias_method :exists?, :persisted?
23
+ end
24
+
25
+ class_methods do
26
+ attr_writer :store
27
+
28
+ def store
29
+ @store.present? ? @store : (self == Authentication::Base ? nil : Authentication::Base.store)
30
+ end
31
+
32
+ def current
33
+ new id: store&.[](store_key)
34
+ end
35
+
36
+ def current=(v)
37
+ store&.[]=(store_key, v&.id)
38
+ end
39
+
40
+ protected
41
+
42
+ def store_key
43
+ :"#{to_s.underscore.gsub '/', '_'}_#{id_attribute}"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ module Authentication
2
+ module Validations
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ validates :public_attribute, presence: true
7
+ validates :private_attribute, presence: true
8
+ validate :succeed
9
+
10
+ protected
11
+
12
+ def succeed
13
+ if public_attribute.present? && private_attribute.present? && !model&.authenticate(private_attribute)
14
+ errors.add(:base,
15
+ I18n.t(
16
+ "activerecord.errors.models.#{self.class.to_s.underscore.sub '/', '_'}.invalid",
17
+ default: I18n.t(
18
+ 'activerecord.errors.messages.invalid',
19
+ default: I18n.t('errors.messages.invalid')
20
+ )
21
+ )
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :authentication_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authentication-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Julien Dargelos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-06 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.0
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.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bcrypt
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: sqlite3
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
+ description: Provide a Rails authentication active model.
56
+ email:
57
+ - contact@juliendargelos.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/authentication/attributes.rb
66
+ - lib/authentication/base.rb
67
+ - lib/authentication/comparisons.rb
68
+ - lib/authentication/core_ext.rb
69
+ - lib/authentication/core_ext/action_controller.rb
70
+ - lib/authentication/engine.rb
71
+ - lib/authentication/rails.rb
72
+ - lib/authentication/rails/railtie.rb
73
+ - lib/authentication/rails/version.rb
74
+ - lib/authentication/storage.rb
75
+ - lib/authentication/validations.rb
76
+ - lib/tasks/authentication/rails_tasks.rake
77
+ homepage: https://www.github.com/juliendargelos/authentication-rails
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.7.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Authenticate with Rails.
101
+ test_files: []