hanami-authentication 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62e60be3cc945a408fffcae2ef4774a2163a7def
4
- data.tar.gz: bc8b07543ec3a6aed09a1bcd58e1838529780a76
3
+ metadata.gz: 8b10f080ce3f404155ac3587c85b888ad678a21a
4
+ data.tar.gz: 43441401575e80b96f300cead6fe346fe63d17c2
5
5
  SHA512:
6
- metadata.gz: e5f82f43e0380ead02a41d6d71fc6c9cef30b633470ba621e836afa4bcc38b57a0f0c23f53416d80071aa399c84af01a438e13d3da85f26fa8918ef2d8e933c0
7
- data.tar.gz: 5eef9c27d5b23856613f0f4e80c5d7271907dc8514456e7f10fe0f3493daffe2c4e0891aabd6b9101f51f974e887d9124bcd5e744b538b11d03f50979da1c64f
6
+ metadata.gz: 183dcd04ce198d45cc797252f1a18971bd6fcebbb7d4f18ee2b03dd313ff217072612c100a44d703f63d0a7b4d6258717ab260a3d53bbd9de29b9eef1825ec2f
7
+ data.tar.gz: b87f623a4b60bec0727850cd3a5be0b818939086c42f2e6de59bb7ecbdfb41d1fff7d9c954e5c023796837b566cf414e8b4c890e43b58bc65201fd7a24399766
@@ -0,0 +1 @@
1
+ 2.4.1
data/README.md CHANGED
@@ -15,6 +15,29 @@ gem 'hanami-authentication'
15
15
 
16
16
  ## Usage
17
17
 
18
+ ### Setup
19
+ ```ruby
20
+ # application.rb
21
+ controller.prepare do
22
+ include Hanami::Authentication
23
+ # before authenticate >> This will use callbacks if authentication is failed.
24
+ # before authenticate! >> This forced to halt by 401 if authentication is failed.
25
+
26
+ after_session_expired do # This will be called if authenticate method is called when session is expired.
27
+ flash[:error] = 'The session is expired.'
28
+ redirect_to routes.root_path
29
+ end
30
+
31
+ after_session_failed do # This will be called if authenticate method is called when user has not logged in.
32
+ flash[:error] = 'Please login'
33
+ redirect_to routes.root_path
34
+ end
35
+ end
36
+ ```
37
+
38
+
39
+ ### Methods
40
+
18
41
  TBD
19
42
 
20
43
  ## Contributing
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ['lib']
24
24
 
25
25
  spec.add_dependency 'bcrypt', '~> 3.1'
26
+ spec.add_dependency 'hanami-utils', '~> 1.0'
26
27
 
27
28
  spec.add_development_dependency 'bundler', '~> 1.15'
28
29
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -7,14 +7,12 @@ module Hanami
7
7
  private
8
8
 
9
9
  def login(user, remember_me: false, expire_seconds: 1 * 60 * 60)
10
- return if authenticated?
11
10
  session[:current_user] = user
12
11
  session[:session_created_at] = Time.now + expire_seconds
13
12
  session[:remember_me] = remember_me
14
13
  end
15
14
 
16
15
  def logout
17
- return unless authenticated?
18
16
  session[:current_user] = nil
19
17
  session[:session_created_at] = nil
20
18
  session[:remember_me] = nil
@@ -42,11 +40,9 @@ module Hanami
42
40
  def authenticate
43
41
  if session_expired?
44
42
  logout
45
- flash[:error] = 'セッションの有効期限が切れました'
46
- redirect_to routes.root_path
43
+ self.class.after_session_expired_callbacks.run(self)
47
44
  elsif !authenticated?
48
- flash[:error] = 'ログインしてください'
49
- redirect_to routes.root_path
45
+ self.class.after_authentication_failed_callbacks.run(self)
50
46
  end
51
47
  end
52
48
 
@@ -57,5 +53,33 @@ module Hanami
57
53
  def session_expired?
58
54
  !session[:remember_me] && session[:session_expired_at] && session[:session_expired_at] < Time.now
59
55
  end
56
+
57
+ def self.included(base)
58
+ base.class_eval do
59
+ extend ClassMethods
60
+ end
61
+ end
62
+
63
+ module ClassMethods
64
+ def self.extended(base)
65
+ base.class_eval do
66
+ include Utils::ClassAttribute
67
+
68
+ class_attribute :after_session_expired_callbacks
69
+ self.after_session_expired_callbacks = Utils::Callbacks::Chain.new
70
+
71
+ class_attribute :after_authentication_failed_callbacks
72
+ self.after_authentication_failed_callbacks = Utils::Callbacks::Chain.new
73
+ end
74
+ end
75
+
76
+ def after_session_expired(*callbacks, &blk)
77
+ after_session_expired_callbacks.append(*callbacks, &blk)
78
+ end
79
+
80
+ def after_authentication_failed(*callbacks, &blk)
81
+ after_authentication_failed_callbacks.append(*callbacks, &blk)
82
+ end
83
+ end
60
84
  end
61
85
  end
@@ -1,5 +1,5 @@
1
1
  module Hanami
2
2
  module Authentication
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-authentication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LegalForce Inc.
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hanami-utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -74,6 +88,7 @@ extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
90
  - ".gitignore"
91
+ - ".ruby-version"
77
92
  - ".travis.yml"
78
93
  - CODE_OF_CONDUCT.md
79
94
  - Gemfile
@@ -103,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
118
  version: '0'
104
119
  requirements: []
105
120
  rubyforge_project:
106
- rubygems_version: 2.4.5.1
121
+ rubygems_version: 2.6.11
107
122
  signing_key:
108
123
  specification_version: 4
109
124
  summary: A simple authentication module for hanami.