rodauth 0.10.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +146 -0
  3. data/README.rdoc +644 -220
  4. data/Rakefile +99 -11
  5. data/doc/account_expiration.rdoc +55 -0
  6. data/doc/base.rdoc +104 -0
  7. data/doc/change_login.rdoc +29 -0
  8. data/doc/change_password.rdoc +26 -0
  9. data/doc/close_account.rdoc +31 -0
  10. data/doc/confirm_password.rdoc +22 -0
  11. data/doc/create_account.rdoc +34 -0
  12. data/doc/disallow_password_reuse.rdoc +37 -0
  13. data/doc/email_base.rdoc +19 -0
  14. data/doc/jwt.rdoc +35 -0
  15. data/doc/lockout.rdoc +83 -0
  16. data/doc/login.rdoc +27 -0
  17. data/doc/login_password_requirements_base.rdoc +50 -0
  18. data/doc/logout.rdoc +21 -0
  19. data/doc/otp.rdoc +100 -0
  20. data/doc/password_complexity.rdoc +50 -0
  21. data/doc/password_expiration.rdoc +52 -0
  22. data/doc/password_grace_period.rdoc +10 -0
  23. data/doc/recovery_codes.rdoc +60 -0
  24. data/doc/release_notes/1.0.0.txt +443 -0
  25. data/doc/remember.rdoc +82 -0
  26. data/doc/reset_password.rdoc +70 -0
  27. data/doc/session_expiration.rdoc +27 -0
  28. data/doc/single_session.rdoc +43 -0
  29. data/doc/sms_codes.rdoc +119 -0
  30. data/doc/two_factor_base.rdoc +27 -0
  31. data/doc/verify_account.rdoc +70 -0
  32. data/doc/verify_account_grace_period.rdoc +15 -0
  33. data/doc/verify_change_login.rdoc +9 -0
  34. data/lib/roda/plugins/rodauth.rb +3 -262
  35. data/lib/rodauth.rb +260 -0
  36. data/lib/rodauth/features/account_expiration.rb +108 -0
  37. data/lib/rodauth/features/base.rb +479 -0
  38. data/lib/rodauth/features/change_login.rb +77 -0
  39. data/lib/rodauth/features/change_password.rb +66 -0
  40. data/lib/rodauth/features/close_account.rb +82 -0
  41. data/lib/rodauth/features/confirm_password.rb +51 -0
  42. data/lib/rodauth/features/create_account.rb +128 -0
  43. data/lib/rodauth/features/disallow_password_reuse.rb +82 -0
  44. data/lib/rodauth/features/email_base.rb +63 -0
  45. data/lib/rodauth/features/jwt.rb +151 -0
  46. data/lib/rodauth/features/lockout.rb +262 -0
  47. data/lib/rodauth/features/login.rb +61 -0
  48. data/lib/rodauth/features/login_password_requirements_base.rb +123 -0
  49. data/lib/rodauth/features/logout.rb +37 -0
  50. data/lib/rodauth/features/otp.rb +338 -0
  51. data/lib/rodauth/features/password_complexity.rb +89 -0
  52. data/lib/rodauth/features/password_expiration.rb +111 -0
  53. data/lib/rodauth/features/password_grace_period.rb +46 -0
  54. data/lib/rodauth/features/recovery_codes.rb +240 -0
  55. data/lib/rodauth/features/remember.rb +200 -0
  56. data/lib/rodauth/features/reset_password.rb +207 -0
  57. data/lib/rodauth/features/session_expiration.rb +55 -0
  58. data/lib/rodauth/features/single_session.rb +87 -0
  59. data/lib/rodauth/features/sms_codes.rb +498 -0
  60. data/lib/rodauth/features/two_factor_base.rb +135 -0
  61. data/lib/rodauth/features/verify_account.rb +232 -0
  62. data/lib/rodauth/features/verify_account_grace_period.rb +76 -0
  63. data/lib/rodauth/features/verify_change_login.rb +20 -0
  64. data/lib/rodauth/migrations.rb +130 -0
  65. data/lib/rodauth/version.rb +9 -0
  66. data/spec/account_expiration_spec.rb +90 -0
  67. data/spec/all.rb +1 -0
  68. data/spec/change_login_spec.rb +149 -0
  69. data/spec/change_password_spec.rb +177 -0
  70. data/spec/close_account_spec.rb +162 -0
  71. data/spec/confirm_password_spec.rb +70 -0
  72. data/spec/create_account_spec.rb +127 -0
  73. data/spec/disallow_password_reuse_spec.rb +84 -0
  74. data/spec/lockout_spec.rb +228 -0
  75. data/spec/login_spec.rb +188 -0
  76. data/spec/migrate/001_tables.rb +103 -16
  77. data/spec/migrate/002_account_password_hash_column.rb +11 -0
  78. data/spec/migrate_password/001_tables.rb +60 -42
  79. data/spec/migrate_travis/001_tables.rb +116 -0
  80. data/spec/password_complexity_spec.rb +108 -0
  81. data/spec/password_expiration_spec.rb +243 -0
  82. data/spec/password_grace_period_spec.rb +93 -0
  83. data/spec/remember_spec.rb +424 -0
  84. data/spec/reset_password_spec.rb +185 -0
  85. data/spec/rodauth_spec.rb +57 -980
  86. data/spec/session_expiration_spec.rb +58 -0
  87. data/spec/single_session_spec.rb +107 -0
  88. data/spec/spec_helper.rb +202 -0
  89. data/spec/two_factor_spec.rb +1310 -0
  90. data/spec/verify_account_grace_period_spec.rb +135 -0
  91. data/spec/verify_account_spec.rb +142 -0
  92. data/spec/verify_change_login_spec.rb +46 -0
  93. data/spec/views/login.str +2 -2
  94. data/templates/add-recovery-codes.str +2 -0
  95. data/templates/button.str +5 -0
  96. data/templates/change-login.str +5 -18
  97. data/templates/change-password.str +6 -14
  98. data/templates/close-account.str +3 -6
  99. data/templates/confirm-password.str +4 -14
  100. data/templates/create-account.str +6 -30
  101. data/templates/login-confirm-field.str +6 -0
  102. data/templates/login-field.str +6 -0
  103. data/templates/login.str +5 -19
  104. data/templates/logout.str +2 -6
  105. data/templates/otp-auth-code-field.str +6 -0
  106. data/templates/otp-auth.str +8 -0
  107. data/templates/otp-disable.str +6 -0
  108. data/templates/otp-setup.str +21 -0
  109. data/templates/password-confirm-field.str +6 -0
  110. data/templates/password-field.str +6 -0
  111. data/templates/recovery-auth.str +12 -0
  112. data/templates/recovery-codes.str +6 -0
  113. data/templates/remember.str +8 -12
  114. data/templates/reset-password-request.str +2 -2
  115. data/templates/reset-password.str +4 -18
  116. data/templates/sms-auth.str +6 -0
  117. data/templates/sms-code-field.str +6 -0
  118. data/templates/sms-confirm.str +7 -0
  119. data/templates/sms-disable.str +7 -0
  120. data/templates/sms-request.str +5 -0
  121. data/templates/sms-setup.str +12 -0
  122. data/templates/unlock-account-request.str +3 -7
  123. data/templates/unlock-account.str +4 -7
  124. data/templates/verify-account-resend.str +2 -2
  125. data/templates/verify-account.str +2 -6
  126. metadata +191 -29
  127. data/lib/roda/plugins/rodauth/base.rb +0 -428
  128. data/lib/roda/plugins/rodauth/change_login.rb +0 -48
  129. data/lib/roda/plugins/rodauth/change_password.rb +0 -42
  130. data/lib/roda/plugins/rodauth/close_account.rb +0 -42
  131. data/lib/roda/plugins/rodauth/create_account.rb +0 -92
  132. data/lib/roda/plugins/rodauth/lockout.rb +0 -292
  133. data/lib/roda/plugins/rodauth/login.rb +0 -81
  134. data/lib/roda/plugins/rodauth/logout.rb +0 -36
  135. data/lib/roda/plugins/rodauth/remember.rb +0 -226
  136. data/lib/roda/plugins/rodauth/reset_password.rb +0 -205
  137. data/lib/roda/plugins/rodauth/verify_account.rb +0 -228
