Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Aug 14, 2015
1 parent b153682 commit 2aaeed9
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
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.
29 changes: 29 additions & 0 deletions composer.json
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/"
}
}
}
10 changes: 10 additions & 0 deletions src/Http/Controllers/Controller.php
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;
}
9 changes: 9 additions & 0 deletions src/Http/Controllers/LaravelCaffeineController.php
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';
}
}
38 changes: 38 additions & 0 deletions src/Http/Middleware/LaravelCaffeineDripMiddleware.php
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;
}
}
5 changes: 5 additions & 0 deletions src/Http/routes.php
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');
30 changes: 30 additions & 0 deletions src/LaravelCaffeinServiceProvider.php
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'];
}
}
5 changes: 5 additions & 0 deletions src/public/js/laravel-caffeine.js
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);
});

0 comments on commit 2aaeed9

Please sign in to comment.