Guest User

Untitled

a guest
Mar 21st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Illuminate\Database\Eloquent\Model;
  6.  
  7. class PipedriveToken extends Model
  8. {
  9. protected $table = 'pipedrive_tokens';
  10.  
  11. protected $fillable = [
  12. 'user_id', 'body', 'salt'
  13. ];
  14.  
  15. public function user()
  16. {
  17. return $this->belongsTo(User::class);
  18. }
  19.  
  20. public function body()
  21. {
  22. return json_decode($this->decrypt_token());
  23. }
  24.  
  25. public function store($token)
  26. {
  27. $salt = $this->token_salt();
  28.  
  29. $this->body = $this->encrypt_token($token, $salt);
  30. $this->salt = $salt;
  31.  
  32. $this->save();
  33. }
  34.  
  35. protected function token_salt()
  36. {
  37. return password_hash(env('TOKEN_PASSWORD'), PASSWORD_BCRYPT, ['cost' => 12]);
  38. }
  39.  
  40. protected function decrypt_token()
  41. {
  42. $key = env('TOKEN_PASSWORD') . $this->salt;
  43. $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
  44.  
  45. return openssl_decrypt(base64_decode($this->body), 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
  46. }
  47.  
  48. protected function encrypt_token($token, $salt)
  49. {
  50. $body = json_encode($token);
  51.  
  52. $password = env('TOKEN_PASSWORD');
  53.  
  54. $key = $password . $salt;
  55. echo "Key:" . $key . "\n";
  56.  
  57. $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
  58.  
  59. return base64_encode(openssl_encrypt($body, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv));
  60. }
  61.  
  62. }
Add Comment
Please, Sign In to add comment