-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b153682
commit 2aaeed9
Showing
8 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Caffeine For Laravel 5.1 | ||
## Goal | ||
Prevent forms from timing out when submitting them after leaving them on-screen for a considerable amount of time. | ||
(Laravel defaults to 120 minutes, but that is configurable and could be different site-by-site.) | ||
|
||
## Implementation | ||
To achieve this, we are sending a caffeine-drip (a request at regular intervals) to keep the session from timing out. | ||
This is only implemented on pages with a `_token` field, so all other pages will time-out as normal. | ||
|
||
## Reasoning | ||
I chose this approach to keep the integrity of site-security, by avoiding the following: | ||
- exposing the CSRF Token on an unsecured endpoint. | ||
- eliminating CSRF Token validation on specific routes, or even altogether. | ||
- removing session-timeout on all pages. | ||
|
||
## Considerations | ||
This package adds the multiple routes under `genealabs/laravel-caffeine`. Please verify that these don't collide with your | ||
existing routes. | ||
|
||
## Installation | ||
1. Install MixPanel via composer: | ||
```sh | ||
composer require genealabs\laravel-caffeine:~0.1 | ||
``` | ||
|
||
2. Add the service provider entry in `config\app.php`: | ||
```php | ||
GeneaLabs\LaravelCaffeine\LaravelCaffeineServiceProvider::class, | ||
``` | ||
|
||
3. Publish the assets for this package: | ||
```sh | ||
php artisan vendor:publish --tag=genealabs-laravel-caffeine --force | ||
``` | ||
|
||
4. Register the middleware class in `app/Http/kernel.php`: | ||
```php | ||
protected $middleware = [ | ||
// other entries above | ||
LaravelCaffeineDripMiddleware::class, | ||
]; | ||
``` | ||
|
||
## Usage | ||
That was it! All you need to do now is load your pages, and it will apply itself automatically where it finds a form | ||
with a `_token` field. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "genealabs/laravel-caffeine", | ||
"description": "Package for Laravel 5.1 that keeps session alive, preventing timeout of forms.", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Mike Bronner", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"GeneaLabs\\LaravelCaffeine\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=5.5.0", | ||
"illuminate/support": "~5.1", | ||
"illuminate/routing": "^5.1" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "4.*" | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"GeneaLabs\\LaravelCaffein\\Tests\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php namespace GeneaLabs\LaravelCaffeine\Http\Controllers; | ||
|
||
use Illuminate\Foundation\Bus\DispatchesJobs; | ||
use Illuminate\Routing\Controller as BaseController; | ||
use Illuminate\Foundation\Validation\ValidatesRequests; | ||
|
||
abstract class Controller extends BaseController | ||
{ | ||
use DispatchesJobs, ValidatesRequests; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php namespace GeneaLabs\LaravelCaffeine\Http\Controllers; | ||
|
||
class LaravelCaffeineController extends Controller | ||
{ | ||
public function drip() | ||
{ | ||
return 'true'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php namespace GeneaLabs\LaravelCaffeine\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Routing\Middleware; | ||
|
||
class LaravelCaffeineDripMiddleware implements Middleware | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
$content = null; | ||
$response = $next($request); | ||
|
||
if (! method_exists($response, 'getOriginalContent')) { | ||
return $response; | ||
} | ||
|
||
$content = $response->getOriginalContent(); | ||
|
||
if (method_exists($content, 'render')) { | ||
$content = $content->render(); | ||
} | ||
|
||
if (is_string($content) && strpos($content, '_token')) { | ||
$content = str_replace('</body>', '<script src="' . asset('/genealabs/laravel-caffeine/js/laravel-caffeine.js') . '"></script></body>', $content); | ||
$response->setContent($content); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
use GeneaLabs\LaravelCaffeine\Http\Controllers\LaravelCaffeineController; | ||
|
||
Route::get('genealabs/laravel-caffeine/drip', LaravelCaffeineController::class . '@drip'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php namespace GeneaLabs\LaravelCaffeine; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LaravelCaffeineServiceProvider extends ServiceProvider | ||
{ | ||
protected $defer = false; | ||
|
||
public function boot() | ||
{ | ||
if (! $this->app->routesAreCached()) { | ||
require __DIR__ . '/Http/routes.php'; | ||
} | ||
|
||
$this->publishes([__DIR__ . '/public' => public_path('genealabs/laravel-caffeine')], 'genealabs-laravel-caffeine'); | ||
} | ||
|
||
public function register() | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function provides() | ||
{ | ||
return ['laravel-caffein']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$(document).ready(function () { | ||
setInterval(function () { | ||
$.get('/genealabs/laravel-caffeine/drip', function (result) {}); | ||
}, 60000); | ||
}); |