current_session 0.1.6 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe0094c715e9d1a1152a795e0edd0583434cabcc1d955a8fc5c6e2c2202c0e85
4
- data.tar.gz: 0d7af31d9cb4f1762c15e6b1a288b2c4c6226dcb153e8709b6e2321ee7007585
3
+ metadata.gz: 505b1421a5df8b13c947388a55f0af6224ebae0d7e61c552292896e53a7ffae8
4
+ data.tar.gz: 1d047d2c47aff4e1665a743d7d38cb5b964493525d0c3f611fcac36f22bc2ec9
5
5
  SHA512:
6
- metadata.gz: 41197858ae7dcd61d71a593332ca369ed9526fa0711e3c2b992d2eadaac55df763f800e105d01a67e254043c67ee3673d169be6e5ee889a51797acdfc8b9d962
7
- data.tar.gz: 6b907dce8ee28afb0a6988cf5384912a732bb56f7ad8f85543cadd02b108c83f15108a6b6ef0593e9c6a86861114ba6aac0e0b053ce2d9cda3bae61469f3acd5
6
+ metadata.gz: fde9c27a98d38fd4bcc0a55c82921cfd19c9525538a9a618d91a575c287fe8611bac043f455c25d62e6b2703d03c962fa2b9e4aaa50f9a3a2b91eb087e41fbc6
7
+ data.tar.gz: c6fb18a168a5cdaf575d63ddcac4ef35bf9ba3943a15a76615e77301e63c2953117b7231e501f1e89381cc7850e19251ae6f32c1b691b3d5c6df0a6d93f97e8f
data/.rubocop.yml CHANGED
@@ -17,7 +17,7 @@ Style/StringLiteralsInInterpolation:
17
17
  EnforcedStyle: double_quotes
18
18
 
19
19
  Layout/LineLength:
20
- Max: 120
20
+ Max: 125
21
21
  Exclude:
22
22
  - current_session.gemspec
23
23
 
data/Gemfile CHANGED
@@ -5,9 +5,9 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in current_user_omniauth.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
8
+ gem "rake"
9
9
 
10
- gem "rspec", "~> 3.0"
10
+ gem "rspec"
11
11
 
12
12
  gem "rubocop"
13
13
  gem "rubocop-performance"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- current_session (0.1.6)
4
+ current_session (0.1.7)
5
5
  activesupport (>= 6.0.0)
6
6
 
7
7
  GEM
@@ -26,7 +26,7 @@ GEM
26
26
  hashie (5.0.0)
27
27
  i18n (1.12.0)
28
28
  concurrent-ruby (~> 1.0)
29
- json (2.6.2)
29
+ json (2.6.3)
30
30
  method_source (1.0.0)
31
31
  minitest (5.16.3)
32
32
  omniauth (2.1.0)
@@ -34,47 +34,47 @@ GEM
34
34
  rack (>= 2.2.3)
35
35
  rack-protection
36
36
  parallel (1.22.1)
37
- parser (3.1.2.1)
37
+ parser (3.1.3.0)
38
38
  ast (~> 2.4.1)
39
39
  pry (0.14.1)
40
40
  coderay (~> 1.1)
41
41
  method_source (~> 1.0)
42
- rack (3.0.0)
43
- rack-protection (3.0.2)
42
+ rack (3.0.2)
43
+ rack-protection (3.0.5)
44
44
  rack
45
45
  rainbow (3.1.1)
46
46
  rake (13.0.6)
47
- regexp_parser (2.6.0)
47
+ regexp_parser (2.6.1)
48
48
  rexml (3.2.5)
49
- rspec (3.11.0)
50
- rspec-core (~> 3.11.0)
51
- rspec-expectations (~> 3.11.0)
52
- rspec-mocks (~> 3.11.0)
53
- rspec-core (3.11.0)
54
- rspec-support (~> 3.11.0)
55
- rspec-expectations (3.11.1)
49
+ rspec (3.12.0)
50
+ rspec-core (~> 3.12.0)
51
+ rspec-expectations (~> 3.12.0)
52
+ rspec-mocks (~> 3.12.0)
53
+ rspec-core (3.12.0)
54
+ rspec-support (~> 3.12.0)
55
+ rspec-expectations (3.12.1)
56
56
  diff-lcs (>= 1.2.0, < 2.0)
57
- rspec-support (~> 3.11.0)
58
- rspec-mocks (3.11.1)
57
+ rspec-support (~> 3.12.0)
58
+ rspec-mocks (3.12.1)
59
59
  diff-lcs (>= 1.2.0, < 2.0)
