brevio-session 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +29 -29
- data/brevio-session.gemspec +2 -2
- data/lib/brevio/session/testing.rb +16 -0
- data/lib/brevio/session/with_session_login.rb +92 -0
- data/lib/brevio/session.rb +9 -95
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7047e1a58ab4d8ece1f39bf73cc37ef0a4c3165da08ab14aa5a8faeb1390f3a5
|
4
|
+
data.tar.gz: 7f26b113da04eeb3bdd80f32335e34463f8c2adc02dd82ffbf8d7d20fd4fe8fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d2e276acc65d46467c83645a708083cad3baf72a462db39319ccdb34bddb9b31686238a9344bcab4a721a0a77aed318d64eb7dec49978bf9c00557ce95af842
|
7
|
+
data.tar.gz: 46a43dae2ab5e18601dabec3b68952141610d31b1788780c8c4726eabd7e6f39bf14a8b61b2c5c2bf871a39a9414f6689e33b57385a05611a3e2f6a29eac5ad1
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
brevio-session (0.1)
|
4
|
+
brevio-session (0.2.1)
|
5
5
|
actionpack (~> 7.0.3)
|
6
6
|
redis (~> 5.0)
|
7
7
|
zeitwerk (~> 2.6.7)
|
@@ -9,63 +9,63 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
actionpack (7.0.
|
13
|
-
actionview (= 7.0.
|
14
|
-
activesupport (= 7.0.
|
15
|
-
rack (~> 2.0, >= 2.2.
|
12
|
+
actionpack (7.0.8)
|
13
|
+
actionview (= 7.0.8)
|
14
|
+
activesupport (= 7.0.8)
|
15
|
+
rack (~> 2.0, >= 2.2.4)
|
16
16
|
rack-test (>= 0.6.3)
|
17
17
|
rails-dom-testing (~> 2.0)
|
18
18
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
19
|
-
actionview (7.0.
|
20
|
-
activesupport (= 7.0.
|
19
|
+
actionview (7.0.8)
|
20
|
+
activesupport (= 7.0.8)
|
21
21
|
builder (~> 3.1)
|
22
22
|
erubi (~> 1.4)
|
23
23
|
rails-dom-testing (~> 2.0)
|
24
24
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
25
|
-
activesupport (7.0.
|
25
|
+
activesupport (7.0.8)
|
26
26
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
27
27
|
i18n (>= 1.6, < 2)
|
28
28
|
minitest (>= 5.1)
|
29
29
|
tzinfo (~> 2.0)
|
30
30
|
ast (2.4.2)
|
31
31
|
builder (3.2.4)
|
32
|
-
concurrent-ruby (1.
|
33
|
-
connection_pool (2.4.
|
32
|
+
concurrent-ruby (1.2.2)
|
33
|
+
connection_pool (2.4.1)
|
34
34
|
crass (1.0.6)
|
35
35
|
debug (1.5.0)
|
36
36
|
irb (>= 1.3.6)
|
37
37
|
reline (>= 0.2.7)
|
38
|
-
erubi (1.
|
39
|
-
i18n (1.
|
38
|
+
erubi (1.12.0)
|
39
|
+
i18n (1.14.1)
|
40
40
|
concurrent-ruby (~> 1.0)
|
41
41
|
io-console (0.6.0)
|
42
42
|
irb (1.6.1)
|
43
43
|
reline (>= 0.3.0)
|
44
44
|
json (2.6.2)
|
45
|
-
loofah (2.
|
45
|
+
loofah (2.21.3)
|
46
46
|
crass (~> 1.0.2)
|
47
|
-
nokogiri (>= 1.
|
48
|
-
|
49
|
-
|
50
|
-
nokogiri (1.13.9)
|
51
|
-
mini_portile2 (~> 2.8.0)
|
47
|
+
nokogiri (>= 1.12.0)
|
48
|
+
minitest (5.20.0)
|
49
|
+
nokogiri (1.15.4-arm64-darwin)
|
52
50
|
racc (~> 1.4)
|
53
51
|
parallel (1.22.1)
|
54
52
|
parser (3.1.2.1)
|
55
53
|
ast (~> 2.4.1)
|
56
|
-
racc (1.
|
57
|
-
rack (2.2.
|
58
|
-
rack-test (2.0
|
54
|
+
racc (1.7.1)
|
55
|
+
rack (2.2.8)
|
56
|
+
rack-test (2.1.0)
|
59
57
|
rack (>= 1.3)
|
60
|
-
rails-dom-testing (2.0
|
61
|
-
activesupport (>=
|
58
|
+
rails-dom-testing (2.2.0)
|
59
|
+
activesupport (>= 5.0.0)
|
60
|
+
minitest
|
62
61
|
nokogiri (>= 1.6)
|
63
|
-
rails-html-sanitizer (1.
|
64
|
-
loofah (~> 2.
|
62
|
+
rails-html-sanitizer (1.6.0)
|
63
|
+
loofah (~> 2.21)
|
64
|
+
nokogiri (~> 1.14)
|
65
65
|
rainbow (3.1.1)
|
66
|
-
redis (5.0.
|
66
|
+
redis (5.0.7)
|
67
67
|
redis-client (>= 0.9.0)
|
68
|
-
redis-client (0.
|
68
|
+
redis-client (0.17.0)
|
69
69
|
connection_pool
|
70
70
|
regexp_parser (2.5.0)
|
71
71
|
reline (0.3.2)
|
@@ -84,10 +84,10 @@ GEM
|
|
84
84
|
rubocop-ast (1.21.0)
|
85
85
|
parser (>= 3.1.1.0)
|
86
86
|
ruby-progressbar (1.11.0)
|
87
|
-
tzinfo (2.0.
|
87
|
+
tzinfo (2.0.6)
|
88
88
|
concurrent-ruby (~> 1.0)
|
89
89
|
unicode-display_width (2.2.0)
|
90
|
-
zeitwerk (2.6.
|
90
|
+
zeitwerk (2.6.12)
|
91
91
|
|
92
92
|
PLATFORMS
|
93
93
|
arm64-darwin-21
|
data/brevio-session.gemspec
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = 'brevio-session'
|
5
|
-
spec.version = '0.
|
5
|
+
spec.version = '0.2.1'
|
6
6
|
spec.authors = ['Brevio AS']
|
7
7
|
spec.email = ['support@brevio.com']
|
8
8
|
spec.files = `git ls-files -z`.split("\0")
|
9
9
|
|
10
10
|
spec.homepage = 'https://github.com/team-brevio/brevio-session'
|
11
11
|
spec.summary = 'Brevio session wrapper'
|
12
|
-
spec.required_ruby_version = '3.2'
|
12
|
+
spec.required_ruby_version = '~> 3.2'
|
13
13
|
spec.description = 'Wrapper around HTTP session set by Brevio ID'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
@@ -26,6 +26,22 @@ module Brevio::Session
|
|
26
26
|
set_cookie(redis_key)
|
27
27
|
end
|
28
28
|
|
29
|
+
def brevio_login_new_user(user_brevio_id, audit_company:)
|
30
|
+
redis_key = SecureRandom.hex(6)
|
31
|
+
|
32
|
+
session_hash = { user_id: user_brevio_id,
|
33
|
+
audit_company_id: audit_company.brevio_id,
|
34
|
+
user_stamp: Time.current.yesterday }
|
35
|
+
|
36
|
+
Testing.config.logger.info "setting Brevio session to #{session_hash}"
|
37
|
+
|
38
|
+
Testing.config.gem_config.redis.set(
|
39
|
+
redis_key,
|
40
|
+
session_hash
|
41
|
+
)
|
42
|
+
set_cookie(redis_key)
|
43
|
+
end
|
44
|
+
|
29
45
|
def brevio_logout
|
30
46
|
cookies[Testing.config.gem_config.session_cookie] = nil
|
31
47
|
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module Brevio::Session
|
6
|
+
module WithSessionLogin
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
# By including this module into a Rails controller you will have access to the convenience methods
|
10
|
+
# fetch_brevio_session(!) and brevio_logged_in? which acts as a wrapper around the decrypting of
|
11
|
+
# predefined session cookies and sessions stored in Redis.
|
12
|
+
#
|
13
|
+
# We also require access to instance attributes like #request and the cookie jar, which are only
|
14
|
+
# available in the context of a controller.
|
15
|
+
#
|
16
|
+
included do
|
17
|
+
unless self <= ActionController::Base || instance_methods.include?(:session)
|
18
|
+
raise 'Included Brevio::Session outside of controller'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Fetches the Brevio session from Redis, based on the encrypted key stored in the client's cookie.
|
23
|
+
# Returns *nil* if there's no session present.
|
24
|
+
#
|
25
|
+
def fetch_brevio_session
|
26
|
+
brevio_session, redis_key = fetch_session
|
27
|
+
return nil if brevio_session.nil?
|
28
|
+
if brevio_config.debug?
|
29
|
+
brevio_config.logger.info "[brevio-session] Found session #{brevio_session.inspect}"
|
30
|
+
end
|
31
|
+
refresh_session(redis_key) unless params.transform_keys(&:underscore)[:no_session].present?
|
32
|
+
brevio_session
|
33
|
+
rescue RuntimeError => e
|
34
|
+
brevio_config.logger.error "[brevio-session] #{e.message}"
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
# Calls the above function, but raises an exception if the session isn't present.
|
39
|
+
def fetch_brevio_session!
|
40
|
+
brevio_session, redis_key = fetch_session
|
41
|
+
raise NilSession, 'Brevio session was nil' if brevio_session.nil?
|
42
|
+
refresh_session(redis_key) unless params.transform_keys(&:underscore)[:no_session].present?
|
43
|
+
brevio_session
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns a boolean flag indicating whether the current client has a Brevio session cookie set,
|
47
|
+
# and whether this cookie contains a user ID.
|
48
|
+
#
|
49
|
+
def brevio_logged_in?
|
50
|
+
brevio_session, = fetch_session
|
51
|
+
brevio_session&.dig(:user_id).present?
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def brevio_config
|
57
|
+
Config.config
|
58
|
+
end
|
59
|
+
|
60
|
+
def fetch_session
|
61
|
+
brevio_config.logger.info '[brevio-session] Fetching Brevio session'
|
62
|
+
cookie = request.cookie_jar[brevio_config.session_cookie]
|
63
|
+
redis_key = Cookies::Parse.perform!(cookie)
|
64
|
+
brevio_session = Redis.get(redis_key)
|
65
|
+
raise NilSession if brevio_session.nil?
|
66
|
+
[brevio_session.with_indifferent_access, redis_key]
|
67
|
+
rescue RuntimeError => e
|
68
|
+
brevio_config.logger.error "[brevio-session] --- 💣 Couldn't fetch Brevio session 💣 ---"
|
69
|
+
brevio_config.logger.error "[brevio-session] #{e.message}"
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
|
73
|
+
# Refreshes the Brevio session cookie, avoding its expiry. This is helpful to
|
74
|
+
# ensure the user stays logged in through interacting with the application.
|
75
|
+
#
|
76
|
+
def refresh_session(redis_key)
|
77
|
+
brevio_config.logger.info '[brevio-session] Refreshing Brevio session' if brevio_config.debug?
|
78
|
+
cookies[brevio_config.session_cookie] = {
|
79
|
+
value: request.cookie_jar[brevio_config.session_cookie],
|
80
|
+
domain: :all,
|
81
|
+
expires: brevio_config.session_expire,
|
82
|
+
httponly: true,
|
83
|
+
secure: true
|
84
|
+
}
|
85
|
+
Redis.expire(redis_key, brevio_config.session_expire)
|
86
|
+
rescue RuntimeError => e
|
87
|
+
brevio_config.logger.error "[brevio-session] --- 💣 Couldn't refresh Brevio session 💣 ---"
|
88
|
+
brevio_config.logger.error "[brevio-session] #{e.message}"
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
data/lib/brevio/session.rb
CHANGED
@@ -1,100 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'active_support/concern'
|
4
|
-
|
5
3
|
module Brevio::Session
|
6
|
-
extend
|
7
|
-
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
|
14
|
-
|
15
|
-
included do
|
16
|
-
unless self <= ActionController::Base || instance_methods.include?(:session)
|
17
|
-
raise 'Included Brevio::Session outside of controller'
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# Fetches the Brevio session from Redis, based on the encrypted key stored in the client's cookie.
|
22
|
-
# Returns *nil* if there's no session present.
|
23
|
-
#
|
24
|
-
def fetch_brevio_session
|
25
|
-
brevio_session, redis_key = fetch_session
|
26
|
-
return nil if brevio_session.nil?
|
27
|
-
if brevio_config.debug?
|
28
|
-
brevio_config.logger.info "[brevio-session] Found session #{brevio_session.inspect}"
|
29
|
-
end
|
30
|
-
refresh_session(redis_key) unless params.transform_keys(&:underscore)[:no_session].present?
|
31
|
-
brevio_session
|
32
|
-
rescue RuntimeError => e
|
33
|
-
brevio_config.logger.error "[brevio-session] #{e.message}"
|
34
|
-
nil
|
35
|
-
end
|
36
|
-
|
37
|
-
# Calls the above function, but raises an exception if the session isn't present.
|
38
|
-
def fetch_brevio_session!
|
39
|
-
brevio_session, redis_key = fetch_session
|
40
|
-
raise NilSession, 'Brevio session was nil' if brevio_session.nil?
|
41
|
-
refresh_session(redis_key) unless params.transform_keys(&:underscore)[:no_session].present?
|
42
|
-
brevio_session
|
43
|
-
end
|
44
|
-
|
45
|
-
# Returns a boolean flag indicating whether the current client has a Brevio session cookie set,
|
46
|
-
# and whether this cookie contains a user ID.
|
47
|
-
#
|
48
|
-
def brevio_logged_in?
|
49
|
-
fetch_session&.dig(:user_id).present?
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def brevio_config
|
55
|
-
Config.config
|
56
|
-
end
|
57
|
-
|
58
|
-
def fetch_session
|
59
|
-
brevio_config.logger.info '[brevio-session] Fetching Brevio session'
|
60
|
-
cookie = request.cookie_jar[brevio_config.session_cookie]
|
61
|
-
redis_key = Cookies::Parse.perform!(cookie)
|
62
|
-
brevio_session = Redis.get(redis_key)
|
63
|
-
raise NilSession if brevio_session.nil?
|
64
|
-
[brevio_session.with_indifferent_access, redis_key]
|
65
|
-
rescue RuntimeError => e
|
66
|
-
brevio_config.logger.error "[brevio-session] --- 💣 Couldn't fetch Brevio session 💣 ---"
|
67
|
-
brevio_config.logger.error "[brevio-session] #{e.message}"
|
68
|
-
nil
|
69
|
-
end
|
70
|
-
|
71
|
-
# Refreshes the Brevio session cookie, avoding its expiry. This is helpful to
|
72
|
-
# ensure the user stays logged in through interacting with the application.
|
73
|
-
#
|
74
|
-
def refresh_session(redis_key)
|
75
|
-
brevio_config.logger.info '[brevio-session] Refreshing Brevio session' if brevio_config.debug?
|
76
|
-
cookies[brevio_config.session_cookie] = {
|
77
|
-
value: request.cookie_jar[brevio_config.session_cookie],
|
78
|
-
domain: :all,
|
79
|
-
expires: brevio_config.session_expire,
|
80
|
-
httponly: true,
|
81
|
-
secure: true
|
82
|
-
}
|
83
|
-
Redis.expire(redis_key, brevio_config.session_expire)
|
84
|
-
rescue RuntimeError => e
|
85
|
-
brevio_config.logger.error "[brevio-session] --- 💣 Couldn't refresh Brevio session 💣 ---"
|
86
|
-
brevio_config.logger.error "[brevio-session] #{e.message}"
|
87
|
-
end
|
88
|
-
|
89
|
-
class << self
|
90
|
-
# Function used to fetch the current audit company session for a given Brevio ID.
|
91
|
-
# This session contains information about an audit company shared by any number of
|
92
|
-
# users. If the audit company has been updated in ID (e.g. changed logo or name), the
|
93
|
-
# database record of the local service (e.g. Confirm/Sign) will fetch the updates if
|
94
|
-
# the timestamp in this session is greater than their own local timestamps.
|
95
|
-
#
|
96
|
-
def audit_company_updated_at(brevio_id)
|
97
|
-
Config.config.redis.get("#{Config::Redis::Prefixes::AUDIT_COMPANY}:#{brevio_id}")
|
98
|
-
end
|
4
|
+
extend self
|
5
|
+
# Function used to fetch the current audit company session for a given Brevio ID.
|
6
|
+
# This session contains information about an audit company shared by any number of
|
7
|
+
# users. If the audit company has been updated in ID (e.g. changed logo or name), the
|
8
|
+
# database record of the local service (e.g. Confirm/Sign) will fetch the updates if
|
9
|
+
# the timestamp in this session is greater than their own local timestamps.
|
10
|
+
#
|
11
|
+
def audit_company_updated_at(brevio_id)
|
12
|
+
Config.config.redis.get("#{Config::Redis::Prefixes::AUDIT_COMPANY}:#{brevio_id}")
|
99
13
|
end
|
100
14
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brevio-session
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brevio AS
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- lib/brevio/session/cookies/parse.rb
|
122
122
|
- lib/brevio/session/redis.rb
|
123
123
|
- lib/brevio/session/testing.rb
|
124
|
+
- lib/brevio/session/with_session_login.rb
|
124
125
|
- lib/brevio_session.rb
|
125
126
|
homepage: https://github.com/team-brevio/brevio-session
|
126
127
|
licenses:
|
@@ -133,7 +134,7 @@ require_paths:
|
|
133
134
|
- lib
|
134
135
|
required_ruby_version: !ruby/object:Gem::Requirement
|
135
136
|
requirements:
|
136
|
-
- -
|
137
|
+
- - "~>"
|
137
138
|
- !ruby/object:Gem::Version
|
138
139
|
version: '3.2'
|
139
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|