Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Auth2FATrait.php
Go to the documentation of this file.
1<?php
2
4
9
22
23 private static $TWO_FA_KEY='2FA-infos';
24
25
26 abstract protected function fMessage(FlashMessage $fMessage, $id = null):string;
27
28 abstract protected function _getFiles(): AuthFiles;
29
30 abstract protected function getBaseUrl():string;
31
32 abstract protected function authLoadView($viewName, $vars = [ ]):void;
33
34 abstract protected function useAjax():bool;
35
36 abstract public function _getBodySelector():string;
37
38 abstract public function _getUserSessionKey():string;
39
40 abstract protected function onConnect($connected);
41
42 abstract protected function initializeAuth();
43
44 abstract protected function finalizeAuth();
45
46
53 protected function has2FA($accountValue=null):bool{
54 return false;
55 }
56
60 protected function onBad2FACode():void{
61 $this->bad2FACode();
62 }
63
70 protected function _send2FACode(string $code,$connected):void{
71
72 }
73
79 protected function getTokenSize():int{
80 return 6;
81 }
82
89 protected function generate2FACode():string{
90 return \bin2hex ( \random_bytes ($this->getTokenSize()));
91 }
92
97 protected function towFACodePrefix():string{
98 return 'U-';
99 }
100
101
106 protected function twoFACodeDuration():\DateInterval{
107 return new \DateInterval('PT5M');
108 }
109
114 protected function twoFAMessage(FlashMessage $fMessage){
115
116 }
121 protected function newTwoFACodeMessage(FlashMessage $fMessage){
122
123 }
124
129 protected function twoFABadCodeMessage(FlashMessage $fMessage){
130
131 }
132
139 protected function check2FACode(string $secret,string $userInput):bool{
140 return $secret===$userInput;
141 }
142
146 #[\Ubiquity\attributes\items\router\NoRoute]
147 public function bad2FACode():void{
148 $this->confirm();
149 $fMessage = new FlashMessage ( 'Invalid 2FA code!', 'Two Factor Authentification', 'warning', 'warning circle' );
150 $this->twoFABadCodeMessage( $fMessage );
151 $message = $this->fMessage ( $fMessage, 'bad-code' );
152 $this->authLoadView ( $this->_getFiles ()->getViewBadTwoFACode(), [ '_message' => $message,'url' => $this->getBaseUrl ().'/sendNew2FACode','bodySelector' => '#bad-two-fa','_btCaption' => 'Send new code' ] );
153 }
154
158 #[\Ubiquity\attributes\items\router\NoRoute]
159 public function confirm(){
160 $fMessage = new FlashMessage( 'Enter the rescue code and validate.', 'Two factor Authentification', 'info', 'key' );
161 $this->twoFAMessage ( $fMessage );
162 $message = $this->fMessage ( $fMessage );
163 if($this->useAjax()){
164 $frm=$this->jquery->semantic()->htmlForm('frm-valid-code');
165 $frm->addExtraFieldRule('code','empty');
166 $frm->setValidationParams(['inline'=>true,'on'=>'blur']);
167 }
168 $this->authLoadView ( $this->_getFiles ()->getViewStepTwo(), [ '_message' => $message,'submitURL' => $this->getBaseUrl ().'/submitCode','bodySelector' => $this->_getBodySelector(),'prefix'=>$this->towFACodePrefix() ] );
169 }
170
171 protected function save2FACode():array{
172 $code=$this->generate2FACode();
173 $expire=(new \DateTime())->add($this->twoFACodeDuration());
174 $codeInfos=USession::get(self::$TWO_FA_KEY,compact('code','expire'));
175 USession::set(self::$TWO_FA_KEY,$codeInfos);
176 return $codeInfos;
177 }
178
184 #[\Ubiquity\attributes\items\router\Post]
185 public function submitCode(){
186 if(URequest::isPost() && USession::exists(self::$TWO_FA_KEY)){
187 $twoFAInfos=USession::get(self::$TWO_FA_KEY);
188 $expired=$twoFAInfos['expire']<new \DateTime();
189 if(!$expired && $this->check2FACode($twoFAInfos['code'],URequest::post('code'))){
190 $this->onConnect(USession::get($this->_getUserSessionKey().'-2FA'));
191 }
192 else{
193 $this->_invalid=true;
194 $this->initializeAuth();
195 $this->onBad2FACode();
196 $this->finalizeAuth();
197 }
198 }
199 }
200
201 protected function send2FACode(){
202 $codeInfos=$this->save2FACode();
203 $this->_send2FACode($codeInfos['code'], USession::get($this->_getUserSessionKey().'-2FA'));
204 }
205
206 public function sendNew2FACode(){
207 if(USession::exists( $this->_getUserSessionKey().'-2FA')) {
208 $this->send2FACode();
209 $fMessage = new FlashMessage ('A new code was submited.', 'Two factor Authentification', 'success', 'key');
210 $this->newTwoFACodeMessage($fMessage);
211 echo $this->fMessage($fMessage);
212 }
213 }
214
215}
216
Ubiquity\controllers\auth$AuthFiles This class is part of Ubiquity.
Definition AuthFiles.php:13
has2FA($accountValue=null)
To override Returns true for a two factor authentification for this account.
fMessage(FlashMessage $fMessage, $id=null)
submitCode()
Submits the 2FA code in post request.
newTwoFACodeMessage(FlashMessage $fMessage)
To override.
twoFABadCodeMessage(FlashMessage $fMessage)
To override for modifying the message displayed if the 2FA code is bad.
twoFACodeDuration()
Returns the default validity duration of a generated 2FA code.
_send2FACode(string $code, $connected)
To override Send the 2FA code to the user (email, sms, phone call...)
twoFAMessage(FlashMessage $fMessage)
To override for modifying the 2FA panel message.
check2FACode(string $secret, string $userInput)
To override for a more secure 2FA code.
onBad2FACode()
To override for defining a new action when 2FA code is invalid.
towFACodePrefix()
Returns the code prefix (which should not be entered by the user).
generate2FACode()
Generates a new random 2FA code.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18
Http Session utilities This class is part of Ubiquity.
Definition USession.php:16