rodauth 1.20.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +170 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +211 -79
  5. data/doc/account_expiration.rdoc +12 -26
  6. data/doc/active_sessions.rdoc +49 -0
  7. data/doc/audit_logging.rdoc +44 -0
  8. data/doc/base.rdoc +75 -128
  9. data/doc/change_login.rdoc +7 -14
  10. data/doc/change_password.rdoc +9 -13
  11. data/doc/change_password_notify.rdoc +2 -2
  12. data/doc/close_account.rdoc +9 -16
  13. data/doc/confirm_password.rdoc +12 -5
  14. data/doc/create_account.rdoc +11 -22
  15. data/doc/disallow_password_reuse.rdoc +6 -13
  16. data/doc/email_auth.rdoc +15 -14
  17. data/doc/email_base.rdoc +6 -15
  18. data/doc/http_basic_auth.rdoc +10 -1
  19. data/doc/internals.rdoc +1 -1
  20. data/doc/jwt.rdoc +22 -22
  21. data/doc/jwt_cors.rdoc +22 -0
  22. data/doc/jwt_refresh.rdoc +12 -8
  23. data/doc/lockout.rdoc +17 -15
  24. data/doc/login.rdoc +10 -2
  25. data/doc/login_password_requirements_base.rdoc +15 -37
  26. data/doc/logout.rdoc +2 -2
  27. data/doc/otp.rdoc +24 -19
  28. data/doc/password_complexity.rdoc +10 -26
  29. data/doc/password_expiration.rdoc +11 -25
  30. data/doc/password_grace_period.rdoc +16 -2
  31. data/doc/recovery_codes.rdoc +18 -12
  32. data/doc/release_notes/1.21.0.txt +12 -0
  33. data/doc/release_notes/1.22.0.txt +11 -0
  34. data/doc/release_notes/1.23.0.txt +32 -0
  35. data/doc/release_notes/2.0.0.txt +361 -0
  36. data/doc/release_notes/2.1.0.txt +31 -0
  37. data/doc/remember.rdoc +40 -64
  38. data/doc/reset_password.rdoc +12 -9
  39. data/doc/session_expiration.rdoc +1 -0
  40. data/doc/single_session.rdoc +16 -25
  41. data/doc/sms_codes.rdoc +24 -14
  42. data/doc/two_factor_base.rdoc +60 -22
  43. data/doc/verify_account.rdoc +14 -12
  44. data/doc/verify_account_grace_period.rdoc +6 -2
  45. data/doc/verify_login_change.rdoc +9 -8
  46. data/doc/webauthn.rdoc +115 -0
  47. data/doc/webauthn_login.rdoc +15 -0
  48. data/doc/webauthn_verify_account.rdoc +9 -0
  49. data/javascript/webauthn_auth.js +45 -0
  50. data/javascript/webauthn_setup.js +35 -0
  51. data/lib/roda/plugins/rodauth.rb +1 -1
  52. data/lib/rodauth.rb +32 -24
  53. data/lib/rodauth/features/account_expiration.rb +5 -5
  54. data/lib/rodauth/features/active_sessions.rb +160 -0
  55. data/lib/rodauth/features/audit_logging.rb +96 -0
  56. data/lib/rodauth/features/base.rb +144 -43
  57. data/lib/rodauth/features/change_password_notify.rb +2 -2
  58. data/lib/rodauth/features/confirm_password.rb +40 -2
  59. data/lib/rodauth/features/create_account.rb +8 -13
  60. data/lib/rodauth/features/disallow_common_passwords.rb +1 -1
  61. data/lib/rodauth/features/disallow_password_reuse.rb +1 -1
  62. data/lib/rodauth/features/email_auth.rb +30 -29
  63. data/lib/rodauth/features/email_base.rb +9 -4
  64. data/lib/rodauth/features/http_basic_auth.rb +55 -35
  65. data/lib/rodauth/features/jwt.rb +58 -10
  66. data/lib/rodauth/features/jwt_cors.rb +53 -0
  67. data/lib/rodauth/features/jwt_refresh.rb +3 -3
  68. data/lib/rodauth/features/lockout.rb +12 -14
  69. data/lib/rodauth/features/login.rb +54 -10
  70. data/lib/rodauth/features/login_password_requirements_base.rb +4 -4
  71. data/lib/rodauth/features/otp.rb +72 -74
  72. data/lib/rodauth/features/password_complexity.rb +4 -11
  73. data/lib/rodauth/features/password_expiration.rb +2 -2
  74. data/lib/rodauth/features/password_grace_period.rb +17 -10
  75. data/lib/rodauth/features/recovery_codes.rb +49 -53
  76. data/lib/rodauth/features/remember.rb +11 -27
  77. data/lib/rodauth/features/reset_password.rb +26 -26
  78. data/lib/rodauth/features/session_expiration.rb +6 -4
  79. data/lib/rodauth/features/single_session.rb +7 -5
  80. data/lib/rodauth/features/sms_codes.rb +62 -71
  81. data/lib/rodauth/features/two_factor_base.rb +132 -28
  82. data/lib/rodauth/features/verify_account.rb +25 -21
  83. data/lib/rodauth/features/verify_account_grace_period.rb +20 -9
  84. data/lib/rodauth/features/verify_login_change.rb +12 -11
  85. data/lib/rodauth/features/webauthn.rb +507 -0
  86. data/lib/rodauth/features/webauthn_login.rb +70 -0
  87. data/lib/rodauth/features/webauthn_verify_account.rb +46 -0
  88. data/lib/rodauth/version.rb +2 -2
  89. data/templates/button.str +1 -3
  90. data/templates/change-login.str +1 -2
  91. data/templates/change-password.str +3 -5
  92. data/templates/close-account.str +2 -2
  93. data/templates/confirm-password.str +1 -1
  94. data/templates/create-account.str +1 -1
  95. data/templates/email-auth-email.str +1 -1
  96. data/templates/email-auth-request-form.str +2 -3
  97. data/templates/email-auth.str +1 -1
  98. data/templates/global-logout-field.str +6 -0
  99. data/templates/login-confirm-field.str +2 -4
  100. data/templates/login-display.str +3 -2
  101. data/templates/login-field.str +2 -4
  102. data/templates/login-form-footer.str +6 -0
  103. data/templates/login-form.str +7 -0
  104. data/templates/login.str +1 -9
  105. data/templates/logout.str +1 -1
  106. data/templates/multi-phase-login.str +3 -0
  107. data/templates/otp-auth-code-field.str +5 -3
  108. data/templates/otp-auth.str +1 -1
  109. data/templates/otp-disable.str +1 -1
  110. data/templates/otp-setup.str +3 -3
  111. data/templates/password-confirm-field.str +2 -4
  112. data/templates/password-field.str +2 -4
  113. data/templates/recovery-auth.str +3 -6
  114. data/templates/recovery-codes.str +1 -1
  115. data/templates/remember.str +15 -20
  116. data/templates/reset-password-email.str +1 -1
  117. data/templates/reset-password-request.str +3 -3
  118. data/templates/reset-password.str +1 -2
  119. data/templates/sms-auth.str +1 -1
  120. data/templates/sms-code-field.str +5 -3
  121. data/templates/sms-confirm.str +1 -2
  122. data/templates/sms-disable.str +1 -2
  123. data/templates/sms-request.str +1 -1
  124. data/templates/sms-setup.str +6 -4
  125. data/templates/two-factor-auth.str +5 -0
  126. data/templates/two-factor-disable.str +6 -0
  127. data/templates/two-factor-manage.str +16 -0
  128. data/templates/unlock-account-email.str +1 -1
  129. data/templates/unlock-account-request.str +4 -4
  130. data/templates/unlock-account.str +1 -1
  131. data/templates/verify-account-email.str +1 -1
  132. data/templates/verify-account-resend.str +3 -3
  133. data/templates/verify-account.str +1 -2
  134. data/templates/verify-login-change-email.str +2 -1
  135. data/templates/verify-login-change.str +1 -1
  136. data/templates/webauthn-auth.str +11 -0
  137. data/templates/webauthn-remove.str +14 -0
  138. data/templates/webauthn-setup.str +12 -0
  139. metadata +89 -50
  140. data/Rakefile +0 -179
  141. data/doc/verify_change_login.rdoc +0 -11
  142. data/lib/rodauth/features/verify_change_login.rb +0 -20
  143. data/spec/account_expiration_spec.rb +0 -225
  144. data/spec/all.rb +0 -1
  145. data/spec/change_login_spec.rb +0 -156
  146. data/spec/change_password_notify_spec.rb +0 -33
  147. data/spec/change_password_spec.rb +0 -202
  148. data/spec/close_account_spec.rb +0 -162
  149. data/spec/confirm_password_spec.rb +0 -70
  150. data/spec/create_account_spec.rb +0 -127
  151. data/spec/disallow_common_passwords_spec.rb +0 -93
  152. data/spec/disallow_password_reuse_spec.rb +0 -179
  153. data/spec/email_auth_spec.rb +0 -285
  154. data/spec/http_basic_auth_spec.rb +0 -143
  155. data/spec/jwt_refresh_spec.rb +0 -256
  156. data/spec/jwt_spec.rb +0 -235
  157. data/spec/lockout_spec.rb +0 -250
  158. data/spec/login_spec.rb +0 -328
  159. data/spec/migrate/001_tables.rb +0 -184
  160. data/spec/migrate/002_account_password_hash_column.rb +0 -11
  161. data/spec/migrate_password/001_tables.rb +0 -73
  162. data/spec/migrate_travis/001_tables.rb +0 -141
  163. data/spec/password_complexity_spec.rb +0 -109
  164. data/spec/password_expiration_spec.rb +0 -244
  165. data/spec/password_grace_period_spec.rb +0 -93
  166. data/spec/remember_spec.rb +0 -451
  167. data/spec/reset_password_spec.rb +0 -229
  168. data/spec/rodauth_spec.rb +0 -343
  169. data/spec/session_expiration_spec.rb +0 -58
  170. data/spec/single_session_spec.rb +0 -127
  171. data/spec/spec_helper.rb +0 -327
  172. data/spec/two_factor_spec.rb +0 -1423
  173. data/spec/update_password_hash_spec.rb +0 -40
  174. data/spec/verify_account_grace_period_spec.rb +0 -171
  175. data/spec/verify_account_spec.rb +0 -240
  176. data/spec/verify_change_login_spec.rb +0 -46
  177. data/spec/verify_login_change_spec.rb +0 -232
  178. data/spec/views/layout-other.str +0 -11
  179. data/spec/views/layout.str +0 -11
  180. data/spec/views/login.str +0 -21
