Skip to content

Commit 62098b6

Browse files
committed
Initial commit
0 parents  commit 62098b6

File tree

6 files changed

+830
-0
lines changed

6 files changed

+830
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/vendor
3+
composer.lock

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "winxaito/laravel-https",
3+
"description": "Redirects http requests to https according to the configuration",
4+
"type": "library",
5+
"license": "GNU GPL3",
6+
"authors": [
7+
{
8+
"name": "WinXaito",
9+
"email": "mail@winxaito.com"
10+
}
11+
],
12+
"require": {},
13+
"autoload": {
14+
"psr-4": {
15+
"WinXaito\\LaravelHttpsRedirect\\": "src/"
16+
}
17+
},
18+
"extra": {
19+
"laravel": {
20+
"providers": [
21+
"WinXaito\\LaravelHttpsRedirect\\LaravelHttpsServiceProvider"
22+
]
23+
}
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @package WinXaito/laravel-https
5+
* @author Kevin Vuilleumier <mail@winxaito.com>
6+
* @copyright 2018 Kevin Vuilleumier
7+
* @license https://www.gnu.org/licenses/gpl.txt GNU GPL3
8+
* @link https://github.com/WinXaito/laravel-https
9+
*/
10+
11+
namespace WinXaito\LaravelHttpsRedirect\Http\Middleware;
12+
13+
use Closure;
14+
15+
class HttpsRedirection{
16+
/**
17+
* Handle an incoming request.
18+
*
19+
* We check depending on the configuration whether or not we need
20+
* to redirect the user to https (Unless already in https)
21+
*
22+
* @param \Illuminate\Http\Request $request
23+
* @param \Closure $next
24+
* @return mixed
25+
*/
26+
public function handle($request, Closure $next){
27+
if(!$request->secure()) {
28+
if(
29+
config('https-redirection.https_force') ||
30+
(!config('https-redirection.https_prohibit') &&
31+
config('https-redirection.app_env') == 'production') ||
32+
(!config('https-redirection.https_prohibit') &&
33+
!config('https-redirection.app_debug'))
34+
){
35+
return redirect()->secure($request->getRequestUri());
36+
}
37+
}
38+
39+
return $next($request);
40+
}
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* @package WinXaito/laravel-https
5+
* @author Kevin Vuilleumier <mail@winxaito.com>
6+
* @copyright 2018 Kevin Vuilleumier
7+
* @license https://www.gnu.org/licenses/gpl.txt GNU GPL3
8+
* @link https://github.com/WinXaito/laravel-https
9+
*/
10+
11+
namespace WinXaito\LaravelHttpsRedirect;
12+
13+
use Illuminate\Support\ServiceProvider;
14+
15+
class LaravelHttpsServiceProvider extends ServiceProvider{
16+
/**
17+
* Indicates if loading of the provider is deferred.
18+
*
19+
* @var bool
20+
*/
21+
protected $defer = false;
22+
23+
/**
24+
* Bootstrap any application services.
25+
*
26+
* @return void
27+
*/
28+
public function boot(){
29+
//Publishes
30+
$this->publishes([
31+
__DIR__.'/config/installer.php' => config_path('https-redirection.php'),
32+
], 'config');
33+
34+
//Middleware
35+
$this->app['Illuminate\Contracts\Http\Kernel']->pushMiddleware('WinXaito\LaravelHttpsRedirect\Http\Middleware\HttpsRedirection');
36+
}
37+
}

src/config/https-redirection.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Force https
7+
|--------------------------------------------------------------------------
8+
|
9+
| When this value is true, https redirection will be occur in all cases
10+
|
11+
*/
12+
13+
'https_force' => env('HTTPS_FORCE', false),
14+
15+
/*
16+
|--------------------------------------------------------------------------
17+
| Prohibit https
18+
|--------------------------------------------------------------------------
19+
|
20+
| When this value is true, https redirection will never occur unless
21+
| "https_force" is true
22+
|
23+
*/
24+
25+
'https_prohibit' => env('HTTPS_PROHIBIT', false),
26+
27+
/*
28+
|--------------------------------------------------------------------------
29+
| Application environment
30+
|--------------------------------------------------------------------------
31+
|
32+
| When this value is "production", the redirection will be activated
33+
| unless "https_prohibit" is true
34+
|
35+
*/
36+
37+
'app_env' => env('APP_ENV', 'production'),
38+
39+
/*
40+
|--------------------------------------------------------------------------
41+
| Application debug
42+
|--------------------------------------------------------------------------
43+
|
44+
| When this value is false and "https_prohibit" is also false,
45+
| redirection takes place
46+
|
47+
*/
48+
49+
'app_debug' => env('APP_DEBUG', false),
50+
];

0 commit comments

Comments
 (0)