-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from itk-dev/feature/encryption-tweaks
1062: Encryption tweaks
- Loading branch information
Showing
6 changed files
with
395 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.