File tree Expand file tree Collapse file tree 3 files changed +55
-25
lines changed Expand file tree Collapse file tree 3 files changed +55
-25
lines changed Original file line number Diff line number Diff line change 3
3
namespace BeyondCode \Mailbox \Http \Controllers ;
4
4
5
5
use BeyondCode \Mailbox \Facades \Mailbox ;
6
- use BeyondCode \Mailbox \InboundEmail ;
7
- use Carbon \Carbon ;
8
- use Illuminate \Http \Request ;
6
+ use BeyondCode \Mailbox \Http \Requests \MailgunRequest ;
9
7
10
8
class MailgunController
11
9
{
12
- public function __invoke (Request $ request )
10
+ public function __invoke (MailgunRequest $ request )
13
11
{
14
- $ this ->authenticate ($ request );
15
-
16
- $ email = InboundEmail::fromMessage ($ request ->get ('body-mime ' ));
17
-
18
- Mailbox::callMailboxes ($ email );
19
- }
20
-
21
- protected function authenticate (Request $ request )
22
- {
23
- $ data = $ request ->timestamp .$ request ->token ;
24
-
25
- $ signature = hash_hmac ('sha256 ' , $ data , config ('mailbox.services.mailgun.key ' ));
26
-
27
- $ signed = hash_equals ($ request ->signature , $ signature );
28
-
29
- abort_unless ($ signed && $ this ->isFresh ($ request ->timestamp ), 401 , 'Invalid Mailgun signature or timestamp. ' );
30
- }
31
-
32
- protected function isFresh ($ timestamp ): bool
33
- {
34
- return now ()->subMinutes (2 )->lte (Carbon::createFromTimestamp ($ timestamp ));
12
+ Mailbox::callMailboxes ($ request ->email ());
35
13
}
36
14
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace BeyondCode \Mailbox \Http \Requests ;
4
+
5
+ use BeyondCode \Mailbox \InboundEmail ;
6
+ use Carbon \Carbon ;
7
+ use Illuminate \Foundation \Http \FormRequest ;
8
+ use Illuminate \Support \Facades \Validator ;
9
+
10
+ class MailgunRequest extends FormRequest
11
+ {
12
+ public function validator ()
13
+ {
14
+ $ validator = Validator::make ($ this ->all (), [
15
+ 'body-mime ' => 'required ' ,
16
+ 'timestamp ' => 'required ' ,
17
+ 'token ' => 'required ' ,
18
+ 'signature ' => 'required '
19
+ ]);
20
+
21
+ $ validator ->after (function ($ validator ) {
22
+ $ this ->verifySignature ();
23
+ });
24
+
25
+ return $ validator ;
26
+ }
27
+
28
+ public function email ()
29
+ {
30
+ return InboundEmail::fromMessage ($ this ->get ('body-mime ' ));
31
+ }
32
+
33
+ protected function verifySignature ()
34
+ {
35
+ $ data = $ this ->timestamp .$ this ->token ;
36
+
37
+ $ signature = hash_hmac ('sha256 ' , $ data , config ('mailbox.services.mailgun.key ' ));
38
+
39
+ $ signed = hash_equals ($ this ->signature , $ signature );
40
+
41
+ abort_unless ($ signed && $ this ->isFresh ($ this ->timestamp ), 401 , 'Invalid Mailgun signature or timestamp. ' );
42
+ }
43
+
44
+ protected function isFresh ($ timestamp ): bool
45
+ {
46
+ return now ()->subMinutes (2 )->lte (Carbon::createFromTimestamp ($ timestamp ));
47
+ }
48
+ }
Original file line number Diff line number Diff line change @@ -11,7 +11,9 @@ class MailgunTest extends TestCase
11
11
public function it_verifies_mailgun_signatures ()
12
12
{
13
13
$ this ->post ('/laravel-mailbox/mailgun/mime ' , [
14
+ 'body-mime ' => 'mime ' ,
14
15
'timestamp ' => 1548104992 ,
16
+ 'token ' => 'something ' ,
15
17
'signature ' => 'something '
16
18
])->assertStatus (401 );
17
19
@@ -23,6 +25,7 @@ public function it_verifies_mailgun_signatures()
23
25
$ validSignature = hash_hmac ('sha256 ' , $ timestamp . $ token , '12345 ' );
24
26
25
27
$ this ->post ('/laravel-mailbox/mailgun/mime ' , [
28
+ 'body-mime ' => 'mime ' ,
26
29
'timestamp ' => $ timestamp ,
27
30
'token ' => $ token ,
28
31
'signature ' => $ validSignature
@@ -40,6 +43,7 @@ public function it_verifies_fresh_timestamps()
40
43
$ validSignature = hash_hmac ('sha256 ' , $ timestamp . $ token , '12345 ' );
41
44
42
45
$ this ->post ('/laravel-mailbox/mailgun/mime ' , [
46
+ 'body-mime ' => 'mime ' ,
43
47
'timestamp ' => $ timestamp ,
44
48
'token ' => $ token ,
45
49
'signature ' => $ validSignature
You can’t perform that action at this time.
0 commit comments