entra_id_active_record_session_store 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1e676373d9f51e4088f95e49a9f8571ddf43974b20a536650588b12bf54fadf2
4
+ data.tar.gz: 82ed9e82cd84971e26ee5ac5d198e4cd7c4df6a29a5c1d4530f96a05767949d2
5
+ SHA512:
6
+ metadata.gz: 40d4f465b054d44d0f51817c1b6597f249599cd08fde75119125772820091e54b8b4e31289161c9189ac3b2cab6ec1ae0085e0a3b8b235a46c6289d9df6252b6
7
+ data.tar.gz: 3a7abedff2baf8f26376bd08671993e4bf148569e4962a3498a8bd19b18a25e71f55a69fd0ef678e40bf8ee95359d526aefeec3b441f97a1b634ced3f3da5d2e
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2024 David Susco
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # EntraIdActiveRecordSessionStore
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/entra_id_active_record_session_store`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/entra_id_active_record_session_store.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'minitest/test_task'
3
+
4
+ Minitest::TestTask.create
5
+
6
+ require 'rubocop/rake_task'
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[test rubocop]
@@ -0,0 +1,70 @@
1
+ require 'active_record/session_store'
2
+
3
+ module ActionDispatch
4
+ module Session
5
+ class EntraIdActiveRecordStore < ActionDispatch::Session::ActiveRecordStore
6
+ private
7
+
8
+ def write_session(request, sid, session_data, options)
9
+ entra_id_request = RackEntraIdAuth::EntraIdRequest.new(request)
10
+
11
+ logger.silence do
12
+ record, sid = get_session_model(request, sid)
13
+ record.data = session_data
14
+
15
+ if entra_id_request.login_response?
16
+ # store the sessionindex for IdP initiated single logout requests
17
+ auth_response = entra_id_request.saml_auth_response()
18
+
19
+ record.sessionindex = auth_response.sessionindex
20
+ end
21
+
22
+ return false unless record.save
23
+
24
+ session_data = record.data
25
+ if session_data && session_data.respond_to?(:each_value)
26
+ session_data.each_value do |obj|
27
+ obj.clear_association_cache if obj.respond_to?(:clear_association_cache)
28
+ end
29
+ end
30
+
31
+ sid
32
+ end
33
+ end
34
+
35
+ def delete_session(request, session_id, options)
36
+ entra_id_request = RackEntraIdAuth::EntraIdRequest.new(request)
37
+
38
+ logger.silence do
39
+ if entra_id_request.logout_request?
40
+ # delete all the sessions with sessionindexes declared in the IdP
41
+ # initiated single logout request
42
+ logout_request = entra_id_request.saml_logout_request()
43
+ sessions = session_class.where(sessionindex: logout_request.session_indexes)
44
+ data = sessions.last.data rescue ''
45
+ sessions.destroy_all
46
+ elsif sid = current_session_id(request)
47
+ if model = get_session_with_fallback(sid)
48
+ data = model.data
49
+ model.destroy
50
+ end
51
+ end
52
+
53
+ request.env[SESSION_RECORD_KEY] = nil
54
+
55
+ unless options[:drop]
56
+ new_sid = generate_sid
57
+
58
+ if options[:renew]
59
+ new_model = session_class.new(:session_id => new_sid.private_id, :data => data)
60
+ new_model.save
61
+ request.env[SESSION_RECORD_KEY] = new_model
62
+ end
63
+ new_sid
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module EntraIdActiveRecordSessionStore
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'rack_entra_id_auth'
2
+ require 'action_dispatch/session/entra_id_active_record_store'
3
+
4
+ module EntraIdActiveRecordSessionStore
5
+ end
@@ -0,0 +1,13 @@
1
+ module EntraIdActiveRecordSessionStore
2
+ module Generators
3
+ class InitializerGenerator < Rails::Generators::Base
4
+ desc 'Create an initializer to set the session store to EntraIdActiveRecordSessionStore.'
5
+
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def create_sessions_migration
9
+ template 'initializer.rb', Rails.root.join('config', 'initializers', 'session_store.rb')
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_record/session_store'
2
+ require 'rails/generators/active_record'
3
+
4
+ module EntraIdActiveRecordSessionStore
5
+ module Generators
6
+ class MigrationGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ argument :file_name, :type => :string, :default => 'create_sessions'
10
+
11
+ desc 'Create a migration to create a sessions table with a sessionindex column.'
12
+
13
+ source_root File.expand_path('templates', __dir__)
14
+
15
+ def create_sessions_migration
16
+ migration_template 'create_sessions_migration.rb', Rails.root.join('db', 'migrate', "#{file_name}.rb")
17
+ end
18
+
19
+ protected
20
+
21
+ def self.next_migration_number (dirname)
22
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
23
+ end
24
+
25
+ def session_table_name
26
+ current_table_name = ActiveRecord::SessionStore::Session.table_name
27
+
28
+ if current_table_name == 'session' || current_table_name == 'sessions'
29
+ current_table_name = ActiveRecord::Base.pluralize_table_names ? 'sessions' : 'session'
30
+ end
31
+
32
+ current_table_name
33
+ end
34
+
35
+ def migration_version
36
+ "[#{ActiveRecord::Migration.current_version}]"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :<%= session_table_name %> do |t|
4
+ t.string :session_id, :null => false
5
+ t.string :sessionindex
6
+ t.text :data
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :<%= session_table_name %>, :session_id, :unique => true
11
+ add_index :<%= session_table_name %>, :sessionindex, :unique => true
12
+ add_index :<%= session_table_name %>, :updated_at
13
+ end
14
+ end
@@ -0,0 +1 @@
1
+ Rails.application.config.session_store :entra_id_active_record_store, :key => '_<%= File.basename(Rails.root) %>_session'
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: entra_id_active_record_session_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Susco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord-session_store
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack_entra_id_auth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.21'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.21'
83
+ description: A subclass of Active Record Session Store that tracks sessionindexes
84
+ for single logout.
85
+ email:
86
+ - dsusco@clarku.edu
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - MIT-LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - lib/action_dispatch/session/entra_id_active_record_store.rb
95
+ - lib/entra_id_active_record_session_store.rb
96
+ - lib/entra_id_active_record_session_store/version.rb
97
+ - lib/generators/entra_id_active_record_session_store/initializer_generator.rb
98
+ - lib/generators/entra_id_active_record_session_store/migration_generator.rb
99
+ - lib/generators/entra_id_active_record_session_store/templates/create_sessions_migration.rb
100
+ - lib/generators/entra_id_active_record_session_store/templates/initializer.rb
101
+ homepage: https://github.com/dsusco/entra_id_active_record_session_store
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ bug_tracker_uri: https://github.com/dsusco/entra_id_active_record_session_store/issues
106
+ changelog_uri: https://github.com/dsusco/entra_id_active_record_session_store/releases/tag/v1.0.0
107
+ homepage_uri: https://github.com/dsusco/entra_id_active_record_session_store
108
+ source_code_uri: https://github.com/dsusco/entra_id_active_record_session_store
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.0
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.5.11
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: A subclass of Active Record Session Store that tracks sessionindexes for
128
+ single logout.
129
+ test_files: []