Skip to content

Commit eb79900

Browse files
committed
Readme
1 parent f8c130b commit eb79900

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

README.md

+28-7
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,41 @@ composer require binarcode/laravel-stateless-session
2727
1. Trigger session, make a GET request to: `/api/csrf-header`. This will return a header with the session key and an optional header with CSRF token `XSRF-TOKEN`.
2828
The header name could be configured in: `stateless.header`
2929

30-
2. Use this session key for every request you want to take care of the session.
30+
2. Use this header session key/value for every request you want to take care of the session.
3131

32-
3. If you want to benefit of the CSRF protection of your requests, you should add the follow middleware to your routes:
32+
3. If you want to benefit of the CSRF protection of your requests, you should add the follow middlewares to your routes:
3333
```php
34-
->middleware(Binarcode\LaravelStatelessSession\Http\Middleware\VerifyHeaderCsrfToken::class);
34+
use Binarcode\LaravelStatelessSession\Http\Middleware\StatelessStartSession;
35+
use Binarcode\LaravelStatelessSession\Http\Middleware\StatelessVerifyCsrfToken;
36+
37+
->middleware([
38+
StatelessStartSession::class,
39+
StatelessVerifyCsrfToken::class,
40+
]);
41+
```
42+
43+
You can create a middleware group in your Http\Kernel with these 2 routes as:
44+
45+
```php
46+
protected $middlewareGroups = [
47+
// ...
48+
'stateless.csrf' => [
49+
StatelessStartSession::class,
50+
StatelessVerifyCsrfToken::class,
51+
],
52+
// ...
53+
]
3554
```
3655

37-
Now the server will return 419 (Page expired code). Unless you send back a request header named: `X-CSRF-TOKEN` with the value received by the first GET request in the `XSRF-TOKEN` header.
56+
Now the server will return 419 (Page expired code).
57+
58+
Unless you send back a request header named: `X-CSRF-TOKEN` with the value received by the first GET request in the `XSRF-TOKEN` header.
3859

39-
That's it.
60+
Done.
4061

41-
At this point you have CSRF protection.
62+
- At this point you have CSRF protection.
4263

43-
And you can play with `SessionManager` and use the `session()` helper to store/get information (e.g. flash sessions).
64+
- And you can play with `SessionManager` and use the `session()` helper to store/get information (e.g. flash sessions).
4465

4566
## Config
4667

src/Http/Middleware/StartStatelessSession.php renamed to src/Http/Middleware/StatelessStartSession.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Illuminate\Session\Store;
1010
use Symfony\Component\HttpFoundation\Response;
1111

12-
class StartStatelessSession extends StartSession
12+
class StatelessStartSession extends StartSession
1313
{
1414
public function handle($request, Closure $next)
1515
{

src/Http/Middleware/VerifyHeaderCsrfToken.php renamed to src/Http/Middleware/StatelessVerifyCsrfToken.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
77
use Illuminate\Session\TokenMismatchException;
88

9-
class VerifyHeaderCsrfToken extends Middleware
9+
class StatelessVerifyCsrfToken extends Middleware
1010
{
1111
/**
1212
* Handle an incoming request.

src/LaravelStatelessSessionServiceProvider.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Binarcode\LaravelStatelessSession;
44

5-
use Binarcode\LaravelStatelessSession\Http\Middleware\StartStatelessSession;
6-
use Illuminate\Support\ServiceProvider;
7-
use Illuminate\Support\Facades\Route;
85
use Binarcode\LaravelStatelessSession\Http\Controllers\CsrfHeaderController;
6+
use Binarcode\LaravelStatelessSession\Http\Middleware\StatelessStartSession;
7+
use Binarcode\LaravelStatelessSession\Http\Middleware\StatelessVerifyCsrfToken;
8+
use Illuminate\Support\Facades\Route;
9+
use Illuminate\Support\ServiceProvider;
910

1011
class LaravelStatelessSessionServiceProvider extends ServiceProvider
1112
{
@@ -18,7 +19,7 @@ public function boot()
1819

1920
if ($this->app->runningInConsole()) {
2021
$this->publishes([
21-
__DIR__.'/../config/config.php' => config_path('stateless.php'),
22+
__DIR__ . '/../config/config.php' => config_path('stateless.php'),
2223
], 'config');
2324
}
2425
}
@@ -31,7 +32,7 @@ public function register()
3132
$this->registerSessionManager();
3233

3334
// Automatically apply the package configuration
34-
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'stateless');
35+
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'stateless');
3536

3637
$this->app->singleton('laravel-stateless-session', function () {
3738
return new LaravelStatelessSession;
@@ -59,8 +60,11 @@ protected function defineRoutes()
5960
Route::group(['prefix' => config('stateless.prefix', 'api')], function () {
6061
Route::get(
6162
'/csrf-header',
62-
CsrfHeaderController::class.'@show'
63-
)->middleware(StartStatelessSession::class);
63+
CsrfHeaderController::class . '@show'
64+
)->middleware([
65+
StatelessStartSession::class,
66+
StatelessVerifyCsrfToken::class,
67+
]);
6468
});
6569
}
6670
}

0 commit comments

Comments
 (0)