|
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
|
|
|
|
|
42
|
1 |
|
return SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Make a web response. |
|
47
|
|
|
* |
|
48
|
|
|
* @param $statusCode |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Illuminate\Http\Response |
|
51
|
|
|
*/ |
|
52
|
7 |
|
protected function makeHtmlResponse($statusCode) |
|
53
|
|
|
{ |
|
54
|
7 |
|
$view = $this->getView(); |
|
55
|
|
|
|
|
56
|
7 |
|
if ($statusCode !== SymfonyResponse::HTTP_OK) { |
|
57
|
3 |
|
$view->withErrors($this->getErrorBagForStatusCode($statusCode)); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
7 |
|
return new IlluminateHtmlResponse($view, $statusCode); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create a response to request the OTP. |
|
65
|
|
|
* |
|
66
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
|
67
|
|
|
*/ |
|
68
|
7 |
|
public function makeRequestOneTimePasswordResponse() |
|
69
|
|
|
{ |
|
70
|
7 |
|
event( |
|
71
|
7 |
|
app()->version() < '5.4' |
|
72
|
|
|
? new OneTimePasswordRequested53($this->getUser()) |
|
73
|
7 |
|
: new OneTimePasswordRequested($this->getUser()) |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
7 |
|
$expectJson = app()->version() < '5.4' |
|
77
|
|
|
? $this->getRequest()->wantsJson() |
|
78
|
7 |
|
: $this->getRequest()->expectsJson(); |
|
79
|
|
|
|
|
80
|
7 |
|
return $expectJson |
|
81
|
|
|
? $this->makeJsonResponse($this->makeStatusCode()) |
|
82
|
7 |
|
: $this->makeHtmlResponse($this->makeStatusCode()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the OTP view. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
89
|
|
|
*/ |
|
90
|
7 |
|
private function getView() |
|
91
|
|
|
{ |
|
92
|
7 |
|
return view($this->config('view')); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
abstract protected function getErrorBagForStatusCode($statusCode); |
|
96
|
|
|
|
|
97
|
|
|
abstract protected function inputHasOneTimePassword(); |
|
98
|
|
|
|
|
99
|
|
|
abstract public function checkOTP(); |
|
100
|
|
|
|
|
101
|
|
|
abstract protected function getUser(); |
|
102
|
|
|
|
|
103
|
|
|
abstract public function getRequest(); |
|
104
|
|
|
|
|
105
|
|
|
abstract protected function config($string, $children = []); |
|
106
|
|
|
} |
|
107
|
|
|
|
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: