authstrategies 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NmY5MzNhOTc3ZDVlMGNiODg4OTkxZmY4ZjU2MTE1MmFjOTdmNGI2OQ==
4
+ NmUzMWJmYmNhMzgwOTNkYzllMTc3ZDA1OWU1YzcxNzdkYzFhYTZmMw==
5
5
  data.tar.gz: !binary |-
6
- ZjE4YmVhYjhhNDVhMmU2N2ZhOTBiZmRlNmU5M2VlYWRhM2E4OGE0Mg==
6
+ NWJmMWNjMzUzMTUwNjg3NWMyZjUyNGJlMDkxYzMxMWM5NGU2ZTA1Mg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OWMwZDJlZTQ5NTRlNzUyNTI4NWQ3ZjY0ODQ3MGQzYjAyNTU0ZmM5NWY3Y2Zi
10
- NWFmMWNlMTI0N2IwZGNmYjNiOTRjMWFkOWMxNDI4NGNhYzg2ODZjNDIyYTA2
11
- Y2EzNjVkNTQxZjlhMTAzOGNmZjk5ZGNmNTVmMzE5OTI3ZGQwNjQ=
9
+ NTlhMTlkZDM1YTI1NDZhNWEyNjg4NDY2YzA2OTM0YWVmYzc1NGQ0ZWU5ZDQz
10
+ MmM2YWU0YmU4N2ZmYTRkMzc5ZDJlMjk0MmMxZDJiNmZhNDllNGUzMWZkNmU1
11
+ OWIxM2MyZTM3MTE4NmU2MDk3MjczYWVhOWYyNzVhMjUxMGI5MGY=
12
12
  data.tar.gz: !binary |-
13
- NzM4MDJiNzQzMWFhNmQ3MGY1YWZmMTBiZjAyZjI2ZjdhNWM3NGQwMzhiMjkw
14
- MGJiNjliMDc1YjliYTZlOGI0NzEzN2FmOWJmNjQ3ZDQxOTU5ZWM5ZDNlMTRj
15
- MTczMDEyZjQxZjczNmE5ODMxNDQ0NDYyZTc1ZTEwNjFjNWI1MTI=
13
+ ZmUwYzBmOGUxMDdhOTdkMDdmMjE0NWM4ZTgwOTA4ZDZlZDY2ZjAxMGEyODk2
14
+ ZmQ1MTIzN2VjN2ZhZjk2NmNlYjE5ODEwNzg3OWY4MmJkZDEzMThmMjM4YWYw
15
+ OTFiY2Y1Njk3NDA0ODdkNzFmMWE2YTYxY2M0NGU0MWViNTU2ZmQ=
data/README.md CHANGED
@@ -25,14 +25,14 @@ Authstrategies uses sinatra-activerecord as orm. There is currently no rake task
25
25
  t.string :email, :null => false, :defautl => ""
26
26
  t.string :encrypted_password, :null => false, :default => ""
27
27
 
28
- t.string remember_token
28
+ t.string :remember_token
29
29
  t.boolean :remember_me
30
30
 
31
31
  t.timestamps
32
32
  end
33
33
 
34
- add_index :users, :email, :unique => true
35
- add_index :remember_token, :unique => true
34
+ add_index :users, :email, :unique => true
35
+ add_index :users, :remember_token, :unique => true
36
36
  end
37
37
 
38
38
 
@@ -68,6 +68,88 @@ To logout the user class logout.
68
68
  login_path returns the login path as a string
69
69
  logout_path returns the logout path as a string
70
70
  signup_path returns the signup path as a string
