ATM, accessing / showing the unencrypted password is not possible for non-developers. I've implemented a second field formatter that displays the unencrypted password for users with the "View unencrypted passwords" permission. This allows passwords to be shown for privileged users.

Comments

Duvan.Slabbert created an issue. See original summary.

Duvan.Slabbert’s picture

Priority: Normal » Critical
gaurav.kapoor’s picture

@Duvan.Slabbert Possible to share a snippet/patch for the field formatted which you have implemented? Thanks.

Duvan.Slabbert’s picture

I'm not a developer at all, but what i have so far is the following.

I created the file password_field.permissions.yml inside the module folder with the following code

view unencrypted passwords:
  title: 'View unencrypted passwords'
  description: 'Allow access to unencrypted password field formatter.'
  restrict access: TRUE

I have changed the PasswordFormatter.php file like so.

<?php

namespace Drupal\password_field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'Password Field' formatter.
 *
 * @FieldFormatter(
 *   id = "PasswordFieldFormatter",
 *   label = @Translation("Password text"),
 *   field_types = {
 *     "Password"
 *   }
 * )
 */
class PasswordFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $element = [];
    foreach ($items as $delta => $item) {
      // Render each element as markup.
      $element[$delta] = ['#markup' => $item->value];
    }

    if (($element[0]['#markup'] != NULL) && \Drupal::currentUser()->hasPermission('view unencrypted passwords')) {
      $pw_out = password_field_encrypt_decrypt('decrypt', $element[0]['#markup']);
      $element[0]['#markup'] = '<span>' . $pw_out . '</span>';
    }

    elseif ($element[0]['#markup'] != NULL) {
      $element[0]['#markup'] = '<span>*******</span>';
    }
    return $element;
  }

}

But it does not decrypt the password and i have no idea why

Duvan.Slabbert’s picture

Ticket can be closed, I managed to resolve this issue

maharag’s picture

@Duvan.Slabbert Hi,

I have same issue and your approach should suite me, could your share please the solution for your script to work?

thank you

Duvan.Slabbert’s picture

@maharag

This was such a long time ago that I cant remember all the things I have changed, but I will post the code content of my files and maybe you can just match up with yours. Just note that when you use it, you can't use escape characters in your password, for some reason during the encrypt and decrypt it drops everything after the escape characters. Here is what I have.

password_field.permissions.yml

view unencrypted passwords:
  title: 'View unencrypted passwords'
  description: 'Allow access to unencrypted password field formatter.'
  restrict access: TRUE

PasswordFormatter.php

<?php

namespace Drupal\password_field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'Password Field' formatter.
 *
 * @FieldFormatter(
 *   id = "PasswordFieldFormatter",
 *   label = @Translation("Password text"),
 *   field_types = {
 *     "Password"
 *   }
 * )
 */
class PasswordFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $element = [];
    foreach ($items as $delta => $item) {
      // Render each element as markup.
      $element[$delta] = ['#markup' => $item->value];
    }

    if (($element[0]['#markup'] != NULL) && \Drupal::currentUser()->hasPermission('view unencrypted passwords')) {
      $pw_out = password_field_encrypt_decrypt('decrypt', $element[0]['#markup']);
      $element[0]['#markup'] = '<span>' . $pw_out . '</span>';
    }

    elseif ($element[0]['#markup'] != NULL) {
      $element[0]['#markup'] = '<span>*******</span>';
    }
    return $element;
  }

}

password_field.module
Just remember to replace the insert_secret_key_here with your key and insert_iv_here with your iv.

<?php

/**
 * @file
 * Contains password_field.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function password_field_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the password_field module.
    case 'help.page.password_field':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('module that saves password') . '</p>';
      return $output;

    default:
  }
}

/**
 * Helper function to encrypt-decryt data.
 */
function password_field_encrypt_decrypt($action, $string) {
  $output = FALSE;
  $encrypt_method = "AES-256-CBC";
  $secret_key = 'insert_secret_key_here';
  $secret_iv = 'insert_iv_here';
  // Hash.
  $key = hash('sha256', $secret_key);

  // Iv - encrypt method AES-256-CBC expects 16 bytes else you will get a warning.
  $iv = substr(hash('sha256', $secret_iv), 0, 16);
  if ($action == 'encrypt') {
    $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
    $output = base64_encode($output);
  }
  elseif ($action == 'decrypt') {
    $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    $output = openssl_decrypt(base64_decode($output), $encrypt_method, $key, 0, $iv);
  }
  return $output;
}

To be honest with you, I will highly recommend you to try and not use this module as there is no active development on this module. I have since moved on and started to use the Field Encryption module. You will need to use a couple of other modules with it and there is a great video you can check out as well. See the links bellow.

Field Encryption
Key
Encrypt
Real AES

Video to Guide you

maharag’s picture

@Duvan.Slabbert
great, i'll try that module. thank you!