current_session 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile +2 -2
- data/Gemfile.lock +27 -26
- data/lib/current_session/auth_method.rb +10 -28
- data/lib/current_session/base.rb +1 -42
- data/lib/current_session/dsl.rb +35 -0
- data/lib/current_session/interface.rb +5 -19
- data/lib/current_session/session_method.rb +18 -39
- data/lib/current_session/version.rb +1 -1
- data/lib/current_session.rb +3 -3
- metadata +4 -7
- data/lib/current_session/session_methods/active_record_session.rb +0 -38
- data/lib/current_session/session_methods/env_session.rb +0 -32
- data/lib/current_session/session_methods/uid_session.rb +0 -21
- data/lib/current_session/session_methods.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 505b1421a5df8b13c947388a55f0af6224ebae0d7e61c552292896e53a7ffae8
|
4
|
+
data.tar.gz: 1d047d2c47aff4e1665a743d7d38cb5b964493525d0c3f611fcac36f22bc2ec9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fde9c27a98d38fd4bcc0a55c82921cfd19c9525538a9a618d91a575c287fe8611bac043f455c25d62e6b2703d03c962fa2b9e4aaa50f9a3a2b91eb087e41fbc6
|
7
|
+
data.tar.gz: c6fb18a168a5cdaf575d63ddcac4ef35bf9ba3943a15a76615e77301e63c2953117b7231e501f1e89381cc7850e19251ae6f32c1b691b3d5c6df0a6d93f97e8f
|
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.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.
|
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.
|
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.
|
43
|
-
rack-protection (3.0.
|
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.
|
47
|
+
regexp_parser (2.6.1)
|
48
48
|
rexml (3.2.5)
|
49
|
-
rspec (3.
|
50
|
-
rspec-core (~> 3.
|
51
|
-
rspec-expectations (~> 3.
|
52
|
-
rspec-mocks (~> 3.
|
53
|
-
rspec-core (3.
|
54
|
-
rspec-support (~> 3.
|
55
|
-
rspec-expectations (3.
|
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.
|
58
|
-
rspec-mocks (3.
|
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.
|
61
|
-
rspec-support (3.
|
62
|
-
rubocop (1.
|
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.
|
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.
|
72
|
+
rubocop-ast (1.24.0)
|
73
73
|
parser (>= 3.1.1.0)
|
74
|
-
rubocop-performance (1.15.
|
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.
|
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.
|
87
|
-
sqlite3 (1.5.
|
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
|
103
|
-
rspec
|
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
|
-
#
|
5
|
+
# auth method
|
6
6
|
#
|
7
7
|
class AuthMethod
|
8
|
-
|
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
|
data/lib/current_session/base.rb
CHANGED
@@ -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
|
-
|
15
|
+
self.current_user = @session_methods.new(request).find
|
16
16
|
self
|
17
17
|
end
|
18
18
|
|
19
19
|
def create(request)
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
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
|
-
#
|
5
|
+
# session Method
|
6
6
|
#
|
7
7
|
class SessionMethod
|
8
|
-
def self.
|
9
|
-
Class.new(
|
10
|
-
|
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
|
32
|
-
|
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
|
-
|
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
|
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(
|
43
|
+
SecureRandom.urlsafe_base64(128)
|
65
44
|
end
|
66
45
|
end
|
67
46
|
end
|
data/lib/current_session.rb
CHANGED
@@ -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(
|
18
|
-
"session_#{
|
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.
|
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-
|
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.
|
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
|