71
+
72
+ ## Callbacks
73
+
74
+ after_set_user
75
+ This is called every time the user is set. The user is set:
76
+ => on each request when they are accessed for the first time via env['warden'].user
77
+ => when the user is initially authenticated
78
+ => when the user is set via the set_user method
79
+ Courtesy of Warden, for more information check the warden callbacks wiki
80
+
81
+ after_authentication
82
+
83
+ Executed every time the user is authenticated
84
+ (first time in each session).
85
+ Courtesy of Warden, for more information check the warden callbacks wiki
86
+
87
+ before_login_failure
88
+
89
+ This callback is run right before the failure application is called.
90
+ Courtesy of Warden, for more information
91
+ check the warden callbacks wiki
92
+
93
+ after_login_failure
94
+
95
+ This is called in the failure application
96
+ Useful for redirecting the user after he logs in
97
+ 2 params are passed to this callback
98
+ =>request - the request data
99
+ =>response - the response data
100
+
101
+ before_logout
102
+
103
+ This callback is run before each user is logged out.
104
+ Courtesy of Warden, for more information
105
+ check the warden callbacks wiki
106
+
107
+ after_logout
108
+
109
+ This is called after the user is logged out.
110
+ Useful for redirecting the user after logging out
111
+ 2 parameters are passed to this callback
112
+ =>request - the request data
113
+ =>response - the response data
114
+
115
+ after_logout_path
116
+
117
+ This defines a path to redirect the user to
118
+ after he logs out and a flash message to print
119
+ path default is root path
120
+ message default is 'Logged out successfully!'
121
+
122
+ after_login
123
+
124
+ This is called each time after the user logs in
125
+ 3 parameters are passed to this callback
126
+ =>current_user - the user that hase just been set
127
+ =>request - the request data
128
+ =>response - the response data
129
+
130
+ after_login_path
131
+
132
+ This defines a path to redirect the user to
133
+ after he logs in and a flash message to print
134
+ path default is root path
135
+ message default is 'Logged in successfully!'
136
+
137
+ after_signup
138
+
139
+ This is called after the user is saved into
140
+ the database
141
+ 3 parameters are passed to this callback
142
+ =>user - the user that just signed up
143
+ =>request - the request data
144
+ =>response - the response data
145
+ Also since the user is set to session via env['warden'].set_user
146
+ after_set_user is also called after the user signs up
147
+
148
+ after_signup_path
149
+ This defines a path to redirect the user to
150
+ after he signs up and a flash message to print
151
+ path default is root path
152
+ message default is 'Successfully signed up!
71
153
  ## Contributing
72
154
 
73
155
  1. Fork it
@@ -26,7 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_runtime_dependency "protected_attributes"
27
27
  spec.add_runtime_dependency "warden"
28
28
  spec.add_runtime_dependency "bcrypt-ruby"
29
- spec.add_runtime_dependency "rack"
30
29
  spec.add_runtime_dependency "rack-flash3", '1.0.5'
31
30
 
32
31
  end
@@ -1,64 +1,68 @@
1
1
  module Authstrategies
2
2
  class Middleware < Sinatra::Base
3
- register Base
4
- register RememberMe
3
+ register Base
4
+ register RememberMe
5
5
 
6
- get '/login/?' do
7
- redirect '/' if authenticated?
8
- erb :login
9
- end
6
+ include Manager
10
7
 
11
- post '/login' do
12
- redirect '/' if authenticated?
13
- authenticate!
14
- if authenticated?
15
- if params["remember_me"] == "on"
16
- current_user.remember_me!
17
- response.set_cookie("authstrategies",
18
- :value => current_user.remember_token,
19
- :expires => Time.now + 7 * 24 * 3600
20
- )
21
- end
22
- flash[:notice] = "Logged in successfully!"
23
- redirect '/'
24
- else
25
- flash[:error] = env["warden"].message
26
- redirect '/login'
27
- end
28
- end
8
+ get '/login/?' do
9
+ redirect '/' if authenticated?
10
+ erb :login
11
+ end
29
12
 
30
- get '/logout/?' do
31
- if authenticated?
32
- current_user.forget_me!
33
- response.delete_cookie("authstrategies")
34
- logout
35
- flash[:notice] = "Successfully logged out!"
36
- redirect '/'
37
- end
38
- redirect '/'
39
- end
13
+ post '/login' do
14
+ redirect '/' if authenticated?
15
+ authenticate!
16
+ if authenticated?
17
+ if params["remember_me"] == "on"
18
+ current_user.remember_me!
19
+ response.set_cookie("authstrategies",
20
+ :value => current_user.remember_token,
21
+ :expires => Time.now + 7 * 24 * 3600
22
+ )
23
+ end
24
+ Manager.call :after_login, [current_user, request, response]
25
+ flash[:notice] = Manager._after_login_msg
26
+ redirect Manager._after_login_path
27
+ end
28
+ end
40
29
 
41
- post '/unauthenticated' do
42
- flash[:error] = env["warden"].message
43
- redirect '/login'
44
- end
30
+ get '/signup/?' do
31
+ redirect '/' if authenticated?
32
+ erb :signup
33
+ end
45
34
 
