session_authentication 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: b1cf629ec07561032fcdcbcc41990a6fb16958d3cae3bd25ccc5a2b7112fa548
4
+ data.tar.gz: 7e5b60d10ebe148d8d4a1ecc61a3f498009f0e09c7b8e69e7ea75efab989ba12
5
+ SHA512:
6
+ metadata.gz: b3e34ff7e55d33509c9f63d3e393c6dc5c1cbff641734b02e00abdc64b7611081e4733dc28062f899d757637e36190f375e6d549576e1cc02a6d0f92f7827985
7
+ data.tar.gz: acd9485e5260126457234c266e953f56c90afe153784e9aeee3be103ed41c708ed46808be0b90cacc8c874d0efea670c1fcf0f9accf16c762483dafe826c51c6
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in session_authentication.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,157 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ session_authentication (0.1.0)
5
+ rails (~> 5.2.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.8.1)
11
+ actionpack (= 5.2.8.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.8.1)
15
+ actionpack (= 5.2.8.1)
16
+ actionview (= 5.2.8.1)
17
+ activejob (= 5.2.8.1)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.8.1)
21
+ actionview (= 5.2.8.1)
22
+ activesupport (= 5.2.8.1)
23
+ rack (~> 2.0, >= 2.0.8)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.2.8.1)
28
+ activesupport (= 5.2.8.1)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.2.8.1)
34
+ activesupport (= 5.2.8.1)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.8.1)
37
+ activesupport (= 5.2.8.1)
38
+ activerecord (5.2.8.1)
39
+ activemodel (= 5.2.8.1)
40
+ activesupport (= 5.2.8.1)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.8.1)
43
+ actionpack (= 5.2.8.1)
44
+ activerecord (= 5.2.8.1)
45
+ marcel (~> 1.0.0)
46
+ activesupport (5.2.8.1)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ arel (9.0.0)
52
+ builder (3.2.4)
53
+ concurrent-ruby (1.2.2)
54
+ crass (1.0.6)
55
+ date (3.3.3)
56
+ diff-lcs (1.5.0)
57
+ erubi (1.12.0)
58
+ globalid (1.1.0)
59
+ activesupport (>= 5.0)
60
+ i18n (1.14.1)
61
+ concurrent-ruby (~> 1.0)
62
+ loofah (2.21.3)
63
+ crass (~> 1.0.2)
64
+ nokogiri (>= 1.12.0)
65
+ mail (2.8.1)
66
+ mini_mime (>= 0.1.1)
67
+ net-imap
68
+ net-pop
69
+ net-smtp
70
+ marcel (1.0.2)
71
+ method_source (1.0.0)
72
+ mini_mime (1.1.5)
73
+ mini_portile2 (2.8.4)
74
+ minitest (5.20.0)
75
+ net-imap (0.3.7)
76
+ date
77
+ net-protocol
78
+ net-pop (0.1.2)
79
+ net-protocol
80
+ net-protocol (0.2.1)
81
+ timeout
82
+ net-smtp (0.3.3)
83
+ net-protocol
84
+ nio4r (2.5.9)
85
+ nokogiri (1.13.10)
86
+ mini_portile2 (~> 2.8.0)
87
+ racc (~> 1.4)
88
+ racc (1.7.1)
89
+ rack (2.2.8)
90
+ rack-test (2.1.0)
91
+ rack (>= 1.3)
92
+ rails (5.2.8.1)
93
+ actioncable (= 5.2.8.1)
94
+ actionmailer (= 5.2.8.1)
95
+ actionpack (= 5.2.8.1)
96
+ actionview (= 5.2.8.1)
97
+ activejob (= 5.2.8.1)
98
+ activemodel (= 5.2.8.1)
99
+ activerecord (= 5.2.8.1)
100
+ activestorage (= 5.2.8.1)
101
+ activesupport (= 5.2.8.1)
102
+ bundler (>= 1.3.0)
103
+ railties (= 5.2.8.1)
104
+ sprockets-rails (>= 2.0.0)
105
+ rails-dom-testing (2.2.0)
106
+ activesupport (>= 5.0.0)
107
+ minitest
108
+ nokogiri (>= 1.6)
109
+ rails-html-sanitizer (1.5.0)
110
+ loofah (~> 2.19, >= 2.19.1)
111
+ railties (5.2.8.1)
112
+ actionpack (= 5.2.8.1)
113
+ activesupport (= 5.2.8.1)
114
+ method_source
115
+ rake (>= 0.8.7)
116
+ thor (>= 0.19.0, < 2.0)
117
+ rake (10.5.0)
118
+ rspec (3.12.0)
119
+ rspec-core (~> 3.12.0)
120
+ rspec-expectations (~> 3.12.0)
121
+ rspec-mocks (~> 3.12.0)
122
+ rspec-core (3.12.2)
123
+ rspec-support (~> 3.12.0)
124
+ rspec-expectations (3.12.3)
125
+ diff-lcs (>= 1.2.0, < 2.0)
126
+ rspec-support (~> 3.12.0)
127
+ rspec-mocks (3.12.6)
128
+ diff-lcs (>= 1.2.0, < 2.0)
129
+ rspec-support (~> 3.12.0)
130
+ rspec-support (3.12.1)
131
+ sprockets (4.2.1)
132
+ concurrent-ruby (~> 1.0)
133
+ rack (>= 2.2.4, < 4)
134
+ sprockets-rails (3.4.2)
135
+ actionpack (>= 5.2)
136
+ activesupport (>= 5.2)
137
+ sprockets (>= 3.0.0)
138
+ thor (1.2.2)
139
+ thread_safe (0.3.6)
140
+ timeout (0.4.0)
141
+ tzinfo (1.2.11)
142
+ thread_safe (~> 0.1)
143
+ websocket-driver (0.7.6)
144
+ websocket-extensions (>= 0.1.0)
145
+ websocket-extensions (0.1.5)
146
+
147
+ PLATFORMS
148
+ ruby
149
+
150
+ DEPENDENCIES
151
+ bundler (~> 1.17)
152
+ rake (~> 10.0)
153
+ rspec (~> 3.0)
154
+ session_authentication!
155
+
156
+ BUNDLED WITH
157
+ 1.17.2
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # SessionAuthentication
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "session_authentication"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ module SessionAuthentication
2
+ class Configuration
3
+ REQUIRED_OPTIONS = [].freeze
4
+
5
+ DEFAULT_OPTIONS = {
6
+ user_model_name: "User",
7
+ session_key: :user_id,
8
+ active_for_authentication_proc: nil
9
+ }.freeze
10
+
11
+ attr_accessor(*(REQUIRED_OPTIONS + DEFAULT_OPTIONS.keys))
12
+
13
+ def initialize
14
+ set_default_values
15
+ end
16
+
17
+ def [](opt_name)
18
+ public_send(opt_name)
19
+ end
20
+
21
+ def validate!
22
+ missing_options = REQUIRED_OPTIONS.select { |opt| self[opt].nil? }
23
+ return if missing_options.empty?
24
+
25
+ fail "Missing configuration options: #{missing_options.join(', ')}"
26
+ end
27
+
28
+ def user_model
29
+ @user_model ||= user_model_name.constantize
30
+ end
31
+
32
+ private
33
+
34
+ def set_default_values
35
+ DEFAULT_OPTIONS.each do |opt_name, default_value|
36
+ public_send("#{opt_name}=", default_value)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,60 @@
1
+ module SessionAuthentication
2
+ module ControllerHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def current_user
7
+ return @current_user if current_user_set?
8
+ set_current_user
9
+ end
10
+ helper_method :current_user
11
+
12
+ def signed_in?
13
+ !!current_user
14
+ end
15
+ helper_method :signed_in?
16
+
17
+ def sign_in(user)
18
+ session[session_key] = user.id
19
+ @current_user = user
20
+ end
21
+
22
+ def sign_out
23
+ session[session_key] = nil
24
+ end
25
+
26
+ private
27
+
28
+ def set_current_user
29
+ @current_user = find_current_user
30
+ current_user_set!
31
+ @current_user
32
+ end
33
+
34
+ def find_current_user
35
+ return unless session[session_key].present?
36
+ user = SessionAuthentication.config.user_model.find_by(id: session[session_key])
37
+ return unless active_for_authentication?(user)
38
+ user
39
+ end
40
+
41
+ def current_user_set?
42
+ !!@current_user_set
43
+ end
44
+
45
+ def current_user_set!
46
+ @current_user_set = true
47
+ end
48
+
49
+ def session_key
50
+ SessionAuthentication.config.session_key
51
+ end
52
+
53
+ def active_for_authentication?(user)
54
+ proc = SessionAuthentication.config.active_for_authentication_proc
55
+ return true unless proc
56
+ proc.call(user)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module SessionAuthentication
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ require "session_authentication/version"
2
+ require "session_authentication/configuration"
3
+ require "session_authentication/controller_helper"
4
+
5
+ module SessionAuthentication
6
+ class Error < StandardError; end
7
+
8
+ class << self
9
+ def setup
10
+ @config = Configuration.new
11
+ yield @config
12
+ @config.validate!
13
+ end
14
+
15
+ def config
16
+ return @config if @config
17
+ fail ErrorHelper.error_message(:root, :no_config)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "session_authentication/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "session_authentication"
8
+ spec.version = SessionAuthentication::VERSION
9
+ spec.authors = ["Anton Chumakov"]
10
+ spec.email = ["anton@chumakoff.com"]
11
+
12
+ spec.summary = "Session Authentication"
13
+ spec.description = "Session Authentication"
14
+ spec.homepage = ""
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "rails", "~> 5.2.2"
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.17"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: session_authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Chumakov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-08 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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Session Authentication
70
+ email:
71
+ - anton@chumakoff.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/session_authentication.rb
86
+ - lib/session_authentication/configuration.rb
87
+ - lib/session_authentication/controller_helper.rb
88
+ - lib/session_authentication/version.rb
89
+ - session_authentication.gemspec
90
+ homepage: ''
91
+ licenses: []
92
+ metadata:
93
+ allowed_push_host: https://rubygems.org/
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Session Authentication
113
+ test_files: []