Code Monkey home page Code Monkey logo

laravel-lumen-mysql-encryption's Introduction

Latest Stable Version Build Status Total Downloads Scrutinizer Code Quality License

Laravel Mysql Encryption

Database fields encryption in laravel and lumen for mysql databases with native search and anonymize data.

Instalation notes

$ composer require ionghitun/laravel-lumen-mysql-encryption

Dependencies

  • php >= 8.0

Documentation:

Service provider is automatically loaded for laravel, for lumen you need to add

$app->register(IonGhitun\MysqlEncryption\MysqlEncryptionServiceProvider::class);

to your bootstrap/app.php file.

You need to add ENCRYPTION_KEY to your .env file, has to be 16 chars long.

Each of your Models should extend IonGhitun\MysqlEncryption\Models\BaseModel and for the fields you want to be encrypted you have to do the following:

  • in migrations set field to binary

  • add $encrypted to your model:

      /** @var array */
      protected $encrypted = [
          'name',
          'email'
      ];
    

You can use Validator on these fields with:

  • unique_encrypted

      unique_encrypted:<table>,<field(optional)>
    
  • exists_encrypted

      exists_encrypted:<table>,<field(optional)>
    

You cannot use basic where, orWhere, orderBy on encrypted fields so there are 5 predefined scopes that you can use as a replacer:

  - whereEncrypted
  - whereNotEncrypted
  - orWhereEncrypted
  - orWhereNotEncrypted
  - orderByEncrypted

Possibility to anonymize data:

Example:

    //without extra parameters needed for randomDigit
    protected $anonymizable = [
        'age' => ['randomDigit']
    ];
    
    //with extra parameters needed for numberBetween
    protected $anonymizable = [
        'age' => ['numberBetween', '18','50']
    ];
  • get your model instance and use anonymize method: $user->anonymize();

The method accepts a locale parameter, if you want to use faker with localization, the default locale can be set in .env file: FAKER_LOCALE = 'en_US'

If is not specified by any method above, the default Faker local will be used by default

Note: Model is not automatically saved!

Happy coding!

In order to revert data encrypted with this package you need to remove columns from the $encrypted array, do a foreach through your models and save the $this->aesDecrypt($value) for all the columns that were in $encrypted array, then change the column from binary to something more suitable.

After this you can remove the package and extend the basic laravel model, and so cleanup, remove anything related to this package!

laravel-lumen-mysql-encryption's People

Contributors

ionghitun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-lumen-mysql-encryption's Issues

Unique ENC KEY for every tenant db

I was wondering as in this package we are setting an encrytion key in .env but lets just take security measures into consideration , as i am have multi tenancy and i want to set unique enc key for each tenant database ? that can be done ? just need some guidance.
Thanks

Scopes support with Query Builder ?

Hello, first of all thanks for this wonderful package. just stuck at a point, is there any way we could use these predefined scopes with query builder ? its working with ORM but not with query builder . Thanks.

"message": "Malformed UTF-8 characters, possibly incorrectly encoded",

Hello, im facing this issue, fetching data with laravel relationship and when returning response i got this error. Kindly guide me.

//controller
$group = ContactGroup::with(['contacts' => function ($query) use ($select_fields) {
$query->select($select_fields);
}])->findOrFail($id);

    return $this->successResponse(
        ['model' => $this->model],
        __('contacts.contact.success_fetched'),
        ["contacts_group" =>$group]
    );
}

"response consist : data->contact_group->contacts "

Contacts is relation.
Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.