1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Ubiquity\controllers\auth; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Ubiquity\cache\CacheManager; |
8
|
|
|
use Ubiquity\utils\base\UASystem; |
9
|
|
|
|
10
|
|
|
class AuthTokens { |
11
|
|
|
protected int $length; |
12
|
|
|
protected int $duration; |
13
|
|
|
protected string $root; |
14
|
|
|
protected bool $sameOrigin; |
15
|
|
|
const CACHE_KEY='auth'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* AuthTokens constructor. |
19
|
|
|
* @param string $root |
20
|
|
|
* @param int $length |
21
|
|
|
* @param int $duration |
22
|
|
|
* @param bool $sameOrigin |
23
|
|
|
*/ |
24
|
1 |
|
public function __construct(string $root,int $length = 10, int $duration = 3600,bool $sameOrigin=false){ |
25
|
1 |
|
$this->root='/'.\trim($root,'/').'/'; |
26
|
1 |
|
$this->length=$length; |
27
|
1 |
|
$this->duration=$duration; |
28
|
1 |
|
$this->sameOrigin=$sameOrigin; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
1 |
|
protected function tokenGenerator() { |
36
|
1 |
|
return \bin2hex ( \random_bytes ( $this->length??10 ) ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
1 |
|
protected function getOrigin():string{ |
43
|
1 |
|
return $this->generateOrigin(['platform'=>UASystem::getPlatform(),'browser'=>UASystem::getBrowserComplete(),'ip'=>$_SERVER['REMOTE_ADDR']]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $data |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
1 |
|
protected function generateOrigin(array $data):string{ |
51
|
1 |
|
$data=['platform'=>$data['platform']??'','browser'=>$data['browser']??'','ip'=>$data['ip']??'']; |
52
|
1 |
|
return \md5(\json_encode($data)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
1 |
|
protected function generateToken():string { |
60
|
|
|
do { |
61
|
1 |
|
$token = $this->tokenGenerator (); |
62
|
1 |
|
} while ( $this->exists($token) ); |
63
|
1 |
|
return $token; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $token |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
|
protected function getKey(string $token):string{ |
71
|
1 |
|
return self::CACHE_KEY.($this->root).$token; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $token |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
1 |
|
public function exists(string $token):bool{ |
79
|
1 |
|
return CacheManager::$cache->exists($this->getKey($token)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $token |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
1 |
|
public function expired(string $token):bool{ |
87
|
1 |
|
$tokenKey=$this->getKey($token); |
88
|
1 |
|
$expired= CacheManager::$cache->expired($tokenKey,$this->duration); |
89
|
1 |
|
if($expired){ |
90
|
|
|
CacheManager::$cache->remove($tokenKey); |
91
|
|
|
} |
92
|
1 |
|
return $expired; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Stores some data associated to a new token. |
97
|
|
|
* |
98
|
|
|
* @param array $data |
99
|
|
|
* @return string |
100
|
|
|
* @throws \Ubiquity\exceptions\CacheException |
101
|
|
|
*/ |
102
|
1 |
|
public function store(array $data):string{ |
103
|
1 |
|
if($this->sameOrigin){ |
104
|
1 |
|
$data['origin']=$this->getOrigin(); |
105
|
|
|
} |
106
|
1 |
|
$token=$this->generateToken(); |
107
|
1 |
|
CacheManager::$cache->store($this->getKey($token),$data); |
108
|
1 |
|
return $token; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Removes an existing token. |
113
|
|
|
* |
114
|
|
|
* @param string $token |
115
|
|
|
*/ |
116
|
1 |
|
public function remove(string $token){ |
117
|
1 |
|
CacheManager::$cache->remove($this->getKey($token)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets the data associated to a token. |
122
|
|
|
* |
123
|
|
|
* @param $token |
124
|
|
|
* @return false|mixed |
125
|
|
|
*/ |
126
|
1 |
|
public function fetch($token){ |
127
|
1 |
|
$tokenKey=$this->getKey($token); |
128
|
1 |
|
if(CacheManager::$cache->exists($tokenKey) && !CacheManager::$cache->expired($tokenKey,$this->duration)) { |
129
|
1 |
|
$data= CacheManager::$cache->fetch($tokenKey); |
130
|
1 |
|
if(!$this->sameOrigin || $this->isSameOrigin($data)){ |
131
|
1 |
|
return $data; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $data |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
1 |
|
protected function isSameOrigin($data):bool{ |
142
|
1 |
|
return ($data['origin']??'')===$this->getOrigin(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param int $length |
147
|
|
|
*/ |
148
|
|
|
public function setLength(int $length): void { |
149
|
|
|
$this->length = $length; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param int $duration |
154
|
|
|
*/ |
155
|
|
|
public function setDuration(int $duration): void { |
156
|
|
|
$this->duration = $duration; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param bool $sameOrigin |
161
|
|
|
*/ |
162
|
|
|
public function setSameOrigin(bool $sameOrigin): void { |
163
|
|
|
$this->sameOrigin = $sameOrigin; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function setParams(int $length,int $duration,bool $sameOrigin){ |
167
|
|
|
$this->length=$length; |
168
|
|
|
$this->duration=$duration; |
169
|
|
|
$this->sameOrigin=$sameOrigin; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |