brevio-session 0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c515c318b1b9d9dcc8f157a9fd507511d2462d43ddb62de9f439ce55e21c5da3
4
- data.tar.gz: d104172c65410bf4e52d8087a2e8bcb30220cf1ef18d75d7d3f0a5eeaba2cb7d
3
+ metadata.gz: fe6f0b7a1308fc36fc7af2653b213a467243d0ad33425f19e07d212434601b04
4
+ data.tar.gz: b4260b34995a0d3be0fee13a39e6bfa4fca2da00998f00831d457f9735ccec11
5
5
  SHA512:
6
- metadata.gz: 5cfcdc09b39e5162e49d1ae0f386dfa59a5e5c62a6a80c7e763d5b67fd029bea0b422efba229caf6c525e1e9011f4d84bdf48ada003d92b5918c7a00da80b244
7
- data.tar.gz: f2deb2e307e1bdcf2f91744bffd969ef5a48dd34638079b22693ca6ba0ac38cdbe342a7ecd525b12b666faff15338ec682add782738693f1ddf0ad0e69288e2e
6
+ metadata.gz: 613c235b33dd2333ec2bf8e0239a669f55c047efa550e7a3145bb07aa9cdf32dea980c00e5c54577e6db90aa7c792bde203709ff156971faf7f4c2b493db047b
7
+ data.tar.gz: 92c6e7374afa7ec52a57a6b6485b18140f40bd260856cff26aec3c2cb0349d038dd6ee06dae416fcfffe6567f22b142751df9877521abc828c9f82477405939a
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.0)
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.4)
13
- actionview (= 7.0.4)
14
- activesupport (= 7.0.4)
15
- rack (~> 2.0, >= 2.2.0)
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.4)
20
- activesupport (= 7.0.4)
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.4)
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.1.10)
33
- connection_pool (2.4.0)
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.11.0)
39
- i18n (1.12.0)
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.19.0)
45
+ loofah (2.21.3)
46
46
  crass (~> 1.0.2)
47
- nokogiri (>= 1.5.9)
48
- mini_portile2 (2.8.1)
49
- minitest (5.16.3)
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.6.1)
57
- rack (2.2.4)
58
- rack-test (2.0.2)
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.3)
61
- activesupport (>= 4.2.0)
58
+ rails-dom-testing (2.2.0)
59
+ activesupport (>= 5.0.0)
60
+ minitest
62
61
  nokogiri (>= 1.6)
63
- rails-html-sanitizer (1.4.3)
64
- loofah (~> 2.3)
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.6)
66
+ redis (5.0.7)
67
67
  redis-client (>= 0.9.0)
68
- redis-client (0.14.1)
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.5)
87
+ tzinfo (2.0.6)
88
88
  concurrent-ruby (~> 1.0)
89
89
  unicode-display_width (2.2.0)
90
- zeitwerk (2.6.7)
90
+ zeitwerk (2.6.12)
91
91
 
92
92
  PLATFORMS
93
93
  arm64-darwin-21
@@ -2,14 +2,14 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'brevio-session'
5
- spec.version = '0.1'
5
+ spec.version = '0.2.0'
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
@@ -13,7 +13,9 @@ module Brevio::Session
13
13
  # available in the context of a controller.
14
14
  #
15
15
  included do
16
- raise 'Included Brevio::Session outside of controller' unless self <= ActionController::Base
16
+ unless self <= ActionController::Base || instance_methods.include?(:session)
17
+ raise 'Included Brevio::Session outside of controller'
18
+ end
17
19
  end
18
20
 
19
21
  # Fetches the Brevio session from Redis, based on the encrypted key stored in the client's cookie.
@@ -44,7 +46,8 @@ module Brevio::Session
44
46
  # and whether this cookie contains a user ID.
45
47
  #
46
48
  def brevio_logged_in?
47
- fetch_session&.dig(:user_id).present?
49
+ brevio_session, _ = fetch_session
50
+ brevio_session&.dig(:user_id).present?
48
51
  end
49
52
 
50
53
  private
@@ -11,6 +11,8 @@ loader.setup # ready!
11
11
 
12
12
  require 'action_dispatch'
13
13
 
14
+ module BrevioSession; end # Necessary for reasons
15
+
14
16
  module Brevio::Session
15
17
  class NilSession < StandardError; end
16
18
  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.1'
4
+ version: 0.2.0
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-04-18 00:00:00.000000000 Z
11
+ date: 2023-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,11 +122,6 @@ files:
122
122
  - lib/brevio/session/redis.rb
123
123
  - lib/brevio/session/testing.rb
124
124
  - lib/brevio_session.rb
125
- - test/brevio_session_integration_test.rb
126
- - test/redis_json_wrapper.rb
127
- - test/stubs.rb
128
- - test/test_app.rb
129
- - test/test_helper.rb
130
125
  homepage: https://github.com/team-brevio/brevio-session
131
126
  licenses:
132
127
  - MIT
@@ -138,7 +133,7 @@ require_paths:
138
133
  - lib
139
134
  required_ruby_version: !ruby/object:Gem::Requirement
140
135
  requirements:
