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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/LICENSE +21 -0
- data/README.md +266 -0
- data/db/migrate/001_create_bankid_tables.rb +116 -0
- data/lib/rodauth/bankid/client.rb +192 -0
- data/lib/rodauth/bankid/errors.rb +33 -0
- data/lib/rodauth/bankid/flow.rb +178 -0
- data/lib/rodauth/bankid/qr.rb +21 -0
- data/lib/rodauth/bankid/store.rb +115 -0
- data/lib/rodauth/bankid/version.rb +8 -0
- data/lib/rodauth/bankid.rb +37 -0
- data/lib/rodauth/features/bankid.rb +915 -0
- data/lib/rodauth/features/bankid_confirm.rb +324 -0
- data/lib/rodauth/features/bankid_sign.rb +464 -0
- data/public/javascripts/rodauth_bankid.js +145 -0
- data/templates/bankid-confirm.erb +33 -0
- data/templates/bankid-login.erb +31 -0
- data/templates/bankid-script.erb +1 -0
- data/templates/bankid-sign.erb +34 -0
- metadata +337 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rodauth/features/bankid'
|
|
4
|
+
|
|
5
|
+
module Rodauth
|
|
6
|
+
# The +bankid_sign+ feature lets a logged-in account sign a text with
|
|
7
|
+
# BankID and keeps the evidence. Unlike the login log, the signature XML
|
|
8
|
+
# and OCSP response are stored on purpose here: the rows are the proof.
|
|
9
|
+
#
|
|
10
|
+
# rodauth.bankid_sign_prepare(visible_data: 'I accept the 2026 membership terms',
|
|
11
|
+
# reference: 'terms-2026')
|
|
12
|
+
# r.redirect rodauth.bankid_sign_path
|
|
13
|
+
#
|
|
14
|
+
# Routes under the Rodauth prefix:
|
|
15
|
+
#
|
|
16
|
+
# /bankid-sign :: GET shows the text and a Sign button; POST finishes.
|
|
17
|
+
# /bankid-sign-start :: POST starts the order for the prepared text.
|
|
18
|
+
# /bankid-sign-qr, /bankid-sign-collect, /bankid-sign-cancel :: As for login.
|
|
19
|
+
#
|
|
20
|
+
# A sign order is only valid if the BankID that signed belongs to the
|
|
21
|
+
# logged-in account: with +bankid_sign_personal_number+ set the number goes
|
|
22
|
+
# into the order's +requirement+ and BankID refuses any other BankID;
|
|
23
|
+
# without it, the completion's personnummer must match a linked identity.
|
|
24
|
+
# No match means 403 +signer_mismatch+ and the row stays pending.
|
|
25
|
+
#
|
|
26
|
+
# = Configuration
|
|
27
|
+
#
|
|
28
|
+
# bankid_sign_return_url :: Absolute https URL of the sign page, for the
|
|
29
|
+
# mobile return flow. Default: +nil+.
|
|
30
|
+
# bankid_sign_mrtd? :: Require an ID-document scan (step-up). Default: +false+.
|
|
31
|
+
# bankid_sign_visible_data_limit :: BankID's base64 length cap. Default: 40 000.
|
|
32
|
+
# bankid_signatures_table, bankid_signatures_*_column :: Names. Set a
|
|
33
|
+
# column to +nil+ to
|
|
34
|
+
# skip it.
|
|
35
|
+
#
|
|
36
|
+
# = Hooks
|
|
37
|
+
#
|
|
38
|
+
# before_bankid_sign :: Before the order starts.
|
|
39
|
+
# after_bankid_sign :: Inside the claim transaction, with +bankid_signature+
|
|
40
|
+
# and +bankid_completion+ set. Same rollback rules as
|
|
41
|
+
# +after_bankid_collect+.
|
|
42
|
+
#
|
|
43
|
+
# = Methods
|
|
44
|
+
#
|
|
45
|
+
# bankid_sign_prepare(visible_data:, ...) :: Records what the account is
|
|
46
|
+
# about to sign; see below.
|
|
47
|
+
# bankid_signature :: The signature row the session is working on.
|
|
48
|
+
# bankid_sign_personal_number :: The account's personnummer, if the app
|
|
49
|
+
# knows it. Default: +nil+.
|
|
50
|
+
# bankid_sign_verify_signer(completion, signature) :: The signer rule.
|
|
51
|
+
# bankid_store_signature(signature, completion, row) :: Writes the evidence.
|
|
52
|
+
Feature.define(:bankid_sign, :BankidSign) do
|
|
53
|
+
depends :bankid
|
|
54
|
+
|
|
55
|
+
loaded_templates %w[bankid-sign]
|
|
56
|
+
view 'bankid-sign', 'Sign with BankID', 'bankid_sign'
|
|
57
|
+
|
|
58
|
+
before 'bankid_sign'
|
|
59
|
+
after 'bankid_sign'
|
|
60
|
+
|
|
61
|
+
error_flash 'There is nothing to sign', 'bankid_sign_missing'
|
|
62
|
+
error_flash 'The BankID used does not belong to your account', 'bankid_sign_wrong_signer'
|
|
63
|
+
error_flash 'Signing was cancelled', 'bankid_sign_cancelled'
|
|
64
|
+
notice_flash 'Signed with BankID', 'bankid_sign'
|
|
65
|
+
|
|
66
|
+
redirect(:bankid_sign) { default_redirect }
|
|
67
|
+
redirect(:bankid_sign_missing) { default_redirect }
|
|
68
|
+
redirect(:bankid_sign_cancelled) { default_redirect }
|
|
69
|
+
|
|
70
|
+
session_key :bankid_sign_attempt_session_key, :bankid_sign_attempt
|
|
71
|
+
session_key :bankid_signature_session_key, :bankid_signature
|
|
72
|
+
|
|
73
|
+
auth_value_method :bankid_sign_return_url, nil
|
|
74
|
+
auth_value_method :bankid_sign_mrtd?, false
|
|
75
|
+
auth_value_method :bankid_sign_visible_data_limit, 40_000
|
|
76
|
+
|
|
77
|
+
auth_value_method :bankid_signatures_table, :account_bankid_signatures
|
|
78
|
+
auth_value_method :bankid_signatures_id_column, :account_bankid_signature_id
|
|
79
|
+
auth_value_method :bankid_signatures_account_id_column, :account_id
|
|
80
|
+
auth_value_method :bankid_signatures_reference_column, :reference
|
|
81
|
+
auth_value_method :bankid_signatures_visible_data_column, :visible_data
|
|
82
|
+
auth_value_method :bankid_signatures_non_visible_data_column, :non_visible_data
|
|
83
|
+
auth_value_method :bankid_signatures_format_column, :visible_data_format
|
|
84
|
+
auth_value_method :bankid_signatures_status_column, :status
|
|
85
|
+
auth_value_method :bankid_signatures_order_ref_column, :order_ref
|
|
86
|
+
auth_value_method :bankid_signatures_signature_column, :signature
|
|
87
|
+
auth_value_method :bankid_signatures_ocsp_response_column, :ocsp_response
|
|
88
|
+
auth_value_method :bankid_signatures_signer_name_column, :signer_name
|
|
89
|
+
auth_value_method :bankid_signatures_identity_digest_column, :identity_digest
|
|
90
|
+
auth_value_method :bankid_signatures_issue_date_column, :bankid_issue_date
|
|
91
|
+
auth_value_method :bankid_signatures_ip_address_column, :ip_address
|
|
92
|
+
auth_value_method :bankid_signatures_uhi_column, :uhi
|
|
93
|
+
auth_value_method :bankid_signatures_step_up_mrtd_column, :step_up_mrtd
|
|
94
|
+
auth_value_method :bankid_signatures_risk_column, :risk
|
|
95
|
+
auth_value_method :bankid_signatures_created_at_column, :created_at
|
|
96
|
+
auth_value_method :bankid_signatures_signed_at_column, :signed_at
|
|
97
|
+
auth_value_method :bankid_attempts_signature_id_column, :account_bankid_signature_id
|
|
98
|
+
|
|
99
|
+
translatable_method :bankid_sign_intro, 'Read the text below, then sign it with your BankID app'
|
|
100
|
+
translatable_method :bankid_sign_start_button, 'Sign with BankID'
|
|
101
|
+
translatable_method :bankid_sign_continue_button, 'Continue'
|
|
102
|
+
|
|
103
|
+
auth_methods(
|
|
104
|
+
:bankid_sign_personal_number,
|
|
105
|
+
:bankid_sign_prepare,
|
|
106
|
+
:bankid_sign_requirement,
|
|
107
|
+
:bankid_sign_verify_signer,
|
|
108
|
+
:bankid_store_signature
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
# GET shows the text and a start button. POST finishes: it collects if
|
|
112
|
+
# the browser did not, then redirects once the signature is stored.
|
|
113
|
+
route(:bankid_sign) do |r|
|
|
114
|
+
bankid_sign_route_setup
|
|
115
|
+
before_bankid_sign_route
|
|
116
|
+
|
|
117
|
+
r.get do
|
|
118
|
+
bankid_signature_or_redirect
|
|
119
|
+
bankid_sign_view
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
r.post do
|
|
123
|
+
bankid_guard do
|
|
124
|
+
bankid_signature_or_redirect
|
|
125
|
+
token = session[bankid_session_key]
|
|
126
|
+
|
|
127
|
+
if token
|
|
128
|
+
row = bankid_flow.read(token)
|
|
129
|
+
unless row[:completed_at]
|
|
130
|
+
status = bankid_sign_collect_attempt(token)
|
|
131
|
+
bankid_sign_html_status_response(status) unless status == 'complete'
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
unless bankid_signature_signed?
|
|
136
|
+
set_redirect_error_flash bankid_hint_outstanding_transaction
|
|
137
|
+
redirect bankid_sign_path
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
bankid_sign_finish(token)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# POST. Starts the sign order for the prepared text.
|
|
146
|
+
route(:bankid_sign_start) do |r|
|
|
147
|
+
bankid_sign_route_setup
|
|
148
|
+
before_bankid_sign_start_route
|
|
149
|
+
|
|
150
|
+
r.post do
|
|
151
|
+
bankid_guard do
|
|
152
|
+
signature = bankid_signature_or_redirect
|
|
153
|
+
bankid_cancel_current_attempt
|
|
154
|
+
before_bankid_sign
|
|
155
|
+
|
|
156
|
+
nonce = Rodauth::BankID::Store.generate_token[0, 32]
|
|
157
|
+
token, row = bankid_flow.start(
|
|
158
|
+
end_user_ip: bankid_end_user_ip,
|
|
159
|
+
nonce:,
|
|
160
|
+
kind: :sign,
|
|
161
|
+
attributes: { bankid_attempts_signature_id_column => signature[bankid_signatures_id_column],
|
|
162
|
+
account_id: },
|
|
163
|
+
**bankid_sign_options(signature, nonce)
|
|
164
|
+
)
|
|
165
|
+
bankid_signature_ds.update(bankid_signatures_order_ref_column => row[:order_ref])
|
|
166
|
+
set_session_value(bankid_session_key, token)
|
|
167
|
+
bankid_start_response(row, bankid_sign_path)
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
route(:bankid_sign_qr) do |r|
|
|
173
|
+
bankid_sign_route_setup
|
|
174
|
+
before_bankid_sign_qr_route
|
|
175
|
+
|
|
176
|
+
r.post do
|
|
177
|
+
bankid_qr_response
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
route(:bankid_sign_collect) do |r|
|
|
182
|
+
bankid_sign_route_setup
|
|
183
|
+
before_bankid_sign_collect_route
|
|
184
|
+
|
|
185
|
+
r.post do
|
|
186
|
+
bankid_guard do
|
|
187
|
+
status = bankid_sign_collect_attempt(session[bankid_session_key])
|
|
188
|
+
result = @bankid_collect_result
|
|
189
|
+
|
|
190
|
+
case status
|
|
191
|
+
when 'pending'
|
|
192
|
+
bankid_json_response(status: 'pending', hint: bankid_hint_message(result['hintCode']),
|
|
193
|
+
pollAfter: result['pollAfter'] || bankid_collect_interval)
|
|
194
|
+
when 'failed'
|
|
195
|
+
remove_session_value(bankid_session_key)
|
|
196
|
+
bankid_json_response(status: 'failed', hint: bankid_hint_message(result['hintCode']))
|
|
197
|
+
when 'complete'
|
|
198
|
+
bankid_json_response(status: 'complete')
|
|
199
|
+
else
|
|
200
|
+
remove_session_value(bankid_session_key)
|
|
201
|
+
bankid_json_response({ error: 'signer_mismatch', hint: bankid_sign_wrong_signer_error_flash }, 403)
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
route(:bankid_sign_cancel) do |r|
|
|
208
|
+
bankid_sign_route_setup
|
|
209
|
+
before_bankid_sign_cancel_route
|
|
210
|
+
|
|
211
|
+
r.post do
|
|
212
|
+
bankid_cancel_current_attempt
|
|
213
|
+
bankid_sign_abandon
|
|
214
|
+
bankid_cancel_response(bankid_sign_cancelled_error_flash, bankid_sign_cancelled_redirect)
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# --- Public API -----------------------------------------------------
|
|
219
|
+
|
|
220
|
+
# Records what the logged-in account is about to sign and returns the
|
|
221
|
+
# signature id; redirect to +bankid_sign_path+ afterwards.
|
|
222
|
+
# +visible_data+ is shown in the app (plain text, or +simpleMarkdownV1+
|
|
223
|
+
# when +format+ says so); +non_visible_data+ is signed but not shown;
|
|
224
|
+
# +reference+ is your own key for the row. Raises ArgumentError for
|
|
225
|
+
# empty, oversized, or wrongly typed data.
|
|
226
|
+
def bankid_sign_prepare(visible_data:, non_visible_data: nil, format: nil, reference: nil)
|
|
227
|
+
require_account
|
|
228
|
+
bankid_sign_route_setup
|
|
229
|
+
bankid_validate_sign_data(visible_data, non_visible_data, format)
|
|
230
|
+
bankid_cancel_current_attempt
|
|
231
|
+
|
|
232
|
+
values = {
|
|
233
|
+
bankid_signatures_account_id_column => account_id,
|
|
234
|
+
bankid_signatures_visible_data_column => visible_data,
|
|
235
|
+
bankid_signatures_status_column => 'pending'
|
|
236
|
+
}
|
|
237
|
+
values[bankid_signatures_reference_column] = reference if reference && bankid_signatures_reference_column
|
|
238
|
+
values[bankid_signatures_non_visible_data_column] = non_visible_data if non_visible_data
|
|
239
|
+
values[bankid_signatures_format_column] = format if format
|
|
240
|
+
values[bankid_signatures_created_at_column] = Sequel::CURRENT_TIMESTAMP if bankid_signatures_created_at_column
|
|
241
|
+
|
|
242
|
+
id = db[bankid_signatures_table].insert(values)
|
|
243
|
+
set_session_value(bankid_signature_session_key, id)
|
|
244
|
+
@bankid_signature = nil
|
|
245
|
+
id
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# Returns the signature row the session is working on, scoped to the
|
|
249
|
+
# account, or +nil+.
|
|
250
|
+
def bankid_signature
|
|
251
|
+
return @bankid_signature if defined?(@bankid_signature) && @bankid_signature
|
|
252
|
+
|
|
253
|
+
@bankid_signature = bankid_signature_ds.first
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# True once the current signature row holds evidence.
|
|
257
|
+
def bankid_signature_signed?
|
|
258
|
+
signature = bankid_signature
|
|
259
|
+
signature && signature[bankid_signatures_status_column] == 'signed'
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# --- Hooks for the app ---------------------------------------------
|
|
263
|
+
|
|
264
|
+
# Returns the personnummer of the logged-in account, if the app knows
|
|
265
|
+
# it. When given, it goes into the order's +requirement+ so only that
|
|
266
|
+
# BankID can sign, and it is the basis for signer verification.
|
|
267
|
+
def bankid_sign_personal_number
|
|
268
|
+
nil
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# Returns the BankID +requirement+ object for the sign order, or +nil+.
|
|
272
|
+
def bankid_sign_requirement(_signature)
|
|
273
|
+
requirement = {}
|
|
274
|
+
number = bankid_sign_personal_number
|
|
275
|
+
requirement['personalNumber'] = number if number
|
|
276
|
+
requirement['mrtd'] = true if bankid_sign_mrtd?
|
|
277
|
+
requirement.empty? ? nil : requirement
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# True when the BankID that completed the order belongs to the account:
|
|
281
|
+
# the app-supplied personnummer when there is one, else a linked
|
|
282
|
+
# identity digest. No match, no signature.
|
|
283
|
+
def bankid_sign_verify_signer(completion, signature)
|
|
284
|
+
number = completion['user']['personalNumber']
|
|
285
|
+
required = bankid_sign_personal_number
|
|
286
|
+
return required == number if required
|
|
287
|
+
|
|
288
|
+
db[bankid_identities_table].where(
|
|
289
|
+
bankid_identities_account_id_column => signature[bankid_signatures_account_id_column],
|
|
290
|
+
bankid_identities_digest_column => bankid_identity_digest(number)
|
|
291
|
+
).one?
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# Writes the evidence onto the signature row and marks it +signed+.
|
|
295
|
+
# Runs inside the claim transaction.
|
|
296
|
+
def bankid_store_signature(signature, completion, _row)
|
|
297
|
+
values = { bankid_signatures_status_column => 'signed' }
|
|
298
|
+
bankid_signature_values(completion).each do |column, value|
|
|
299
|
+
values[column] = value if column
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
db[bankid_signatures_table]
|
|
303
|
+
.where(bankid_signatures_id_column => signature[bankid_signatures_id_column])
|
|
304
|
+
.update(values)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# Returns column => value pairs taken from +completion+. +nil+ columns
|
|
308
|
+
# are skipped by the caller.
|
|
309
|
+
def bankid_signature_values(completion)
|
|
310
|
+
user = completion['user']
|
|
311
|
+
device = completion['device'].is_a?(Hash) ? completion['device'] : {}
|
|
312
|
+
step_up = completion['stepUp'].is_a?(Hash) ? completion['stepUp'] : {}
|
|
313
|
+
mrtd = step_up['mrtd'] ? true : false if step_up.key?('mrtd')
|
|
314
|
+
|
|
315
|
+
{
|
|
316
|
+
bankid_signatures_signature_column => completion['signature'],
|
|
317
|
+
bankid_signatures_ocsp_response_column => completion['ocspResponse'],
|
|
318
|
+
bankid_signatures_signer_name_column => user['name'],
|
|
319
|
+
bankid_signatures_identity_digest_column => bankid_identity_digest(user['personalNumber']),
|
|
320
|
+
bankid_signatures_issue_date_column => bankid_parse_date(completion['bankIdIssueDate']),
|
|
321
|
+
bankid_signatures_ip_address_column => device['ipAddress'],
|
|
322
|
+
bankid_signatures_uhi_column => device['uhi'],
|
|
323
|
+
bankid_signatures_risk_column => completion['risk'],
|
|
324
|
+
bankid_signatures_step_up_mrtd_column => mrtd,
|
|
325
|
+
bankid_signatures_signed_at_column => Sequel::CURRENT_TIMESTAMP
|
|
326
|
+
}
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def bankid_return_url_for(kind)
|
|
330
|
+
kind.to_s == 'sign' ? bankid_sign_return_url : super
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
private
|
|
334
|
+
|
|
335
|
+
def bankid_sign_route_setup
|
|
336
|
+
bankid_no_store
|
|
337
|
+
require_account
|
|
338
|
+
bankid_scope(session_key: bankid_sign_attempt_session_key, error_redirect: bankid_sign_path)
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def bankid_signature_ds
|
|
342
|
+
id = session[bankid_signature_session_key]
|
|
343
|
+
return db[bankid_signatures_table].where(false) unless id
|
|
344
|
+
|
|
345
|
+
db[bankid_signatures_table].where(
|
|
346
|
+
bankid_signatures_id_column => id,
|
|
347
|
+
bankid_signatures_account_id_column => account_id
|
|
348
|
+
)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def bankid_signature_or_redirect
|
|
352
|
+
signature = bankid_signature
|
|
353
|
+
return signature if signature && signature[bankid_signatures_status_column] != 'cancelled'
|
|
354
|
+
|
|
355
|
+
remove_session_value(bankid_signature_session_key)
|
|
356
|
+
if bankid_json_request?
|
|
357
|
+
bankid_json_response({ error: 'nothing_to_sign', hint: bankid_sign_missing_error_flash }, 410)
|
|
358
|
+
else
|
|
359
|
+
set_redirect_error_flash bankid_sign_missing_error_flash
|
|
360
|
+
redirect bankid_sign_missing_redirect
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def bankid_validate_sign_data(visible_data, non_visible_data, format)
|
|
365
|
+
unless visible_data.is_a?(String) && !visible_data.empty?
|
|
366
|
+
raise ArgumentError,
|
|
367
|
+
'visible_data must be a non-empty string'
|
|
368
|
+
end
|
|
369
|
+
if [visible_data].pack('m0').length > bankid_sign_visible_data_limit
|
|
370
|
+
raise ArgumentError,
|
|
371
|
+
"visible_data exceeds BankID's limit of #{bankid_sign_visible_data_limit} base64 characters"
|
|
372
|
+
end
|
|
373
|
+
raise ArgumentError, 'non_visible_data must be a string' if non_visible_data && !non_visible_data.is_a?(String)
|
|
374
|
+
return if format.nil? || format == 'simpleMarkdownV1'
|
|
375
|
+
|
|
376
|
+
raise ArgumentError, 'format must be nil or simpleMarkdownV1'
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
def bankid_sign_options(signature, nonce)
|
|
380
|
+
opts = {
|
|
381
|
+
user_visible_data: signature[bankid_signatures_visible_data_column],
|
|
382
|
+
requirement: bankid_sign_requirement(signature),
|
|
383
|
+
return_risk: bankid_return_risk?,
|
|
384
|
+
web: bankid_web_parameters
|
|
385
|
+
}
|
|
386
|
+
non_visible = bankid_signatures_non_visible_data_column && signature[bankid_signatures_non_visible_data_column]
|
|
387
|
+
opts[:user_non_visible_data] = non_visible if non_visible
|
|
388
|
+
format = bankid_signatures_format_column && signature[bankid_signatures_format_column]
|
|
389
|
+
opts[:user_visible_data_format] = format if format
|
|
390
|
+
opts[:return_url] = bankid_attempt_return_url(nonce, 'sign') if bankid_sign_return_url
|
|
391
|
+
opts
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
def bankid_sign_collect_attempt(token)
|
|
395
|
+
result = bankid_flow.collect(token)
|
|
396
|
+
@bankid_collect_result = result
|
|
397
|
+
return result['status'] unless result['status'] == 'complete'
|
|
398
|
+
|
|
399
|
+
completion = result['completionData']
|
|
400
|
+
return 'complete' unless completion
|
|
401
|
+
|
|
402
|
+
bankid_handle_sign_completion(token, completion)
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# One transaction: claim, verify the signer, store the evidence, hook.
|
|
406
|
+
def bankid_handle_sign_completion(token, completion)
|
|
407
|
+
@bankid_completion = completion
|
|
408
|
+
digest = bankid_identity_digest(completion['user']['personalNumber'])
|
|
409
|
+
stored = bankid_completion_to_store(completion)
|
|
410
|
+
|
|
411
|
+
transaction do
|
|
412
|
+
unless bankid_flow.claim(token, identity_digest: digest, completion: stored)
|
|
413
|
+
bankid_flow.read(token)
|
|
414
|
+
next 'complete'
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
row = bankid_flow.read(token)
|
|
418
|
+
signature = bankid_signature_ds
|
|
419
|
+
.where(bankid_signatures_id_column => row[bankid_attempts_signature_id_column])
|
|
420
|
+
.where(bankid_signatures_status_column => 'pending')
|
|
421
|
+
.first
|
|
422
|
+
|
|
423
|
+
unless signature && bankid_sign_verify_signer(completion, signature)
|
|
424
|
+
bankid_flow.remove(token)
|
|
425
|
+
next 'signer_mismatch'
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
bankid_store_signature(signature, completion, row)
|
|
429
|
+
@bankid_signature = bankid_signature_ds.first
|
|
430
|
+
after_bankid_sign
|
|
431
|
+
'complete'
|
|
432
|
+
end
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def bankid_sign_finish(token)
|
|
436
|
+
bankid_flow.remove(token) if token
|
|
437
|
+
remove_session_value(bankid_session_key)
|
|
438
|
+
remove_session_value(bankid_signature_session_key)
|
|
439
|
+
set_notice_flash bankid_sign_notice_flash
|
|
440
|
+
redirect bankid_sign_redirect
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
def bankid_sign_abandon
|
|
444
|
+
bankid_signature_ds.where(bankid_signatures_status_column => 'pending')
|
|
445
|
+
.update(bankid_signatures_status_column => 'cancelled')
|
|
446
|
+
remove_session_value(bankid_signature_session_key)
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
def bankid_sign_html_status_response(status)
|
|
450
|
+
result = @bankid_collect_result || {}
|
|
451
|
+
case status
|
|
452
|
+
when 'pending'
|
|
453
|
+
set_redirect_error_flash bankid_hint_message(result['hintCode'])
|
|
454
|
+
when 'failed'
|
|
455
|
+
remove_session_value(bankid_session_key)
|
|
456
|
+
set_redirect_error_flash bankid_hint_message(result['hintCode'])
|
|
457
|
+
else
|
|
458
|
+
remove_session_value(bankid_session_key)
|
|
459
|
+
set_redirect_error_flash bankid_sign_wrong_signer_error_flash
|
|
460
|
+
end
|
|
461
|
+
redirect bankid_sign_path
|
|
462
|
+
end
|
|
463
|
+
end
|
|
464
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
var root = document.getElementById("bankid");
|
|
3
|
+
if (!root || !window.fetch) { return; }
|
|
4
|
+
var d = root.dataset;
|
|
5
|
+
var el = {
|
|
6
|
+
start: document.getElementById("bankid-start"),
|
|
7
|
+
startForm: document.getElementById("bankid-start-form"),
|
|
8
|
+
startButton: document.getElementById("bankid-start-button"),
|
|
9
|
+
active: document.getElementById("bankid-active"),
|
|
10
|
+
qr: document.getElementById("bankid-qr"),
|
|
11
|
+
hint: document.getElementById("bankid-hint"),
|
|
12
|
+
open: document.getElementById("bankid-open"),
|
|
13
|
+
cont: document.getElementById("bankid-continue"),
|
|
14
|
+
error: document.getElementById("bankid-error"),
|
|
15
|
+
restart: document.getElementById("bankid-restart")
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// One generation per attempt. Responses from an older generation are
|
|
19
|
+
// ignored, and stop() aborts every request of the current one.
|
|
20
|
+
var gen = 0;
|
|
21
|
+
var s = { running: false, qrBusy: false, collectBusy: false, failures: 0,
|
|
22
|
+
qrTimer: null, collectTimer: null, aborter: null, starting: false };
|
|
23
|
+
|
|
24
|
+
function post(url, token, signal) {
|
|
25
|
+
var body = new URLSearchParams();
|
|
26
|
+
body.set(d.csrfField, token);
|
|
27
|
+
return fetch(url, { method: "POST", credentials: "same-origin", body: body, signal: signal,
|
|
28
|
+
headers: { "Accept": "application/json" } }).then(function (r) {
|
|
29
|
+
return r.json().then(function (b) {
|
|
30
|
+
return { status: r.status, body: b, retryAfter: r.headers.get("Retry-After") };
|
|
31
|
+
}, function () { return { status: r.status, body: {} }; });
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isMobile() { return /Android|iPhone|iPad|iPod/i.test(navigator.userAgent); }
|
|
36
|
+
function live(g) { return g === gen && s.running; }
|
|
37
|
+
|
|
38
|
+
function stop(message) {
|
|
39
|
+
s.running = false;
|
|
40
|
+
clearTimeout(s.qrTimer);
|
|
41
|
+
clearTimeout(s.collectTimer);
|
|
42
|
+
if (s.aborter) { s.aborter.abort(); s.aborter = null; }
|
|
43
|
+
if (message) {
|
|
44
|
+
el.error.textContent = message;
|
|
45
|
+
el.error.hidden = false;
|
|
46
|
+
el.restart.hidden = false;
|
|
47
|
+
el.qr.innerHTML = "";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function scheduleQr(g, ms) { if (live(g)) { s.qrTimer = setTimeout(function () { pollQr(g); }, ms); } }
|
|
52
|
+
function pollQr(g) {
|
|
53
|
+
if (!live(g)) { return; }
|
|
54
|
+
if (s.qrBusy) { return scheduleQr(g, 1000); }
|
|
55
|
+
s.qrBusy = true;
|
|
56
|
+
post(d.qrUrl, d.csrfQr, s.aborter.signal).then(function (res) {
|
|
57
|
+
if (!live(g)) { return; }
|
|
58
|
+
s.qrBusy = false;
|
|
59
|
+
if (res.status === 200 && res.body.svg) { el.qr.innerHTML = res.body.svg; }
|
|
60
|
+
else if (res.status === 410) { return stop(res.body.hint || d.errorText); }
|
|
61
|
+
scheduleQr(g, 1000);
|
|
62
|
+
}, function () { if (!live(g)) { return; } s.qrBusy = false; scheduleQr(g, 1000); });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function scheduleCollect(g, ms) { if (live(g)) { s.collectTimer = setTimeout(function () { collect(g); }, ms); } }
|
|
66
|
+
function collect(g) {
|
|
67
|
+
if (!live(g)) { return; }
|
|
68
|
+
if (s.collectBusy) { return scheduleCollect(g, 500); }
|
|
69
|
+
s.collectBusy = true;
|
|
70
|
+
post(d.collectUrl, d.csrfCollect, s.aborter.signal).then(function (res) {
|
|
71
|
+
if (!live(g)) { return; }
|
|
72
|
+
s.collectBusy = false;
|
|
73
|
+
var b = res.body || {};
|
|
74
|
+
if (b.hint) { el.hint.textContent = b.hint; }
|
|
75
|
+
if (res.status === 200 && b.status === "complete") { stop(); el.cont.submit(); return; }
|
|
76
|
+
if (res.status === 200 && b.status === "pending") { s.failures = 0; return scheduleCollect(g, (b.pollAfter || d.pollAfter || 2) * 1000); }
|
|
77
|
+
if (res.status === 200 && b.status === "failed") { return stop(b.hint || d.errorText); }
|
|
78
|
+
if (res.status === 429) { return scheduleCollect(g, (parseInt(res.retryAfter, 10) || 2) * 1000); }
|
|
79
|
+
if (res.status === 502 && s.failures < 3) { s.failures += 1; return scheduleCollect(g, 3000); }
|
|
80
|
+
stop(b.hint || d.errorText);
|
|
81
|
+
}, function () {
|
|
82
|
+
if (!live(g)) { return; }
|
|
83
|
+
s.collectBusy = false;
|
|
84
|
+
if (s.failures < 3) { s.failures += 1; return scheduleCollect(g, 3000); }
|
|
85
|
+
stop(d.errorText);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function begin() {
|
|
90
|
+
gen += 1;
|
|
91
|
+
var g = gen;
|
|
92
|
+
s.running = true;
|
|
93
|
+
s.failures = 0;
|
|
94
|
+
s.qrBusy = false;
|
|
95
|
+
s.collectBusy = false;
|
|
96
|
+
s.aborter = new AbortController();
|
|
97
|
+
el.start.hidden = true;
|
|
98
|
+
el.active.hidden = false;
|
|
99
|
+
el.error.hidden = true;
|
|
100
|
+
el.restart.hidden = true;
|
|
101
|
+
scheduleQr(g, 0);
|
|
102
|
+
scheduleCollect(g, (parseInt(d.pollAfter, 10) || 2) * 1000);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function setStarting(on) {
|
|
106
|
+
s.starting = on;
|
|
107
|
+
el.startButton.disabled = on;
|
|
108
|
+
el.restart.disabled = on;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function startAttempt() {
|
|
112
|
+
if (s.starting) { return; }
|
|
113
|
+
stop();
|
|
114
|
+
setStarting(true);
|
|
115
|
+
el.error.hidden = true;
|
|
116
|
+
el.restart.hidden = true;
|
|
117
|
+
post(d.authUrl, d.csrfAuth).then(function (res) {
|
|
118
|
+
setStarting(false);
|
|
119
|
+
if (res.status !== 200) { return stop(res.body.hint || d.errorText); }
|
|
120
|
+
el.open.href = isMobile() ? res.body.universalUrl : res.body.autostartUrl;
|
|
121
|
+
el.qr.innerHTML = "";
|
|
122
|
+
begin();
|
|
123
|
+
}, function () {
|
|
124
|
+
setStarting(false);
|
|
125
|
+
stop(d.errorText);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
el.startForm.addEventListener("submit", function (ev) { ev.preventDefault(); startAttempt(); });
|
|
130
|
+
el.restart.addEventListener("click", startAttempt);
|
|
131
|
+
|
|
132
|
+
// Returned from the app. The fragment must name this session's attempt.
|
|
133
|
+
var match = /^#bankid=([0-9a-f]{32})$/.exec(window.location.hash || "");
|
|
134
|
+
if (match && match[1] !== d.nonce) {
|
|
135
|
+
el.active.hidden = true;
|
|
136
|
+
return stop(d.mismatchText);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (d.state === "pending") {
|
|
140
|
+
el.open.href = isMobile() ? d.universalUrl : d.autostartUrl;
|
|
141
|
+
begin();
|
|
142
|
+
} else if (d.state === "complete") {
|
|
143
|
+
el.cont.submit();
|
|
144
|
+
}
|
|
145
|
+
})();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<div id="bankid" <%== rodauth.bankid_data_attributes(start_path: rodauth.bankid_confirm_start_path, qr_path: rodauth.bankid_confirm_qr_path, collect_path: rodauth.bankid_confirm_collect_path) %>>
|
|
2
|
+
|
|
3
|
+
<p id="bankid-confirm-intro"><%= rodauth.bankid_confirm_intro %></p>
|
|
4
|
+
|
|
5
|
+
<div id="bankid-start"<%= ' hidden' unless rodauth.bankid_attempt_state == 'idle' %>>
|
|
6
|
+
<form id="bankid-start-form" method="post" action="<%= rodauth.bankid_confirm_start_path %>">
|
|
7
|
+
<%== rodauth.csrf_tag(rodauth.bankid_confirm_start_path) %>
|
|
8
|
+
<button id="bankid-start-button" type="submit"><%= rodauth.bankid_confirm_start_button %></button>
|
|
9
|
+
</form>
|
|
10
|
+
</div>
|
|
11
|
+
|
|
12
|
+
<div id="bankid-active"<%= ' hidden' if rodauth.bankid_attempt_state == 'idle' %>>
|
|
13
|
+
<p id="bankid-scan"><%= rodauth.bankid_scan_text %></p>
|
|
14
|
+
<div id="bankid-qr"><%== rodauth.bankid_current_qr_svg %></div>
|
|
15
|
+
<p id="bankid-hint"><%= rodauth.bankid_hint_outstanding_transaction %></p>
|
|
16
|
+
<p><a id="bankid-open" href="<%= rodauth.bankid_current_attempt ? rodauth.bankid_autostart_url : '#' %>"><%= rodauth.bankid_open_app_button %></a></p>
|
|
17
|
+
|
|
18
|
+
<form id="bankid-continue" method="post" action="<%= rodauth.bankid_confirm_path %>">
|
|
19
|
+
<%== rodauth.csrf_tag(rodauth.bankid_confirm_path) %>
|
|
20
|
+
<button type="submit"><%= rodauth.bankid_confirm_continue_button %></button>
|
|
21
|
+
</form>
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
<form id="bankid-cancel" method="post" action="<%= rodauth.bankid_confirm_cancel_path %>">
|
|
25
|
+
<%== rodauth.csrf_tag(rodauth.bankid_confirm_cancel_path) %>
|
|
26
|
+
<button type="submit"><%= rodauth.bankid_cancel_button %></button>
|
|
27
|
+
</form>
|
|
28
|
+
|
|
29
|
+
<p id="bankid-error" hidden></p>
|
|
30
|
+
<button id="bankid-restart" type="button" hidden><%= rodauth.bankid_restart_button %></button>
|
|
31
|
+
</div>
|
|
32
|
+
|
|
33
|
+
<%== rodauth.render('bankid-script') %>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<div id="bankid" <%== rodauth.bankid_data_attributes(start_path: rodauth.bankid_auth_path, qr_path: rodauth.bankid_qr_path, collect_path: rodauth.bankid_collect_path) %>>
|
|
2
|
+
|
|
3
|
+
<div id="bankid-start"<%= ' hidden' unless rodauth.bankid_attempt_state == 'idle' %>>
|
|
4
|
+
<form id="bankid-start-form" method="post" action="<%= rodauth.bankid_auth_path %>">
|
|
5
|
+
<%== rodauth.csrf_tag(rodauth.bankid_auth_path) %>
|
|
6
|
+
<button id="bankid-start-button" type="submit"><%= rodauth.bankid_start_button %></button>
|
|
7
|
+
</form>
|
|
8
|
+
</div>
|
|
9
|
+
|
|
10
|
+
<div id="bankid-active"<%= ' hidden' if rodauth.bankid_attempt_state == 'idle' %>>
|
|
11
|
+
<p id="bankid-scan"><%= rodauth.bankid_scan_text %></p>
|
|
12
|
+
<div id="bankid-qr"><%== rodauth.bankid_current_qr_svg %></div>
|
|
13
|
+
<p id="bankid-hint"><%= rodauth.bankid_hint_outstanding_transaction %></p>
|
|
14
|
+
<p><a id="bankid-open" href="<%= rodauth.bankid_current_attempt ? rodauth.bankid_autostart_url : '#' %>"><%= rodauth.bankid_open_app_button %></a></p>
|
|
15
|
+
|
|
16
|
+
<form id="bankid-continue" method="post" action="<%= rodauth.bankid_login_path %>">
|
|
17
|
+
<%== rodauth.csrf_tag(rodauth.bankid_login_path) %>
|
|
18
|
+
<button type="submit"><%= rodauth.bankid_continue_button %></button>
|
|
19
|
+
</form>
|
|
20
|
+
|
|
21
|
+
<form id="bankid-cancel" method="post" action="<%= rodauth.bankid_cancel_path %>">
|
|
22
|
+
<%== rodauth.csrf_tag(rodauth.bankid_cancel_path) %>
|
|
23
|
+
<button type="submit"><%= rodauth.bankid_cancel_button %></button>
|
|
24
|
+
</form>
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
<p id="bankid-error" hidden></p>
|
|
28
|
+
<button id="bankid-restart" type="button" hidden><%= rodauth.bankid_restart_button %></button>
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
<%== rodauth.render('bankid-script') %>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<script src="<%= rodauth.bankid_script_path %>?v=<%= Rodauth::BankID::VERSION %>" defer></script>
|