coka/doctrine-secret-type

Custom Doctrine type that encrypted column value with openssl.

1.2.0 2020-05-05 19:49 UTC

This package is auto-updated.

Last update: 2021-05-05 22:11:53 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads composer.lock Travis CI

The coka/doctrine-secret-type package provides the ability to use custom Doctrine type that encrypted column value with openssl.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require coka/doctrine-secret-type

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

### Generate the SSH keys

$ mkdir -p config/cert
$ openssl genpkey -out config/cert/private.pem -aes256 -algorithm rsa -pkeyopt rsa_keygen_bits:4096
$ openssl pkey -in config/cert/private.pem -out config/cert/public.pem -pubout

Configuration

To configure Doctrine to use secret as a field type, you'll need to set up the following in your bootstrap:

<?php
// in bootstrapping code

// ...

use Doctrine\DBAL\Types\Type;

// ...

// Register my type
Type::addType('secret', 'Oka\Doctrine\Types\SecretType');

/** @var \Oka\Doctrine\Types\SecretType $secretType */
$secretType = Type::getType('secret');
$secretType->setPrivateKeyPath($privateKeyPath)
			->setPublicKeyPath($publicKeyPath)
			->setPassPhrase($passphrase);

The type has to be registered with the database platform as well:

<?php
$conn = $em->getConnection();
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('LONGTEXT', 'secret');

In Symfony 3 Version :

# app/config/config.yml
doctrine:
    dbal:
        types:
            secret: 'Oka\Doctrine\Types\SecretType'
        mapping_types:
            secret: text

In Symfony 4 Version :

# config/packages/oka_doctrine_secret_type.yaml
doctrine:
    dbal:
        types:
            secret: 'Oka\Doctrine\Types\SecretType'
        mapping_types:
            secret: LONGTEXT

Usage

Then, in your models, you may annotate properties by setting the @Column type to secret. Doctrine will handle the rest.

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="api_key")
 */
class Apikey
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var int int
     */
    protected $id;
    
    /**
     * @ORM\Column(type="secret")
     * @var string $value
     */
    protected $value;

    // Getters and setters...
}

If you use the XML Mapping instead of PHP annotations :

<field name="value" type="secret" />

You can also use the YAML Mapping :

    value:
      type: secret

Copyright and License

The coka/doctrine-secret-type library is copyright © Baidai Cedrick Oka https://github.com/CedrickOka and licensed for use under the MIT License (MIT). Please see LICENSE for more information.

source release license build downloads