Skip to content

Commit 7c29624

Browse files
authored
Merge pull request #1 from ByteFlick/v2.0.0-work
v2.0.0
2 parents aaa3e35 + 538b13c commit 7c29624

File tree

5 files changed

+81
-18
lines changed

5 files changed

+81
-18
lines changed

README.md

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You can install the package via composer:
2121
composer require byteflick/laravel-strict-domain
2222
```
2323

24-
You can publish the config file with:
24+
You can publish the config file with (Optional):
2525

2626
```bash
2727
php artisan vendor:publish --tag="laravel-strict-domain-config"
@@ -31,45 +31,85 @@ This is the contents of the published config file:
3131

3232
```php
3333
return [
34-
'domain' => env('APP_DOMAIN', 'http://localhost'),
34+
'domain' => env('APP_DOMAIN', 'localhost.com'),
3535
];
3636
```
3737

3838
## Usage
3939

4040
### Step 1: Configure the Environment
4141

42-
You need to add an environment variable called `APP_DOMAIN` to your localhost.
43-
The value of this variable is used for checking the desired domain name against the one in the incoming request.
42+
You need to add an environment variable called `APP_DOMAIN` to your `.env` file. The value of this variable is used
43+
for validating the incoming traffic.
4444

4545
```php
46-
APP_DOMAIN=localhost
46+
APP_DOMAIN=localhost.com
4747
```
4848

4949
### Step 2: Apply the Middleware
5050

51-
#### On Specific Routes Only
51+
#### 2.1 Redirecting External Traffic
5252

53-
You can add the middleware to individual routes or apply it as a route group as well.
53+
If you want to redirect incoming traffic to your application from other domain/hosts to your own then you can
54+
use `RedirectExternalTraffic` middleware. This is useful when you want to redirect all the traffic from `johndoe.com` (
55+
referrer domain) and other domains/hosts to `janedoe.com` (your designated domain).
5456

55-
#### Globally For Laravel 11
57+
##### On Specific Routes Only
5658

57-
Append the middleware to your default middlewares into your `bootstrap/app.php` via the code below.
59+
You can add the middleware to individual routes or apply it via a route group.
60+
61+
##### Globally For Laravel 11
62+
63+
Append the middleware to your default middlewares into your `bootstrap/app.php` via the code below to redirect all
64+
external traffic outside your designated host to your designated host.
5865

5966
```php
6067
->withMiddleware(function (Middleware $middleware) {
61-
$middleware->append(CheckDomain::class);
68+
$middleware->append(\ByteFlick\LaravelStrictDomain\Middlewares\RedirectExternalTraffic::class);
6269
})
6370
```
6471

65-
#### Globally For Laravel 10
72+
##### Globally For Laravel 10
6673

67-
Add the middleware to your middlewares into your `App\Http\Kernel.php` via the code below.
74+
Add the middleware to your default middlewares into your `App\Http\Kernel.php` via the code below to redirect all
75+
external traffic outside your designated host to your designated host.
6876

6977
```php
7078
protected $middleware = [
71-
\ByteFlick\LaravelStrictDomain\Middlewares\CheckDomain::class,
72-
];
79+
\ByteFlick\LaravelStrictDomain\Middlewares\RedirectExternalTraffic::class,
80+
];
81+
```
82+
83+
#### 2.2 Blocking External Traffic
84+
85+
If you want to block incoming traffic to your application from other domain/hosts to your own then you can
86+
use `BlockExternalTraffic` middleware. This is useful when you want to allow traffic from `janedoe.com` but
87+
block `johndoe.com` and others to your application.
88+
89+
##### On Specific Routes Only
90+
91+
You can add the middleware to individual routes or apply it via a route group.
92+
93+
##### Globally For Laravel 11
94+
95+
Append the middleware to your default middlewares into your `bootstrap/app.php` via the code below to block all
96+
external traffic outside your designated host.
97+
98+
```php
99+
->withMiddleware(function (Middleware $middleware) {
100+
$middleware->append(\ByteFlick\LaravelStrictDomain\Middlewares\BlockExternalTraffic::class);
101+
})
102+
```
103+
104+
##### Globally For Laravel 10
105+
106+
Add the middleware to your default middlewares into your `App\Http\Kernel.php` via the code below to block all
107+
external traffic outside your designated host.
108+
109+
```php
110+
protected $middleware = [
111+
\ByteFlick\LaravelStrictDomain\Middlewares\BlockExternalTraffic::class,
112+
];
73113
```
74114

75115
## Changelog
@@ -87,6 +127,7 @@ Please review [our security policy](../../security/policy) on how to report secu
87127
## Credits
88128

89129
- [ByteFlick](https://github.com/ByteFlick)
130+
- [ORPtech](https://orptech.com)
90131
- [All Contributors](../../contributors)
91132

92133
## License

config/strict-domain.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

33
return [
4-
'domain' => env('APP_DOMAIN', 'http://localhost'),
4+
'domain' => env('APP_DOMAIN', 'localhost.com'),
55
];

src/LaravelStrictDomainServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public function configurePackage(Package $package): void
1111
{
1212
$package
1313
->name('laravel-strict-domain')
14-
->hasConfigFile();
14+
->hasConfigFile(['strict-domain']);
1515
}
1616
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace ByteFlick\LaravelStrictDomain\Middlewares;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Validation\ValidationException;
8+
9+
class BlockExternalTraffic
10+
{
11+
public function handle(Request $request, Closure $next)
12+
{
13+
$response = $next($request);
14+
$domain = config('strict-domain.domain');
15+
16+
if ($request->getHttpHost() !== $domain) {
17+
throw ValidationException::withMessages([sprintf('Traffic outside %s host is not allowed.', $domain)]);
18+
}
19+
20+
return $response;
21+
}
22+
}

src/Middlewares/CheckDomain.php renamed to src/Middlewares/RedirectExternalTraffic.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
use Closure;
66
use Illuminate\Http\Request;
77

8-
class CheckDomain
8+
class RedirectExternalTraffic
99
{
1010
public function handle(Request $request, Closure $next)
1111
{
1212
$response = $next($request);
1313

1414
if ($request->getHttpHost() !== config('strict-domain.domain')) {
15-
return redirect(config('app.url'));
15+
return redirect(config('app.url').request()->path());
1616
}
1717

1818
return $response;

0 commit comments

Comments
 (0)