141
- - - '='
136
+ - - "~>"
142
137
  - !ruby/object:Gem::Version
143
138
  version: '3.2'
144
139
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class BrevioSessionIntegrationTest < Minitest::Test
6
- include Rack::Test::Methods
7
- include Brevio::Session::Testing
8
-
9
- def brevio_redis
10
- Brevio::Session::Config.config.redis
11
- end
12
-
13
- def user
14
- UserStub.new(brevio_id: 1)
15
- end
16
-
17
- def test_sets_brevio_session_in_redis
18
- session = brevio_login(user)
19
- get '/', nil, cookie: "brevio_session=#{session}"
20
- assert last_response.ok?
21
- assert current_session.cookie_jar['brevio_session']
22
- assert_equal user.brevio_id, brevio_redis.get("brevio-id:session:#{session}")[:user_id]
23
- json_body = JSON.parse(last_response.body)
24
- assert_equal user.brevio_id, json_body['user_id']
25
- end
26
-
27
- def test_user_stamp
28
- time = Time.current
29
- session = brevio_login(user, updated_at: time)
30
- get '/', nil, cookie: "brevio_session=#{session}"
31
- assert last_response.ok?
32
- json_body = JSON.parse(last_response.body)
33
- # ceil ceils the subsecond to a default decimal (0) by default. We don't really care about
34
- # microsecond precision here during the JSON parsing/dumping
35
- assert_equal time.ceil, Time.parse(json_body['user_stamp']).ceil
36
- end
37
-
38
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Brevio ID stores its session using redis-session-store, which stores all values as JSON.
4
- # The returned value is a JSON string, which needs to be parsed by the redis client. The native
5
- # Redis.get would simply return the JSON string, so we need to wrap it.
6
- #
7
- # The services consuming the Brevio::Session (e.g. Confirm/Sign) already assume the values
8
- # in Redis are all JSON (see domain/brevio/redis/client.rb).
9
- #
10
- #
11
- class RedisJSONWrapper
12
- def set(key, value)
13
- redis.set(key, value.to_json)
14
- end
15
-
16
- def get(key)
17
- raw = redis.get(key)
18
- return nil if raw.nil?
19
-
20
- value = JSON.parse(raw)
21
- return value if value.empty?
22
-
23
- value.with_indifferent_access
24
- end
25
-
26
- def del(keys)
27
- redis.del(keys)
28
- end
29
-
30
- def expire(key, expiry)
31
- redis.expire(key, expiry)
32
- end
33
-
34
- private
35
-
36
- def redis
37
- @redis ||= ::Redis.new
38
- end
39
- end
data/test/stubs.rb DELETED
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class UserStub
4
- attr_accessor :brevio_id, :audit_company
5
-
6
- def initialize(attrs)
7
- @brevio_id = attrs[:brevio_id]
8
- @audit_company = AuditCompanyStub.new(brevio_id: @brevio_id + 1)
9
- end
10
- end
11
-
12
- class AuditCompanyStub
13
- attr_accessor :brevio_id
14
-
15
- def initialize(attrs)
16
- @brevio_id = attrs[:brevio_id]
17
- end
18
- end
data/test/test_app.rb DELETED
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Based on https://github.com/rails/rails/blob/6-0-stable/guides/bug_report_templates/action_controller_master.rb
4
-
5
- require 'action_controller/railtie'
6
- require 'rack/test'
7
- require 'brevio-session'
8
-
9
- class TestApp < Rails::Application
10
- config.root = __dir__
11
- config.hosts << 'example.org'
12
- config.session_store :cookie_store, key: 'brevio_session'
13
- secrets.secret_key_base = 'secret_key_base'
14
-
15
- config.logger = Logger.new($stdout)
16
- config.log_level = :info
17
- Rails.logger = config.logger
18
-
19
- routes.draw do
20
- get '/' => 'test#index'
21
- end
22
- end
23
-
24
- class TestController < ActionController::Base
25
- include Brevio::Session
26
- include Rails.application.routes.url_helpers
27
-
28
- def logger
29
- Rails.logger
30
- end
31
-
32
- def index
33
- brevio_session = fetch_brevio_session
34
- render json: brevio_session.to_json
35
- end
36
- end
data/test/test_helper.rb DELETED
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
- require 'rack/test'
5
-
6
- require 'debug'
7
- require 'fakeredis'
8
- require 'priscilla'
9
- require 'brevio-session'
10
- require 'brevio_session/testing'
11
-
12
- require 'redis_json_wrapper'
13
- require 'stubs'
14
- require 'test_app'
15
-
16
- Brevio::Session::Config.configure do |config|
17
- config.debug = true
18
- config.production = false
19
- config.redis = RedisJSONWrapper.new
20
- config.secret_key = 'secret_key_base'
21
- config.session_cookie = 'brevio_session'
22
- config.session_expire = 45.minutes
23
- end
24
-
25
- Brevio::Session::Testing.setup!(logger: Rails.logger)
26
-
27
- module Minitest
28
- class Test
29
- include Rack::Test::Methods
30
- include TestApp.routes.url_helpers
31
-
32
- def app
33
- TestApp
34
- end
35
- end
36
- end