manyfold 0.1.5 → 0.1.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: 20aec86cdd49554cdb045892a1f03d0209e503852f9a1edd3da6e359b45478f0
4
- data.tar.gz: 06f400d8c1e7b24df66e3b9c3400147200de75c6f4be00389caa36e2e17c2881
3
+ metadata.gz: b5a866044e1f1c441f0945578738856e60ddb6c8fd575d44f60727c0855f6f0a
4
+ data.tar.gz: dbb7d9cb38f0c5725dceba6f0857ac3f92b114dfaa085b7071a77662c86fd6df
5
5
  SHA512:
6
- metadata.gz: f62596bde0d8fedde0dd4f953d5ee801203cf057460b4bb7a0afcb4c2cdde0198d09ff2e1d41ce6219526fa017cf2826bfc8546999489b054155b41af8a81516
7
- data.tar.gz: c9f6ea5ad920251fbe4a066691bc8ca400717ecc8d4338d1f1744605dbf572c3557dc6478a503ad3395bee6bb7cfbdf56560c77209130691cefee40f2aa9642a
6
+ metadata.gz: e4fe5d54b60775c2fe1832eca015e4ca1fd1929faa238941a48fbef505aba4bb78e3edc7dba4c3b58940de18100c32f40178460cbd0caef39f617dcb8dbbbbd0
7
+ data.tar.gz: ee99dca2f0d462def90f5d706ca75eaada0cbf7c370847ee3114ac4beeb9e6b92c14075d5c4aa324dcf2c898ae9a9646fad302c62d69ed3b8019924de6cb6b23
@@ -5,19 +5,6 @@ module Manyfold
5
5
  class AppGenerator < Rails::Generators::Base
6
6
  source_root File.expand_path("templates", __dir__)
7
7
 
8
- def gems
9
- if [run("grep debase Gemfile"), run("grep ruby-debug-ide Gemfile")].any?(&:blank?)
10
- gem_group :development, :test do
11
- gem "debase", "~> 0.2.9" if run("grep debase Gemfile").blank?
12
- gem "ruby-debug-ide", "~> 0.7.4" if run("grep ruby-debug-ide Gemfile").blank?
13
- end
14
- end
15
-
16
- Bundler.with_unbundled_env do
17
- run 'bundle install'
18
- end
19
- end
20
-
21
8
  def setup
22
9
  copy_file "lib/active_model/type/symbol.rb", "lib/active_model/type/symbol.rb"
23
10
  copy_file "config/initializers/active_model.rb", "config/initializers/active_model.rb"
@@ -7,18 +7,18 @@ module Manyfold
7
7
 
8
8
  def gems
9
9
  gem "bcrypt" if run("grep bcrypt Gemfile").blank?
10
+
10
11
  Bundler.with_unbundled_env do
11
12
  run 'bundle install'
12
13
  end
13
14
  end
14
15
 
15
16
  def setup
16
- copy_file "app/controllers/concerns/authentication.rb", "app/controllers/concerns/authentication.rb"
17
- copy_file "app/controllers/request_context/session_context.rb", "app/controllers/request_context/session_context.rb"
17
+ copy_file "app/controllers/concerns/authentication/controller.rb", "app/controllers/concerns/authentication/controller.rb"
18
+ copy_file "app/controllers/concerns/authentication/controller/rememberable.rb", "app/controllers/concerns/authentication/controller/rememberable.rb"
19
+ copy_file "app/controllers/concerns/authentication/controller/routing.rb", "app/controllers/concerns/authentication/controller/routing.rb"
18
20
 
19
- copy_file "app/lib/request_context/context/base.rb", "app/lib/request_context/context/base.rb"
20
- copy_file "app/lib/request_context/session/base.rb", "app/lib/request_context/session/base.rb"
21
- copy_file "app/lib/request_context/session/browser_session.rb", "app/lib/request_context/session/browser_session.rb"
21
+ copy_file "app/models/concerns/authentication/model/rememberable.rb", "app/models/concerns/authentication/model/rememberable.rb"
22
22
 
23
23
  inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do
