current_session 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +16 -7
- data/lib/current_session/auth_method.rb +46 -0
- data/lib/current_session/base.rb +8 -10
- data/lib/current_session/interface.rb +4 -9
- data/lib/current_session/session_method.rb +67 -0
- data/lib/current_session/session_methods/active_record_session.rb +8 -5
- data/lib/current_session/session_methods/env_session.rb +1 -1
- data/lib/current_session/session_methods/uid_session.rb +1 -1
- data/lib/current_session/version.rb +1 -1
- data/lib/current_session.rb +2 -3
- metadata +4 -5
- data/lib/current_session/auth.rb +0 -19
- data/lib/current_session/raise_not_implemented_error.rb +0 -36
- data/lib/current_session/repository.rb +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe0094c715e9d1a1152a795e0edd0583434cabcc1d955a8fc5c6e2c2202c0e85
|
4
|
+
data.tar.gz: 0d7af31d9cb4f1762c15e6b1a288b2c4c6226dcb153e8709b6e2321ee7007585
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41197858ae7dcd61d71a593332ca369ed9526fa0711e3c2b992d2eadaac55df763f800e105d01a67e254043c67ee3673d169be6e5ee889a51797acdfc8b9d962
|
7
|
+
data.tar.gz: 6b907dce8ee28afb0a6988cf5384912a732bb56f7ad8f85543cadd02b108c83f15108a6b6ef0593e9c6a86861114ba6aac0e0b053ce2d9cda3bae61469f3acd5
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
current_session (0.1.
|
4
|
+
current_session (0.1.6)
|
5
5
|
activesupport (>= 6.0.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -40,7 +40,7 @@ GEM
|
|
40
40
|
coderay (~> 1.1)
|
41
41
|
method_source (~> 1.0)
|
42
42
|
rack (3.0.0)
|
43
|
-
rack-protection (3.0.
|
43
|
+
rack-protection (3.0.2)
|
44
44
|
rack
|
45
45
|
rainbow (3.1.1)
|
46
46
|
rake (13.0.6)
|
@@ -59,18 +59,23 @@ GEM
|
|
59
59
|
diff-lcs (>= 1.2.0, < 2.0)
|
60
60
|
rspec-support (~> 3.11.0)
|
61
61
|
rspec-support (3.11.1)
|
62
|
-
rubocop (1.
|
62
|
+
rubocop (1.37.0)
|
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.
|
69
|
+
rubocop-ast (>= 1.22.0, < 2.0)
|
70
70
|
ruby-progressbar (~> 1.7)
|
71
71
|
unicode-display_width (>= 1.4.0, < 3.0)
|
72
|
-
rubocop-ast (1.
|
72
|
+
rubocop-ast (1.23.0)
|
73
73
|
parser (>= 3.1.1.0)
|
74
|
+
rubocop-performance (1.15.0)
|
75
|
+
rubocop (>= 1.7.0, < 2.0)
|
76
|
+
rubocop-ast (>= 0.4.0)
|
77
|
+
rubocop-rspec (2.13.2)
|
78
|
+
rubocop (~> 1.33)
|
74
79
|
ruby-progressbar (1.11.0)
|
75
80
|
simplecov (0.21.2)
|
76
81
|
docile (~> 1.1)
|
@@ -78,13 +83,15 @@ GEM
|
|
78
83
|
simplecov_json_formatter (~> 0.1)
|
79
84
|
simplecov-html (0.12.3)
|
80
85
|
simplecov_json_formatter (0.1.4)
|
81
|
-
sqlite3 (1.5.
|
86
|
+
sqlite3 (1.5.3-arm64-darwin)
|
87
|
+
sqlite3 (1.5.3-x86_64-linux)
|
82
88
|
tzinfo (2.0.5)
|
83
89
|
concurrent-ruby (~> 1.0)
|
84
90
|
unicode-display_width (2.3.0)
|
85
91
|
|
86
92
|
PLATFORMS
|
87
93
|
arm64-darwin-20
|
94
|
+
x86_64-linux
|
88
95
|
|
89
96
|
DEPENDENCIES
|
90
97
|
activerecord
|
@@ -94,7 +101,9 @@ DEPENDENCIES
|
|
94
101
|
pry
|
95
102
|
rake (~> 13.0)
|
96
103
|
rspec (~> 3.0)
|
97
|
-
rubocop
|
104
|
+
rubocop
|
105
|
+
rubocop-performance
|
106
|
+
rubocop-rspec
|
98
107
|
simplecov
|
99
108
|
sqlite3
|
100
109
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CurrentSession
|
4
|
+
#
|
5
|
+
# Base class for providing auth methods
|
6
|
+
#
|
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
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(request)
|
38
|
+
@request = request
|
39
|
+
end
|
40
|
+
attr_reader :request
|
41
|
+
|
42
|
+
def auth
|
43
|
+
request.env["omniauth.auth"]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/current_session/base.rb
CHANGED
@@ -6,7 +6,6 @@ module CurrentSession
|
|
6
6
|
#
|
7
7
|
class Base < ActiveSupport::CurrentAttributes
|
8
8
|
include CurrentSession::Interface
|
9
|
-
include CurrentSession::RaiseNotImplementedError
|
10
9
|
attribute :current_user
|
11
10
|
|
12
11
|
class << self
|
@@ -22,30 +21,29 @@ module CurrentSession
|
|
22
21
|
Time.current
|
23
22
|
end
|
24
23
|
|
25
|
-
def session_methods=(
|
26
|
-
@
|
24
|
+
def session_methods=(session_methods)
|
25
|
+
@session_methods = session_methods
|
26
|
+
@session_class = CurrentSession::SessionMethod.new_session_class(session_methods)
|
27
27
|
end
|
28
28
|
|
29
29
|
def session_methods(&block)
|
30
30
|
if block
|
31
|
-
session_methods = Module.new(&block)
|
32
|
-
@
|
33
|
-
@session_methods = session_methods
|
31
|
+
@session_methods = Module.new(&block)
|
32
|
+
@session_class = CurrentSession::SessionMethod.new_session_class(session_methods)
|
34
33
|
else
|
35
34
|
@session_methods
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
39
38
|
def auth_methods=(auth_methods_module)
|
40
|
-
@auth_class =
|
39
|
+
@auth_class = CurrentSession::AuthMethod.new_auth_class(auth_methods_module)
|
41
40
|
end
|
42
41
|
|
43
42
|
def auth_methods(&block)
|
44
43
|
if block
|
45
|
-
auth_methods = Module.new(&block)
|
46
|
-
@auth_class =
|
44
|
+
@auth_methods = Module.new(&block)
|
45
|
+
@auth_class = CurrentSession::AuthMethod.new_auth_class(auth_methods)
|
47
46
|
@auth_class.user_class = user_class
|
48
|
-
@auth_methods = auth_methods
|
49
47
|
else
|
50
48
|
@auth_methods
|
51
49
|
end
|
@@ -12,15 +12,13 @@ module CurrentSession
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def update(request)
|
15
|
-
session_repository(request).
|
16
|
-
repository.find { |user| self.current_user = user }
|
17
|
-
end
|
15
|
+
session_repository(request).find_by_token { |user| self.current_user = user }
|
18
16
|
self
|
19
17
|
end
|
20
18
|
|
21
19
|
def create(request)
|
22
20
|
auth = @auth_class.new(request)
|
23
|
-
auth.
|
21
|
+
auth.call do |user|
|
24
22
|
auth.update(user)
|
25
23
|
session_repository(request).update_session_token(user)
|
26
24
|
end
|
@@ -28,16 +26,13 @@ module CurrentSession
|
|
28
26
|
|
29
27
|
def destroy(request)
|
30
28
|
self.current_user = nil
|
31
|
-
session_repository(request).
|
32
|
-
repository.destroy
|
33
|
-
repository.delete_session_token
|
34
|
-
end
|
29
|
+
session_repository(request).delete_session_token
|
35
30
|
end
|
36
31
|
|
37
32
|
private
|
38
33
|
|
39
34
|
def session_repository(request)
|
40
|
-
@
|
35
|
+
@session_class.new(
|
41
36
|
request: request,
|
42
37
|
user_class: user_class,
|
43
38
|
session_token_class: session_token_class,
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CurrentSession
|
4
|
+
#
|
5
|
+
# Base class for processing to get session_token from request.session
|
6
|
+
#
|
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
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def update_session_token(user)
|
32
|
+
create(user) { |value| request.session[key] = value }
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def try_session_token
|
38
|
+
session_token.presence.try do
|
39
|
+
yield self
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def find
|
44
|
+
fail NotImplementedError, "You must implement #{self.class}##{__method__}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def create(user, &block)
|
48
|
+
fail NotImplementedError, "You must implement #{self.class}##{__method__}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def destroy
|
52
|
+
fail NotImplementedError, "You must implement #{self.class}##{__method__}"
|
53
|
+
end
|
54
|
+
|
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
|
+
def new_session_token
|
64
|
+
SecureRandom.urlsafe_base64(64)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -16,9 +16,16 @@ module CurrentSession
|
|
16
16
|
def create(user)
|
17
17
|
session_token_class.create(user_id: user.id, value: new_session_token) do |record|
|
18
18
|
update(record)
|
19
|
-
|
19
|
+
yield record.value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def destroy
|
24
|
+
session_token_class.find_by(value: session_token)&.destroy
|
20
25
|
end
|
21
26
|
|
27
|
+
private
|
28
|
+
|
22
29
|
def update(token)
|
23
30
|
token.update(
|
24
31
|
last_request_at: current_time,
|
@@ -26,10 +33,6 @@ module CurrentSession
|
|
26
33
|
last_request_user_agent: request.user_agent
|
27
34
|
)
|
28
35
|
end
|
29
|
-
|
30
|
-
def destroy
|
31
|
-
session_token_class.find_by(value: session_token)&.destroy
|
32
|
-
end
|
33
36
|
end
|
34
37
|
end
|
35
38
|
end
|
data/lib/current_session.rb
CHANGED
@@ -10,9 +10,8 @@ module CurrentSession
|
|
10
10
|
extend ActiveSupport::Autoload
|
11
11
|
autoload :Base
|
12
12
|
autoload :Interface
|
13
|
-
autoload :
|
14
|
-
autoload :
|
15
|
-
autoload :RaiseNotImplementedError
|
13
|
+
autoload :AuthMethod
|
14
|
+
autoload :SessionMethod
|
16
15
|
autoload :SessionMethods
|
17
16
|
|
18
17
|
def self.key(user_class)
|
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.
|
4
|
+
version: 0.1.6
|
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-
|
11
|
+
date: 2022-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -146,11 +146,10 @@ files:
|
|
146
146
|
- bin/setup
|
147
147
|
- current_session.gemspec
|
148
148
|
- lib/current_session.rb
|
149
|
-
- lib/current_session/
|
149
|
+
- lib/current_session/auth_method.rb
|
150
150
|
- lib/current_session/base.rb
|
151
151
|
- lib/current_session/interface.rb
|
152
|
-
- lib/current_session/
|
153
|
-
- lib/current_session/repository.rb
|
152
|
+
- lib/current_session/session_method.rb
|
154
153
|
- lib/current_session/session_methods.rb
|
155
154
|
- lib/current_session/session_methods/active_record_session.rb
|
156
155
|
- lib/current_session/session_methods/env_session.rb
|
data/lib/current_session/auth.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CurrentSession
|
4
|
-
#
|
5
|
-
# Base class for providing auth methods
|
6
|
-
#
|
7
|
-
class Auth
|
8
|
-
class_attribute :user_class
|
9
|
-
|
10
|
-
def initialize(request)
|
11
|
-
@request = request
|
12
|
-
end
|
13
|
-
attr_reader :request
|
14
|
-
|
15
|
-
def auth
|
16
|
-
request.env["omniauth.auth"]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CurrentSession
|
4
|
-
#
|
5
|
-
# Class for verifying whether it is implemented correctly
|
6
|
-
#
|
7
|
-
module RaiseNotImplementedError
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
class_methods do
|
10
|
-
def raise_not_implemented_error
|
11
|
-
raise_not_implemented_error_for_repository
|
12
|
-
raise_not_implemented_error_for_auth
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
# rubocop:disable Layout/LineLength
|
18
|
-
def raise_not_implemented_error_for_repository
|
19
|
-
raise NotImplementedError, "You must setting self.session_repository_class= or session_methods" unless @session_repository_class
|
20
|
-
raise NotImplementedError, "You must implement #{@session_repository_class}#find" unless @session_repository_class.method_defined? :find
|
21
|
-
raise NotImplementedError, "You must implement #{@session_repository_class}#destroy" unless @session_repository_class.method_defined? :destroy
|
22
|
-
raise NotImplementedError, "You must implement #{@session_repository_class}#destroy" unless @session_repository_class.method_defined? :destroy
|
23
|
-
raise NotImplementedError, "You must implement #{@session_repository_class}#create" unless @session_repository_class.method_defined? :create
|
24
|
-
end
|
25
|
-
# rubocop:enable Layout/LineLength
|
26
|
-
|
27
|
-
# rubocop:disable Layout/LineLength
|
28
|
-
def raise_not_implemented_error_for_auth
|
29
|
-
raise NotImplementedError, "You must setting self.auth_class= or auth_methods" unless @auth_class
|
30
|
-
raise NotImplementedError, "You must implement #{@auth_class}#find_or_create_by_auth" unless @auth_class.method_defined? :find_or_create_by_auth
|
31
|
-
raise NotImplementedError, "You must implement #{@auth_class}#update" unless @auth_class.method_defined? :update
|
32
|
-
end
|
33
|
-
# rubocop:enable Layout/LineLength
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CurrentSession
|
4
|
-
#
|
5
|
-
# Base class for processing to get session_token from request.session
|
6
|
-
#
|
7
|
-
class Repository
|
8
|
-
def initialize(current_time:, request:, user_class:, session_token_class:)
|
9
|
-
@current_time = current_time
|
10
|
-
@request = request
|
11
|
-
@user_class = user_class
|
12
|
-
@session_token_class = session_token_class
|
13
|
-
end
|
14
|
-
attr_reader :current_time, :request, :user_class, :session_token_class
|
15
|
-
|
16
|
-
def key
|
17
|
-
@key ||= CurrentSession.key(user_class)
|
18
|
-
end
|
19
|
-
|
20
|
-
def session_token
|
21
|
-
@session_token ||= request.session[key]
|
22
|
-
end
|
23
|
-
|
24
|
-
def new_session_token
|
25
|
-
SecureRandom.urlsafe_base64(64)
|
26
|
-
end
|
27
|
-
|
28
|
-
def delete_session_token
|
29
|
-
request.session.delete(key)
|
30
|
-
end
|
31
|
-
|
32
|
-
def update_session_token(user)
|
33
|
-
request.session[key] = create(user)
|
34
|
-
end
|
35
|
-
|
36
|
-
def try
|
37
|
-
session_token.presence.try do |_|
|
38
|
-
yield self
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|