9
9
use Illuminate \Contracts \Config \Repository as Configurations ;
10
10
use SoapBox \SignedRequests \Exceptions \ExpiredRequestException ;
11
11
use SoapBox \SignedRequests \Exceptions \InvalidSignatureException ;
12
+ use SoapBox \SignedRequests \Exceptions \InvalidConfigurationException ;
12
13
13
14
class VerifySignature
14
15
{
@@ -53,32 +54,34 @@ public function __construct(Configurations $configurations, Cache $cache)
53
54
* Thrown if request replays are disabled and either the request
54
55
* timestamp is outside the window of tolerance, or the request has
55
56
* previously been served.
57
+ * @throws \SoapBox\SignedRequests\Exceptions\InvalidConfigurationException
58
+ * Thrown if the request key is not defined in the config
56
59
*
57
60
* @param \Illuminate\Http\Request $request
58
61
* An instance of the request.
59
62
* @param \Closure $next
60
63
* A callback function of where to go next.
61
- * @param mixed $prefix
64
+ * @param mixed $requestKey
62
65
*
63
66
* @return mixed
64
67
*/
65
- public function handle (Request $ request , Closure $ next , $ prefix = '' )
68
+ public function handle (Request $ request , Closure $ next , $ requestKey = 'default ' )
66
69
{
67
- if (!empty ( $ prefix )) {
68
- $ prefix .= ' - ' ;
70
+ if (!array_key_exists ( $ requestKey , $ this -> configurations -> get ( ' signed-requests ' ) )) {
71
+ throw new InvalidConfigurationException () ;
69
72
}
70
73
71
74
$ signed = new Verifier ($ request );
72
75
73
76
$ key = sprintf (
74
77
'%s.%s ' ,
75
- $ this ->configurations ->get ($ prefix . ' signed-requests.cache-prefix ' ),
78
+ $ this ->configurations ->get (" signed-requests. $ requestKey . cache-prefix" ),
76
79
$ signed ->getId ()
77
80
);
78
81
79
- $ tolerance = $ this ->configurations ->get ($ prefix . ' signed-requests.request-replay.tolerance ' );
82
+ $ tolerance = $ this ->configurations ->get (" signed-requests. $ requestKey . request-replay.tolerance" );
80
83
81
- if (true !== $ this ->configurations ->get ($ prefix . ' signed-requests.request-replay.allow ' )) {
84
+ if (true !== $ this ->configurations ->get (" signed-requests. $ requestKey . request-replay.allow" )) {
82
85
$ isExpired = $ signed ->isExpired ($ tolerance );
83
86
84
87
if ($ isExpired || $ this ->cache ->has ($ key )) {
@@ -87,10 +90,10 @@ public function handle(Request $request, Closure $next, $prefix = '')
87
90
}
88
91
89
92
$ signed
90
- ->setSignatureHeader ($ this ->configurations ->get ($ prefix . ' signed-requests.headers.signature ' ))
91
- ->setAlgorithmHeader ($ this ->configurations ->get ($ prefix . ' signed-requests.headers.algorithm ' ));
93
+ ->setSignatureHeader ($ this ->configurations ->get (" signed-requests. $ requestKey . headers.signature" ))
94
+ ->setAlgorithmHeader ($ this ->configurations ->get (" signed-requests. $ requestKey . headers.algorithm" ));
92
95
93
- if (!$ signed ->isValid ($ this ->configurations ->get ($ prefix . ' signed-requests.key ' ))) {
96
+ if (!$ signed ->isValid ($ this ->configurations ->get (" signed-requests. $ requestKey . key" ))) {
94
97
throw new InvalidSignatureException ();
95
98
}
96
99
0 commit comments