rodauth-bankid 1.0.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.
@@ -0,0 +1,324 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rodauth/features/bankid'
4
+
5
+ module Rodauth
6
+ # The +bankid_confirm+ feature re-authenticates a logged-in account with
7
+ # BankID before a sensitive action — the BankID counterpart of Rodauth's
8
+ # +confirm_password+. Guard a route with +require_bankid_authentication+;
9
+ # when the account has not proved its identity within
10
+ # +bankid_confirm_grace_period+ seconds, the browser is sent to the confirm
11
+ # page and back to the requested path afterwards. A fresh BankID login
12
+ # counts as a proof, so nobody confirms twice in a row.
13
+ #
14
+ # r.on 'settings' do
15
+ # rodauth.require_bankid_authentication
16
+ # ...
17
+ # end
18
+ #
19
+ # Routes under the Rodauth prefix:
20
+ #
21
+ # /bankid-confirm :: GET renders the page; POST finishes.
22
+ # /bankid-confirm-start :: POST starts an identification order.
23
+ # /bankid-confirm-qr, /bankid-confirm-collect, /bankid-confirm-cancel :: As for login.
24
+ #
25
+ # Only the account's own BankID passes: with +bankid_confirm_personal_number+
26
+ # set the number goes into the order's +requirement+, otherwise the
27
+ # completion's personnummer must match a linked identity. No match means
28
+ # 403 +identity_mismatch+.
29
+ #
30
+ # = Configuration
31
+ #
32
+ # bankid_confirm_grace_period :: Seconds a BankID proof stays valid.
33
+ # Default: 300.
34
+ # bankid_confirm_return_url :: Absolute https URL of the confirm page, for
35
+ # the mobile return flow. Default: +nil+.
36
+ # bankid_confirm_redirect :: Where to go after confirming when no return
37
+ # path was stored. Default: +default_redirect+.
38
+ # bankid_authentication_required_error_status :: Status of the redirect
39
+ # +require_bankid_authentication+
40
+ # issues. Default: 401.
41
+ #
42
+ # = Hooks
43
+ #
44
+ # before_bankid_confirm :: Before the order starts.
45
+ # after_bankid_confirm :: Inside the claim transaction, with
46
+ # +bankid_completion+ set. Same rollback rules as
47
+ # +after_bankid_collect+.
48
+ #
49
+ # = Methods
50
+ #
51
+ # require_bankid_authentication :: The guard described above.
52
+ # bankid_recently_authenticated? :: Whether the grace period is still open.
53
+ # bankid_confirm_personal_number :: The account's personnummer, if the app
54
+ # knows it. Default: +nil+.
55
+ # bankid_confirm_verify_identity(completion) :: The identity rule.
56
+ Feature.define(:bankid_confirm, :BankidConfirm) do
57
+ depends :bankid
58
+
59
+ loaded_templates %w[bankid-confirm]
60
+ view 'bankid-confirm', 'Confirm with BankID', 'bankid_confirm'
61
+
62
+ before 'bankid_confirm'
63
+ after 'bankid_confirm'
64
+
65
+ error_flash 'You need to confirm your identity with BankID before continuing', 'bankid_authentication_required'
66
+ error_flash 'The BankID used does not belong to your account', 'bankid_confirm_wrong_identity'
67
+ error_flash 'BankID confirmation was cancelled', 'bankid_confirm_cancelled'
68
+ notice_flash 'Your identity has been confirmed with BankID', 'bankid_confirm'
69
+
70
+ redirect(:bankid_confirm) { default_redirect }
71
+ redirect(:bankid_confirm_cancelled) { default_redirect }
72
+ redirect(:bankid_authentication_required) { bankid_confirm_path }
73
+
74
+ session_key :bankid_confirm_attempt_session_key, :bankid_confirm_attempt
75
+ session_key :bankid_confirm_redirect_session_key, :bankid_confirm_redirect
76
+
77
+ auth_value_method :bankid_confirm_grace_period, 300
78
+ auth_value_method :bankid_confirm_return_url, nil
79
+ auth_value_method :bankid_authentication_required_error_status, 401
80
+
81
+ translatable_method :bankid_confirm_intro, 'Confirm that it is you with your BankID app'
82
+ translatable_method :bankid_confirm_start_button, 'Confirm with BankID'
83
+ translatable_method :bankid_confirm_continue_button, 'Continue'
84
+
85
+ auth_methods(
86
+ :bankid_confirm_personal_number,
87
+ :bankid_confirm_requirement,
88
+ :bankid_confirm_verify_identity,
89
+ :bankid_recently_authenticated?,
90
+ :require_bankid_authentication
91
+ )
92
+
93
+ # GET renders the page. POST finishes: it collects if the browser did
94
+ # not, then records the proof in the session and redirects back.
95
+ route(:bankid_confirm) do |r|
96
+ bankid_confirm_route_setup
97
+ before_bankid_confirm_route
98
+
99
+ r.get do
100
+ bankid_confirm_view
101
+ end
102
+
103
+ r.post do
104
+ bankid_guard do
105
+ token = session[bankid_session_key]
106
+ row = bankid_flow.read(token)
107
+
108
+ unless row[:completed_at]
109
+ status = bankid_confirm_collect_attempt(token)
110
+ bankid_confirm_html_status_response(status) unless status == 'complete'
111
+ row = bankid_flow.read(token)
112
+ end
113
+
114
+ unless row[:account_id] == account_id
115
+ bankid_flow.remove(token)
116
+ remove_session_value(bankid_session_key)
117
+ set_redirect_error_flash bankid_confirm_wrong_identity_error_flash
118
+ redirect bankid_confirm_path
119
+ end
120
+
121
+ bankid_confirm_finish(token)
122
+ end
123
+ end
124
+ end
125
+
126
+ # POST. Starts an identification order bound to the account.
127
+ route(:bankid_confirm_start) do |r|
128
+ bankid_confirm_route_setup
129
+ before_bankid_confirm_start_route
130
+
131
+ r.post do
132
+ bankid_guard do
133
+ bankid_cancel_current_attempt
134
+ before_bankid_confirm
135
+
136
+ nonce = Rodauth::BankID::Store.generate_token[0, 32]
137
+ token, row = bankid_flow.start(
138
+ end_user_ip: bankid_end_user_ip,
139
+ nonce:,
140
+ kind: :confirm,
141
+ attributes: { account_id: },
142
+ **bankid_confirm_options(nonce)
143
+ )
144
+ set_session_value(bankid_session_key, token)
145
+ bankid_start_response(row, bankid_confirm_path)
146
+ end
147
+ end
148
+ end
149
+
150
+ route(:bankid_confirm_qr) do |r|
151
+ bankid_confirm_route_setup
152
+ before_bankid_confirm_qr_route
153
+
154
+ r.post do
155
+ bankid_qr_response
156
+ end
157
+ end
158
+
159
+ route(:bankid_confirm_collect) do |r|
160
+ bankid_confirm_route_setup
161
+ before_bankid_confirm_collect_route
162
+
163
+ r.post do
164
+ bankid_guard do
165
+ status = bankid_confirm_collect_attempt(session[bankid_session_key])
166
+ result = @bankid_collect_result
167
+
168
+ case status
169
+ when 'pending'
170
+ bankid_json_response(status: 'pending', hint: bankid_hint_message(result['hintCode']),
171
+ pollAfter: result['pollAfter'] || bankid_collect_interval)
172
+ when 'failed'
173
+ remove_session_value(bankid_session_key)
174
+ bankid_json_response(status: 'failed', hint: bankid_hint_message(result['hintCode']))
175
+ when 'complete'
176
+ bankid_json_response(status: 'complete')
177
+ else
178
+ remove_session_value(bankid_session_key)
179
+ bankid_json_response({ error: 'identity_mismatch', hint: bankid_confirm_wrong_identity_error_flash }, 403)
180
+ end
181
+ end
182
+ end
183
+ end
184
+
185
+ route(:bankid_confirm_cancel) do |r|
186
+ bankid_confirm_route_setup
187
+ before_bankid_confirm_cancel_route
188
+
189
+ r.post do
190
+ bankid_cancel_current_attempt
191
+ bankid_cancel_response(bankid_confirm_cancelled_error_flash, bankid_confirm_cancelled_redirect)
192
+ end
193
+ end
194
+
195
+ # --- Public API -----------------------------------------------------
196
+
197
+ # Requires a login and a BankID proof no older than
198
+ # +bankid_confirm_grace_period+. Otherwise stores the current path,
199
+ # sets the flash, and redirects to the confirm page.
200
+ def require_bankid_authentication
201
+ require_login
202
+ return if bankid_recently_authenticated?
203
+
204
+ set_redirect_error_status(bankid_authentication_required_error_status)
205
+ set_redirect_error_flash bankid_authentication_required_error_flash
206
+ set_session_return_to_path(bankid_confirm_redirect_session_key)
207
+ redirect bankid_authentication_required_redirect
208
+ end
209
+
210
+ # True while the session's last BankID proof (login or confirmation) is
211
+ # within the grace period.
212
+ def bankid_recently_authenticated?
213
+ at = session[bankid_authenticated_at_session_key]
214
+ at.is_a?(Integer) && at + bankid_confirm_grace_period > Time.now.to_i
215
+ end
216
+
217
+ # --- Hooks for the app ---------------------------------------------
218
+
219
+ # Returns the personnummer of the logged-in account, if the app knows
220
+ # it. When given, only that BankID can complete the order.
221
+ def bankid_confirm_personal_number
222
+ nil
223
+ end
224
+
225
+ # Returns the BankID +requirement+ object for the order, or +nil+.
226
+ def bankid_confirm_requirement
227
+ number = bankid_confirm_personal_number
228
+ number ? { 'personalNumber' => number } : nil
229
+ end
230
+
231
+ # True when the BankID that completed the order belongs to the logged-in
232
+ # account: the app-supplied personnummer when there is one, else a
233
+ # linked identity digest.
234
+ def bankid_confirm_verify_identity(completion)
235
+ number = completion['user']['personalNumber']
236
+ required = bankid_confirm_personal_number
237
+ return required == number if required
238
+
239
+ db[bankid_identities_table].where(
240
+ bankid_identities_account_id_column => account_id,
241
+ bankid_identities_digest_column => bankid_identity_digest(number)
242
+ ).one?
243
+ end
244
+
245
+ def bankid_return_url_for(kind)
246
+ kind.to_s == 'confirm' ? bankid_confirm_return_url : super
247
+ end
248
+
249
+ private
250
+
251
+ def bankid_confirm_route_setup
252
+ bankid_no_store
253
+ require_account
254
+ bankid_scope(session_key: bankid_confirm_attempt_session_key, error_redirect: bankid_confirm_path)
255
+ end
256
+
257
+ def bankid_confirm_options(nonce)
258
+ opts = {
259
+ requirement: bankid_confirm_requirement,
260
+ return_risk: bankid_return_risk?,
261
+ web: bankid_web_parameters
262
+ }
263
+ opts[:return_url] = bankid_attempt_return_url(nonce, 'confirm') if bankid_confirm_return_url
264
+ opts
265
+ end
266
+
267
+ def bankid_confirm_collect_attempt(token)
268
+ result = bankid_flow.collect(token)
269
+ @bankid_collect_result = result
270
+ return result['status'] unless result['status'] == 'complete'
271
+
272
+ completion = result['completionData']
273
+ return 'complete' unless completion
274
+
275
+ bankid_handle_confirm_completion(token, completion)
276
+ end
277
+
278
+ # One transaction: claim, verify the identity, hook. The attempt keeps
279
+ # the account it was started for; the finishing POST checks it again.
280
+ def bankid_handle_confirm_completion(token, completion)
281
+ @bankid_completion = completion
282
+ digest = bankid_identity_digest(completion['user']['personalNumber'])
283
+ stored = bankid_completion_to_store(completion)
284
+
285
+ transaction do
286
+ unless bankid_flow.claim(token, identity_digest: digest, completion: stored)
287
+ bankid_flow.read(token)
288
+ next 'complete'
289
+ end
290
+
291
+ unless bankid_confirm_verify_identity(completion)
292
+ bankid_flow.remove(token)
293
+ next 'identity_mismatch'
294
+ end
295
+
296
+ after_bankid_confirm
297
+ 'complete'
298
+ end
299
+ end
300
+
301
+ def bankid_confirm_finish(token)
302
+ bankid_flow.remove(token)
303
+ remove_session_value(bankid_session_key)
304
+ bankid_mark_authenticated
305
+ set_notice_flash bankid_confirm_notice_flash
306
+ redirect(remove_session_value(bankid_confirm_redirect_session_key) || bankid_confirm_redirect)
307
+ end
308
+
309
+ def bankid_confirm_html_status_response(status)
310
+ result = @bankid_collect_result || {}
311
+ case status
312
+ when 'pending'
313
+ set_redirect_error_flash bankid_hint_message(result['hintCode'])
314
+ when 'failed'
315
+ remove_session_value(bankid_session_key)
316
+ set_redirect_error_flash bankid_hint_message(result['hintCode'])
317
+ else
318
+ remove_session_value(bankid_session_key)
319
+ set_redirect_error_flash bankid_confirm_wrong_identity_error_flash
320
+ end
321
+ redirect bankid_confirm_path
322
+ end
323
+ end
324
+ end