Test Failed
Push — master ( a3ba5a...4fa899 )
by Jean-Christophe
18:16
created

AuthControllerValidationTrait::submitCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 10
rs 10
cc 3
nc 3
nop 0
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 );
0 ignored issues
show
Bug introduced by
It seems like twoFABadCodeMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
		$this->/** @scrutinizer ignore-call */ 
24
         twoFABadCodeMessage( $fMessage );
Loading history...
24
		$message = $this->fMessage ( $fMessage, 'bad-code' );
0 ignored issues
show
Bug introduced by
It seems like fMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
		/** @scrutinizer ignore-call */ 
25
  $message = $this->fMessage ( $fMessage, 'bad-code' );
Loading history...
25
		$this->authLoadView ( $this->_getFiles ()->getViewBadTwoFACode(), [ '_message' => $message,'url' => $this->getBaseUrl ().'/sendNew2FACode','bodySelector' => '#bad-two-fa','_btCaption' => 'Send new code' ] );
0 ignored issues
show
Bug introduced by
It seems like _getFiles() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
		$this->authLoadView ( $this->/** @scrutinizer ignore-call */ _getFiles ()->getViewBadTwoFACode(), [ '_message' => $message,'url' => $this->getBaseUrl ().'/sendNew2FACode','bodySelector' => '#bad-two-fa','_btCaption' => 'Send new code' ] );
Loading history...
Bug introduced by
It seems like getBaseUrl() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
		$this->authLoadView ( $this->_getFiles ()->getViewBadTwoFACode(), [ '_message' => $message,'url' => $this->/** @scrutinizer ignore-call */ getBaseUrl ().'/sendNew2FACode','bodySelector' => '#bad-two-fa','_btCaption' => 'Send new code' ] );
Loading history...
Bug introduced by
It seems like authLoadView() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
		$this->/** @scrutinizer ignore-call */ 
26
         authLoadView ( $this->_getFiles ()->getViewBadTwoFACode(), [ '_message' => $message,'url' => $this->getBaseUrl ().'/sendNew2FACode','bodySelector' => '#bad-two-fa','_btCaption' => 'Send new code' ] );
Loading history...
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 );
0 ignored issues
show
Bug introduced by
It seems like twoFAMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
		$this->/** @scrutinizer ignore-call */ 
31
         twoFAMessage ( $fMessage );
Loading history...
31
		$message = $this->fMessage ( $fMessage );
32
		if($this->useAjax()){
0 ignored issues
show
Bug introduced by
It seems like useAjax() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
		if($this->/** @scrutinizer ignore-call */ useAjax()){
Loading history...
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() ] );
0 ignored issues
show
Bug introduced by
It seems like _getBodySelector() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
		$this->authLoadView ( $this->_getFiles ()->getViewStepTwo(), [ '_message' => $message,'submitURL' => $this->getBaseUrl ().'/submitCode','bodySelector' => $this->/** @scrutinizer ignore-call */ _getBodySelector(),'prefix'=>$this->towFACodePrefix() ] );
Loading history...
Bug introduced by
It seems like towFACodePrefix() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
		$this->authLoadView ( $this->_getFiles ()->getViewStepTwo(), [ '_message' => $message,'submitURL' => $this->getBaseUrl ().'/submitCode','bodySelector' => $this->_getBodySelector(),'prefix'=>$this->/** @scrutinizer ignore-call */ towFACodePrefix() ] );
Loading history...
38
	}
39
	
40
	protected function save2FACode(){
41
		$code=USession::get('2FACode',$this->generate2FACode());
0 ignored issues
show
Bug introduced by
It seems like generate2FACode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
		$code=USession::get('2FACode',$this->/** @scrutinizer ignore-call */ generate2FACode());
Loading history...
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'));
0 ignored issues
show
Bug introduced by
It seems like _getUserSessionKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
				$this->onConnect(USession::get($this->/** @scrutinizer ignore-call */ _getUserSessionKey().'-2FA'));
Loading history...
Bug introduced by
It seems like onConnect() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
				$this->/** @scrutinizer ignore-call */ 
53
           onConnect(USession::get($this->_getUserSessionKey().'-2FA'));
Loading history...
53
			}
54
			else{
55
				$this->_invalid=true;
0 ignored issues
show
Bug Best Practice introduced by
The property _invalid does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
				$this->initializeAuth();
0 ignored issues
show
Bug introduced by
It seems like initializeAuth() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
				$this->/** @scrutinizer ignore-call */ 
57
           initializeAuth();
Loading history...
57
				$this->onBad2FACode();
0 ignored issues
show
Bug introduced by
It seems like onBad2FACode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
				$this->/** @scrutinizer ignore-call */ 
58
           onBad2FACode();
Loading history...
58
				$this->finalizeAuth();
0 ignored issues
show
Bug introduced by
It seems like finalizeAuth() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
				$this->/** @scrutinizer ignore-call */ 
59
           finalizeAuth();
Loading history...
59
			}
60
		}
61
	}
62
	
63
	public function send2FACode(){
64
		$code=$this->save2FACode();
65
		$this->_send2FACode($code, USession::get($this->_getUserSessionKey().'-2FA'));
0 ignored issues
show
Bug introduced by
The method _send2FACode() does not exist on Ubiquity\controllers\aut...ntrollerValidationTrait. Did you maybe mean send2FACode()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
		$this->/** @scrutinizer ignore-call */ 
66
         _send2FACode($code, USession::get($this->_getUserSessionKey().'-2FA'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
It seems like newTwoFACodeMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
		$this->/** @scrutinizer ignore-call */ 
72
         newTwoFACodeMessage ( $fMessage );
Loading history...
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())];
0 ignored issues
show
Bug introduced by
It seems like emailValidationDuration() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
		$data=['email'=>$email,'expire'=>$d->add($this->/** @scrutinizer ignore-call */ emailValidationDuration())];
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like _sendEmailValidation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
		$this->/** @scrutinizer ignore-call */ 
86
         _sendEmailValidation($email, $validationURL);
Loading history...
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{
1 ignored issue
show
Unused Code introduced by
The parameter $mail is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
	protected function validateEmail(/** @scrutinizer ignore-unused */ string $mail):bool{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like emailValidationSuccess() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
						$this->/** @scrutinizer ignore-call */ 
116
             emailValidationSuccess($fMessage);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like emailValidationError() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
			$this->/** @scrutinizer ignore-call */ 
128
          emailValidationError($fMessage);
Loading history...
128
		}
129
		$this->initializeAuth();
130
		echo $this->fMessage($fMessage);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $fMessage does not seem to be defined for all execution paths leading up to this point.
Loading history...
131
		$this->finalizeAuth();
132
	}
133
}
134
135