Creating a BankID Case Plugin
The 2.x release of the BankID module introduces a Case Plugin system (BankIDCase). This architecture allows developers to create custom authentication and signing workflows (e.g., login, signing documents, approving financial transactions, or verifying identity for custom forms).
Architecture Overview
A custom BankID workflow consists of three main parts:
| [Trigger (Link / Button)] ──> [BankID Dialog & QR Code] ──> [BankID Case Plugin] ──> [Outcome (JS Event & PHP TempStore)]
|
- The Plugin: Defines order parameters (visible text, requirements) and processes completion/failure/cancellation responses.
- The Trigger: A renderable button or link generated via
BankIDManager using a CaseData value object.
- The Outcome: Emits a
bankid custom DOM event for clientside handling and stores temporary data in Drupal's PrivateTempStore for backend processing.
1. Defining the Case Plugin
Create a new plugin class in your custom module under src/Plugin/BankIDCase/. Annotate it with the #[BankIDCase] PHP attribute and extend BankIDCasePluginBase.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 | <?php
declare(strict_types=1);
namespace Drupal\my_module\Plugin\BankIDCase;
use Drupal\bankid\Attribute\BankIDCase;
use Drupal\bankid\BankIDCasePluginBase;
use Drupal\bankid\Enum\OrderOperation;
use Drupal\bankid\ValueObjects\BankIDOrderDoneData;
use Drupal\bankid\ValueObjects\BankIDRequestData;
use Drupal\bankid\ValueObjects\BankIDRequestRequirements;
use Drupal\bankid\ValueObjects\BankIDResponse;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Provides a custom BankID workflow.
*/
#[BankIDCase(
id: 'my_custom_case',
title: new TranslatableMarkup('My Custom Workflow'),
description: new TranslatableMarkup('Custom workflow for authenticating or signing.'),
)]
class MyCustomCase extends BankIDCasePluginBase {
/**
* {@inheritdoc}
*/
public function getBankIdRequestData(): BankIDRequestData {
return new BankIDRequestData(
userVisibleData: 'Please confirm this action with Mobile BankID.',
userNonVisibleData: '',
requirements: new BankIDRequestRequirements(),
userVisibleDataFormat: 'simpleMarkdownV1',
);
}
/**
* {@inheritdoc}
*/
public function orderCompleted(BankIDResponse $response): BankIDOrderDoneData {
$completionData = $response->getCompletionData() ?? [];
$user = $completionData['user'] ?? [];
$personalNumber = $user['personalNumber'] ?? '';
// Data passed to the JS event (clientData) and stored in PrivateTempStore (orderDoneTempData).
return new BankIDOrderDoneData(
clientData: [
'status' => 'success',
'name' => $user['name'] ?? '',
],
orderRef: $response->getOrderRef() ?? '',
userId: NULL,
orderDoneTempData: [
'personalNumber' => $personalNumber,
'signedAt' => time(),
],
);
}
/**
* {@inheritdoc}
*/
public function orderFailed(BankIDResponse $response): BankIDOrderDoneData {
return new BankIDOrderDoneData([], $response->getOrderRef() ?? '', NULL, []);
}
/**
* {@inheritdoc}
*/
public function orderCancelled(BankIDResponse $response): BankIDOrderDoneData {
return new BankIDOrderDoneData([], $response->getOrderRef() ?? '', NULL, []);
}
}
|
2. Triggering the Flow
To display the BankID modal dialog, create a BankIDCaseData object and use BankIDManager to generate a trigger link or Ajax button.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | use Drupal\bankid\Enum\OrderOperation;
use Drupal\bankid\ValueObjects\BankIDCaseData;
// 1. Prepare BankIDCaseData
$case = new BankIDCaseData(
operation: OrderOperation::Auth, // Or OrderOperation::Sign
pluginId: 'my_custom_case',
pluginConfiguration: ['custom_setting' => 'example'],
private: ['internal_reference' => 12345], // Stored privately in session
);
// 2. Generate a Trigger using BankIDManager
/** @var \Drupal\bankid\BankIDManager $bankIdManager */
$bankIdManager = \Drupal::service('bankid.manager');
// Option A: Renderable Ajax button (recommended for forms)
$form['bankid_button'] = $bankIdManager->buildDialogAjaxButton($case);
// Option B: Renderable link (for stand-alone links or menus)
$build['bankid_link'] = $bankIdManager->buildDialogLink($case);
|
3. Handling the Outcome
A. Clientside (JavaScript Event)
When an order reaches a terminal state (complete, failed, cancel), the module dispatches a bankid event on the document:
1
2
3
4
5
6
7
8
9
10
11
12 | document.addEventListener('bankid', (event) => {
const { type, orderRef, data } = event.detail;
if (type === 'complete') {
console.log('Order completed for ref:', orderRef);
console.log('Client data from plugin:', data);
// Submit an underlying form or redirect user
}
else if (type === 'failed') {
console.warn('Order failed.');
}
});
|
B. Serverside (PHP Temporary Store)
In a form submission handler or controller, you can retrieve the data that the plugin persisted in orderDoneTempData:
| $orderRef = $form_state->getValue('order_ref');
/** @var \Drupal\bankid\BankIDManager $bankIdManager */
$bankIdManager = \Drupal::service('bankid.manager');
$tempData = $bankIdManager->getOrderDoneTempData($orderRef);
if ($tempData && isset($tempData['personalNumber'])) {
$ssn = $tempData['personalNumber'];
// Complete business logic...
}
|
Reference Implementation
For a complete reference implementation demonstrating dependency injection and form integration, review:
- Case Plugin:
modules/bankid_login/src/Plugin/BankIDCase/BankIDLoginCase.php
- Trigger Form:
modules/bankid_login/src/Form/BankIDLoginForm.php
- Auto-Submit JS:
js/bankid-submit-on-order-done.js