24
24
  <<~CODE
@@ -29,21 +29,31 @@ module Manyfold
29
29
 
30
30
  inject_into_file "app/controllers/application_controller.rb", before: /^end/ do
31
31
  <<~CODE
32
-
33
- \tattr_accessor :request_context
34
-
35
- \tonly_allow_authenticated_access
32
+ \tallow_all_access
36
33
 
37
34
  \tdef request_context
38
- \t@request_context ||= RequestContext::SessionContext.new(RequestContext::Session::BrowserSession.new(session))
35
+ \treturn @request_context unless @request_context.nil?
36
+
37
+ \tuser = nil
38
+
39
+ \tif session[:user_id]
40
+ \tuser = User.find_by(id: session[:user_id])
41
+ \tend
42
+
43
+ \tif user.nil? && remembered_session
44
+ \tuser_to_remember = User.find_by(id: remembered_session[:data][:user_id])
45
+ \tuser = user_to_remember if user_to_remember&.remember?(remembered_session[:token])
46
+ \tend
47
+
48
+ \t@request_context = { user: user }
39
49
  \tend
40
50
 
41
51
  \tdef current_user
42
- \trequest_context.user
52
+ \trequest_context[:user]
43
53
  \tend
44
54
 
45
- \tdef authentication_user
46
- \trequest_context.user
55
+ \tdef authenticated?
56
+ \t!request_context[:user].nil?
47
57
  \tend
48
58
  CODE
49
59
  end
@@ -56,11 +66,11 @@ module Manyfold
56
66
  end
57
67
 
58
68
  double_space = ' '
59
- inject_into_file "config/etc/app.yml.erb", after: /default: &default/ do
69
+ inject_into_file "config/etc/app.yml", after: /default: &default/ do
60
70
  <<~CODE.chomp
61
71
 
62
- #{double_space}unauthenticated_root_path:
63
- #{double_space}authenticated_root_path:
72
+ #{double_space}unauthenticated_root_path: # TODO - Generated by Manyfold
73
+ #{double_space}authenticated_root_path: # TODO - Generated by Manyfold
64
74
  CODE
65
75
  end
66
76
 
@@ -76,6 +86,8 @@ module Manyfold
76
86
 
77
87
  inject_into_class 'app/models/user.rb', "User" do
78
88
  <<~CODE.chomp
89
+ \tinclude Authentication::Model::Rememberable
90
+
79
91
  \thas_secure_password
80
92
 
81
93
  CODE
