Skip to content

Commit

Permalink
Merge pull request #114 from itk-dev/feature/encryption-tweaks
Browse files Browse the repository at this point in the history
1062: Encryption tweaks
  • Loading branch information
jekuaitk authored Jun 26, 2024
2 parents e07f404 + a6613f0 commit fabd526
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ before starting to add changes. Use example [placed in the end of the page](#exa

## [Unreleased]

- [#114](https://github.com/OS2Forms/os2forms/pull/114)
Encrypted computed elements.

## [3.15.3] 2024-06-25

- [OS-74] Replacing DAWA matrikula select with Datafordeler select
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
"2733781 - Add Export to Word Support": "https://www.drupal.org/files/issues/2019-11-22/2733781-47.patch"
},
"drupal/webform": {
"Unlock possibility of using Entity print module export to Word": "https://www.drupal.org/files/issues/2020-02-29/3096552-6.patch"
"Unlock possibility of using Entity print module export to Word": "https://www.drupal.org/files/issues/2020-02-29/3096552-6.patch",
"Webform computed element post save alter": "https://www.drupal.org/files/issues/2024-06-25/webform_computed_post_save_field_alter.patch"
},
"drupal/user_default_page": {
"Warning: in_array() expects parameter 2 to be array, null given in user_default_page_user_logout() (https://www.drupal.org/node/3246986)": "https://www.drupal.org/files/issues/2021-11-01/user_default_page-3246986-2.patch"
Expand Down
12 changes: 12 additions & 0 deletions modules/os2forms_encrypt/os2forms_encrypt.module
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ function os2forms_encrypt_entity_type_alter(array &$entity_types): void {
$entity_types['webform_submission']->setStorageClass('Drupal\os2forms_encrypt\WebformOs2FormsEncryptSubmissionStorage');
}
}

/**
* Implements hook_webform_computed_post_save_field_alter().
*
* Ensure encryption of computed element values.
*/
function os2forms_encrypt_webform_computed_post_save_field_alter(array &$fields): void {
/** @var \Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor $os2formsEncryptor */
$os2formsEncryptor = \Drupal::service('os2forms_encrypt.encryptor');

$fields['value'] = $os2formsEncryptor->encryptValue($fields['value'], $fields['name'], $fields['webform_id']);
}
4 changes: 4 additions & 0 deletions modules/os2forms_encrypt/os2forms_encrypt.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
os2forms_encrypt.encryptor:
class: Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor
arguments: ['@encryption', '@entity_type.manager']
65 changes: 65 additions & 0 deletions modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Drupal\os2forms_encrypt\Helper;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\encrypt\EncryptServiceInterface;
use Drupal\encrypt\Entity\EncryptionProfile;

/**
* The Os2FormsEncryptor class.
*/
class Os2FormsEncryptor {

/**
* The encryption Service.
*
* @var \Drupal\encrypt\EncryptServiceInterface
*/
private EncryptServiceInterface $encryptionService;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private EntityTypeManagerInterface $entityTypeManager;

public function __construct(EncryptServiceInterface $encryptService, EntityTypeManagerInterface $entityTypeManager) {
$this->encryptionService = $encryptService;
$this->entityTypeManager = $entityTypeManager;
}

/**
* Encrypts value if element is configured to be encrypted.
*
* @param string $value
* The value that should be encrypted.
* @param string $element
* The element.
* @param string $webformId
* The webform id.
*
* @return string
* The resulting value.
*/
public function encryptValue(string $value, string $element, string $webformId): string {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this->entityTypeManager->getStorage('webform')->load($webformId);

$config = $webform->getThirdPartySetting('webform_encrypt', 'element');
$encryption_profile = isset($config[$element]) ? EncryptionProfile::load($config[$element]['encrypt_profile']) : FALSE;

if (!$encryption_profile) {
return $value;
}

$encrypted_data = [
'data' => base64_encode($this->encryptionService->encrypt($value, $encryption_profile)),
'encrypt_profile' => $encryption_profile->id(),
];

return serialize($encrypted_data);
}

}
Loading

0 comments on commit fabd526

Please sign in to comment.