60
- rspec-support (~> 3.11.0)
61
- rspec-support (3.11.1)
62
- rubocop (1.37.0)
60
+ rspec-support (~> 3.12.0)
61
+ rspec-support (3.12.0)
62
+ rubocop (1.41.1)
63
63
  json (~> 2.3)
64
64
  parallel (~> 1.10)
65
65
  parser (>= 3.1.2.1)
66
66
  rainbow (>= 2.2.2, < 4.0)
67
67
  regexp_parser (>= 1.8, < 3.0)
68
68
  rexml (>= 3.2.5, < 4.0)
69
- rubocop-ast (>= 1.22.0, < 2.0)
69
+ rubocop-ast (>= 1.23.0, < 2.0)
70
70
  ruby-progressbar (~> 1.7)
71
71
  unicode-display_width (>= 1.4.0, < 3.0)
72
- rubocop-ast (1.23.0)
72
+ rubocop-ast (1.24.0)
73
73
  parser (>= 3.1.1.0)
74
- rubocop-performance (1.15.0)
74
+ rubocop-performance (1.15.1)
75
75
  rubocop (>= 1.7.0, < 2.0)
76
76
  rubocop-ast (>= 0.4.0)
77
- rubocop-rspec (2.13.2)
77
+ rubocop-rspec (2.16.0)
78
78
  rubocop (~> 1.33)
79
79
  ruby-progressbar (1.11.0)
80
80
  simplecov (0.21.2)
@@ -83,14 +83,15 @@ GEM
83
83
  simplecov_json_formatter (~> 0.1)
84
84
  simplecov-html (0.12.3)
85
85
  simplecov_json_formatter (0.1.4)
86
- sqlite3 (1.5.3-arm64-darwin)
87
- sqlite3 (1.5.3-x86_64-linux)
86
+ sqlite3 (1.5.4-arm64-darwin)
87
+ sqlite3 (1.5.4-x86_64-linux)
88
88
  tzinfo (2.0.5)
89
89
  concurrent-ruby (~> 1.0)
90
90
  unicode-display_width (2.3.0)
91
91
 
92
92
  PLATFORMS
93
93
  arm64-darwin-20
94
+ arm64-darwin-21
94
95
  x86_64-linux
95
96
 
96
97
  DEPENDENCIES
@@ -99,8 +100,8 @@ DEPENDENCIES
99
100
  ffaker
100
101
  omniauth
101
102
  pry
102
- rake (~> 13.0)
103
- rspec (~> 3.0)
103
+ rake
104
+ rspec
104
105
  rubocop
105
106
  rubocop-performance
106
107
  rubocop-rspec
@@ -2,36 +2,11 @@
2
2
 
3
3
  module CurrentSession
4
4
  #
5
- # Base class for providing auth methods
5
+ # auth method
6
6
  #
7
7
  class AuthMethod
8
- class_attribute :user_class
9
-
10
- # only exist users
11
- class FindBy < self
12
- def call(&block)
13
- find_by_auth.try(&block)
14
- end
15
- end
16
-
17
- # admit new users
18
- class FindOrCreateBy < self
19
- def call(&block)
20
- find_or_create_by_auth.try(&block)
21
- end
22
- end
23
-
24
- def self.new_auth_class(auth_methods_module)
25
- new_auth_class =
26
- if auth_methods_module.method_defined?(:find_by_auth)
27
- Class.new(CurrentSession::AuthMethod::FindBy) { include auth_methods_module }
28
- elsif auth_methods_module.method_defined?(:find_or_create_by_auth)
29
- Class.new(CurrentSession::AuthMethod::FindOrCreateBy) { include auth_methods_module }
30
- else
31
- fail NotImplementedError, "You must implement find_by_auth or find_or_create_by_auth"
32
- end
33
- new_auth_class.user_class = user_class
34
- new_auth_class
8
+ def self.new_class(methods)
9
+ Class.new(self) { include methods }
35
10
  end
36
11
 
37
12
  def initialize(request)
@@ -42,5 +17,12 @@ module CurrentSession
42
17
  def auth
43
18
  request.env["omniauth.auth"]
44
19
  end
20
+
21
+ #
22
+ # @return User
23
+ #
24
+ def connect
25
+ fail NotImplementedError, "You must implement #{self.class}##{__method__}"
26
+ end
45
27
  end
46
28
  end
@@ -6,48 +6,7 @@ module CurrentSession
6
6
  #
7
7
  class Base < ActiveSupport::CurrentAttributes
8
8
  include CurrentSession::Interface
9
+ include CurrentSession::Dsl
9
10
  attribute :current_user
