auth_master 0.0.4 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c266d6e5222227d0f002f1ed663a9420d599d9f3456d082f3de21473f2c6af25
4
- data.tar.gz: 68ef46005182ad74059391ad7df9f869099ae381b5b7913224f6fcdfe17cbcdd
3
+ metadata.gz: 8fa2578341d81c087e5a6cfc4a4611d2f4ab621fab11f9d331495930233b83e9
4
+ data.tar.gz: 851d685d1799899ca304b9748ec9609544c8ebe62b2e438038829ef8c7696f9f
5
5
  SHA512:
6
- metadata.gz: c16626eee3bf0fbbe5b2e6db7beea09a9a27d2785450ef50c4a572bd60b4fd1be9cf313100308b3cd1cc483555fd48fed06e936f4bda5aaf5f1400cf863cc41d
7
- data.tar.gz: 735d46ca1a99308be75b5891df5203c9b63c1b8459312d63d5de623b77322204526b49958842f6983ae35744f785a803e9e5cd4b8de40a65f30e2ad459bdf898
6
+ metadata.gz: f0c045b0c3a2f33abd399a13fa0609d9892a5a3b9d4cddf1fa8db7c081f298b3d097c15885a6425088f10ec2578f9fb76a924835a153b10ca2a3450670744cd3
7
+ data.tar.gz: 973d3ecf3aaa6e9205b2aeede3d917fc125a4b4b4052081cb9e7ec7f826fe85d8abb97d36ea308932a4efbea6b5f91ac6437304d292339fb43a459c574812176
@@ -35,6 +35,18 @@ module AuthMaster
35
35
  session.delete(session_key)
36
36
  session[target_session_key] = auth_master_session.id
37
37
 
38
+ # TODO: Use config for
39
+ # a) session key;
40
+ # b) default redirect path
41
+ saved_path = session.delete("redirect_to")
42
+ redirect_to(saved_path || "/")
43
+ end
44
+
45
+ def destroy
46
+ auth_master_session_id = session.delete(target_session_key)
47
+ AuthMaster::LogoutOperation.call!(auth_master_session_id)
48
+
49
+ # TODO: Use config for redirect_path
38
50
  redirect_to("/")
39
51
  end
40
52
 
@@ -1,11 +1,7 @@
1
1
  module AuthMaster::CurrentConcern
2
2
  extend ActiveSupport::Concern
3
3
 
4
- # included do
5
- # helper_method :current_auth_master
6
- # end
7
-
8
- def current_auth_master(target_param_name)
4
+ def auth_master_current(target_param_name)
9
5
  session_accessor_key = [ "current", target_param_name, "id" ].join("_")
10
6
  auth_master_session_id = session[session_accessor_key]
11
7
  return nil if auth_master_session_id.blank?
@@ -19,4 +15,45 @@ module AuthMaster::CurrentConcern
19
15
 
20
16
  target
21
17
  end
18
+
19
+ def auth_master_prepare_token(target)
20
+ return if target.blank?
21
+ return if !target.persisted?
22
+
23
+ uuid = auth_master_session_id_generator
24
+ session[auth_master_session_key(target.class)] = uuid
25
+
26
+ AuthMaster::PrepareTokenOperation.call!(target, uuid:)
27
+ end
28
+
29
+ def auth_master_login_by_token(target_class, token)
30
+ uuid = session[auth_master_session_key(target_class)]
31
+
32
+ auth_master_session = AuthMaster::LoginByTokenOperation.call!(token, uuid:, target_class:)
33
+ return if auth_master_session.blank?
34
+
35
+ session.delete(auth_master_session_key(target_class))
36
+ session[auth_master_target_session_key(target_class)] = auth_master_session.id
37
+
38
+ true
39
+ end
40
+
41
+ def auth_master_logout(target_class)
42
+ auth_master_session_id = session.delete(auth_master_target_session_key(target_class))
43
+ AuthMaster::LogoutOperation.call!(auth_master_session_id)
44
+ end
45
+
46
+ private
47
+
48
+ def auth_master_session_key(target_class)
49
+ [ "auth_master", target_class.name.underscore, "id" ].join("_")
50
+ end
51
+
52
+ def auth_master_target_session_key(target_class)
53
+ [ "current", target_class.name.underscore, "id" ].join("_")
54
+ end
55
+
56
+ def auth_master_session_id_generator
57
+ Random.uuid
58
+ end
22
59
  end
@@ -2,6 +2,6 @@ module AuthMaster
2
2
  class Session < ApplicationRecord
3
3
  belongs_to :target, polymorphic: true
4
4
 
5
- enum :status, [ :inactive, :active ], default: :inactive
5
+ enum :status, [ :inactive, :active, :logout ], default: :inactive
6
6
  end
7
7
  end