46
- get '/signup/?' do
47
- redirect '/' if authenticated?
48
- erb :signup
49
- end
35
+ post '/signup' do
36
+ redirect '/' if authenticated?
37
+ user = User.new(params)
38
+ if user.valid?
39
+ user.save
40
+ env['warden'].set_user(user)
41
+ Manager.call :after_signup, [user, request, response]
42
+ flash[:notice] = Manager._after_signup_msg
43
+ redirect Manager._after_signup_path
44
+ else
45
+ flash[:error] = user.errors.messages
46
+ redirect '/signup'
47
+ end
48
+ end
50
49
 
51
- post '/signup' do
52
- redirect '/' if authenticated?
53
- user = User.new(params)
54
- if user.valid?
55
- user.save
56
- flash[:notice] = "Successfully signed up!"
57
- redirect '/'
58
- else
59
- flash[:error] = user.errors.messages
60
- redirect '/signup'
61
- end
62
- end
50
+ get '/logout/?' do
51
+ if authenticated?
52
+ current_user.forget_me!
53
+ response.delete_cookie("authstrategies")
54
+ logout
55
+ Manager.call :after_logout, [request, response]
56
+ flash[:notice] = Manager._after_logout_msg
57
+ redirect Manager._after_logout_path
58
+ end
59
+ redirect '/'
60
+ end
61
+
62
+ post '/unauthenticated' do
63
+ Manager.call :after_login_failure, [request, response]
64
+ flash[:error] = env["warden"].message
65
+ redirect '/login'
66
+ end
63
67
  end
64
68
  end
@@ -20,8 +20,8 @@ class User < ActiveRecord::Base
20
20
  end
21
21
 
22
22
  def password= password
23
- @password = Password.create(password)
24
- self.encrypted_password = @password
23
+ @password = password
24
+ self.encrypted_password = Password.create(@password)
25
25
  end
26
26
 
27
27
  def authenticate request
@@ -1,3 +1,3 @@
1
1
  module Authstrategies
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -3,6 +3,6 @@
3
3
  <form action="login" method="post">
4
4
  <p>Username:<input type="text" name="email" /></p>
5
5
  <p>Password:<input type="password" name="password" /></p>
6
- <p>Remember Me: <input type="radio" name="remember_me" /></p>
6
+ <p>Remember Me: <input type="checkbox" name="remember_me" /></p>
7
7
  <p><input type="submit" name="Submit" /></p>
8
8
  </form>
@@ -11,26 +11,179 @@ require "authstrategies/remember_me.rb"
11
11
  require "authstrategies/models/user.rb"
12
12
 
13
13
  module Authstrategies
