Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AuthTokens.php
Go to the documentation of this file.
1<?php
2
3
5
6
9
11 protected int $length;
12 protected int $duration;
13 protected string $root;
14 protected bool $sameOrigin;
15 const CACHE_KEY='auth';
16
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;
29 }
30
35 protected function tokenGenerator() {
36 return \bin2hex ( \random_bytes ( $this->length??10 ) );
37 }
38
42 protected function getOrigin():string{
43 return $this->generateOrigin(['platform'=>UASystem::getPlatform(),'browser'=>UASystem::getBrowserComplete(),'ip'=>$_SERVER['REMOTE_ADDR']]);
44 }
45
50 protected function generateOrigin(array $data):string{
51 $data=['platform'=>$data['platform']??'','browser'=>$data['browser']??'','ip'=>$data['ip']??''];
52 return \md5(\json_encode($data));
53 }
54
59 protected function generateToken():string {
60 do {
61 $token = $this->tokenGenerator ();
62 } while ( $this->exists($token) );
63 return $token;
64 }
65
70 protected function getKey(string $token):string{
71 return self::CACHE_KEY.($this->root).$token;
72 }
73
78 public function exists(string $token):bool{
79 return CacheManager::$cache->exists($this->getKey($token));
80 }
81
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
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
116 public function remove(string $token){
117 CacheManager::$cache->remove($this->getKey($token));
118 }
119
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
141 protected function isSameOrigin($data):bool{
142 return ($data['origin']??'')===$this->getOrigin();
143 }
144
148 public function setLength(int $length): void {
149 $this->length = $length;
150 }
151
155 public function setDuration(int $duration): void {
156 $this->duration = $duration;
157 }
158
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}
Manager for caches (Router, Rest, models).
store(array $data)
Stores some data associated to a new token.
setParams(int $length, int $duration, bool $sameOrigin)
__construct(string $root, int $length=10, int $duration=3600, bool $sameOrigin=false)
AuthTokens constructor.
fetch($token)
Gets the data associated to a token.
User agent detection.
Definition UASystem.php:15