@@ -1,81 +0,0 @@
1
- class Roda
2
- module RodaPlugins
3
- module Rodauth
4
- Login = Feature.define(:login) do
5
- route 'login'
6
- notice_flash "You have been logged in"
7
- error_flash "There was an error logging in"
8
- view 'login', 'Login'
9
- after
10
- additional_form_tags
11
- button 'Login'
12
- redirect
13
-
14
- auth_value_methods :invalid_password_message, :login_form_footer
15
- auth_methods(
16
- :after_login_failure,
17
- :before_login_attempt,
18
- :password_match?
19
- )
20
-
21
- get_block do |r, auth|
22
- auth.login_view
23
- end
24
-
25
- post_block do |r, auth|
26
- auth.clear_session
27
-
28
- if auth._account_from_login(r[auth.login_param].to_s)
29
- auth.before_login_attempt
30
-
31
- if auth.open_account?
32
- if auth.password_match?(r[auth.password_param].to_s)
33
- auth.update_session
34
- auth.after_login
35
- auth.set_notice_flash auth.login_notice_flash
36
- r.redirect auth.login_redirect
37
- else
38
- auth.after_login_failure
39
- @password_error = auth.invalid_password_message
40
- end
41
- else
42
- @login_error = auth.unverified_account_message
43
- end
44
- else
45
- @login_error = auth.no_matching_login_message
46
- end
47
-
48
- auth.set_error_flash auth.login_error_flash
49
- auth.login_view
50
- end
51
-
52
- def before_login_attempt
53
- end
54
-
55
- def after_login_failure
56
- end
57
-
58
- def login_form_footer
59
- ""
60
- end
61
-
62
- def invalid_password_message
63
- "invalid password"
64
- end
65
-
66
- def password_match?(password)
67
- require 'bcrypt'
68
- if account_password_hash_column
69
- BCrypt::Password.new(account.send(account_password_hash_column)) == password
70
- else
71
- id = account.send(account_id)
72
- if salt = db.get{rodauth_get_salt(id)}
73
- hash = BCrypt::Engine.hash_secret(password, salt)
74
- db.get{rodauth_valid_password_hash(id, hash)}
75
- end
76
- end
77
- end
78
- end
79
- end
80
- end
81
- end
@@ -1,36 +0,0 @@
1
- class Roda
2
- module RodaPlugins
3
- module Rodauth
4
- Logout = Feature.define(:logout) do
5
- route 'logout'
6
- notice_flash "You have been logged out"
7
- view 'logout', 'Logout'
8
- additional_form_tags
9
- after
10
- button 'Logout'
11
- redirect{require_login_redirect}
12
-
13
- auth_methods :logout
14
-
15
- get_block do |r, auth|
16
- auth.logout_view
17
- end
18
-
19
- post_block do |r, auth|
20
- auth.logout
21
- auth.after_logout
22
- auth.set_notice_flash auth.logout_notice_flash
23
- r.redirect auth.logout_redirect
24
- end
25
-
26
- def logout
27
- clear_session
28
- end
29
-
30
- def check_before_logout
31
- nil
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,226 +0,0 @@
1
- class Roda
2
- module RodaPlugins
3
- module Rodauth
4
- Remember = Feature.define(:remember) do
5
- depends :logout
6
- route 'remember'
7
- notice_flash "Your remember setting has been updated"
8
- view 'remember', 'Change Remember Setting'
9
- additional_form_tags
10
- button 'Change Remember Setting'
11
- after
12
- redirect
13
- require_account
14
-
15
- auth_value_methods(
16
- :extend_remember_deadline?,
17
- :remember_confirm_view,
18
- :remember_confirm_additional_form_tags,
19
- :remember_cookie_key,
20
- :remember_cookie_options,
21
- :remember_deadline_column,
22
- :remember_id_column,
23
- :remember_key_column,
24
- :remember_period,
25
- :remember_table,
26
- :remembered_session_key
27
- )
28
- auth_methods(
29
- :add_remember_key,
30
- :after_load_memory,
31
- :after_remember_confirm,
32
- :clear_remembered_session_key,
33
- :disable_remember_login,
34
- :forget_login,
35
- :generate_remember_key_value,
36
- :get_remember_key,
37
- :load_memory,
38
- :logged_in_via_remember_key?,
39
- :remember_key_value,
40
- :remember_login,
41
- :remove_remember_key
42
- )
43
-
44
- get_block do |r, auth|
45
- if r['confirm']
46
- auth.remember_confirm_view
47
- else
48
- auth.remember_view
49
- end
50
- end
51
-
52
- post_block do |r, auth|
53
- if r['confirm']
54
- if auth._account_from_session && auth.password_match?(r[auth.password_param].to_s)
55
- auth.transaction do
56
- auth.clear_remembered_session_key
57
- auth.after_remember_confirm
58
- end
59
- r.redirect auth.remember_confirm_redirect
60
- else
61
- @password_error = auth.invalid_password_message
62
- auth.remember_confirm_view
63
- end
64
- else
65
- auth.transaction do
66
- case r['remember']
67
- when 'remember'
68
- auth.remember_login
69
- when 'forget'
70
- auth.forget_login
71
- when 'disable'
72
- auth.disable_remember_login
73
- end
74
- auth.after_remember
75
- end
76
- auth.set_notice_flash auth.remember_notice_flash
77
- r.redirect auth.remember_redirect
78
- end
79
- end
80
-
81
- def after_logout
82
- super
83
- forget_login
84
- end
85
-
86
- def after_remember_confirm
87
- end
88
-
89
- def remember_confirm_view
90
- view('confirm-password', 'Confirm Password')
91
- end
92
-
93
- def remember_confirm_button
94
- 'Confirm Password'
95
- end
96
-
97
- def remember_confirm_redirect
98
- default_redirect
99
- end
100
-
101
- def remember_confirm_additional_form_tags
102
- end
103
-
104
- attr_reader :remember_key_value
105
-
106
- def generate_remember_key_value
107
- @remember_key_value = random_key
108
- end
109
-
110
- def after_load_memory
111
- end
112
-
113
- def load_memory
114
- if !session[session_key] && (cookie = request.cookies[remember_cookie_key])
115
- id, key = cookie.split('_', 2)
116
- if id && key
117
- id = id.to_i
118
- if session[session_key] = active_remember_key_dataset(id).
119
- where(remember_key_column=>key.to_s).
120
- get(remember_id_column)
121
- account_from_session
122
-
123
- session[remembered_session_key] = true
124
- if extend_remember_deadline?
125
- active_remember_key_dataset(id).update(:deadline=>Sequel.expr(:deadline) + Sequel.cast(remember_period, :interval))
126
- end
127
- after_load_memory
128
- end
129
- end
130
- end
131
- end
132
-
133
- def remember_login
134
- get_remember_key
135
- opts = Hash[remember_cookie_options]
136
- opts[:value] = "#{account_id_value}_#{remember_key_value}"
137
- ::Rack::Utils.set_cookie_header!(response.headers, remember_cookie_key, opts)
138
- end
139
-
140
- def remember_cookie_options
141
- {}
142
- end
143
-
144
- def extend_remember_deadline?
145
- false
146
- end
147
-
148
- def remember_period
149
- '2 weeks'
150
- end
151
-
152
- def forget_login
153
- ::Rack::Utils.delete_cookie_header!(response.headers, remember_cookie_key, remember_cookie_options)
154
- end
155
-
156
- def remember_key_dataset(id_value=account_id_value)
157
- db[remember_table].
158
- where(remember_id_column=>id_value)
159
- end
160
- def active_remember_key_dataset(id_value=account_id_value)
161
- remember_key_dataset(id_value).where(Sequel.expr(remember_deadline_column) > Sequel::CURRENT_TIMESTAMP)
162
- end
163
-
164
- def get_remember_key
165
- unless @remember_key_value = active_remember_key_dataset.get(remember_key_column)
166
- generate_remember_key_value
167
- transaction do
168
- remove_remember_key
169
- add_remember_key
170
- end
171
- end
172
- nil
173
- end
174
-
175
- def disable_remember_login
176
- remove_remember_key
177
- end
178
-
179
- def add_remember_key
180
- remember_key_dataset.insert(remember_id_column=>account_id_value, remember_key_column=>remember_key_value)
181
- end
182
-
183
- def remove_remember_key
184
- remember_key_dataset.delete
185
- end
186
-
187
- def remember_id_column
188
- :id
189
- end
190
-
191
- def remember_key_column
192
- :key
193
- end
194
-
195
- def remember_deadline_column
196
- :deadline
197
- end
198
-
199
- def remember_table
200
- :account_remember_keys
201
- end
202
-
203
- def remember_cookie_key
204
- '_remember'
205
- end
206
-
207
- def clear_remembered_session_key
208
- session.delete(remembered_session_key)
209
- end
210
-
211
- def logged_in_via_remember_key?
212
- !!session[remembered_session_key]
213
- end
214
-
215
- def remembered_session_key
216
- :remembered
217
- end
218
-
219
- def after_close_account
220
- super
221
- remove_remember_key
222
- end
223
- end
224
- end
225
- end
226
- end
@@ -1,205 +0,0 @@
1
- class Roda
2
- module RodaPlugins
3
- module Rodauth
4
- ResetPassword = Feature.define(:reset_password) do
5
- depends :login
6
- route 'reset-password'
7
- notice_flash "Your password has been reset"
8
- error_flash "There was an error resetting your password"
9
- view 'reset-password', 'Reset Password'
10
- additional_form_tags
11
- after
12
- button 'Reset Password'
13
- redirect
14
-
15
- auth_value_methods(
16
- :no_matching_reset_password_key_message,
17
- :reset_password_autologin?,
18
- :reset_password_email_sent_notice_message,
19
- :reset_password_email_sent_redirect,
20
- :reset_password_email_subject,
21
- :reset_password_id_column,
22
- :reset_password_key_column,
23
- :reset_password_key_param,
24
- :reset_password_request_additional_form_tags,
25
- :reset_password_request_button,
26
- :reset_password_table
27
- )
28
- auth_methods(
29
- :account_from_reset_password_key,
30
- :after_reset_password_request,
31
- :create_reset_password_key,
32
- :create_reset_password_email,
33
- :remove_reset_password_key,
34
- :reset_password_email_body,
35
- :reset_password_email_link,
36
- :reset_password_key_insert_hash,
37
- :reset_password_key_value,
38
- :send_reset_password_email
39
- )
40
-
41
- get_block do |r, auth|
42
- if key = r[auth.reset_password_key_param]
43
- if auth._account_from_reset_password_key(key)
44
- auth.reset_password_view
45
- else
46
- auth.set_redirect_error_flash auth.no_matching_reset_password_key_message
47
- r.redirect auth.require_login_redirect
48
- end
49
- end
50
- end
51
-
52
- post_block do |r, auth|
53
- if login = r[auth.login_param]
54
- if auth._account_from_login(login.to_s) && auth.open_account?
55
- auth.generate_reset_password_key_value
56
- auth.transaction do
57
- auth.create_reset_password_key
58
- auth.send_reset_password_email
59
- auth.after_reset_password_request
60
- end
61
- auth.set_notice_flash auth.reset_password_email_sent_notice_message
62
- r.redirect auth.reset_password_email_sent_redirect
63
- end
64
- elsif key = r[auth.reset_password_key_param]
65
- if auth._account_from_reset_password_key(key)
66
- if r[auth.password_param] == r[auth.password_confirm_param]
67
- if auth.password_meets_requirements?(r[auth.password_param].to_s)
68
- auth.transaction do
69
- auth.set_password(r[auth.password_param])
70
- auth.remove_reset_password_key
71
- auth.after_reset_password
72
- end
73
- if auth.reset_password_autologin?
74
- auth.update_session
75
- end
76
- auth.set_notice_flash auth.reset_password_notice_flash
77
- r.redirect(auth.reset_password_redirect)
78
- else
79
- @password_error = auth.password_does_not_meet_requirements_message
80
- end
81
- else
82
- @password_error = auth.passwords_do_not_match_message
83
- end
84
- auth.set_error_flash auth.reset_password_error_flash
85
- auth.reset_password_view
86
- end
87
- end
88
- end
89
-
90
- def after_login_failure
91
- super
92
- scope.instance_variable_set(:@login_form_header, render("reset-password-request"))
93
- end
94
-
95
- def generate_reset_password_key_value
96
- @reset_password_key_value = random_key
97
- end
98
-
99
- def create_reset_password_key
100
- ds = db[reset_password_table].where(reset_password_id_column=>account_id_value)
101
- transaction do
102
- ds.where{deadline < Sequel::CURRENT_TIMESTAMP}.delete
103
- if ds.empty?
104
- ds.insert(reset_password_key_insert_hash)
105
- end
106
- end
107
- end
108
-
109
- def reset_password_key_insert_hash
110
- {reset_password_id_column=>account_id_value, reset_password_key_column=>reset_password_key_value}
111
- end
112
-
113
- def remove_reset_password_key
114
- db[reset_password_table].where(reset_password_id_column=>account_id_value).delete
115
- end
116
-
117
- def reset_password_email_sent_notice_message
118
- "An email has been sent to you with a link to reset the password for your account"
119
- end
120
-
121
- def no_matching_reset_password_key_message
122
- "invalid password reset key"
123
- end
124
-
125
- def _account_from_reset_password_key(key)
126
- @account = account_from_reset_password_key(key)
127
- end
128
-
129
- def account_from_reset_password_key(key)
130
- id, key = key.split('_', 2)
131
- id_column = reset_password_id_column
132
- rpds = db[reset_password_table].
133
- select(id_column).
134
- where(id_column=>id, reset_password_key_column=>key)
135
- ds = account_model.where(account_id=>rpds)
136
- ds = ds.where(account_status_id=>account_open_status_value) unless skip_status_checks?
137
- ds.first
138
- end
139
-
140
- def after_reset_password_request
141
- nil
142
- end
143
-
144
- def reset_password_request_button
145
- 'Request Password Reset'
146
- end
147
-
148
- def reset_password_request_additional_form_tags
149
- nil
150
- end
151
-
152
- def reset_password_email_sent_redirect
153
- default_redirect
154
- end
155
-
156
- def reset_password_table
157
- :account_password_reset_keys
158
- end
159
-
160
- def reset_password_id_column
161
- :id
162
- end
163
-
164
- def reset_password_key_column
165
- :key
166
- end
167
-
168
- attr_reader :reset_password_key_value
169
-
170
- def create_reset_password_email
171
- create_email(reset_password_email_subject, reset_password_email_body)
172
- end
173
-
174
- def send_reset_password_email
175
- create_reset_password_email.deliver!
176
- end
177
-
178
- def reset_password_email_body
179
- render('reset-password-email')
180
- end
181
-
182
- def reset_password_email_link
183
- "#{request.base_url}#{prefix}/#{reset_password_route}?#{reset_password_key_param}=#{account_id_value}_#{reset_password_key_value}"
184
- end
185
-
186
- def reset_password_email_subject
187
- 'Reset Password'
188
- end
189
-
190
- def reset_password_key_param
191
- 'key'
192
- end
193
-
194
- def reset_password_autologin?
195
- false
196
- end
197
-
198
- def after_close_account
199
- super
200
- db[reset_password_table].where(reset_password_id_column=>account_id_value).delete
201
- end
202
- end
203
- end
204
- end
205
- end