14
+ module Manager
15
+
16
+ @@callbacks = {}
17
+
18
+ @@_after_login_path = '/'
19
+ @@_after_login_msg = 'Successfully logged in!'
20
+
21
+ @@_after_logout_path = '/'
22
+ @@_after_logout_msg = 'Successfully logged out!'
23
+
24
+ @@_after_signup_path = '/'
25
+ @@_after_signup_msg = 'Successfully signed up!'
26
+
27
+ def self.registered? hook
28
+ @@callbacks.has_key? hook
29
+ end
30
+
31
+ def self.register hook, &block
32
+ if @@callbacks[hook].class == Array
33
+ @@callbacks[hook].push block
34
+ else
35
+ @@callbacks[hook] = [block]
36
+ end
37
+ end
38
+
39
+ def self.call hook, args = []
40
+ if @@callbacks.has_key? hook
41
+ @@callbacks[hook].each do |callback|
42
+ callback.call(args)
43
+ end
44
+ end
45
+ end
46
+
47
+ # This is called every time the user is set. The user is set:
48
+ # => on each request when they are accessed for the first time via env['warden'].user
49
+ # => when the user is initially authenticated
50
+ # => when the user is set via the set_user method
51
+ # Courtesy of Warden, for more information
52
+ # check the warden callbacks wiki
53
+ def self.after_set_user &block
54
+ Warden::Manager.after_set_user do |user, auth, opts|
55
+ yield(user, auth, opts)
56
+ end
57
+ end
58
+
59
+ # Executed every time the user is authenticated
60
+ # (first time in each session).
61
+ # Courtesy of Warden, for more information
62
+ # check the warden callbacks wiki
63
+ def self.after_authentication &block
64
+ Warden::Manager.after_authentication do |user, auth, opts|
65
+ yield(user, auth, opts)
66
+ end
67
+ end
68
+
69
+ # This callback is run right before the failure application is called.
70
+ # Courtesy of Warden, for more information
71
+ # check the warden callbacks wiki
72
+ def self.before_login_failure &block
73
+ Warden::Manager.before_failure do |env, opts|
74
+ yield(env, opts)
75
+ end
76
+ end
77
+
78
+ # This is called in the failure application
79
+ # Useful for redirecting the user after he logs in
80
+ # 2 params are passed to this callback
81
+ # =>request - the request data
82
+ # =>response - the response data
83
+ def self.after_login_failure &block
84
+ self.register :after_login_failure, &block
85
+ end
86
+
87
+ #This callback is run before each user is logged out.
88
+ # Courtesy of Warden, for more information
89
+ # check the warden callbacks wiki
90
+ def self.before_logout &block
91
+ Warden::Manager.before_logout do |user, auth, opts|
92
+ yield(user, auth, opts)
93
+ end
94
+ end
95
+
96
+ # This is called after the user is logged out.
97
+ # Useful for redirecting the user after logging out
98
+ # 2 parameters are passed to this callback
99
+ # =>request - the request data
100
+ # =>response - the response data
101
+ def self.after_logout &block
102
+ self.register :after_logout, &block
103
+ end
104
+
105
+ # This defines a path to redirect the user to
106
+ # after he logs out and a flash message to print
107
+ # path default is root path
108
+ # message default is 'Logged out successfully!'
109
+ def self.after_logout_path path, message
110
+ @@_after_logout_path, @@after_logout_msg = path, message
111
+ end
112
+
113
+ def self._after_logout_path
114
+ @@_after_logout_path
115
+ end
116
+
117
+ def self._after_logout_msg
118
+ @@_after_logout_msg
119
+ end
120
+
121
+ # This is called each time after the user logs in
122
+ # 3 parameters are passed to this callback
123
+ # =>current_user - the user that hase just been set
124
+ # =>request - the request data
125
+ # =>response - the response data
126
+ def self.after_login &block
127
+ self.register :after_login, &block
128
+ end
129
+
130
+ # This defines a path to redirect the user to
131
+ # after he logs in and a flash message to print
132
+ # path default is root path
133
+ # message default is 'Logged in successfully!'
134
+ def self.after_login_path path, message
135
+ @@_after_login_path, @@after_login_msg = path, message
136
+ end
137
+
138
+ def self._after_login_path
139
+ @@_after_login_path
140
+ end
141
+
142
+ def self._after_login_msg
143
+ @@after_login_msg
144
+ end
145
+
146
+ # This is called after the user is saved into
147
+ # the database
148
+ # 3 parameters are passed to this callback
149
+ # =>user - the user that just signed up
150
+ # =>request - the request data
151
+ # =>response - the response data
152
+ # Also since the user is set to session via env['warden'].set_user
153
+ # after_set_user is also called after the user signs up
154
+ def self.after_signup &block
155
+ self.register :after_signup, &block
156
+ end
157
+
158
+ # This defines a path to redirect the user to
159
+ # after he signs up and a flash message to print
160
+ # path default is root path
161
+ # message default is 'Successfully signed up!
162
+ def self.after_signup_path path, message
163
+ @@_after_signup_path, @@after_signup_msg = path, message
164
+ end
165
+
166
+ end
167
+
14
168
  module Base
15
- def self.registered(app)
169
+ def self.registered(app)
16
170
  app.helpers Helpers
171
+ app.use Warden::Manager do |manager|
172
+ manager.failure_app = app
173
+ manager.default_strategies :password
174
+ end
17
175
 
18
- app.use Warden::Manager do |manager|
19
- manager.failure_app = app
20
- manager.default_strategies :password, :remember_me
21
- end
22
-
23
- Warden::Manager.before_failure do |env,opts|
24
- env['REQUEST_METHOD'] = 'POST'
176
+ Manager.before_login_failure do |env,opts|
177
+ env['REQUEST_METHOD'] = 'POST'
25
178
  end
26
179
  Warden::Strategies.add(:password, PasswordStrategy)
27
- end
180
+ end
28
181
  end
29
182
 
30
183
  module RememberMe
31
184
  def self.registered(app)
32
185
  Warden::Strategies.add(:remember_me, RememberMeStrategy)
33
- end
34
- end
186
+ end
187
+ end
35
188
  end
36
189
  require "authstrategies/middleware.rb"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authstrategies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dobromir Ivanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-24 00:00:00.000000000 Z
11
+ date: 2013-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,20 +108,6 @@ dependencies:
108
108
  - - ! '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: rack
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ! '>='
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :runtime
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ! '>='
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: rack-flash3
127
113
  requirement: !ruby/object:Gem::Requirement