| Total Complexity | 43 |
| Total Lines | 318 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 4 | ||
| Bugs | 0 | Features | 3 |
Complex classes like AuthControllerValidationTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AuthControllerValidationTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | trait AuthControllerValidationTrait { |
||
| 23 | |||
| 24 | private static $TWO_FA_KEY='2FA-infos'; |
||
| 25 | |||
| 26 | protected static $TOKENS_VALIDATE_EMAIL='email.validation'; |
||
| 27 | |||
| 28 | protected static $TOKENS_RECOVERY_ACCOUNT='account.recovery'; |
||
| 29 | |||
| 30 | abstract protected function twoFABadCodeMessage(FlashMessage $fMessage); |
||
| 31 | |||
| 32 | abstract protected function fMessage(FlashMessage $fMessage, $id = null):string; |
||
| 33 | |||
| 34 | abstract protected function _getFiles(): AuthFiles; |
||
| 35 | |||
| 36 | abstract protected function getBaseUrl():string; |
||
| 37 | |||
| 38 | abstract protected function authLoadView($viewName, $vars = [ ]):void; |
||
| 39 | |||
| 40 | abstract protected function twoFAMessage(FlashMessage $fMessage); |
||
| 41 | |||
| 42 | abstract protected function useAjax():bool; |
||
| 43 | |||
| 44 | abstract public function _getBodySelector():string; |
||
| 45 | |||
| 46 | abstract protected function generate2FACode():string; |
||
| 47 | |||
| 48 | abstract public function _getUserSessionKey():string; |
||
| 49 | |||
| 50 | abstract protected function onConnect($connected); |
||
| 51 | |||
| 52 | abstract protected function initializeAuth(); |
||
| 53 | |||
| 54 | abstract protected function finalizeAuth(); |
||
| 55 | |||
| 56 | abstract protected function onBad2FACode():void; |
||
| 57 | |||
| 58 | abstract protected function _send2FACode(string $code,$connected):void; |
||
| 59 | |||
| 60 | abstract protected function newTwoFACodeMessage(FlashMessage $fMessage); |
||
| 61 | |||
| 62 | abstract protected function emailValidationDuration():\DateInterval; |
||
| 63 | |||
| 64 | abstract protected function _sendEmailValidation(string $email,string $validationURL,string $expire):void; |
||
| 65 | |||
| 66 | abstract protected function emailValidationSuccess(FlashMessage $fMessage); |
||
| 67 | |||
| 68 | abstract protected function emailValidationError(FlashMessage $fMessage); |
||
| 69 | |||
| 70 | abstract protected function towFACodePrefix():string; |
||
| 71 | |||
| 72 | abstract protected function twoFACodeDuration():\DateInterval; |
||
| 73 | |||
| 74 | abstract protected function getAuthTokensEmailValidation():AuthTokens; |
||
| 75 | |||
| 76 | abstract protected function isValidEmailForRecovery(string $email):bool; |
||
| 77 | |||
| 78 | abstract protected function recoveryEmailSendMessage(FlashMessage $fMessage); |
||
| 79 | |||
| 80 | abstract protected function _sendEmailAccountRecovery(string $email,string $url,string $expire):bool; |
||
| 81 | |||
| 82 | abstract protected function recoveryEmailErrorMessage(FlashMessage $fMessage); |
||
| 83 | |||
| 84 | abstract protected function accountRecoveryDuration():\DateInterval; |
||
| 85 | |||
| 86 | abstract protected function getAuthTokensAccountRecovery():AuthTokens; |
||
| 87 | |||
| 88 | abstract protected function passwordResetAction(string $email,string $newPasswordHash):bool; |
||
| 89 | |||
| 90 | abstract protected function resetPasswordSuccessMessage(FlashMessage $fMessage); |
||
| 91 | |||
| 92 | abstract protected function resetPasswordErrorMessage(FlashMessage $fMessage); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @noRoute |
||
| 96 | */ |
||
| 97 | #[\Ubiquity\attributes\items\router\NoRoute] |
||
| 98 | public function bad2FACode():void{ |
||
| 99 | $this->confirm(); |
||
| 100 | $fMessage = new FlashMessage ( 'Invalid 2FA code!', 'Two Factor Authentification', 'warning', 'warning circle' ); |
||
| 101 | $this->twoFABadCodeMessage( $fMessage ); |
||
| 102 | $message = $this->fMessage ( $fMessage, 'bad-code' ); |
||
| 103 | $this->authLoadView ( $this->_getFiles ()->getViewBadTwoFACode(), [ '_message' => $message,'url' => $this->getBaseUrl ().'/sendNew2FACode','bodySelector' => '#bad-two-fa','_btCaption' => 'Send new code' ] ); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @noRoute |
||
| 108 | */ |
||
| 109 | #[\Ubiquity\attributes\items\router\NoRoute] |
||
| 110 | public function confirm(){ |
||
| 111 | $fMessage = new FlashMessage( 'Enter the rescue code and validate.', 'Two factor Authentification', 'info', 'key' ); |
||
| 112 | $this->twoFAMessage ( $fMessage ); |
||
| 113 | $message = $this->fMessage ( $fMessage ); |
||
| 114 | if($this->useAjax()){ |
||
| 115 | $frm=$this->jquery->semantic()->htmlForm('frm-valid-code'); |
||
| 116 | $frm->addExtraFieldRule('code','empty'); |
||
| 117 | $frm->setValidationParams(['inline'=>true,'on'=>'blur']); |
||
| 118 | } |
||
| 119 | $this->authLoadView ( $this->_getFiles ()->getViewStepTwo(), [ '_message' => $message,'submitURL' => $this->getBaseUrl ().'/submitCode','bodySelector' => $this->_getBodySelector(),'prefix'=>$this->towFACodePrefix() ] ); |
||
| 120 | } |
||
| 121 | |||
| 122 | protected function save2FACode():array{ |
||
| 123 | $code=$this->generate2FACode(); |
||
| 124 | $expire=(new \DateTime())->add($this->twoFACodeDuration()); |
||
| 125 | $codeInfos=USession::get(self::$TWO_FA_KEY,compact('code','expire')); |
||
| 126 | USession::set(self::$TWO_FA_KEY,$codeInfos); |
||
| 127 | return $codeInfos; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Submits the 2FA code in post request. |
||
| 132 | * |
||
| 133 | * @post |
||
| 134 | */ |
||
| 135 | #[\Ubiquity\attributes\items\router\Post] |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @noRoute |
||
| 154 | */ |
||
| 155 | #[\Ubiquity\attributes\items\router\NoRoute] |
||
| 156 | public function send2FACode(){ |
||
| 157 | $codeInfos=$this->save2FACode(); |
||
| 158 | $this->_send2FACode($codeInfos['code'], USession::get($this->_getUserSessionKey().'-2FA')); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function sendNew2FACode(){ |
||
| 162 | $this->send2FACode(); |
||
| 163 | $fMessage = new FlashMessage ( 'A new code was submited.', 'Two factor Authentification', 'success', 'key' ); |
||
| 164 | $this->newTwoFACodeMessage ( $fMessage ); |
||
| 165 | echo $this->fMessage ( $fMessage ); |
||
| 166 | } |
||
| 167 | |||
| 168 | protected function generateEmailValidationUrl($email):array { |
||
| 169 | $duration=$this->emailValidationDuration(); |
||
| 170 | $tokens=$this->getAuthTokensEmailValidation(); |
||
| 171 | $d=new \DateTime(); |
||
| 172 | $dExpire=$d->add($duration); |
||
| 173 | $key=$tokens->store(['email'=>$email]); |
||
| 174 | return ['url'=>$key.'/'.\md5($email),'expire'=>$dExpire]; |
||
| 175 | } |
||
| 176 | |||
| 177 | protected function prepareEmailValidation(string $email){ |
||
| 178 | $data=$this->generateEmailValidationUrl($email); |
||
| 179 | $validationURL=$this->getBaseUrl().'/checkEmail/'.$data['url']; |
||
| 180 | $this->_sendEmailValidation($email, $validationURL,UDateTime::elapsed($data['expire'])); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * To override |
||
| 185 | * Checks an email. |
||
| 186 | * |
||
| 187 | * @param string $mail |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | protected function validateEmail(string $mail):bool{ |
||
| 191 | return true; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * To override for a more secure 2FA code. |
||
| 196 | * @param string $secret |
||
| 197 | * @param string $userInput |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | protected function check2FACode(string $secret,string $userInput):bool{ |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Route for email validation checking when creating a new account. |
||
| 206 | * @param string $key |
||
| 207 | * @param string $hashMail |
||
| 208 | */ |
||
| 209 | public function checkEmail(string $key,string $hashMail){ |
||
| 210 | $isValid=false; |
||
| 211 | $tokens=$this->getAuthTokensEmailValidation(); |
||
| 212 | if($tokens->exists($key)){ |
||
| 213 | if(!$tokens->expired($key)){ |
||
| 214 | $data=$tokens->fetch($key); |
||
| 215 | $email=$data['email']; |
||
| 216 | if(\md5($email)===$hashMail && $this->validateEmail($email)){ |
||
| 217 | $fMessage = new FlashMessage ( "Your email <b>$email</b> has been validated.", 'Account creation', 'success', 'user' ); |
||
| 218 | $this->emailValidationSuccess($fMessage); |
||
| 219 | $isValid=true; |
||
| 220 | } |
||
| 221 | $msg='This validation link is not valid!'; |
||
| 222 | }else{ |
||
| 223 | $msg='This validation link is no longer active!'; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | if(!$isValid){ |
||
| 227 | $fMessage = new FlashMessage ( $msg??'This validation link is not valid!', 'Account creation', 'error', 'user' ); |
||
| 228 | $this->emailValidationError($fMessage); |
||
| 229 | } |
||
| 230 | echo $this->fMessage($fMessage); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function recoveryInit(){ |
||
| 234 | $fMessage = new FlashMessage( 'Enter the email associated with your account to receive a password reset link.', 'Account recovery', 'info', 'user' ); |
||
| 235 | $this->recoveryInitMessage ( $fMessage ); |
||
| 236 | $message = $this->fMessage ( $fMessage ); |
||
| 237 | if($this->useAjax()){ |
||
| 238 | $frm=$this->jquery->semantic()->htmlForm('frm-account-recovery'); |
||
| 239 | $frm->addExtraFieldRules('email',['empty','email']); |
||
| 240 | $frm->setValidationParams(['inline'=>true,'on'=>'blur']); |
||
| 241 | } |
||
| 242 | $this->authLoadView ( $this->_getFiles ()->getViewInitRecovery(), [ '_message' => $message,'submitURL' => $this->getBaseUrl ().'/recoveryInfo','bodySelector' => $this->_getBodySelector()] ); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @post |
||
| 247 | */ |
||
| 248 | #[\Ubiquity\attributes\items\router\Post] |
||
| 249 | public function recoveryInfo(){ |
||
| 250 | if(URequest::isPost()){ |
||
| 251 | if($this->isValidEmailForRecovery($email=URequest::filterPost('email',FILTER_VALIDATE_EMAIL))) { |
||
|
1 ignored issue
–
show
|
|||
| 252 | $this->prepareEmailAccountRecovery($email); |
||
|
1 ignored issue
–
show
|
|||
| 253 | $fMessage = new FlashMessage (sprintf('A password reset email has been sent to <b>%s</b>.<br>You can only use this link temporarily, from the same machine, on this browser.',$email), 'Account recovery', 'success', 'email'); |
||
| 254 | $this->recoveryEmailSendMessage($fMessage); |
||
| 255 | }else{ |
||
| 256 | $fMessage = new FlashMessage (sprintf('No account is associated with the email address <b>%s</b>.<br><a href="%s" data-target="%s">Try again.</a>.',$email,$this->getBaseUrl().'/recoveryInit',$this->_getBodySelector()), 'Account recovery', 'error', 'user'); |
||
| 257 | $this->recoveryEmailErrorMessage($fMessage); |
||
| 258 | } |
||
| 259 | echo $this->fMessage ( $fMessage ); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | public function recovery(string $key,string $hashMail) { |
||
| 264 | $tokens = $this->getAuthTokensAccountRecovery(); |
||
| 265 | if ($tokens->exists($key)) { |
||
| 266 | if (!$tokens->expired($key)) { |
||
| 267 | $data = $tokens->fetch($key); |
||
| 268 | if(\is_array($data)) { |
||
| 269 | $email = $data['email']; |
||
| 270 | if (\md5($email) === $hashMail && $this->validateEmail($email)) { |
||
| 271 | $fMessage = new FlashMessage ("Enter a new password associated to the account <b>$email</b>.", 'Account recovery', 'success', 'user'); |
||
| 272 | $this->emailAccountRecoverySuccess($fMessage); |
||
| 273 | $message=$this->fMessage($fMessage); |
||
| 274 | if($this->useAjax()) { |
||
| 275 | $frm = $this->_addFrmAjaxBehavior('frm-account-recovery'); |
||
| 276 | $passwordInputName = $this->_getPasswordInputName(); |
||
| 277 | $frm->addExtraFieldRules($passwordInputName . '-conf', ['empty', "match[$passwordInputName]"]); |
||
| 278 | } |
||
| 279 | $this->authLoadView ( $this->_getFiles ()->getViewRecovery(), [ 'key'=>$key,'email'=>$email,'_message' => $message,'submitURL' => $this->getBaseUrl ().'/recoverySubmit','bodySelector' => $this->_getBodySelector(),'passwordInputName' => $this->_getPasswordInputName (),'passwordLabel' => $this->passwordLabel (),'passwordConfLabel'=>$this->passwordConfLabel()] ); |
||
| 280 | return ; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | $msg = 'This recovery link was not generated on this device!'; |
||
| 284 | } else { |
||
| 285 | $msg = 'This recovery link is no longer active!'; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | $fMessage = new FlashMessage ($msg ?? 'This account recovery link is not valid!', 'Account recovery', 'error', 'user'); |
||
| 289 | $this->emailAccountRecoveryError($fMessage); |
||
| 290 | echo $this->fMessage($fMessage); |
||
| 291 | } |
||
| 292 | |||
| 293 | protected function generateEmailAccountRecoveryUrl($email):array { |
||
| 300 | } |
||
| 301 | |||
| 302 | protected function prepareEmailAccountRecovery(string $email){ |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @post |
||
| 310 | */ |
||
| 311 | #[\Ubiquity\attributes\items\router\Post] |
||
| 312 | public function recoverySubmit(){ |
||
| 313 | if(URequest::isPost() && URequest::has('key')){ |
||
| 314 | $tokens = $this->getAuthTokensAccountRecovery(); |
||
| 344 |