breath 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '00738e8ac9332e8c99ac5dc5e392836a393433ea3701a435e95ea4d0d16f9413'
4
+ data.tar.gz: ff403849fbcffcf621f92d3c58ca32148c3f25fee31609aa3bb310d07dc74f3a
5
+ SHA512:
6
+ metadata.gz: ecc5d2e65d8b180a8894177925d9918b144f41f2daa60ec3cdafd24a476b50d1f82e1d7be715d56a128bec8d62dc327f80df99a67ee4c5f1feb76441d108f57b
7
+ data.tar.gz: eea3081deac0324cc3eb3fa890770c59532c8374e35e64b41f479f0e66c74f51baaa876261bb56c0946a735d9ccf967368b8bc9ea460149695eabf87ac44ce39
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 testCodeV01
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Breath
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "breath"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install breath
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ 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,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ class ActionDispatch::Routing::Mapper
2
+ def breath(routes_name, &resources)
3
+ namespace routes_name do
4
+ get "/login" => "sessions#new"
5
+ post "login" => "sessions#login"
6
+ delete "logout" => "sessions#logout"
7
+
8
+ resources.call if resources.present?
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,80 @@
1
+ module Breath
2
+ module ApplicationControllerHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ class AuthenticationError < StandardError; end
6
+
7
+ included do
8
+ target_class = to_s.split("::")[-2].singularize.constantize
9
+ target_name = target_class.to_s.underscore
10
+ current_target = "current_#{target_name}"
11
+
12
+ include ActionController::Cookies
13
+
14
+ define_method :authenticate! do
15
+ raise AuthenticationError unless cookies.key?("#{target_name}_id".to_sym)
16
+ raise AuthenticationError if current_target.nil?
17
+ raise AuthenticationError unless current_target.remember?(cookies[:remember_token])
18
+
19
+ target = target_class.enabled.find_by(id: cookies.signed["#{target_name}_id".to_sym])
20
+
21
+ raise AuthenticationError if target.nil?
22
+ rescue StandardError => e
23
+ send :render_401, e.to_s
24
+ end
25
+
26
+ define_method current_target do
27
+ @current_target ||= target_class.enabled.find_by(id: cookies.signed["#{target_name}_id".to_sym])
28
+ end
29
+ end
30
+
31
+ class_methods do
32
+ def crsf_protect(value)
33
+ if value
34
+ # include ActionController::HttpAuthentication::Token::ControllerMethods
35
+ include ActionController::RequestForgeryProtection
36
+ protect_from_forgery with: :exception
37
+ after_action :set_csrf_token_header
38
+
39
+ define_method :set_csrf_token_header do
40
+ response.header["X-CSRF-Token"] = form_authenticity_token
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def render_400(res)
47
+ Rails.logger.error error_message(res)
48
+
49
+ render json: res, status: 400
50
+ end
51
+
52
+ def render_401(res)
53
+ Rails.logger.error error_message(res)
54
+
55
+ render json: res, status: 401
56
+ end
57
+
58
+ def render_404(res)
59
+ Rails.logger.error error_message(res)
60
+
61
+ render json: res, status: 404
62
+ end
63
+
64
+ def render_409(res)
65
+ Rails.logger.error error_message(res)
66
+
67
+ render json: res, status: 409
68
+ end
69
+
70
+ def render_500(res)
71
+ Rails.logger.error error_message(res)
72
+
73
+ render json: res, status: 500
74
+ end
75
+
76
+ def error_message(error)
77
+ "[ERROR] #{error.to_s}"
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,61 @@
1
+ module Breath
2
+ module SessionsControllerHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ class InvalidPasswordConfirmation < StandardError; end
6
+ class TargetNotFound < StandardError; end
7
+ class InvalidPassword < StandardError; end
8
+
9
+ included do
10
+ target_class = to_s.split("::")[-2].singularize.constantize
11
+ target_name = target_class.to_s.underscore
12
+ current_target = "current_#{target_name}"
13
+
14
+ skip_before_action :authenticate!, only: %i[new login logout]
15
+
16
+ # GET /schedule_kun/target/login
17
+ define_method :new do
18
+ render status: 200
19
+ end
20
+
21
+ # POST /schedule_kun/target/login
22
+ define_method :login do
23
+ object = target_class.enabled.find_by(**{ "#{target_class.auth_attribute}": sessions_params[target_class.auth_attribute.to_sym] })
24
+ raise TargetNotFound if object.nil?
25
+ raise InvalidPassword unless object.authenticate(sessions_params[:password])
26
+
27
+ object.remember
28
+ write_cookie(target_name, object)
29
+
30
+ render status: 200
31
+ rescue StandardError => e
32
+ send :render_401, e.to_s
33
+ end
34
+
35
+ # DELETE /schedule_kun/target/logout
36
+ define_method :logout do
37
+ send(current_target)&.forget
38
+ @current_target = nil
39
+
40
+ cookies.delete("#{target_name}_id".to_sym)
41
+ cookies.delete(:remember_token)
42
+
43
+ render status: 200
44
+ end
45
+
46
+ define_method :sessions_params do
47
+ params.require(:sessions).permit(target_class.auth_attribute.to_sym, :password, :password_confirmation)
48
+ end
49
+ end
50
+
51
+ def write_cookie(target_name, object)
52
+ if Rails.application.config.respond_to? :breath_expires
53
+ cookies.signed["#{target_name}_id".to_sym] = { value: object.id, expires: Rails.application.config.breath_expires }
54
+ cookies[:remember_token] = { value: object.remember_token, expires: Rails.application.config.breath_expires }
55
+ else
56
+ cookies.permanent.signed["#{target_name}_id".to_sym] = object.id
57
+ cookies.permanent[:remember_token] = object.remember_token
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,48 @@
1
+ module Breath::Model
2
+ extend ActiveSupport::Concern
3
+
4
+ attr_accessor :remember_token
5
+
6
+ class_methods do
7
+ # 与えられた文字列のハッシュ値を返す
8
+ def digest(string)
9
+ cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
10
+ BCrypt::Engine.cost
11
+ BCrypt::Password.create(string, cost: cost)
12
+ end
13
+
14
+ # ランダムなトークンを返す
15
+ def new_token
16
+ SecureRandom.urlsafe_base64
17
+ end
18
+
19
+ def attr_breath(attribute)
20
+ class_attribute :auth_attribute, default: attribute
21
+ end
22
+
23
+ unless respond_to?(:enabled)
24
+ define_method :enabled do
25
+ self
26
+ end
27
+ end
28
+ end
29
+
30
+ included do
31
+ has_secure_password
32
+ end
33
+
34
+ # 永続的セッションで使用するユーザーをデータベースに記憶する
35
+ def remember
36
+ self.remember_token = self.class.new_token
37
+ update_attribute(:remember_digest, self.class.digest(remember_token))
38
+ end
39
+
40
+ # 渡されたトークンがダイジェストと一致したらtrueを返す
41
+ def remember?(remember_token)
42
+ BCrypt::Password.new(remember_digest).is_password?(remember_token)
43
+ end
44
+
45
+ def forget
46
+ update_attribute(:remember_digest, nil)
47
+ end
48
+ end
@@ -0,0 +1,4 @@
1
+ module Breath
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Breath
2
+ VERSION = "0.1.0"
3
+ end
data/lib/breath.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "breath/version"
2
+ require "breath/railtie"
3
+ require "breath/controller/application_controller_helper.rb"
4
+ require "breath/controller/sessions_controller_helper.rb"
5
+ require "breath/config/route.rb"
6
+ require "breath/model/breath_model.rb"
7
+
8
+ module Breath
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :breath do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: breath
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - testCodeV01
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bcrypt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Breath is a simple authentication gem. Easy introduce login and logout.
42
+ email:
43
+ - qft-tk-1213@tk-labo.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/breath.rb
52
+ - lib/breath/config/route.rb
53
+ - lib/breath/controller/application_controller_helper.rb
54
+ - lib/breath/controller/sessions_controller_helper.rb
55
+ - lib/breath/model/breath_model.rb
56
+ - lib/breath/railtie.rb
57
+ - lib/breath/version.rb
58
+ - lib/tasks/breath_tasks.rake
59
+ homepage: https://github.com/testCodeV01
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ allowed_push_host: https://rubygems.org/
64
+ homepage_uri: https://github.com/testCodeV01
65
+ source_code_uri: https://github.com/testCodeV01/breath
66
+ changelog_uri: https://github.com/testCodeV01
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.3.3
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Simple authentication gem.
86
+ test_files: []