Skip to content

Removing env(), typo & tests updated #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Laravel

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
laravel-tests:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Homestead.json

# Rocketeer PHP task runner and deployment package. https://github.com/rocketeers/rocketeer
.rocketeer/
.phpunit.result.cache
63 changes: 21 additions & 42 deletions src/Models/Traits/HasEncryptedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ trait HasEncryptedAttributes
*/
public $hashAlg = 'sha256';


/**
* @var array
*/
Expand All @@ -68,8 +67,8 @@ trait HasEncryptedAttributes
* @param $value
* @return string
*/
private function setEncryptedHashedBIAttributes($key, $value){

private function setEncryptedHashedBIAttributes($key, $value)
{
if (array_key_exists($key, $this->encrypted)) {

$this->checkTypes($key, $value);
Expand All @@ -80,34 +79,32 @@ private function setEncryptedHashedBIAttributes($key, $value){
return $this->setHashed($key, $value);
}


/**
* @param $value
* @return string
*/
private function setEncrypted($value){
private function setEncrypted($value)
{
if (!is_null($value)) {
$value = encrypt((string)$value);
}
return $value;
}


/**
* @param $key
* @param $value
* @return string
*/
private function setHashed($key, $value){

private function setHashed($key, $value)
{
if (in_array($key, $this->hashed??[]) && !is_null($value) && '' != $value) {
$value = $this->getHash($value);
}

return $value;
}


/**
* @param $key
* @param $originalValue
Expand All @@ -118,17 +115,14 @@ private function setBlindIndex($key, $originalValue)

if (is_null($originalValue)) {
$this->{$this->encrypted[$key]['hasBlindIndex']} = null;

} else if ('' === $originalValue) {
$this->{$this->encrypted[$key]['hasBlindIndex']} = '';

} else if ('' === $originalValue) { // TODO: add config('ENCRYPT_EMPTY_STRING_BI')
$this->{$this->encrypted[$key]['hasBlindIndex']} = '';
} else {
$this->{$this->encrypted[$key]['hasBlindIndex']} = $this->getHash($originalValue);
}
}
}


/**
* @param $key
* @param $value
Expand All @@ -137,7 +131,6 @@ private function setBlindIndex($key, $originalValue)
private function getDecryptIfEncrypted($key, $value)
{
if (array_key_exists($key, $this->encrypted) && !empty($value)) {

$decryptionType = array_key_exists('type', $this->encrypted[$key]) ? $this->encrypted[$key]['type'] : 'string';

if ($decryptionType == 'date') {
Expand All @@ -151,7 +144,6 @@ private function getDecryptIfEncrypted($key, $value)
return $value;
}


/**
* @param $value
* @param $type
Expand All @@ -167,7 +159,6 @@ private function castPrimitive($value, $type)
return (string)$value;
}


/**
* @param $value
* @param null $format
Expand All @@ -182,17 +173,15 @@ private function castDate($value, $format = null)
}
}


/**
* @param $originalValue
* @return string
*/
private function getHash($originalValue)
{
return hash_hmac($this->hashAlg, (string)$originalValue, env('APP_KEY'));
return hash_hmac($this->hashAlg, (string)$originalValue, config('app.key'));
}


/**
* @param $key
* @param $value
Expand All @@ -206,7 +195,7 @@ private function checkTypes($key, $value)
$type = array_key_exists('type', $this->encrypted[$key]) ? $this->encrypted[$key]['type'] : 'string';
$format = array_key_exists('dateFormat', $this->encrypted[$key]) ? $this->encrypted[$key]['dateFormat'] : null;

switch($type) {
switch ($type) {
case 'date':
$this->castDate($value, $format);
break;
Expand All @@ -217,7 +206,6 @@ private function checkTypes($key, $value)
if(!in_array($type, $this->primitiveTypes)) throw new TypeError("Encryption error, $type not a supported type of 'integer', 'boolean', 'float', 'string', 'date'");
$this->checkType($key, $value, [$type]);
}

}

return true;
Expand All @@ -229,25 +217,24 @@ private function checkTypes($key, $value)
* @param array $types
* @return bool
*/
private function checkType($key, $value, array $types){

private function checkType($key, $value, array $types)
{
$hasMatchedType = false;

foreach($types as $type){
if (gettype($value) == $type){
foreach ($types as $type) {
if (gettype($value) == $type) {
$hasMatchedType = true;
break;
}
}

if (!$hasMatchedType){
if (!$hasMatchedType) {
throw new TypeError("Encryption error, $key not of correct type or null. Type is: " . gettype($value));
}

return true;
}


/**
* @param $query
* @param array $blindIndexQuery
Expand All @@ -271,7 +258,6 @@ public function scopeWhereBI($query, array $blindIndexQuery)
throw new \Exception("Blind index column for " . key($blindIndexQuery) . " not found");
}


/**
* @param $query
* @param array $blindIndexQuery
Expand All @@ -281,7 +267,6 @@ public function scopeWhereBI($query, array $blindIndexQuery)
public function scopeOrWhereBI($query, array $blindIndexQuery)
{
if (array_key_exists(key($blindIndexQuery), $this->encrypted) && array_key_exists('hasBlindIndex', $this->encrypted[key($blindIndexQuery)])) {

$columnToSearch = $this->encrypted[key($blindIndexQuery)]['hasBlindIndex'];
$unHashedValueToSearch = $blindIndexQuery[key($blindIndexQuery)];

Expand All @@ -295,22 +280,19 @@ public function scopeOrWhereBI($query, array $blindIndexQuery)
throw new \Exception("Blind index column for " . key($blindIndexQuery) . " not found");
}


//
//Eloquent Model method overrides below
//


/**
* @param $key
* @return mixed
*/
protected function getAttributeFromArray($key){
protected function getAttributeFromArray($key)
{
return $this->getDecryptIfEncrypted($key, parent::getAttributeFromArray($key));
}



/**
* @return array
*/
Expand All @@ -325,27 +307,24 @@ protected function getArrayableAttributes()
return $array;
}



/**
* @param $key
* @param $value
*/
public function setAttribute($key, $value)
{
if($value !== $this->$key){
if ($value !== $this->$key) {
parent::setAttribute($key, $this->setEncryptedHashedBIAttributes($key, $value));
}
}


/**
* List all encripted columns so we can do some magic after
* List all encrypted columns so we can do some magic after
* @return array
*/
public function encriptedColumns()
public function encryptedColumns()
{
return (!empty($this->encrypted))? $this->encrypted : [];
return (!empty($this->encrypted))? $this->encrypted : [];
}

}
Loading