1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\auth; |
4
|
|
|
|
5
|
|
|
use Ubiquity\utils\flash\FlashMessage; |
6
|
|
|
use Ubiquity\utils\http\USession; |
7
|
|
|
use Ubiquity\utils\http\URequest; |
8
|
|
|
use Ubiquity\cache\CacheManager; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* Ubiquity\controllers\auth$AuthControllerValidationTrait |
13
|
|
|
* This class is part of Ubiquity |
14
|
|
|
* @author jc |
15
|
|
|
* @version 1.0.0 |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
trait AuthControllerValidationTrait { |
19
|
|
|
|
20
|
|
|
public function bad2FACode(){ |
21
|
|
|
$this->confirm(); |
22
|
|
|
$fMessage = new FlashMessage ( 'Invalid 2FA code!', 'Two Factor Authentification', 'warning', 'warning circle' ); |
23
|
|
|
$this->twoFABadCodeMessage( $fMessage ); |
|
|
|
|
24
|
|
|
$message = $this->fMessage ( $fMessage, 'bad-code' ); |
|
|
|
|
25
|
|
|
$this->authLoadView ( $this->_getFiles ()->getViewBadTwoFACode(), [ '_message' => $message,'url' => $this->getBaseUrl ().'/sendNew2FACode','bodySelector' => '#bad-two-fa','_btCaption' => 'Send new code' ] ); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function confirm(){ |
29
|
|
|
$fMessage = new FlashMessage( 'Enter the rescue code and validate.', 'Two factor Authentification', 'info', 'key' ); |
30
|
|
|
$this->twoFAMessage ( $fMessage ); |
|
|
|
|
31
|
|
|
$message = $this->fMessage ( $fMessage ); |
32
|
|
|
if($this->useAjax()){ |
|
|
|
|
33
|
|
|
$frm=$this->jquery->semantic()->htmlForm('frm-valid-code'); |
34
|
|
|
$frm->addExtraFieldRule('code','empty'); |
35
|
|
|
$frm->setValidationParams(['inline'=>true,'on'=>'blur']); |
36
|
|
|
} |
37
|
|
|
$this->authLoadView ( $this->_getFiles ()->getViewStepTwo(), [ '_message' => $message,'submitURL' => $this->getBaseUrl ().'/submitCode','bodySelector' => $this->_getBodySelector(),'prefix'=>$this->towFACodePrefix() ] ); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function save2FACode(){ |
41
|
|
|
$code=USession::get('2FACode',$this->generate2FACode()); |
|
|
|
|
42
|
|
|
USession::set('2FACode',$code); |
43
|
|
|
return $code; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Submits the 2FA code in post request. |
48
|
|
|
*/ |
49
|
|
|
public function submitCode(){ |
50
|
|
|
if(URequest::isPost()){ |
51
|
|
|
if(USession::get('2FACode')===URequest::post('code')){ |
52
|
|
|
$this->onConnect(USession::get($this->_getUserSessionKey().'-2FA')); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
else{ |
55
|
|
|
$this->_invalid=true; |
|
|
|
|
56
|
|
|
$this->initializeAuth(); |
|
|
|
|
57
|
|
|
$this->onBad2FACode(); |
|
|
|
|
58
|
|
|
$this->finalizeAuth(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function send2FACode(){ |
64
|
|
|
$code=$this->save2FACode(); |
65
|
|
|
$this->_send2FACode($code, USession::get($this->_getUserSessionKey().'-2FA')); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function sendNew2FACode(){ |
69
|
|
|
$this->send2FACode(); |
70
|
|
|
$fMessage = new FlashMessage ( 'A new code was submited.', 'Two factor Authentification', 'success', 'key' ); |
71
|
|
|
$this->newTwoFACodeMessage ( $fMessage ); |
|
|
|
|
72
|
|
|
echo $this->fMessage ( $fMessage ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function generateEmailValidationUrl($email):string { |
76
|
|
|
$key=\uniqid('v',true); |
77
|
|
|
$d=new \DateTime(); |
78
|
|
|
$data=['email'=>$email,'expire'=>$d->add($this->emailValidationDuration())]; |
|
|
|
|
79
|
|
|
CacheManager::$cache->store('auth/'.$key, $data); |
80
|
|
|
return $key.'/'.\md5($email); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function prepareEmailValidation(string $email){ |
84
|
|
|
$validationURL=$this->getBaseUrl().'/checkEmail/'.$this->generateEmailValidationUrl($email); |
85
|
|
|
$this->_sendEmailValidation($email, $validationURL); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* To override |
90
|
|
|
* Checks an email. |
91
|
|
|
* |
92
|
|
|
* @param string $mail |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
protected function validateEmail(string $mail):bool{ |
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Route for email validation checking. |
101
|
|
|
* @param string $uuid |
102
|
|
|
* @param string $hashMail |
103
|
|
|
*/ |
104
|
|
|
public function checkEmail(string $uuid,string $hashMail){ |
105
|
|
|
$key='auth/'.$uuid; |
106
|
|
|
$isValid=false; |
107
|
|
|
if(CacheManager::$cache->exists($key)){ |
108
|
|
|
$data=CacheManager::$cache->fetch($key); |
109
|
|
|
$email=$data['email']; |
110
|
|
|
$date=$data['expire']; |
111
|
|
|
if($date>new \DateTime()){ |
112
|
|
|
if(\md5($email)===$hashMail){ |
113
|
|
|
if($this->validateEmail($email)){ |
114
|
|
|
$fMessage = new FlashMessage ( "Your email <b>$email</b> has been validated.", 'Account creation', 'success', 'user' ); |
115
|
|
|
$this->emailValidationSuccess($fMessage); |
|
|
|
|
116
|
|
|
$isValid=true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
CacheManager::$cache->remove($key); |
120
|
|
|
$msg='This validation link is not valid!'; |
121
|
|
|
}else{ |
122
|
|
|
$msg='This validation link is no longer active!'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
if(!$isValid){ |
126
|
|
|
$fMessage = new FlashMessage ( $msg??'This validation link is not valid!', 'Account creation', 'error', 'user' ); |
127
|
|
|
$this->emailValidationError($fMessage); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
$this->initializeAuth(); |
130
|
|
|
echo $this->fMessage($fMessage); |
|
|
|
|
131
|
|
|
$this->finalizeAuth(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|