1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Google2FALaravel\Support; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse as IlluminateJsonResponse; |
6
|
|
|
use Illuminate\Http\Response as IlluminateHtmlResponse; |
7
|
|
|
use PragmaRX\Google2FALaravel\Events\OneTimePasswordRequested; |
8
|
|
|
use PragmaRX\Google2FALaravel\Events\OneTimePasswordRequested53; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
10
|
|
|
|
11
|
|
|
trait Response |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Make a JSON response. |
15
|
|
|
* |
16
|
|
|
* @param $statusCode |
17
|
|
|
* |
18
|
|
|
* @return IlluminateJsonResponse |
19
|
|
|
*/ |
20
|
|
|
protected function makeJsonResponse($statusCode) |
21
|
|
|
{ |
22
|
|
|
return new IlluminateJsonResponse( |
23
|
|
|
$this->getErrorBagForStatusCode($statusCode), |
24
|
|
|
$statusCode |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Make the status code, to respond accordingly. |
30
|
|
|
* |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
7 |
|
protected function makeStatusCode() |
34
|
|
|
{ |
35
|
7 |
|
if ($this->getRequest()->isMethod('get') || ($this->checkOTP() === Constants::OTP_VALID)) { |
36
|
4 |
|
return SymfonyResponse::HTTP_OK; |
37
|
|
|
} |
38
|
3 |
|
if ($this->checkOTP() === Constants::OTP_EMPTY) { |
39
|
2 |
|
return SymfonyResponse::HTTP_BAD_REQUEST; |
40
|
|
|
} |
41
|
1 |
|
return SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Make a web response. |
46
|
|
|
* |
47
|
|
|
* @param $statusCode |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
7 |
|
protected function makeHtmlResponse($statusCode) |
52
|
|
|
{ |
53
|
7 |
|
$view = $this->getView(); |
54
|
|
|
|
55
|
7 |
|
if ($statusCode !== SymfonyResponse::HTTP_OK) { |
56
|
3 |
|
$view->withErrors($this->getErrorBagForStatusCode($statusCode)); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
return new IlluminateHtmlResponse($view, $statusCode); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create a response to request the OTP. |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
66
|
|
|
*/ |
67
|
7 |
|
public function makeRequestOneTimePasswordResponse() |
68
|
|
|
{ |
69
|
7 |
|
event( |
70
|
7 |
|
app()->version() < '5.4' |
71
|
|
|
? new OneTimePasswordRequested53($this->getUser()) |
72
|
7 |
|
: new OneTimePasswordRequested($this->getUser()) |
73
|
|
|
); |
74
|
|
|
|
75
|
7 |
|
$expectJson = app()->version() < '5.4' |
76
|
|
|
? $this->getRequest()->wantsJson() |
77
|
7 |
|
: $this->getRequest()->expectsJson(); |
78
|
|
|
|
79
|
7 |
|
return $expectJson |
80
|
|
|
? $this->makeJsonResponse($this->makeStatusCode()) |
81
|
7 |
|
: $this->makeHtmlResponse($this->makeStatusCode()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the OTP view. |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
88
|
|
|
*/ |
89
|
7 |
|
private function getView() |
90
|
|
|
{ |
91
|
7 |
|
return view($this->config('view')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
abstract protected function getErrorBagForStatusCode($statusCode); |
95
|
|
|
|
96
|
|
|
abstract protected function inputHasOneTimePassword(); |
97
|
|
|
|
98
|
|
|
abstract public function checkOTP(); |
99
|
|
|
|
100
|
|
|
abstract protected function getUser(); |
101
|
|
|
|
102
|
|
|
abstract public function getRequest(); |
103
|
|
|
|
104
|
|
|
abstract protected function config($string, $children = []); |
105
|
|
|
} |
106
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: