Test Failed
Push — master ( cf3dbd...f03535 )
by Jean-Christophe
27:09
created

AuthTokens::setParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 3
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
	public function __construct(string $root,int $length = 10, int $duration = 3600,bool $sameOrigin=false){
25
		$this->root='/'.\trim($root,'/').'/';
26
		$this->length=$length;
27
		$this->duration=$duration;
28
		$this->sameOrigin=$sameOrigin;
29
	}
30
31
	/**
32
	 * @return string
33
	 * @throws \Exception
34
	 */
35
	protected function tokenGenerator() {
36
		return \bin2hex ( \random_bytes ( $this->length??10 ) );
37
	}
38
39
	/**
40
	 * @return string
41
	 */
42
	protected function getOrigin():string{
43
		return $this->generateOrigin(['platform'=>UASystem::getPlatform(),'browser'=>UASystem::getBrowserComplete()]);
44
	}
45
46
	/**
47
	 * @param array $data
48
	 * @return string
49
	 */
50
	protected function generateOrigin(array $data):string{
51
		$data=['platform'=>$data['platform']??'','browser'=>$data['browser']??''];
52
		return \md5(\json_encode($data));
53
	}
54
55
	/**
56
	 * @return string
57
	 * @throws \Exception
58
	 */
59
	protected function generateToken():string {
60
		do {
61
			$token = $this->tokenGenerator ();
62
		} while ( $this->exists($token) );
63
		return $token;
64
	}
65
66
	/**
67
	 * @param string $token
68
	 * @return string
69
	 */
70
	protected function getKey(string $token):string{
71
		return self::CACHE_KEY.($this->root).$token;
72
	}
73
74
	/**
75
	 * @param string $token
76
	 * @return bool
77
	 */
78
	public function exists(string $token):bool{
79
		return CacheManager::$cache->exists($this->getKey($token));
0 ignored issues
show
Bug Best Practice introduced by
The expression return Ubiquity\cache\Ca...($this->getKey($token)) could return the type string[] which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
80
	}
81
82
	/**
83
	 * @param string $token
84
	 * @return bool
85
	 */
86
	public function expired(string $token):bool{
87
		$tokenKey=$this->getKey($token);
88
		$expired= CacheManager::$cache->expired($tokenKey,$this->duration);
89
		if($expired){
90
			CacheManager::$cache->remove($tokenKey);
91
		}
92
		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
	public function store(array $data):string{
103
		if($this->sameOrigin){
104
			$data['origin']=$this->getOrigin();
105
		}
106
		$token=$this->generateToken();
107
		CacheManager::$cache->store($this->getKey($token),$data);
108
		return $token;
109
	}
110
111
	/**
112
	 * Removes an existing token.
113
	 *
114
	 * @param string $token
115
	 */
116
	public function remove(string $token){
117
		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
	public function fetch($token){
127
		$tokenKey=$this->getKey($token);
128
		if(CacheManager::$cache->exists($tokenKey) && !CacheManager::$cache->expired($tokenKey,$this->duration)) {
129
			$data= CacheManager::$cache->fetch($tokenKey);
130
			if(!$this->sameOrigin || $this->isSameOrigin($data)){
131
				return $data;
132
			}
133
		}
134
		return false;
135
	}
136
137
	/**
138
	 * @param $data
139
	 * @return bool
140
	 */
141
	protected function isSameOrigin($data):bool{
142
		return $this->generateOrigin($data)===$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
}