Skip to content

Commit 7119503

Browse files
committed
Added Key support for Authorization header.
1 parent a161afc commit 7119503

File tree

5 files changed

+126
-6
lines changed

5 files changed

+126
-6
lines changed

codecov.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
codecov:
2+
require_ci_to_pass: yes
3+
4+
coverage:
5+
status:
6+
project:
7+
default:
8+
target: auto
9+
threshold: 0
10+
if_ci_failed: error
11+
patch:
12+
default:
13+
target: auto
14+
threshold: 0
15+
if_ci_failed: error
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: Webform OpenFisca - Key Authentication
2+
type: module
3+
description: Add authentication with Key module to Webform OpenFisca.
4+
package: Webform
5+
core_version_requirement: ^10 || ^11
6+
dependencies:
7+
- webform_openfisca:webform_openfisca
8+
- key:key
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Webform OpenFisca Key Authentication.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Drupal\Core\Entity\EntityFormInterface;
11+
use Drupal\Core\Form\FormStateInterface;
12+
use Drupal\key\KeyInterface;
13+
use Drupal\webform\Entity\Webform;
14+
use Drupal\webform\WebformInterface;
15+
use Drupal\webform_openfisca\WebformOpenFiscaSettings;
16+
17+
/**
18+
* Implements hook_config_schema_info_alter().
19+
*/
20+
function webform_openfisca_key_auth_config_schema_info_alter(array &$definitions) : void {
21+
$definitions['webform.settings.third_party.webform_openfisca']['mapping']['fisca_api_authorization_header_token_key'] = [
22+
'type' => 'string',
23+
'label' => 'Fisca API Authorization Header - Token Key',
24+
];
25+
}
26+
27+
/**
28+
* Implements hook_webform_third_party_settings_form_alter().
29+
*/
30+
function webform_openfisca_key_auth_webform_third_party_settings_form_alter(array &$form, FormStateInterface $form_state) : void {
31+
$form_object = $form_state->getFormObject();
32+
if (!$form_object instanceof EntityFormInterface) {
33+
return;
34+
}
35+
$webform = $form_object->getEntity();
36+
if (!$webform instanceof WebformInterface) {
37+
return;
38+
}
39+
40+
$token_key = (string) $webform->getThirdPartySetting('webform_openfisca', 'fisca_api_authorization_header_token_key', '');
41+
$form['third_party_settings']['webform_openfisca']['fisca_api_authorization_header_token_key'] = [
42+
'#type' => 'key_select',
43+
'#key_filters' => ['type' => 'authentication'],
44+
'#title' => t('Authorization token key'),
45+
'#description' => t('Specify the key storing the token for the Authorization header above. The token will be appended to the Authorization header when making API calls to OpenFisca.'),
46+
'#default_value' => $token_key,
47+
'#weight' => -59,
48+
];
49+
}
50+
51+
/**
52+
* Implements hook_webform_openfisca_client_options_alter().
53+
*/
54+
function webform_openfisca_key_auth_webform_openfisca_client_options_alter(array &$options, array &$context, array &$webform_openfisca_context) : void {
55+
/** @var \Drupal\webform_openfisca\WebformOpenFiscaSettings $webform_openfisca_settings */
56+
$webform_openfisca_settings = $webform_openfisca_context['webform_openfisca_settings'] ?? NULL;
57+
if (!$webform_openfisca_settings instanceof WebformOpenFiscaSettings) {
58+
return;
59+
}
60+
61+
$webform = Webform::load($webform_openfisca_settings->getWebformId());
62+
if (!$webform instanceof WebformInterface) {
63+
return;
64+
}
65+
66+
$token_key = (string) $webform->getThirdPartySetting('webform_openfisca', 'fisca_api_authorization_header_token_key', '');
67+
if (!$token_key) {
68+
return;
69+
}
70+
71+
/** @var \Drupal\key\KeyRepositoryInterface $key_repository */
72+
$key_repository = \Drupal::service('key.repository');
73+
$key = $key_repository->getKey($token_key);
74+
if (!$key instanceof KeyInterface) {
75+
return;
76+
}
77+
78+
$token = $key->getKeyValue();
79+
if (!empty($token)) {
80+
$auth_header = $options['headers']['Authorization'] ?? '';
81+
$auth_header = trim($auth_header) . ' ' . $token;
82+
$options['headers']['Authorization'] = $auth_header;
83+
}
84+
}

src/WebformThirdPartySettingsFormAlter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,36 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
3636
'#title' => $this->t('Enable OpenFisca RaC integration'),
3737
'#description' => '',
3838
'#default_value' => $openfisca_settings->isEnabled(),
39+
'#weight' => -100,
3940
];
4041
$form['third_party_settings']['webform_openfisca']['fisca_debug_mode'] = [
4142
'#type' => 'checkbox',
4243
'#title' => $this->t('Enable debug mode'),
4344
'#description' => '',
4445
'#default_value' => $openfisca_settings->isDebugEnabled(),
46+
'#weight' => -90,
4547
];
4648
$form['third_party_settings']['webform_openfisca']['fisca_logging_mode'] = [
4749
'#type' => 'checkbox',
4850
'#title' => $this->t('Log OpenFisca calculation'),
4951
'#description' => '',
5052
'#default_value' => $openfisca_settings->isLoggingEnabled(),
53+
'#weight' => -80,
5154
];
5255
$form['third_party_settings']['webform_openfisca']['fisca_api_endpoint'] = [
5356
'#type' => 'textfield',
5457
'#title' => $this->t('OpenFisca API endpoint'),
5558
'#description' => $this->t('Specify the API endpoint to the Fisca Rule'),
5659
'#default_value' => $openfisca_settings->getApiEndpoint(),
60+
'#weight' => -70,
5761
];
5862
$form['third_party_settings']['webform_openfisca']['fisca_api_authorization_header'] = [
5963
'#type' => 'textfield',
6064
'#title' => $this->t('Authorization header to connect to OpenFisca API'),
6165
'#description' => $this->t('Specify the Authorization header to connect to a private OpenFisca API, e.g. a Basic auth or a Bearer token.'),
6266
'#default_value' => $openfisca_settings->getApiAuthorizationHeader(),
6367
'#field_prefix' => $this->t('Authorization:'),
68+
'#weight' => -60,
6469
];
6570
$form['third_party_settings']['webform_openfisca']['fisca_return_key'] = [
6671
'#type' => 'webform_codemirror',
@@ -69,6 +74,7 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
6974
'#title' => $this->t('The keys for the return value'),
7075
'#description' => $this->t('Specify the keys for the return value that needs to be checked. Comma separated.'),
7176
'#default_value' => $openfisca_settings->getPlainReturnKeys(),
77+
'#weight' => -50,
7278
];
7379
$form['third_party_settings']['webform_openfisca']['fisca_field_mappings'] = [
7480
'#type' => 'webform_codemirror',
@@ -81,6 +87,7 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
8187
'#title' => $this->t('OpenFisca Field mappings'),
8288
'#description' => $this->t('Specify the field mappings'),
8389
'#default_value' => $openfisca_settings->getJsonFieldMappings(),
90+
'#weight' => -40,
8491
];
8592
$form['third_party_settings']['webform_openfisca']['fisca_variables'] = [
8693
'#type' => 'webform_codemirror',
@@ -93,6 +100,7 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
93100
'#title' => $this->t('OpenFisca Variables'),
94101
'#description' => $this->t('Specify the variables from OpenFisca API'),
95102
'#default_value' => $openfisca_settings->getJsonVariables(),
103+
'#weight' => -30,
96104
];
97105
$form['third_party_settings']['webform_openfisca']['fisca_parameter_tokens'] = [
98106
'#type' => 'webform_codemirror',
@@ -101,6 +109,7 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
101109
'#title' => $this->t('OpenFisca parameter tokens'),
102110
'#description' => $this->t('Specify the OpenFisca parameters used as tokens. Comma separated.'),
103111
'#default_value' => $openfisca_settings->getPlainParameterTokens(),
112+
'#weight' => -20,
104113
];
105114
$form['third_party_settings']['webform_openfisca']['fisca_entity_roles'] = [
106115
'#type' => 'webform_codemirror',
@@ -113,6 +122,7 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
113122
'#title' => $this->t('OpenFisca Field Entity Roles'),
114123
'#description' => $this->t('Specify the field entity roles for OpenFisca API'),
115124
'#default_value' => $openfisca_settings->getJsonEntityRoles(),
125+
'#weight' => -10,
116126
];
117127
$form['third_party_settings']['webform_openfisca']['fisca_immediate_response_mapping'] = [
118128
'#type' => 'webform_codemirror',
@@ -125,6 +135,7 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
125135
'#title' => $this->t('OpenFisca immediate response mapping'),
126136
'#description' => $this->t('Specify the field immediate response mapping'),
127137
'#default_value' => $openfisca_settings->getJsonImmediateResponseMapping(),
138+
'#weight' => 0,
128139
];
129140
$form['third_party_settings']['webform_openfisca']['fisca_immediate_exit_mapping'] = [
130141
'#type' => 'webform_codemirror',
@@ -133,12 +144,14 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
133144
'#title' => $this->t('OpenFisca immediate exit mapping'),
134145
'#description' => $this->t('Specify the return keys of OpenFisca response to map to immediate exit. Comma separated.'),
135146
'#default_value' => $openfisca_settings->getPlainImmediateExitKeys(),
147+
'#weight' => 10,
136148
];
137149
$form['third_party_settings']['webform_openfisca']['fisca_immediate_response_ajax_indicator'] = [
138150
'#type' => 'checkbox',
139151
'#title' => $this->t('Display Ajax indicator'),
140152
'#description' => $this->t('Whether to display an Ajax indicator when an immediate response is required.'),
141153
'#default_value' => $openfisca_settings->hasImmediateResponseAjaxIndicator(),
154+
'#weight' => 20,
142155
];
143156
$form['#validate'][] = [$this, 'validateForm'];
144157
}

webform_openfisca.info.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
name: 'Webform OpenFisca'
1+
name: Webform OpenFisca
22
type: module
3-
description: 'Adds OpenFisca support for RaC enabled webforms.'
3+
description: Adds OpenFisca support for RaC enabled webforms.
44
package: Webform
55
core_version_requirement: ^10 || ^11
66
configure: webform_openfisca.settings
77
dependencies:
8-
- 'webform:webform'
9-
- 'webform_ui:webform_ui'
10-
- 'paragraphs:paragraphs'
11-
- 'token:token'
8+
- webform:webform
9+
- webform_ui:webform_ui
10+
- paragraphs:paragraphs
11+
- token:token

0 commit comments

Comments
 (0)