brevio-session 0.2.0 → 0.2.1

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: fe6f0b7a1308fc36fc7af2653b213a467243d0ad33425f19e07d212434601b04
4
- data.tar.gz: b4260b34995a0d3be0fee13a39e6bfa4fca2da00998f00831d457f9735ccec11
3
+ metadata.gz: 7047e1a58ab4d8ece1f39bf73cc37ef0a4c3165da08ab14aa5a8faeb1390f3a5
4
+ data.tar.gz: 7f26b113da04eeb3bdd80f32335e34463f8c2adc02dd82ffbf8d7d20fd4fe8fa
5
5
  SHA512:
6
- metadata.gz: 613c235b33dd2333ec2bf8e0239a669f55c047efa550e7a3145bb07aa9cdf32dea980c00e5c54577e6db90aa7c792bde203709ff156971faf7f4c2b493db047b
7
- data.tar.gz: 92c6e7374afa7ec52a57a6b6485b18140f40bd260856cff26aec3c2cb0349d038dd6ee06dae416fcfffe6567f22b142751df9877521abc828c9f82477405939a
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.2.0)
4
+ brevio-session (0.2.1)
5
5
  actionpack (~> 7.0.3)
6
6
  redis (~> 5.0)
7
7
  zeitwerk (~> 2.6.7)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'brevio-session'
5
- spec.version = '0.2.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")
@@ -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
@@ -1,101 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/concern'
4
-
5
3
  module Brevio::Session
6
- extend ActiveSupport::Concern
7
-
8
- # By including this module into a Rails controller you will have access to the convenience methods
9
- # fetch_brevio_session(!) and brevio_logged_in? which acts as a wrapper around the decrypting of
10
- # predefined session cookies and sessions stored in Redis.
11
- #
12
- # We also require access to instance attributes like #request and the cookie jar, which are only
13
- # available in the context of a controller.
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
- brevio_session, _ = fetch_session
50
- brevio_session&.dig(:user_id).present?
51
- end
52
-
53
- private
54
-
55
- def brevio_config
56
- Config.config
57
- end
58
-
59
- def fetch_session
60
- brevio_config.logger.info '[brevio-session] Fetching Brevio session'
61
- cookie = request.cookie_jar[brevio_config.session_cookie]
62
- redis_key = Cookies::Parse.perform!(cookie)
63
- brevio_session = Redis.get(redis_key)
64
- raise NilSession if brevio_session.nil?
65
- [brevio_session.with_indifferent_access, redis_key]
66
- rescue RuntimeError => e
67
- brevio_config.logger.error "[brevio-session] --- 💣 Couldn't fetch Brevio session 💣 ---"
68
- brevio_config.logger.error "[brevio-session] #{e.message}"
69
- nil
70
- end
71
-
72
- # Refreshes the Brevio session cookie, avoding its expiry. This is helpful to
73
- # ensure the user stays logged in through interacting with the application.
74
- #
75
- def refresh_session(redis_key)
76
- brevio_config.logger.info '[brevio-session] Refreshing Brevio session' if brevio_config.debug?
77
- cookies[brevio_config.session_cookie] = {
78
- value: request.cookie_jar[brevio_config.session_cookie],
79
- domain: :all,
80
- expires: brevio_config.session_expire,
81
- httponly: true,
82
- secure: true
83
- }
84
- Redis.expire(redis_key, brevio_config.session_expire)
85
- rescue RuntimeError => e
86
- brevio_config.logger.error "[brevio-session] --- 💣 Couldn't refresh Brevio session 💣 ---"
87
- brevio_config.logger.error "[brevio-session] #{e.message}"
88
- end
89
-
90
- class << self
91
- # Function used to fetch the current audit company session for a given Brevio ID.
92
- # This session contains information about an audit company shared by any number of
93
- # users. If the audit company has been updated in ID (e.g. changed logo or name), the
94
- # database record of the local service (e.g. Confirm/Sign) will fetch the updates if
95
- # the timestamp in this session is greater than their own local timestamps.
96
- #
97
- def audit_company_updated_at(brevio_id)
98
- Config.config.redis.get("#{Config::Redis::Prefixes::AUDIT_COMPANY}:#{brevio_id}")
99
- 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}")
100
13
  end
101
14
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brevio-session
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brevio AS
@@ -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: