├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── phpunit.xml ├── src └── ForceHttpsUrlScheme.php └── tests ├── ForceHttpsUrlSchemeTest.php └── Utils └── Helpers.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | vendor/ 3 | composer.lock -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - 7.0 8 | 9 | before_script: 10 | - composer self-update 11 | - composer install --prefer-source --no-interaction --dev 12 | 13 | script: 14 | - ./vendor/bin/phpunit 15 | 16 | notifications: 17 | hipchat: 18 | rooms: 19 | secure: l3Eqsq6idQLE1Mh9PVBer9ZmoevskL4Qvr5tMuJi3Nqq+48CgwcOcZqp3tQZdULxeBen1aHAfoMARzQLCf7GkHFN62qVfEaSy9mJJkvXQ3S3/LfJdgm7saJ1gtpLyHQbAka9QPrZP6hd8gQEryCyegZyxbGRGBkMnaJhybNPIKw= 20 | 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-force-https-url-scheme 2 | Force https url schema middleware for Laravel 5 3 | 4 | [![Build Status](https://travis-ci.org/shin1x1/laravel-force-https-url-scheme.svg?branch=travis)](https://travis-ci.org/shin1x1/laravel-force-https-url-scheme) 5 | [![Latest Stable Version](https://poser.pugx.org/shin1x1/laravel-force-https-url-scheme/version.svg)](https://packagist.org/packages/shin1x1/laravel-force-https-url-scheme) 6 | 7 | ## Install 8 | 9 | ``` 10 | $ composer require shin1x1/laravel-force-https-url-scheme 11 | ``` 12 | 13 | ## Usage 14 | 15 | This package provide to redirect http to https. It's implemented `Illuminate\Contracts\Routing\Middleware` interface that means you can use it as Laravel middleware. This feature is enabled in `production` environments only. 16 | 17 | ### As global HTTP middleware 18 | 19 | * app/Http/Kernel.php 20 | 21 | ``` 22 | 'App\Http\Middleware\Authenticate', 65 | 'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth', 66 | 'guest' => 'App\Http\Middleware\RedirectIfAuthenticated', 67 | 'force_https_url_scheme' => 'Shin1x1\ForceHttpsUrlScheme\ForceHttpsUrlScheme', // <---added 68 | ]; 69 | } 70 | ``` 71 | 72 | * app/Http/routes.php 73 | 74 | ``` 75 | Route::group(['middleware' => 'force_https_url_scheme'], function () { 76 | get('/admin/', function () { 77 | // something here 78 | }); 79 | }); 80 | ``` 81 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shin1x1/laravel-force-https-url-scheme", 3 | "description": "Force https url schema middleware for Laravel 5", 4 | "keywords": ["Laravel", "middleware"], 5 | "homepage": "https://github.com/shin1x1/laravel-force-https-url-scheme", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Masashi Shinbara", 10 | "email": "shin1x1@gmail.com", 11 | "homepage": "https://github.com/shin1x1" 12 | } 13 | ], 14 | "require": { 15 | "php": ">5.4.0", 16 | "illuminate/container": "~5.0", 17 | "illuminate/http": "~5.0" 18 | }, 19 | "require-dev": { 20 | "phpunit/phpunit": "@stable", 21 | "mockery/mockery": "dev-master" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "Shin1x1\\ForceHttpsUrlScheme\\": "src", 26 | "Shin1x1\\ForceHttpsUrlScheme\\Test\\": "tests" 27 | } 28 | }, 29 | "minimum-stability": "stable" 30 | } 31 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/ForceHttpsUrlScheme.php: -------------------------------------------------------------------------------- 1 | app = $app; 25 | } 26 | 27 | /** 28 | * Handle an incoming request. 29 | * 30 | * @param \Illuminate\Http\Request $request 31 | * @param \Closure $next 32 | * @return mixed 33 | */ 34 | public function handle($request, Closure $next) 35 | { 36 | if ($this->app->environment() === 'production') { 37 | // for Proxies 38 | Request::setTrustedProxies([$request->getClientIp()]); 39 | if (!$request->isSecure()) { 40 | return redirect()->secure($request->getRequestUri()); 41 | } 42 | } 43 | 44 | return $next($request); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/ForceHttpsUrlSchemeTest.php: -------------------------------------------------------------------------------- 1 | app = Mockery::mock('Illuminate\Contracts\Foundation\Application'); 33 | $this->app->shouldReceive('environment')->andReturn('production'); 34 | 35 | $this->sut = new ForceHttpsUrlScheme($this->app); 36 | Helpers::$requestUri = '/path'; 37 | } 38 | 39 | /** 40 | * @test 41 | * @dataProvider getDataFor_handle_redirect 42 | */ 43 | public function handle_redirect($headers) 44 | { 45 | $request = Mockery::mock('Illuminate\Http\Request[getClientIp, getRequestUri]', [[], [], [], [], [], $headers]); 46 | $request->shouldReceive('getClientIp')->andReturn('127.0.0.1'); 47 | $request->shouldReceive('getRequestUri')->andReturn('/path'); 48 | 49 | $next = function () { 50 | throw new Exception(); 51 | }; 52 | 53 | $this->sut->handle($request, $next); 54 | } 55 | 56 | /** 57 | * 58 | */ 59 | public function getDataFor_handle_redirect() 60 | { 61 | return [ 62 | [[]], 63 | [['HTTP_X_FORWARDED_PROTO' => 'http', 'REMOTE_ADDR' => '127.0.0.1']], 64 | ]; 65 | } 66 | 67 | /** 68 | * @test 69 | * @dataProvider getDataFor_handle_no_redirect 70 | * @param array $headers 71 | */ 72 | public function handle_no_redirect(array $headers) 73 | { 74 | $request = Mockery::mock('Illuminate\Http\Request[[getClientIp, getRequestUri]', [[], [], [], [], [], $headers]); 75 | $request->shouldReceive('getClientIp')->andReturn('127.0.0.1'); 76 | $request->shouldReceive('getRequestUri')->andThrow(new Exception()); 77 | 78 | $next = function () { 79 | $this->assertTrue(true); 80 | }; 81 | 82 | $this->sut->handle($request, $next); 83 | } 84 | 85 | /** 86 | * 87 | */ 88 | public function getDataFor_handle_no_redirect() 89 | { 90 | return [ 91 | [['HTTPS' => 'on']], 92 | [['HTTP_X_FORWARDED_PROTO' => 'https', 'REMOTE_ADDR' => '127.0.0.1']], 93 | ]; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /tests/Utils/Helpers.php: -------------------------------------------------------------------------------- 1 | shouldReceive('secure')->with(Helpers::$requestUri)->andReturn(); 25 | 26 | return $mock; 27 | } 28 | } 29 | 30 | 31 | --------------------------------------------------------------------------------