@@ -0,0 +1,31 @@
1
+ module Authentication
2
+ module Controller
3
+ module Rememberable
4
+
5
+ def remember_session!(data, with:)
6
+ rememberable_token = with
7
+ cookies.encrypted.permanent[AppConfig.rememberable_token] = {
8
+ value: { token: rememberable_token, data: data },
9
+ httponly: true,
10
+ secure: AppConfig.secure_sessions,
11
+ same_site: :lax
12
+ }
13
+ end
14
+
15
+ def forget_session!
16
+ cookies.delete(AppConfig.rememberable_token)
17
+ end
18
+
19
+ def remembered_session
20
+ return @remembered_session unless @remembered_session.nil?
21
+ @remembered_session = cookies.encrypted[AppConfig.rememberable_token]
22
+
23
+ if @remembered_session.is_a?(Hash)
24
+ @remembered_session.deep_symbolize_keys!
25
+ end
26
+
27
+ @remembered_session
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,68 @@
1
+ module Authentication
2
+ module Controller
3
+ module Routing
4
+ extend ActiveSupport::Concern
5
+
6
+ private
7
+
8
+ def redirect_unauthenticated
9
+ redirect_to self.class.auth_redirect_paths[:unauthenticated]
10
+ end
11
+
12
+ def redirect_authenticated
13
+ redirect_to self.class.auth_redirect_paths[:authenticated]
14
+ end
15
+
16
+ class_methods do
17
+
18
+ def inherited(subclass)
19
+ super
20
+ subclass.instance_variable_set(:@auth_redirect_paths, auth_redirect_paths)
21
+ end
22
+
23
+ def allow_all_access(**options)
24
+ allow_authenticated_access(**options)
25
+ allow_unauthenticated_access(**options)
26
+ end
27
+
28
+ def only_allow_authenticated_access(**options)
29
+ allow_authenticated_access(**options)
30
+ restrict_unauthenticated_access(**options)
31
+ end
32
+
33
+ def only_allow_unauthenticated_access(**options)
34
+ allow_unauthenticated_access(**options)
35
+ restrict_authenticated_access(**options)
36
+ end
37
+
38
+ def restrict_unauthenticated_access(**options)
39
+ before_action :redirect_unauthenticated, unless: AppConfig.authenticated_method, **options
40
+ end
41
+
42
+ def allow_unauthenticated_access(**options)
43
+ skip_before_action :redirect_unauthenticated, unless: AppConfig.authenticated_method, raise: false, **options
44
+ end
45
+
46
+ def restrict_authenticated_access(**options)
47
+ before_action :redirect_authenticated, if: AppConfig.authenticated_method, **options
48
+ end
49
+
50
+ def allow_authenticated_access(**options)
51
+ skip_before_action :redirect_authenticated, if: AppConfig.authenticated_method, raise: false, **options
52
+ end
53
+
54
+ def unauthenticated_root_path(redirect_path)
55
+ auth_redirect_paths[:unauthenticated] = redirect_path
56
+ end
57
+
58
+ def authenticated_root_path(redirect_path)
59
+ auth_redirect_paths[:authenticated] = redirect_path
60
+ end
61
+
62
+ def auth_redirect_paths
63
+ @auth_redirect_paths ||= {}
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,8 @@
1
+ module Authentication
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ include Rememberable
6
+ include Routing
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ module Authentication
2
+ module Model
3
+ module Rememberable
4
+ extend ActiveSupport::Concern
5
+
6
+ def remember!
7
+ raw_token = SecureRandom.urlsafe_base64
8
+ update!(AppConfig.rememberable_column => BCrypt::Password.create(raw_token))
9
+ raw_token
10
+ end
11
+
12
+ def forget!
13
+ update!(AppConfig.rememberable_column => nil)
14
+ end
15
+
16
+ def rememberable_token
17
+ public_send(AppConfig.rememberable_column)
18
+ end
19
+
20
+ def remember?(raw_token)
21
+ return nil if raw_token.nil?
22
+ BCrypt::Password.new(rememberable_token).is_password?(raw_token)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -8,4 +8,4 @@ development:
8
8
  production:
9
9
  <<: *default
10
10
  application_name: <%= @application_name %>
11
- host:
11
+ host: # TODO - Generated by Manyfold
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Manyfold
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.6"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manyfold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bevan Holborn
@@ -20,11 +20,10 @@ files:
20
20
  - lib/generators/manyfold/app/templates/lib/active_model/type/symbol.rb
21
21
  - lib/generators/manyfold/authentication/USAGE
22
22
  - lib/generators/manyfold/authentication/authentication_generator.rb
23
- - lib/generators/manyfold/authentication/templates/app/controllers/concerns/authentication.rb
24
- - lib/generators/manyfold/authentication/templates/app/controllers/request_context/session_context.rb
25
- - lib/generators/manyfold/authentication/templates/app/lib/request_context/context/base.rb
26
- - lib/generators/manyfold/authentication/templates/app/lib/request_context/session/base.rb
27
- - lib/generators/manyfold/authentication/templates/app/lib/request_context/session/browser_session.rb
23
+ - lib/generators/manyfold/authentication/templates/app/controllers/concerns/authentication/controller.rb
24
+ - lib/generators/manyfold/authentication/templates/app/controllers/concerns/authentication/controller/rememberable.rb
25
+ - lib/generators/manyfold/authentication/templates/app/controllers/concerns/authentication/controller/routing.rb
26
+ - lib/generators/manyfold/authentication/templates/app/models/concerns/authentication/model/rememberable.rb
28
27
  - lib/generators/manyfold/authorization/USAGE