@@ -0,0 +1,21 @@
1
+ module AuthMaster
2
+ class LoginByTokenOperation < AuthMaster::AbstractOperation
3
+ def self.call!(encrypted_token, uuid:, target_class:)
4
+ purpose = token_purpose_config(target_class.name.underscore)
5
+ secret = secret_config(target_class.name.underscore)
6
+
7
+ auth_master_session_id = TokenGuard.decrypt(encrypted_token, purpose:, secret:)
8
+ return if auth_master_session_id.blank?
9
+
10
+ # NOTE: Auth from the same device
11
+ return if auth_master_session_id != uuid
12
+
13
+ auth_master_session = AuthMaster::SessionService.inactive_find(auth_master_session_id)
14
+ return if auth_master_session.blank?
15
+
16
+ AuthMaster::SessionService.activate!(auth_master_session)
17
+
18
+ auth_master_session
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ module AuthMaster
2
+ class LogoutOperation < AuthMaster::AbstractOperation
3
+ def self.call!(auth_master_session_id)
4
+ auth_master_session = AuthMaster::Session.active.find_by(id: auth_master_session_id)
5
+ return if auth_master_session.blank?
6
+
7
+ AuthMaster::SessionService.logout!(auth_master_session)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ module AuthMaster
2
+ class PrepareTokenOperation < AuthMaster::AbstractOperation
3
+ def self.call!(target, uuid:)
4
+ auth_master_session = AuthMaster::SessionService.create!(target, uuid:)
5
+ return if auth_master_session.blank?
6
+
7
+ AuthMaster::TokenService.create!(auth_master_session)
8
+ # purpose = token_purpose_config(target)
9
+ # secret = secret_config(target)
10
+
11
+ # TokenGuard.encrypt(auth_master_session.id, purpose:, secret:)
12
+
13
+ # mailer = target_mailer_config(target)
14
+ # mailer_action = target_mailer_login_link_method(target)
15
+
16
+ # url = AuthMaster::Engine.routes.url_helpers.auth_master_link_url(
17
+ # target: target_name(target),
18
+ # token: token,
19
+ # host: Rails.application.config.action_mailer.default_url_options[:host]
20
+ # )
21
+
22
+ # mailer.with(email: target.email, url:).public_send(mailer_action).deliver_later
23
+
24
+ # auth_master_session
25
+ end
26
+ end
27
+ end
@@ -16,8 +16,13 @@ module AuthMaster
16
16
  end
17
17
 
18
18
  def activate!(auth_master_session)
19
+ # TODO: Save IP Address, User Agent, etc
19
20
  auth_master_session.active!
21
+ end
22
+
23
+ def logout!(auth_master_session)
20
24
  # TODO: Save IP Address, User Agent, etc
25
+ auth_master_session.logout!
21
26
  end
22
27
 
23
28
  private
@@ -0,0 +1,16 @@
1
+ require "token_guard"
2
+
3
+ module AuthMaster
4
+ class TokenService
5
+ extend AuthMaster::Config
6
+
7
+ class << self
8
+ def create!(auth_master_session)
9
+ purpose = token_purpose_config(auth_master_session.target)
10
+ secret = secret_config(auth_master_session.target)
11
+
12
+ TokenGuard.encrypt(auth_master_session.id, purpose:, secret:)
13
+ end
14
+ end
15
+ end
16
+ end
data/config/routes.rb CHANGED
@@ -1,11 +1,9 @@
1
1
  AuthMaster::Engine.routes.draw do
2
- get "/:target/login", to: "sessions#new", as: :auth_master_login
3
- post "/:target/login", to: "sessions#create"
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#activate"
9
-
10
- get "/:target/denied", to: "sessions#denied", as: :auth_master_denied
2
+ get "/:target/login", to: "sessions#new", as: :auth_master_login
3
+ post "/:target/login", to: "sessions#create"
4
+ get "/:target/sent", to: "sessions#sent", as: :auth_master_sent
5
+ get "/:target/link", to: "sessions#link", as: :auth_master_link
6
+ post "/:target/link", to: "sessions#activate"
7
+ get "/:target/denied", to: "sessions#denied", as: :auth_master_denied
8
+ delete "/:target/logout", to: "sessions#destroy", as: :auth_master_logout
11
9
  end
@@ -1,3 +1,3 @@
1
1
  module AuthMaster
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auth_master
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - vickodin
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-29 00:00:00.000000000 Z
10
+ date: 2025-04-30 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -64,8 +64,12 @@ files:
64
64
  - app/models/auth_master/session.rb
65
65
  - app/operations/auth_master/abstract_operation.rb
66
66
  - app/operations/auth_master/check_link_operation.rb
67
+ - app/operations/auth_master/login_by_token_operation.rb
68
+ - app/operations/auth_master/logout_operation.rb
69
+ - app/operations/auth_master/prepare_token_operation.rb
67
70
  - app/operations/auth_master/send_link_operation.rb
68
71
  - app/services/auth_master/session_service.rb
72
+ - app/services/auth_master/token_service.rb
69
73
  - app/views/auth_master/sessions/link.html.erb
70
74
  - app/views/auth_master/sessions/new.html.erb
71
75
  - app/views/auth_master/sessions/sent.html.erb