Skip to content

Commit 6cf5a4b

Browse files
authored
Merge pull request #2 from sander3/encryption-feature
Encryption feature
2 parents a0b9d01 + 05239fa commit 6cf5a4b

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

readme.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GDPR compliant data portability with ease
22

3-
This package helps you to be compliant with the GDPR article 20.
3+
This package helps you to be compliant with the GDPR (article 20).
44

55
## Requirements
66

@@ -57,7 +57,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
5757
class User extends Authenticatable
5858
{
5959
use Portable, Notifiable;
60-
60+
6161
/**
6262
* Get the GDPR compliant data portability array for the model.
6363
*
@@ -66,9 +66,9 @@ class User extends Authenticatable
6666
public function toPortableArray()
6767
{
6868
$array = $this->toArray();
69-
69+
7070
// Customize array...
71-
71+
7272
return $array;
7373
}
7474
}
@@ -91,7 +91,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
9191
class User extends Authenticatable
9292
{
9393
use Portable, Notifiable;
94-
94+
9595
/**
9696
* The relations to include in the downloadable data.
9797
*
@@ -118,7 +118,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
118118
class User extends Authenticatable
119119
{
120120
use Portable, Notifiable;
121-
121+
122122
/**
123123
* The attributes that should be hidden for the downloadable data.
124124
*
@@ -143,7 +143,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
143143
class User extends Authenticatable
144144
{
145145
use Portable, Notifiable;
146-
146+
147147
/**
148148
* The attributes that should be visible in the downloadable data.
149149
*
@@ -158,6 +158,36 @@ class User extends Authenticatable
158158

159159
This package exposes an endpoint at `/gdpr/download`. Only authenticated users should be able to access the routes. Your application should make a POST call, containing the currently authenticated user's password, to this endpoint. The re-authentication is needed to prevent information leakage.
160160

161+
### Encryption
162+
163+
> Before using encryption, you must set a `key` option in your `config/app.php` configuration file. If this value is not properly set, all encrypted values will be insecure.
164+
165+
You may encrypt/decrypt attributes on the fly using the `Soved\Laravel\Gdpr\EncryptsAttributes` trait on any model. The trait expects the `$encrypted` property to be filled with attribute keys:
166+
167+
```php
168+
<?php
169+
170+
namespace App;
171+
172+
use Soved\Laravel\Gdpr\Portable;
173+
use Illuminate\Notifications\Notifiable;
174+
use Soved\Laravel\Gdpr\EncryptsAttributes;
175+
use Illuminate\Foundation\Auth\User as Authenticatable;
176+
177+
class User extends Authenticatable
178+
{
179+
use EncryptsAttributes, Portable, Notifiable;
180+
181+
/**
182+
* The attributes that should be encrypted and decrypted on the fly.
183+
*
184+
* @var array
185+
*/
186+
protected $encrypted = ['ssnumber'];
187+
}
188+
189+
```
190+
161191
## Security Vulnerabilities
162192

163193
If you discover a security vulnerability within this project, please send an e-mail to Sander de Vos via [sander@tutanota.de](mailto:sander@tutanota.de). All security vulnerabilities will be promptly addressed.

src/EncryptsAttributes.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Soved\Laravel\Gdpr;
4+
5+
trait EncryptsAttributes
6+
{
7+
/**
8+
* Get a plain attribute (not a relationship).
9+
*
10+
* @param string $key
11+
* @return mixed
12+
*/
13+
public function getAttributeValue($key)
14+
{
15+
$value = parent::getAttributeValue($key);
16+
17+
if (in_array($key, $this->encrypted) &&
18+
!is_null($value)) {
19+
return decrypt($value);
20+
}
21+
22+
return $value;
23+
}
24+
25+
/**
26+
* Set a given attribute on the model.
27+
*
28+
* @param string $key
29+
* @param mixed $value
30+
* @return $this
31+
*/
32+
public function setAttribute(
33+
$key,
34+
$value
35+
) {
36+
if (in_array($key, $this->encrypted) &&
37+
!is_null($value)) {
38+
$value = encrypt($value);
39+
}
40+
41+
parent::setAttribute($key, $value);
42+
}
43+
}

0 commit comments

Comments
 (0)