29
28
  - lib/generators/manyfold/authorization/authorization_generator.rb
30
29
  - lib/generators/manyfold/config/USAGE
@@ -1,32 +0,0 @@
1
- module Authentication
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- only_allow_authenticated_access
6
- end
7
-
8
- class_methods do
9
- def only_allow_unauthenticated_access(**options)
10
- skip_before_action :redirect_unauthenticated_user, raise: false, **options
11
- before_action :redirect_authenticated_user, **options
12
- end
13
-
14
- def only_allow_authenticated_access(**options)
15
- before_action :redirect_unauthenticated_user, **options
16
- end
17
-
18
- def allow_all_access(**options)
19
- skip_before_action :redirect_unauthenticated_user, raise: false, **options
20
- skip_before_action :redirect_authenticated_user, raise: false, **options
21
- end
22
- end
23
-
24
- private
25
- def redirect_unauthenticated_user(route = nil)
26
- redirect_to route || AppConfig.unauthenticated_root_path if current_user.nil?
27
- end
28
-
29
- def redirect_authenticated_user(route = nil)
30
- redirect_to route || AppConfig.authenticated_root_path if current_user
31
- end
32
- end
@@ -1,17 +0,0 @@
1
- class RequestContext::SessionContext < RequestContext::Context::Base
2
- attr_reader :user
3
-
4
- def sync
5
- if session[:user_id].nil? && @user
6
- @user = nil
7
- elsif @user || session[:user_id]
8
- if @user.nil? || @user.id != session[:user_id]
9
- @user = User.find_by(id: session[:user_id])
10
- else
11
- @user.reload
12
- end
13
- end
14
-
15
- session.close if @user.nil? || @user.session_key != session[:session_key]
16
- end
17
- end
@@ -1,35 +0,0 @@
1
- module RequestContext
2
- module Context
3
- class Base
4
- extend ActiveModel::Callbacks
5
-
6
- define_model_callbacks :initialize, only: [:after]
7
-
8
- attr_reader :session
9
-
10
- after_initialize :sync
11
-
12
- def initialize(session)
13
- raise NotImplementedError.new("`#{self.class.name}` is abstract and cannot be instantiated directly.") if self.class.name == RequestContext::Context::Base.name
14
-
15
- run_callbacks(:initialize) do
16
- @session = session
17
- end
18
- end
19
-
20
- def sync
21
- # Can be derived to specify how the session and the context should be synchronized
22
- end
23
-
24
- def update(data)
25
- session.update(data)
26
- sync
27
- end
28
-
29
- def close
30
- session.close
31
- sync
32
- end
33
- end
34
- end
35
- end
@@ -1,35 +0,0 @@
1
- module RequestContext
2
- module Session
3
- class Base
4
- include ActiveModel::Callbacks
5
-
6
- attr_reader :session_store
7
-
8
- def initialize(session_store)
9
- raise NotImplementedError.new("`#{self.class.name}` is abstract and cannot be instantiated directly.") if self.class.name == RequestContext::Session::Base.name
10
- @session_store = session_store
11
- end
12
-
13
- def [](key)
14
- raise NotImplementedError
15
- end
16
-
17
- def []=(key, value)
18
- raise NotImplementedError
19
- end
20
-
21
- def delete(key)
22
- raise NotImplementedError
23
- end
24
-
25
- def update(data)
26
- raise NotImplementedError
27
- end
28
-
29
- def close
30
- raise NotImplementedError
31
- end
32
-
33
- end
34
- end
35
- end
@@ -1,27 +0,0 @@
1
- module RequestContext
2
- module Session
3
- class BrowserSession < RequestContext::Session::Base
4
-
5
- def [](key)
6
- session_store[key]
7
- end
8
-
9
- def []=(key, value)
10
- session_store[key] = value
11
- end
12
-
13
- def delete(key)
14
- session_store.delete(key)
15
- end
16
-
17
- def close
18
- session_store.destroy
19
- end
20
-
21
- def update(data)
22
- session_store.update(data)
23
- end
24
-
25
- end
26
- end
27
- end