Skip to content

Commit fbb8bb6

Browse files
committed
wip
1 parent 3a38897 commit fbb8bb6

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

src/Http/Controllers/MailgunController.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,12 @@
33
namespace BeyondCode\Mailbox\Http\Controllers;
44

55
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;
97

108
class MailgunController
119
{
12-
public function __invoke(Request $request)
10+
public function __invoke(MailgunRequest $request)
1311
{
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());
3513
}
3614
}

src/Http/Requests/MailgunRequest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

tests/Controllers/MailgunTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class MailgunTest extends TestCase
1111
public function it_verifies_mailgun_signatures()
1212
{
1313
$this->post('/laravel-mailbox/mailgun/mime', [
14+
'body-mime' => 'mime',
1415
'timestamp' => 1548104992,
16+
'token' => 'something',
1517
'signature' => 'something'
1618
])->assertStatus(401);
1719

@@ -23,6 +25,7 @@ public function it_verifies_mailgun_signatures()
2325
$validSignature = hash_hmac('sha256', $timestamp . $token, '12345');
2426

2527
$this->post('/laravel-mailbox/mailgun/mime', [
28+
'body-mime' => 'mime',
2629
'timestamp' => $timestamp,
2730
'token' => $token,
2831
'signature' => $validSignature
@@ -40,6 +43,7 @@ public function it_verifies_fresh_timestamps()
4043
$validSignature = hash_hmac('sha256', $timestamp . $token, '12345');
4144

4245
$this->post('/laravel-mailbox/mailgun/mime', [
46+
'body-mime' => 'mime',
4347
'timestamp' => $timestamp,
4448
'token' => $token,
4549
'signature' => $validSignature

0 commit comments

Comments
 (0)