@@ -1,143 +0,0 @@
1
- require File.expand_path("spec_helper", File.dirname(__FILE__))
2
-
3
- describe "Rodauth http basic auth feature" do
4
- def basic_auth_visit(opts={})
5
- page.driver.browser.basic_authorize(opts.fetch(:username,"foo@example.com"), opts.fetch(:password, "0123456789"))
6
- visit(opts.fetch(:path, '/'))
7
- end
8
-
9
- def authorization_header(opts={})
10
- ["#{opts.delete(:username)||'foo@example.com'}:#{opts.delete(:password)||'0123456789'}"].pack("m*")
11
- end
12
-
13
- def basic_auth_json_request(opts={})
14
- auth = opts.delete(:auth) || authorization_header(opts)
15
- path = opts.delete(:path) || '/'
16
- json_request(path, opts.merge(:headers => {"HTTP_AUTHORIZATION" => "Basic #{auth}"}, :method=>'GET'))
17
- end
18
-
19
- def newline_basic_auth_json_request(opts={})
20
- auth = opts.delete(:auth) || authorization_header(opts)
21
- auth.chomp!
22
- basic_auth_json_request(opts.merge(:auth => auth))
23
- end
24
-
25
- describe "on page visit" do
26
- before do
27
- rodauth do
28
- enable :http_basic_auth
29
- end
30
- roda do |r|
31
- r.rodauth
32
- r.root{view :content=>(rodauth.logged_in? ? "Logged In" : 'Not Logged')}
33
- end
34
- end
35
-
36
- it "handles logins" do
37
- basic_auth_visit
38
- page.text.must_include "Logged In"
39
- end
40
-
41
- it "keeps the user logged in" do
42
- visit '/'
43
- page.text.must_include "Not Logged"
44
-
45
- basic_auth_visit
46
- page.text.must_include "Logged In"
47
-
48
- visit '/'
49
- page.text.must_include "Logged In"
50
- end
51
-
52
- it "fails when no login is found" do
53
- basic_auth_visit(:username => "foo2@example.com")
54
- page.text.must_include "Not Logged"
55
- page.response_headers.keys.must_include("WWW-Authenticate")
56
- end
57
-
58
- it "fails when passowrd does not match" do
59
- basic_auth_visit(:password => "1111111111")
60
- page.text.must_include "Not Logged"
61
- page.response_headers.keys.must_include("WWW-Authenticate")
62
- end
63
- end
64
-
65
- it "requires authentication if require_http_basic_auth is true" do
66
- rodauth do
67
- enable :http_basic_auth
68
- require_http_basic_auth true
69
- end
70
- roda do |r|
71
- rodauth.require_authentication
72
- r.root{view :content=>(rodauth.logged_in? ? "Logged In" : 'Not Logged')}
73
- end
74
-
75
- visit '/'
76
- page.status_code.must_equal 401
77
- page.response_headers.keys.must_include("WWW-Authenticate")
78
-
79
- basic_auth_visit
80
- page.text.must_include "Logged In"
81
- end
82
-
83
- it "works with standard authentication" do
84
- rodauth do
85
- enable :login, :http_basic_auth
86
- end
87
- roda do |r|
88
- r.rodauth
89
- r.root{view :content=>(rodauth.logged_in? ? "Logged In" : 'Not Logged')}
90
- end
91
-
92
- login
93
- page.text.must_include "Logged In"
94
- end
95
-
96
- it "does not allow login to unverified account" do
97
- rodauth do
98
- enable :http_basic_auth
99
- skip_status_checks? false
100
- end
101
- roda do |r|
102
- r.rodauth
103
- r.root{view :content=>(rodauth.logged_in? ? "Logged In" : 'Not Logged')}
104
- end
105
- DB[:accounts].update(:status_id=>1)
106
-
107
- basic_auth_visit
108
- page.text.must_include "Not Logged"
109
- page.response_headers.keys.must_include("WWW-Authenticate")
110
- end
111
-
112
- it "should login via jwt" do
113
- rodauth do
114
- enable :http_basic_auth
115
- end
116
- roda(:jwt) do |r|
117
- r.rodauth
118
- response['Content-Type'] = 'application/json'
119
- rodauth.require_authentication
120
- {"success"=>'You have been logged in'}
121
- end
122
-
123
- @authorization = nil
124
- res = basic_auth_json_request(:auth=>'.')
125
- res.must_equal [401, {'error'=>"Please login to continue"}]
126
-
127
- @authorization = nil
128
- res = basic_auth_json_request(:username=>'foo@example2.com')
129
- res.must_equal [401, {'error'=>"Please login to continue", "field-error"=>["login", "no matching login"]}]
130
-
131
- @authorization = nil
132
- res = basic_auth_json_request(:password=>'012345678')
133
- res.must_equal [401, {'error'=>"Please login to continue", "field-error"=>["password", "invalid password"]}]
134
-
135
- @authorization = nil
136
- res = newline_basic_auth_json_request
137
- res.must_equal [200, {"success"=>'You have been logged in'}]
138
-
139
- @authorization = nil
140
- res = basic_auth_json_request
141
- res.must_equal [200, {"success"=>'You have been logged in'}]
142
- end
143
- end
@@ -1,256 +0,0 @@
1
- require File.expand_path("spec_helper", File.dirname(__FILE__))
2
-
3
- describe 'Rodauth login feature' do
4
- it "should not have jwt refresh feature assume JWT token given during Basic/Digest authentication" do
5
- rodauth do
6
- enable :login, :logout, :jwt_refresh
7
- end
8
- roda(:jwt) do |r|
9
- rodauth.require_authentication
10
- '1'
11
- end
12
-
13
- res = json_request("/jwt-refresh", :headers=>{'HTTP_AUTHORIZATION'=>'Basic foo'})
14
- res.must_equal [401, {'error'=>'Please login to continue'}]
15
-
16
- res = json_request("/", :headers=>{'HTTP_AUTHORIZATION'=>'Digest foo'})
17
- res.must_equal [401, {'error'=>'Please login to continue'}]
18
- end
19
-
20
- it "should require json request content type in only json mode for rodauth endpoints only" do
21
- oj = false
22
- rodauth do
23
- enable :login, :logout, :jwt_refresh
24
- jwt_secret '1'
25
- json_response_success_key 'success'
26
- only_json?{oj}
27
- end
28
- roda(:csrf=>false, :json=>true) do |r|
29
- r.rodauth
30
- rodauth.require_authentication
31
- '1'
32
- end
33
-
34
- res = json_request("/", :content_type=>'application/x-www-form-urlencoded', :include_headers=>true, :method=>'GET')
35
- res[1].delete('Set-Cookie')
36
- res.must_equal [302, {"Content-Type"=>'text/html', "Content-Length"=>'0', "Location"=>"/login",}, []]
37
-
38
- res = json_request("/", :content_type=>'application/vnd.api+json', :method=>'GET')
39
- res.must_equal [400, ['{"error":"Please login to continue"}']]
40
-
41
- oj = true
42
-
43
- res = json_request("/", :content_type=>'application/x-www-form-urlencoded', :method=>'GET')
44
- res.must_equal [400, ['{"error":"Please login to continue"}']]
45
-
46
- res = json_request("/", :method=>'GET')
47
- res.must_equal [400, {'error'=>'Please login to continue'}]
48
-
49
- res = json_request("/login", :content_type=>'application/x-www-form-urlencoded', :include_headers=>true, :method=>'GET')
50
- msg = "Only JSON format requests are allowed"
51
- res[1].delete('Set-Cookie')
52
- res.must_equal [400, {"Content-Type"=>'text/html', "Content-Length"=>msg.length.to_s}, [msg]]
53
-
54
- jwt_refresh_login
55
-
56
- res = json_request("/", :content_type=>'application/x-www-form-urlencoded', :include_headers=>true, :method=>'GET')
57
- # res.must_equal [200, {"Content-Type"=>'text/html', "Content-Length"=>'1'}, ['1']]
58
- end
59
-
60
- it "should allow non-json requests if only_json? is false" do
61
- rodauth do
62
- enable :login, :logout, :jwt_refresh
63
- jwt_secret '1'
64
- only_json? false
65
- end
66
- roda(:jwt_html) do |r|
67
- r.rodauth
68
- rodauth.require_authentication
69
- view(:content=>'1')
70
- end
71
-
72
- login
73
- page.find('#notice_flash').text.must_equal 'You have been logged in'
74
- end
75
-
76
- it "should require POST for json requests" do
77
- rodauth do
78
- enable :login, :logout, :jwt_refresh
79
- jwt_secret '1'
80
- json_response_success_key 'success'
81
- end
82
- roda(:jwt) do |r|
83
- r.rodauth
84
- end
85
-
86
- res = json_request("/login", :method=>'GET')
87
- res.must_equal [405, {'error'=>'non-POST method used in JSON API'}]
88
- end
89
-
90
- it "should require Accept contain application/json if jwt_check_accept? is true and Accept is present" do
91
- rodauth do
92
- enable :login, :logout, :jwt_refresh
93
- jwt_secret '1'
94
- json_response_success_key 'success'
95
- jwt_check_accept? true
96
- end
97
- roda(:jwt) do |r|
98
- r.rodauth
99
- end
100
-
101
- res = json_request("/login", :headers=>{'HTTP_ACCEPT'=>'text/html'})
102
- res.must_equal [406, {'error'=>'Unsupported Accept header. Must accept "application/json" or compatible content type'}]
103
-
104
- jwt_refresh_validate_login(json_request("/login", :login=>'foo@example.com', :password=>'0123456789'))
105
- jwt_refresh_validate_login(json_request("/login", :headers=>{'HTTP_ACCEPT'=>'*/*'}, :login=>'foo@example.com', :password=>'0123456789'))
106
- jwt_refresh_validate_login(json_request("/login", :headers=>{'HTTP_ACCEPT'=>'application/*'}, :login=>'foo@example.com', :password=>'0123456789'))
107
- jwt_refresh_validate_login(json_request("/login", :headers=>{'HTTP_ACCEPT'=>'application/vnd.api+json'}, :login=>'foo@example.com', :password=>'0123456789'))
108
- end
109
-
110
- it "should clear jwt refresh token when closing account" do
111
- rodauth do
112
- enable :login, :jwt_refresh, :close_account
113
- jwt_secret '1'
114
- end
115
- roda(:jwt) do |r|
116
- r.rodauth
117
- rodauth.require_authentication
118
- response['Content-Type'] = 'application/json'
119
- {'hello' => 'world'}.to_json
120
- end
121
-
122
- jwt_refresh_login
123
-
124
- DB[:account_jwt_refresh_keys].count.must_equal 1
125
- res = json_request('/close-account', :password=>'0123456789')
126
- res[1].delete('access_token').must_be_kind_of(String)
127
- res.must_equal [200, {'success'=>"Your account has been closed"}]
128
- DB[:account_jwt_refresh_keys].count.must_equal 0
129
- end
130
-
131
-
132
- it "should set refresh tokens when creating accounts when using autologin" do
133
- rodauth do
134
- enable :login, :create_account, :jwt_refresh
135
- after_create_account{json_response[:account_id] = account_id}
136
- create_account_autologin? true
137
- end
138
- roda(:jwt) do |r|
139
- r.rodauth
140
- rodauth.require_authentication
141
- response['Content-Type'] = 'application/json'
142
- {'hello' => 'world'}.to_json
143
- end
144
-
145
- res = json_request('/create-account', :login=>'foo@example2.com', "login-confirm"=>'foo@example2.com', :password=>'0123456789', "password-confirm"=>'0123456789')
146
- refresh_token = res.last.delete('refresh_token')
147
- @authorization = res.last.delete('access_token')
148
- res.must_equal [200, {'success'=>"Your account has been created", 'account_id'=>DB[:accounts].max(:id)}]
149
-
150
- res = json_request("/")
151
- res.must_equal [200, {'hello'=>'world'}]
152
-
153
- # We can refresh our token
154
- res = json_request("/jwt-refresh", :refresh_token=>refresh_token)
155
- jwt_refresh_validate(res)
156
- @authorization = res.last.delete('access_token')
157
-
158
- # Which we can use to access protected resources
159
- res = json_request("/")
160
- res.must_equal [200, {'hello'=>'world'}]
161
- end
162
-
163
- [false, true].each do |hs|
164
- it "generates and refreshes Refresh Tokens #{'with hmac_secret' if hs}" do
165
- initial_secret = secret = SecureRandom.random_bytes(32) if hs
166
- rodauth do
167
- enable :login, :logout, :jwt_refresh
168
- hmac_secret{secret} if hs
169
- jwt_secret '1'
170
- end
171
- roda(:jwt) do |r|
172
- r.rodauth
173
- rodauth.require_authentication
174
- response['Content-Type'] = 'application/json'
175
- {'hello' => 'world'}.to_json
176
- end
177
- res = json_request("/")
178
- res.must_equal [401, {'error'=>'Please login to continue'}]
179
-
180
- # We can login
181
- res = jwt_refresh_login
182
- refresh_token = res.last['refresh_token']
183
-
184
- # Which gives us an access token which grants us access to protected resources
185
- @authorization = res.last['access_token']
186
- res = json_request("/")
187
- res.must_equal [200, {'hello'=>'world'}]
188
-
189
- # We can refresh our token
190
- res = json_request("/jwt-refresh", :refresh_token=>refresh_token)
191
- jwt_refresh_validate(res)
192
- second_refresh_token = res.last['refresh_token']
193
-
194
- # Which we can use to access protected resources
195
- @authorization = res.last['access_token']
196
- res = json_request("/")
197
- res.must_equal [200, {'hello'=>'world'}]
198
-
199
- # Subsequent refresh token is valid
200
- res = json_request("/jwt-refresh", :refresh_token=>second_refresh_token)
201
- jwt_refresh_validate(res)
202
- third_refresh_token = res.last['refresh_token']
203
-
204
- # First refresh token is now no longer valid
205
- res = json_request("/jwt-refresh", :refresh_token=>refresh_token)
206
- res.must_equal [400, {"error"=>"invalid JWT refresh token"}]
207
-
208
- # Third refresh token is valid
209
- res = json_request("/jwt-refresh", :refresh_token=>third_refresh_token)
210
- jwt_refresh_validate(res)
211
- fourth_refresh_token = res.last['refresh_token']
212
-
213
- # And still gives us a valid access token
214
- @authorization = res.last['access_token']
215
- res = json_request("/")
216
- res.must_equal [200, {'hello'=>'world'}]
217
-
218
- if hs
219
- # Refresh secret doesn't work if hmac_secret changed
220
- secret = SecureRandom.random_bytes(32)
221
- res = json_request("/jwt-refresh", :refresh_token=>fourth_refresh_token)
222
- res.first.must_equal 400
223
- res.must_equal [400, {'error'=>'invalid JWT refresh token'}]
224
-
225
- # Refresh secret works if hmac_secret changed back
226
- secret = initial_secret
227
- res = json_request("/jwt-refresh", :refresh_token=>fourth_refresh_token)
228
- jwt_refresh_validate(res)
229
-
230
- # And still gives us a valid access token
231
- @authorization = res.last['access_token']
232
- res = json_request("/")
233
- res.must_equal [200, {'hello'=>'world'}]
234
- end
235
- end
236
- end
237
-
238
- it "should not return access_token for failed login attempt" do
239
- rodauth do
240
- enable :login, :create_account, :jwt_refresh
241
- after_create_account{json_response[:account_id] = account_id}
242
- create_account_autologin? true
243
- end
244
- roda(:jwt) do |r|
245
- r.rodauth
246
- rodauth.require_authentication
247
- response['Content-Type'] = 'application/json'
248
- {'hello' => 'world'}.to_json
249
- end
250
-
251
- json_request('/create-account', :login=>'foo@example2.com', "login-confirm"=>'foo@example2.com', :password=>'0123456789', "password-confirm"=>'0123456789')
252
-
253
- res = json_request('/login', :login=>'foo@example2.com', :password=>'123123')
254
- res.must_equal [401, {"field-error"=>['password', 'invalid password'], "error"=>"There was an error logging in"}]
255
- end
256
- end
@@ -1,235 +0,0 @@
1
- require File.expand_path("spec_helper", File.dirname(__FILE__))
2
-
3
- describe 'Rodauth login feature' do
4
- it "should not have jwt feature assume JWT token given during Basic/Digest authentication" do
5
- rodauth do
6
- enable :login, :logout
7
- end
8
- roda(:jwt) do |r|
9
- rodauth.require_authentication
10
- '1'
11
- end
12
-
13
- res = json_request("/", :headers=>{'HTTP_AUTHORIZATION'=>'Basic foo'})
14
- res.must_equal [401, {'error'=>'Please login to continue'}]
15
-
16
- res = json_request("/", :headers=>{'HTTP_AUTHORIZATION'=>'Digest foo'})
17
- res.must_equal [401, {'error'=>'Please login to continue'}]
18
- end
19
-
20
- it "should return error message if invalid JWT format used in request Authorization header" do
21
- rodauth do
22
- enable :login, :logout
23
- end
24
- roda(:jwt) do |r|
25
- r.rodauth
26
- rodauth.require_authentication
27
- '1'
28
- end
29
-
30
- res = json_request('/login', :include_headers=>true, :login=>'foo@example.com', :password=>'0123456789')
31
-
32
- res = json_request("/", :headers=>{'HTTP_AUTHORIZATION'=>res[1]['Authorization'][1..-1]})
33
- res.must_equal [400, {'error'=>'invalid JWT format or claim in Authorization header'}]
34
- end
35
-
36
- it "should use custom JSON error statuses even if the request isn't in JSON format if a JWT is in use" do
37
- rodauth do
38
- only_json? true
39
- end
40
- roda(:jwt) do |r|
41
- r.rodauth
42
- rodauth.require_authentication
43
- '1'
44
- end
45
-
46
- status, headers, body = json_request("/", :headers=>{'CONTENT_TYPE'=>'text/html'}, :include_headers=>true)
47
- status.must_equal 401
48
- headers['Content-Type'].must_equal 'application/json'
49
- JSON.parse(body.join).must_equal("error"=>"Please login to continue")
50
- end
51
-
52
- it "should require json request content type in only json mode for rodauth endpoints only" do
53
- oj = false
54
- rodauth do
55
- enable :login, :logout, :jwt
56
- jwt_secret '1'
57
- json_response_success_key 'success'
58
- only_json?{oj}
59
- end
60
- roda(:csrf=>false, :json=>true) do |r|
61
- r.rodauth
62
- rodauth.require_authentication
63
- '1'
64
- end
65
-
66
- res = json_request("/", :content_type=>'application/x-www-form-urlencoded', :include_headers=>true, :method=>'GET')
67
- res[1].delete('Set-Cookie')
68
- res.must_equal [302, {"Content-Type"=>'text/html', "Content-Length"=>'0', "Location"=>"/login",}, []]
69
-
70
- res = json_request("/", :content_type=>'application/vnd.api+json', :method=>'GET')
71
- res.must_equal [400, ['{"error":"Please login to continue"}']]
72
-
73
- oj = true
74
-
75
- res = json_request("/", :content_type=>'application/x-www-form-urlencoded', :method=>'GET')
76
- res.must_equal [400, ['{"error":"Please login to continue"}']]
77
-
78
- res = json_request("/", :method=>'GET')
79
- res.must_equal [400, {'error'=>'Please login to continue'}]
80
-
81
- res = json_request("/login", :content_type=>'application/x-www-form-urlencoded', :include_headers=>true, :method=>'GET')
82
- msg = "Only JSON format requests are allowed"
83
- res[1].delete('Set-Cookie')
84
- res.must_equal [400, {"Content-Type"=>'text/html', "Content-Length"=>msg.length.to_s}, [msg]]
85
-
86
- json_login
87
-
88
- res = json_request("/", :content_type=>'application/x-www-form-urlencoded', :include_headers=>true, :method=>'GET')
89
- res.must_equal [200, {"Content-Type"=>'text/html', "Content-Length"=>'1'}, ['1']]
90
- end
91
-
92
- it "should allow non-json requests if only_json? is false" do
93
- rodauth do
94
- enable :login, :logout, :jwt
95
- jwt_secret '1'
96
- only_json? false
97
- end
98
- roda(:jwt_html) do |r|
99
- r.rodauth
100
- rodauth.require_authentication
101
- view(:content=>'1')
102
- end
103
-
104
- login
105
- page.find('#notice_flash').text.must_equal 'You have been logged in'
106
- end
107
-
108
- it "should require POST for json requests" do
109
- rodauth do
110
- enable :login, :logout, :jwt
111
- jwt_secret '1'
112
- json_response_success_key 'success'
113
- end
114
- roda(:jwt) do |r|
115
- r.rodauth
116
- end
117
-
118
- res = json_request("/login", :method=>'GET')
119
- res.must_equal [405, {'error'=>'non-POST method used in JSON API'}]
120
- end
121
-
122
- it "should allow customizing JSON response bodies" do
123
- rodauth do
124
- enable :login, :logout, :jwt
125
- json_response_body do |hash|
126
- super('status'=>response.status, 'detail'=>hash)
127
- end
128
- end
129
- roda(:jwt) do |r|
130
- r.rodauth
131
- end
132
-
133
- res = json_request("/login", :method=>'GET')
134
- res.must_equal [405, {'status'=>405, 'detail'=>{'error'=>'non-POST method used in JSON API'}}]
135
- end
136
-
137
- it "should support valid_jwt? method for checking for valid JWT tokens" do
138
- rodauth do
139
- enable :login, :logout, :jwt
140
- jwt_secret '1'
141
- json_response_success_key 'success'
142
- end
143
- roda(:jwt) do |r|
144
- r.rodauth
145
- [rodauth.valid_jwt?.to_s]
146
- end
147
-
148
- res = json_request("/", :method=>'GET')
149
- res.must_equal [200, ['false']]
150
-
151
- res = json_request("/login", :method=>'GET')
152
- res.must_equal [405, {'error'=>'non-POST method used in JSON API'}]
153
-
154
- res = json_request("/", :method=>'GET')
155
- res.must_equal [200, ['true']]
156
- end
157
-
158
- it "should require Accept contain application/json if jwt_check_accept? is true and Accept is present" do
159
- rodauth do
160
- enable :login, :logout, :jwt
161
- jwt_secret '1'
162
- json_response_success_key 'success'
163
- jwt_check_accept? true
164
- end
165
- roda(:jwt) do |r|
166
- r.rodauth
167
- end
168
-
169
- res = json_request("/login", :headers=>{'HTTP_ACCEPT'=>'text/html'})
170
- res.must_equal [406, {'error'=>'Unsupported Accept header. Must accept "application/json" or compatible content type'}]
171
-
172
- json_request("/login", :login=>'foo@example.com', :password=>'0123456789').must_equal [200, {"success"=>'You have been logged in'}]
173
- json_request("/login", :headers=>{'HTTP_ACCEPT'=>'*/*'}, :login=>'foo@example.com', :password=>'0123456789').must_equal [200, {"success"=>'You have been logged in'}]
174
- json_request("/login", :headers=>{'HTTP_ACCEPT'=>'application/*'}, :login=>'foo@example.com', :password=>'0123456789').must_equal [200, {"success"=>'You have been logged in'}]
175
- json_request("/login", :headers=>{'HTTP_ACCEPT'=>'application/vnd.api+json'}, :login=>'foo@example.com', :password=>'0123456789').must_equal [200, {"success"=>'You have been logged in'}]
176
- end
177
-
178
- it "generates and verifies JWTs with claims" do
179
- invalid_jti = false
180
-
181
- rodauth do
182
- enable :login, :logout, :jwt
183
- jwt_secret '1'
184
- json_response_success_key 'success'
185
- jwt_session_key 'data'
186
- jwt_symbolize_deeply? true
187
- jwt_session_hash do
188
- h = super()
189
- h['data']['foo'] = {:bar=>[1]}
190
- h.merge(
191
- :aud => %w[Young Old],
192
- :exp => Time.now.to_i + 120,
193
- :iat => Time.now.to_i,
194
- :iss => "Foobar, Inc.",
195
- :jti => SecureRandom.hex(10),
196
- :nbf => Time.now.to_i - 30,
197
- :sub => session_value
198
- )
199
- end
200
- jwt_decode_opts(
201
- :aud => 'Old',
202
- :iss => "Foobar, Inc.",
203
- :leeway => 30,
204
- :verify_aud => true,
205
- :verify_expiration => true,
206
- :verify_iat => true,
207
- :verify_iss => true,
208
- :verify_jti => proc{|jti| invalid_jti ? false : !!jti},
209
- :verify_not_before => true
210
- )
211
- end
212
- roda(:jwt) do |r|
213
- r.rodauth
214
- r.post{rodauth.session[:foo][:bar]}
215
- end
216
-
217
- json_login.must_equal [200, {"success"=>'You have been logged in'}]
218
-
219
- payload = JWT.decode(@authorization, nil, false)[0]
220
- payload['sub'].must_equal payload['data']['account_id']
221
- payload['iat'].must_be_kind_of Integer
222
- payload['exp'].must_be_kind_of Integer
223
- payload['nbf'].must_be_kind_of Integer
224
- payload['iss'].must_equal "Foobar, Inc."
225
- payload['aud'].must_equal %w[Young Old]
226
- payload['jti'].must_match(/^[0-9a-f]{20}$/)
227
-
228
- json_request.must_equal [200, [1]]
229
-
230
- invalid_jti = true
231
- if RUBY_VERSION >= '1.9'
232
- json_login(:no_check=>true).must_equal [400, {"error"=>'invalid JWT format or claim in Authorization header'}]
233
- end
234
- end
235
- end