10
-
11
- class << self
12
- attr_accessor :session_token_class
13
- attr_reader :user_class
14
-
15
- def user_class=(user_class)
16
- @user_class = user_class
17
- @auth_class.user_class = user_class if @auth_class
18
- end
19
-
20
- def current_time(_)
21
- Time.current
22
- end
23
-
24
- def session_methods=(session_methods)
25
- @session_methods = session_methods
26
- @session_class = CurrentSession::SessionMethod.new_session_class(session_methods)
27
- end
28
-
29
- def session_methods(&block)
30
- if block
31
- @session_methods = Module.new(&block)
32
- @session_class = CurrentSession::SessionMethod.new_session_class(session_methods)
33
- else
34
- @session_methods
35
- end
36
- end
37
-
38
- def auth_methods=(auth_methods_module)
39
- @auth_class = CurrentSession::AuthMethod.new_auth_class(auth_methods_module)
40
- end
41
-
42
- def auth_methods(&block)
43
- if block
44
- @auth_methods = Module.new(&block)
45
- @auth_class = CurrentSession::AuthMethod.new_auth_class(auth_methods)
46
- @auth_class.user_class = user_class
47
- else
48
- @auth_methods
49
- end
50
- end
51
- end
52
11
  end
53
12
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # configuration dsl
6
+ #
7
+ module Dsl
8
+ extend ActiveSupport::Concern
9
+ class_methods do
10
+ def session_methods=(session_method)
11
+ @session_methods = CurrentSession::SessionMethod.new_class(session_method)
12
+ end
13
+
14
+ def session_methods(&block)
15
+ if block
16
+ @session_methods = CurrentSession::SessionMethod.new_class(Module.new(&block))
17
+ else
18
+ @session_methods
19
+ end
20
+ end
21
+
22
+ def auth_methods=(auth_method)
23
+ @auth_methods = CurrentSession::AuthMethod.new_class(auth_method)
24
+ end
25
+
26
+ def auth_methods(&block)
27
+ if block
28
+ @auth_methods = CurrentSession::AuthMethod.new_class(Module.new(&block))
29
+ else
30
+ @auth_methods
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -12,32 +12,18 @@ module CurrentSession
12
12
  end
13
13
 
14
14
  def update(request)
15
- session_repository(request).find_by_token { |user| self.current_user = user }
15
+ self.current_user = @session_methods.new(request).find
16
16
  self
17
17
  end
18
18
 
19
19
  def create(request)
20
- auth = @auth_class.new(request)
21
- auth.call do |user|
22
- auth.update(user)
23
- session_repository(request).update_session_token(user)
24
- end
20
+ self.current_user = @auth_methods.new(request).connect
21
+ @session_methods.new(request).create(current_user)
22
+ self
25
23
  end
26
24
 
27
25
  def destroy(request)
28
- self.current_user = nil
29
- session_repository(request).delete_session_token
30
- end
31
-
32
- private
33
-
34
- def session_repository(request)
35
- @session_class.new(
36
- request: request,
37
- user_class: user_class,
38
- session_token_class: session_token_class,
39
- current_time: current_time(request)
40
- )
26
+ @session_methods.new(request).destroy
41
27
  end
42
28
  end
43
29
  end
@@ -2,49 +2,36 @@
2
2
 
3
3
  module CurrentSession
4
4
  #
5
- # Base class for processing to get session_token from request.session
5
+ # session Method
6
6
  #
7
7
  class SessionMethod
8
- def self.new_session_class(session_methods)
9
- Class.new(CurrentSession::SessionMethod) { include session_methods }
10
- end
11
-
12
- def initialize(current_time:, request:, user_class:, session_token_class:)
13
- @current_time = current_time
14
- @request = request
15
- @user_class = user_class
16
- @session_token_class = session_token_class
17
- end
18
- attr_reader :current_time, :request, :user_class, :session_token_class
19
-
20
- def find_by_token(&block)
21
- try_session_token { find(&block) }
22
- end
23
-
24
- def delete_session_token
25
- try_session_token do
26
- request.session.delete(key)
27
- destroy
8
+ def self.new_class(methods)
9
+ Class.new(self) do
10
+ include methods
28
11
  end
29
12
  end
30
13
 
31
- def update_session_token(user)
32
- create(user) { |value| request.session[key] = value }
14
+ def self.define(&block)
15
+ Module.new do
16
+ define_method(:find, &block)
17
+ define_method(:create) {}
18
+ define_method(:destroy) {}
19
+ end
33
20
  end
34
21
 
35
- protected
36
-
37
- def try_session_token
38
- session_token.presence.try do
39
- yield self
40
- end
22
+ def initialize(request)
23
+ @request = request
41
24
  end
25
+ attr_reader :request
42
26
 
27
+ #
28
+ # @return User
29
+ #
43
30
  def find
44
31
  fail NotImplementedError, "You must implement #{self.class}##{__method__}"
45
32
  end
