|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package cellcote/laravel-proxify |
|
5
|
|
|
* @author Michele Andreoli <michi.andreoli[at]gmail.com> |
|
6
|
|
|
* @copyright Copyright (c) Michele Andreoli |
|
7
|
|
|
* @author Rik Schreurs <rik.schreurs[at]mail.com> |
|
8
|
|
|
* @copyright Copyright (c) Rik Schreurs |
|
9
|
|
|
* @license http://mit-license.org/ |
|
10
|
|
|
* @link https://github.com/cellcote/laravel-proxify |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Cellcote\LaravelProxify\Managers; |
|
14
|
|
|
|
|
15
|
|
|
use Cellcote\LaravelProxify\Exceptions\CookieExpiredException; |
|
16
|
|
|
use Cellcote\LaravelProxify\Exceptions\CookieInvalidException; |
|
17
|
|
|
use Illuminate\Support\Facades\Cookie; |
|
18
|
|
|
use Cellcote\LaravelProxify\ProxyAux; |
|
19
|
|
|
|
|
20
|
|
|
class CookieManager { |
|
21
|
|
|
|
|
22
|
|
|
const COOKIE_NAME = 'name'; |
|
23
|
|
|
const COOKIE_TIME = 'time'; |
|
24
|
|
|
private $info = null; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct($info) { |
|
27
|
|
|
$this->info = $info; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $callMode |
|
32
|
|
|
* @return mixed|string |
|
33
|
|
|
* @throws CookieExpiredException |
|
34
|
|
|
* @throws CookieInvalidException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function tryParseCookie($callMode) { |
|
37
|
|
|
$parsedCookie = Cookie::get($this->info[CookieManager::COOKIE_NAME]); |
|
38
|
|
|
|
|
39
|
|
|
if (isset($parsedCookie)) { |
|
40
|
|
|
$parsedCookie = json_decode($parsedCookie, true); |
|
41
|
|
|
$this->validateCookie($parsedCookie); |
|
42
|
|
|
} else { |
|
43
|
|
|
if ($callMode !== ProxyAux::MODE_LOGIN) { |
|
44
|
|
|
throw new CookieExpiredException(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $parsedCookie; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $content |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
|
|
public function createCookie(Array $content) { |
|
56
|
|
|
if (!isset($this->info[CookieManager::COOKIE_TIME]) || $this->info[CookieManager::COOKIE_TIME] == null) { |
|
57
|
|
|
$cookie = Cookie::forever($this->info[CookieManager::COOKIE_NAME], json_encode($content)); |
|
58
|
|
|
} else { |
|
59
|
|
|
$cookie = Cookie::make($this->info[CookieManager::COOKIE_NAME], json_encode($content), $this->info[CookieManager::COOKIE_TIME]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $cookie; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function destroyCookie() { |
|
69
|
|
|
return Cookie::forget($this->info[CookieManager::COOKIE_NAME]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $parsedCookie |
|
74
|
|
|
* @return bool |
|
75
|
|
|
* @throws CookieInvalidException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function validateCookie($parsedCookie) { |
|
78
|
|
|
if (!isset($parsedCookie) || !array_key_exists(ProxyAux::ACCESS_TOKEN, $parsedCookie)) { |
|
79
|
|
|
throw new CookieInvalidException(ProxyAux::ACCESS_TOKEN); |
|
80
|
|
|
} |
|
81
|
|
|
if (!array_key_exists(ProxyAux::TOKEN_TYPE, $parsedCookie)) { |
|
82
|
|
|
throw new CookieInvalidException(ProxyAux::TOKEN_TYPE); |
|
83
|
|
|
} |
|
84
|
|
|
if (!array_key_exists(ProxyAux::TOKEN_EXPIRES, $parsedCookie)) { |
|
85
|
|
|
throw new CookieInvalidException(ProxyAux::TOKEN_EXPIRES); |
|
86
|
|
|
} |
|
87
|
|
|
if (!array_key_exists(ProxyAux::COOKIE_URI, $parsedCookie)) { |
|
88
|
|
|
throw new CookieInvalidException(ProxyAux::COOKIE_URI); |
|
89
|
|
|
} |
|
90
|
|
|
if (!array_key_exists(ProxyAux::CLIENT_ID, $parsedCookie)) { |
|
91
|
|
|
throw new CookieInvalidException(ProxyAux::CLIENT_ID); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|