auth_master 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c2459f5e3eaff23f81d3e403ded07d9b78577472a011d856b15a5365f189094f
4
+ data.tar.gz: de3257727b34b730c0ec6c0af1366c5723d5e99ff9c119677570f9ec9dae0d73
5
+ SHA512:
6
+ metadata.gz: aaf6f1e9ef4cb6c7e8c0f25641ac735b3d3b1e9c60380df3e5c1c176414be2707a6a46022067dd75da8b35c25aacdf2b3bbb8e7bb93e5b6f5bda73b572d08650
7
+ data.tar.gz: f496418e033c0986041a7216d937aaadee56c9bf51576eadd2a76ffacc92a9a83118fb3c7a9b2208ef0596dba84fc5fd1ff587e6854f75d3414cedaa5e095eb5
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright
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
+ # AuthMaster
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 "auth_master"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install auth_master
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,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,31 @@
1
+ module AuthMaster
2
+ class ApplicationController < ActionController::Base
3
+ def target_scoped_class
4
+ target_scope = config_for(:scope)
5
+ target_scope.present? ? target_accessor.send(target_scope) : target_accessor
6
+ end
7
+
8
+ private
9
+
10
+ def target_accessor
11
+ finder = config_for(:finder)
12
+ finder.is_a?(Proc) ? finder.call : target_class
13
+ end
14
+
15
+ def target_class
16
+ target_param.to_s.classify.constantize
17
+ end
18
+
19
+ def target_param
20
+ params[:target].to_sym
21
+ end
22
+
23
+ def config_for(name)
24
+ AuthMaster.targets[target_param][name.to_sym]
25
+ end
26
+
27
+ def check_target_configuration
28
+ raise ActionController::RoutingError.new("Not Found") if AuthMaster.targets[target_param].blank?
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ module AuthMaster
2
+ class SessionsController < ApplicationController
3
+ TIMING_ATTACK_INTERVAL = 1
4
+
5
+ before_action :check_target_configuration
6
+ around_action :prevent_timing_attack, only: :send_link
7
+
8
+ # NOTE: Show input email form
9
+ def new
10
+ end
11
+
12
+ def send_link
13
+ AuthMaster::SendLinkOperation.call!(params[:email], target_scoped_class:)
14
+ redirect_to auth_master_sent_url(target: target_param)
15
+ end
16
+
17
+ def sent
18
+ end
19
+
20
+ def link
21
+
22
+ end
23
+
24
+ private
25
+
26
+ def prevent_timing_attack
27
+ start_time = Time.current
28
+ yield
29
+
30
+ @timing_attack_interval = timing_attack_interval
31
+ if @timing_attack_interval.positive?
32
+ duration = Time.current - start_time
33
+ sleep(@timing_attack_interval - duration) if duration < @timing_attack_interval
34
+ end
35
+ end
36
+
37
+ def timing_attack_interval
38
+ AuthMaster.timing_attack_interval.presence || TIMING_ATTACK_INTERVAL
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ module AuthMaster
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module AuthMaster
2
+ module SessionsHelper
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module AuthMaster
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module AuthMaster
2
+ class Session < ApplicationRecord
3
+ belongs_to :target, polymorphic: true
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ module AuthMaster
2
+ class SendLinkOperation
3
+ def self.call!(email, target_scoped_class:)
4
+ target = target_scoped_class.find_by(email:)
5
+ return if target.blank?
6
+
7
+ auth_master_session = AuthMaster::SessionService.create!(target)
8
+ AuthMaster::SessionService.send_link!(auth_master_session) if auth_master_session.present?
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,57 @@
1
+ require 'token_guard'
2
+
3
+ module AuthMaster
4
+ class SessionService
5
+ LOGIN_TIMEOUT_INTERVAL = 5.minutes
6
+ LOGIN_ATTEMPTS_COUNT = 3
7
+
8
+ class << self
9
+ def create!(target)
10
+ return if !allow_creation?(target)
11
+
12
+ AuthMaster::Session.create!(target:)
13
+ end
14
+
15
+ def send_link!(auth_master_session)
16
+ target = auth_master_session.target
17
+
18
+ token = TokenGuard.encrypt(auth_master_session.id, purpose: :email, secret: AuthMaster.targets[target_name(target)][:secret])
19
+ target_mailer(target).with(email: target.email, token:).send(target_mailer_login_link_method(target)).deliver_later
20
+ end
21
+
22
+ private
23
+
24
+ def count(target, time:)
25
+ AuthMaster::Session.where(target:).where("created_at > ?", DateTime.current - time).count
26
+ end
27
+
28
+ def allow_creation?(target)
29
+ count(target, time: login_timeout_interval(target)) < login_attempts_count(target)
30
+ end
31
+
32
+ def login_timeout_interval(target)
33
+ AuthMaster.targets[target_name(target)][:login_timeout_interval] || LOGIN_TIMEOUT_INTERVAL
34
+ end
35
+
36
+ def login_attempts_count(target)
37
+ AuthMaster.targets[target_name(target)][:login_attempts_count] || LOGIN_ATTEMPTS_COUNT
38
+ end
39
+
40
+ def target_name(target)
41
+ target.class.to_s.downcase.to_sym
42
+ end
43
+
44
+ def target_mailer(target)
45
+ config_for(target, :mailer_class).to_s.classify.constantize
46
+ end
47
+
48
+ def target_mailer_login_link_method(target)
49
+ config_for(target, :mailer_login_link_method)
50
+ end
51
+
52
+ def config_for(target, name)
53
+ AuthMaster.targets[target_name(target)][name.to_sym]
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1 @@
1
+ <h1>Sessions#new</h1>
@@ -0,0 +1 @@
1
+ <h1>Sessions#sent</h1>
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Auth master</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= yield :head %>
9
+
10
+ <%= stylesheet_link_tag "auth_master/application", media: "all" %>
11
+ </head>
12
+ <body>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,11 @@
1
+ AuthMaster::Engine.routes.draw do
2
+ get "/:target/login", to: "sessions#new", as: :auth_master_login
3
+ post "/:target/login", to: "sessions#send_link"
4
+
5
+ get "/:target/sent", to: "sessions#sent", as: :auth_master_sent
6
+
7
+ get "/:target/link", to: "sessions#link", as: :auth_master_link
8
+ post "/:target/link", to: "sessions#create"
9
+
10
+ get "/:target/denied", to: "sessions#denied", as: :auth_master_denied
11
+ end
@@ -0,0 +1,9 @@
1
+ class CreateAuthMasterSessions < ActiveRecord::Migration[8.0]
2
+ def change
3
+ create_table :auth_master_sessions, id: :uuid do |t|
4
+ t.references :target, polymorphic: true, null: false, type: :uuid
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module AuthMaster
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace AuthMaster
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module AuthMaster
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "auth_master/version"
2
+ require "auth_master/engine"
3
+
4
+ module AuthMaster
5
+ mattr_accessor :targets, :timing_attack_interval
6
+
7
+ def self.configure
8
+ yield self
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :auth_master do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auth_master
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - vickodin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-03-20 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: '8.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 8.0.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '8.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 8.0.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: token_guard
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.1'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.1'
47
+ description: Authentication Engine
48
+ email:
49
+ - vick.orel@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - MIT-LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - app/assets/stylesheets/auth_master/application.css
58
+ - app/controllers/auth_master/application_controller.rb
59
+ - app/controllers/auth_master/sessions_controller.rb
60
+ - app/helpers/auth_master/application_helper.rb
61
+ - app/helpers/auth_master/sessions_helper.rb
62
+ - app/models/auth_master/application_record.rb
63
+ - app/models/auth_master/session.rb
64
+ - app/operations/auth_master/send_link_operation.rb
65
+ - app/services/auth_master/session_service.rb
66
+ - app/views/auth_master/sessions/new.html.erb
67
+ - app/views/auth_master/sessions/sent.html.erb
68
+ - app/views/layouts/auth_master/application.html.erb
69
+ - config/routes.rb
70
+ - db/migrate/20250313120723_create_auth_master_sessions.rb
71
+ - lib/auth_master.rb
72
+ - lib/auth_master/engine.rb
73
+ - lib/auth_master/version.rb
74
+ - lib/tasks/auth_master_tasks.rake
75
+ homepage: https://github.com/vickodin/auth_master
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ allowed_push_host: https://rubygems.org
80
+ homepage_uri: https://github.com/vickodin/auth_master
81
+ source_code_uri: https://github.com/vickodin/auth_master
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.4.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Authentication engine for projects built with Rails (Ruby on Rails)
101
+ test_files: []