46
33
 
47
- def create(user, &block)
34
+ def create(user)
48
35
  fail NotImplementedError, "You must implement #{self.class}##{__method__}"
49
36
  end
50
37
 
@@ -52,16 +39,8 @@ module CurrentSession
52
39
  fail NotImplementedError, "You must implement #{self.class}##{__method__}"
53
40
  end
54
41
 
55
- def key
56
- @key ||= CurrentSession.key(user_class)
57
- end
58
-
59
- def session_token
60
- @session_token ||= request.session[key]
61
- end
62
-
63
42
  def new_session_token
64
- SecureRandom.urlsafe_base64(64)
43
+ SecureRandom.urlsafe_base64(128)
65
44
  end
66
45
  end
67
46
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CurrentSession
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
@@ -9,12 +9,12 @@ require "active_support"
9
9
  module CurrentSession
10
10
  extend ActiveSupport::Autoload
11
11
  autoload :Base
12
+ autoload :Dsl
12
13
  autoload :Interface
13
14
  autoload :AuthMethod
14
15
  autoload :SessionMethod
15
- autoload :SessionMethods
16
16
 
17
- def self.key(user_class)
18
- "session_#{user_class.name.underscore.parameterize(separator: "_")}_key"
17
+ def self.key(namespace)
18
+ "session_#{namespace.name.underscore.parameterize(separator: "_")}_key"
19
19
  end
20
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: current_session
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masa (Aileron inc)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-23 00:00:00.000000000 Z
11
+ date: 2022-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -148,12 +148,9 @@ files:
148
148
  - lib/current_session.rb
149
149
  - lib/current_session/auth_method.rb
150
150
  - lib/current_session/base.rb
151
+ - lib/current_session/dsl.rb
151
152
  - lib/current_session/interface.rb
152
153
  - lib/current_session/session_method.rb
153
- - lib/current_session/session_methods.rb
154
- - lib/current_session/session_methods/active_record_session.rb
155
- - lib/current_session/session_methods/env_session.rb
156
- - lib/current_session/session_methods/uid_session.rb
157
154
  - lib/current_session/version.rb
158
155
  homepage: https://github.com/aileron-inc/current_session
159
156
  licenses:
@@ -175,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
172
  - !ruby/object:Gem::Version
176
173
  version: '0'
177
174
  requirements: []
178
- rubygems_version: 3.2.22
175
+ rubygems_version: 3.3.26
179
176
  signing_key:
180
177
  specification_version: 4
181
178
  summary: Library for current_user using ActiveSupport::CurrentAttributes
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CurrentSession
4
- module SessionMethods
5
- #
6
- # Implementation of session tokens for DB management
7
- #
8
- module ActiveRecordSession
9
- def find
10
- session_token_class.find_by(value: session_token).try do |record|
11
- update(record)
12
- yield record.user
13
- end
14
- end
15
-
16
- def create(user)
17
- session_token_class.create(user_id: user.id, value: new_session_token) do |record|
18
- update(record)
19
- yield record.value
20
- end
21
- end
22
-
23
- def destroy
24
- session_token_class.find_by(value: session_token)&.destroy
25
- end
26
-
27
- private
28
-
29
- def update(token)
30
- token.update(
31
- last_request_at: current_time,
32
- last_request_ip: request.remote_ip,
33
- last_request_user_agent: request.user_agent
34
- )
35
- end
36
- end
37
- end
38
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CurrentSession
4
- module SessionMethods
5
- #
6
- # repository to return the user specified by the environment variable
7
- #
8
- module EnvSession
9
- # rubocop:disable Metrics/MethodLength
10
- def self.build(current_user_id)
11
- Module.new do
12
- define_method(:current_user) { user_class.find(current_user_id) }
13
-
14
- def try_session_token
15
- yield self
16
- end
17
-
18
- def find
19
- yield current_user
20
- end
21
-
22
- def create(_)
23
- end
24
-
25
- def destroy
26
- end
27
- end
28
- end
29
- # rubocop:enable Metrics/MethodLength
30
- end
31
- end
32
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CurrentSession
4
- module SessionMethods
5
- #
6
- # Implementation of using UIDs as session tokens
7
- #
8
- module UidSession
9
- def find(&block)
10
- user_class.find_by(uid: session_token).try(&block)
11
- end
12
-
13
- def create(user)
14
- yield user.uid
15
- end
16
-
17
- def destroy
18
- end
19
- end
20
- end
21
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Reference implementation of session_methods
5
- #
6
- module CurrentSession
7
- module SessionMethods
8
- extend ActiveSupport::Autoload
9
- autoload :UidSession
10
- autoload :EnvSession
11
- autoload :ActiveRecordSession
12
- end
13
- end