current_session 0.1.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: bfc9528a535c2d5ddcae967b83b2efe2542e7973a2bc4195f526da630647340e
4
+ data.tar.gz: 3fea9daed229931366f300225eb74a511d301cb2779cc42ec381df3d0907e1b8
5
+ SHA512:
6
+ metadata.gz: edde5245826209571d82fa6cf0220fd19312c82a5950e2897360120b04088128fd31f5b285fe27f375d38e5f31c86acaf5745b8f782002fcef97452b3c50e689
7
+ data.tar.gz: bda6aa822015d94c24058ea5e5e8bfed2f72d0792928bf47cf0c6e342be61137039946361b796bd3bf7ebefacc7ef05bb708050edffda6e9684f53577a469935
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.2
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+ Exclude:
16
+ - current_session.gemspec
17
+
18
+ Metrics/BlockLength:
19
+ Enabled: false
20
+
21
+ Style/EmptyMethod:
22
+ Enabled: false
23
+
24
+ Lint/EmptyBlock:
25
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-28
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at masa@aileron.cc. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in current_user_omniauth.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,102 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ current_session (0.1.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (7.0.3.1)
11
+ activesupport (= 7.0.3.1)
12
+ activerecord (7.0.3.1)
13
+ activemodel (= 7.0.3.1)
14
+ activesupport (= 7.0.3.1)
15
+ activesupport (7.0.3.1)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ ast (2.4.2)
21
+ coderay (1.1.3)
22
+ concurrent-ruby (1.1.10)
23
+ diff-lcs (1.5.0)
24
+ docile (1.4.0)
25
+ ffaker (2.21.0)
26
+ hashie (5.0.0)
27
+ i18n (1.12.0)
28
+ concurrent-ruby (~> 1.0)
29
+ json (2.6.2)
30
+ method_source (1.0.0)
31
+ minitest (5.16.3)
32
+ omniauth (2.1.0)
33
+ hashie (>= 3.4.6)
34
+ rack (>= 2.2.3)
35
+ rack-protection
36
+ parallel (1.22.1)
37
+ parser (3.1.2.1)
38
+ ast (~> 2.4.1)
39
+ pry (0.14.1)
40
+ coderay (~> 1.1)
41
+ method_source (~> 1.0)
42
+ rack (2.2.4)
43
+ rack-protection (2.2.2)
44
+ rack
45
+ rainbow (3.1.1)
46
+ rake (13.0.6)
47
+ regexp_parser (2.5.0)
48
+ rexml (3.2.5)
49
+ rspec (3.11.0)
50
+ rspec-core (~> 3.11.0)
51
+ rspec-expectations (~> 3.11.0)
52
+ rspec-mocks (~> 3.11.0)
53
+ rspec-core (3.11.0)
54
+ rspec-support (~> 3.11.0)
55
+ rspec-expectations (3.11.0)
56
+ diff-lcs (>= 1.2.0, < 2.0)
57
+ rspec-support (~> 3.11.0)
58
+ rspec-mocks (3.11.1)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.11.0)
61
+ rspec-support (3.11.0)
62
+ rubocop (1.35.1)
63
+ json (~> 2.3)
64
+ parallel (~> 1.10)
65
+ parser (>= 3.1.2.1)
66
+ rainbow (>= 2.2.2, < 4.0)
67
+ regexp_parser (>= 1.8, < 3.0)
68
+ rexml (>= 3.2.5, < 4.0)
69
+ rubocop-ast (>= 1.20.1, < 2.0)
70
+ ruby-progressbar (~> 1.7)
71
+ unicode-display_width (>= 1.4.0, < 3.0)
72
+ rubocop-ast (1.21.0)
73
+ parser (>= 3.1.1.0)
74
+ ruby-progressbar (1.11.0)
75
+ simplecov (0.21.2)
76
+ docile (~> 1.1)
77
+ simplecov-html (~> 0.11)
78
+ simplecov_json_formatter (~> 0.1)
79
+ simplecov-html (0.12.3)
80
+ simplecov_json_formatter (0.1.4)
81
+ sqlite3 (1.4.4)
82
+ tzinfo (2.0.5)
83
+ concurrent-ruby (~> 1.0)
84
+ unicode-display_width (2.2.0)
85
+
86
+ PLATFORMS
87
+ arm64-darwin-20
88
+
89
+ DEPENDENCIES
90
+ activerecord
91
+ current_session!
92
+ ffaker
93
+ omniauth
94
+ pry
95
+ rake (~> 13.0)
96
+ rspec (~> 3.0)
97
+ rubocop (~> 1.7)
98
+ simplecov
99
+ sqlite3
100
+
101
+ BUNDLED WITH
102
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Masa (Aileron inc)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # CurrentSession
2
+
3
+ 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/current_user_omniauth`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'current_session'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install current_session
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/current_user_omniauth. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/current_user_omniauth/blob/main/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the CurrentUserOmniauth project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/current_user_omniauth/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "current_user_omniauth"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/current_session/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "current_session"
7
+ spec.version = CurrentSession::VERSION
8
+ spec.authors = ["Masa (Aileron inc)"]
9
+ spec.email = ["masa@aileron.cc"]
10
+
11
+ spec.summary = "Library for current_user using ActiveSupport::CurrentAttributes"
12
+ spec.description = "This is a minimal implementation to provide the current_user method in the action controller, assuming login with omniauth."
13
+ spec.homepage = "https://github.com/aileron-inc/current_session"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/aileron-inc/current_session"
19
+ spec.metadata["changelog_uri"] = "https://github.com/aileron-inc/current_session/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ spec.add_runtime_dependency "activesupport"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ spec.add_development_dependency "activerecord"
36
+ spec.add_development_dependency "ffaker"
37
+ spec.add_development_dependency "omniauth"
38
+ spec.add_development_dependency "pry"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency "simplecov"
41
+ spec.add_development_dependency "sqlite3"
42
+
43
+ spec.metadata = {
44
+ "rubygems_mfa_required" => "true"
45
+ }
46
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # Implementation of session tokens for DB management
6
+ #
7
+ class ActiveRecordSession < Repository
8
+ def find
9
+ session_token_class.find_by(value: session_token).try do |record|
10
+ update(record)
11
+ yield record.user
12
+ end
13
+ end
14
+
15
+ def create(user)
16
+ session_token_class.create(user_id: user.id, value: new_session_token) do |record|
17
+ update(record)
18
+ end.value
19
+ end
20
+
21
+ def update(token)
22
+ token.update(
23
+ last_request_at: current_time,
24
+ last_request_ip: request.remote_ip,
25
+ last_request_user_agent: request.user_agent
26
+ )
27
+ end
28
+
29
+ def destroy
30
+ session_token_class.find_by(value: session_token)&.destroy
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # Base class for providing auth methods
6
+ #
7
+ class Auth
8
+ def initialize(request, user_class)
9
+ @request = request
10
+ @user_class = user_class
11
+ end
12
+ attr_reader :request, :user_class
13
+
14
+ def auth
15
+ request.env["omniauth.auth"]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # Base class for implementing current_user
6
+ #
7
+ class Base < ActiveSupport::CurrentAttributes
8
+ include CurrentSession::Interface
9
+ include CurrentSession::RaiseNotImplementedError
10
+ attribute :current_user
11
+
12
+ class << self
13
+ attr_accessor :user_class, :session_token_class
14
+
15
+ def current_time(_)
16
+ Time.current
17
+ end
18
+
19
+ def session_repository_class=(session_repository_class = nil)
20
+ @session_repository_class = session_repository_class
21
+ end
22
+
23
+ def session_methods(&block)
24
+ @session_repository_class = Class.new(CurrentSession::Repository, &block)
25
+ end
26
+
27
+ def auth_methods(&block)
28
+ @auth_class = Class.new(CurrentSession::Auth, &block)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # repository to return the user specified by the environment variable
6
+ #
7
+ class EnvSession < Repository
8
+ # rubocop:disable Metrics/MethodLength
9
+ def self.build(current_user_id)
10
+ Class.new(Repository) do
11
+ define_method(:current_user) { user_class.find(current_user_id) }
12
+
13
+ def try
14
+ yield self
15
+ end
16
+
17
+ def find
18
+ yield current_user
19
+ end
20
+
21
+ def create(_)
22
+ end
23
+
24
+ def destroy
25
+ end
26
+ end
27
+ end
28
+ # rubocop:enable Metrics/MethodLength
29
+ end
30
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # Interface for manipulating http request objects
6
+ #
7
+ module Interface
8
+ extend ActiveSupport::Concern
9
+ class_methods do
10
+ def call(request)
11
+ update(request).current_user.present?
12
+ end
13
+
14
+ def update(request)
15
+ session_repository(request).try do |repository|
16
+ repository.find { |user| self.current_user = user }
17
+ end
18
+ self
19
+ end
20
+
21
+ def create(request)
22
+ auth = @auth_class.new(request, user_class)
23
+ auth.find_or_create_by_auth.try do |user|
24
+ auth.update(user)
25
+ session_repository(request).update_session_token(user)
26
+ end
27
+ end
28
+
29
+ def destroy(request)
30
+ self.current_user = nil
31
+ session_repository(request).try do |repository|
32
+ repository.destroy
33
+ repository.delete_session_token
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def session_repository(request)
40
+ @session_repository_class.new(
41
+ request: request,
42
+ user_class: user_class,
43
+ session_token_class: session_token_class,
44
+ current_time: current_time(request)
45
+ )
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # Class for verifying whether it is implemented correctly
6
+ #
7
+ module RaiseNotImplementedError
8
+ extend ActiveSupport::Concern
9
+ class_methods do
10
+ def raise_not_implemented_error
11
+ raise_not_implemented_error_for_repository
12
+ raise_not_implemented_error_for_auth
13
+ end
14
+
15
+ private
16
+
17
+ # rubocop:disable Layout/LineLength
18
+ def raise_not_implemented_error_for_repository
19
+ raise NotImplementedError, "You must setting self.session_repository_class= or session_methods" unless @session_repository_class
20
+ raise NotImplementedError, "You must implement #{@session_repository_class}#find" unless @session_repository_class.method_defined? :find
21
+ raise NotImplementedError, "You must implement #{@session_repository_class}#destroy" unless @session_repository_class.method_defined? :destroy
22
+ raise NotImplementedError, "You must implement #{@session_repository_class}#destroy" unless @session_repository_class.method_defined? :destroy
23
+ raise NotImplementedError, "You must implement #{@session_repository_class}#create" unless @session_repository_class.method_defined? :create
24
+ end
25
+ # rubocop:enable Layout/LineLength
26
+
27
+ # rubocop:disable Layout/LineLength
28
+ def raise_not_implemented_error_for_auth
29
+ raise NotImplementedError, "You must setting self.auth_class= or auth_methods" unless @auth_class
30
+ raise NotImplementedError, "You must implement #{@auth_class}#find_or_create_by_auth" unless @auth_class.method_defined? :find_or_create_by_auth
31
+ raise NotImplementedError, "You must implement #{@auth_class}#update" unless @auth_class.method_defined? :update
32
+ end
33
+ # rubocop:enable Layout/LineLength
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # Base class for processing to get session_token from request.session
6
+ #
7
+ class Repository
8
+ def initialize(current_time:, request:, user_class:, session_token_class:)
9
+ @current_time = current_time
10
+ @request = request
11
+ @user_class = user_class
12
+ @session_token_class = session_token_class
13
+ end
14
+ attr_reader :current_time, :request, :user_class, :session_token_class
15
+
16
+ def key
17
+ @key ||= CurrentSession.key(user_class)
18
+ end
19
+
20
+ def session_token
21
+ @session_token ||= request.session[key]
22
+ end
23
+
24
+ def new_session_token
25
+ SecureRandom.urlsafe_base64(64)
26
+ end
27
+
28
+ def delete_session_token
29
+ request.session.delete(key)
30
+ end
31
+
32
+ def update_session_token(user)
33
+ request.session[key] = create(user)
34
+ end
35
+
36
+ def try
37
+ session_token.presence.try do |_|
38
+ yield self
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # Implementation of using UIDs as session tokens
6
+ #
7
+ class UidSession < Repository
8
+ def find(&block)
9
+ user_class.find_by(uid: session_token).try(&block)
10
+ end
11
+
12
+ def create(user)
13
+ user.uid
14
+ end
15
+
16
+ def destroy
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "current_session/version"
4
+ require "active_support"
5
+
6
+ module CurrentSession
7
+ extend ActiveSupport::Autoload
8
+ autoload :Base
9
+ autoload :Interface
10
+ autoload :Auth
11
+ autoload :Repository
12
+ autoload :RaiseNotImplementedError
13
+
14
+ autoload :UidSession
15
+ autoload :EnvSession
16
+ autoload :ActiveRecordSession
17
+
18
+ def self.key(user_class)
19
+ "session_#{user_class.name.underscore.parameterize(separator: "_")}_key"
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: current_session
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masa (Aileron inc)
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ffaker
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: omniauth
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: This is a minimal implementation to provide the current_user method in
126
+ the action controller, assuming login with omniauth.
127
+ email:
128
+ - masa@aileron.cc
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".github/workflows/main.yml"
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - ".rubocop.yml"
137
+ - CHANGELOG.md
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - Gemfile.lock
141
+ - LICENSE.txt
142
+ - README.md
143
+ - Rakefile
144
+ - bin/console
145
+ - bin/setup
146
+ - current_session.gemspec
147
+ - lib/current_session.rb
148
+ - lib/current_session/active_record_session.rb
149
+ - lib/current_session/auth.rb
150
+ - lib/current_session/base.rb
151
+ - lib/current_session/env_session.rb
152
+ - lib/current_session/interface.rb
153
+ - lib/current_session/raise_not_implemented_error.rb
154
+ - lib/current_session/repository.rb
155
+ - lib/current_session/uid_session.rb
156
+ - lib/current_session/version.rb
157
+ homepage: https://github.com/aileron-inc/current_session
158
+ licenses:
159
+ - MIT
160
+ metadata:
161
+ rubygems_mfa_required: 'true'
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '3.0'
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubygems_version: 3.2.22
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: Library for current_user using ActiveSupport::CurrentAttributes
181
+ test_files: []