Git Product home page Git Product logo

embryo-csrf's Introduction

Embryo CSRF

A PSR-15 compatible middleware to prevent CSRF. This middleware checks every POST, PATCH, PUT and DELETE requests for a CSRF token. The token is stored in session request attribute.

Requirements

Installation

Using Composer:

$ composer require davidecesarano/embryo-csrf

Usage

Add Embryo\CSRF\CsrfMiddleware to middleware dispatcher:

use Embryo\Http\Emitter\Emitter;
use Embryo\Http\Server\RequestHandler;
use Embryo\Http\Factory\{ServerRequestFactory, ResponseFactory};
use Embryo\CSRF\CsrfMiddleware;

$request    = (new ServerRequestFactory)->createServerRequestFromServer();
$response   = (new ResponseFactory)->createResponse();
$session    = new Session;
$middleware = new RequestHandler;
$emitter    = new Emitter;

// example: generate form input
class GenerateInputMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = $handler->handle($request);
        $session  = $request->getAttribute('session');
        $token    = $session->get('csrf_token');
        return $response->write('<input type="hidden" name="csrf_token" value="'.end($token).'">');
    }
}

// SessionMiddleware
$middleware->add(
    (new SessionMiddleware)
        ->setSession($session)
        ->setOptions([
            'use_cookies'      => false,
            'use_only_cookies' => true
        ])
);

// CsrfMiddleware
$middleware->add(CsrfMiddleware::class);

// GenerateInputMiddleware
$middleware->add(GenerateInputMiddleware::class);

$response = $middleware->dispatch($request, $response);

$emitter->emit($response);

You may quickly test this using the built-in PHP server going to http://localhost:8000.

$ cd example
$ php -S localhost:8000

Options

setSessionRequestAttribute(string $sessionRequestAttribute)

Set session request attribute. If it's not provided, use $request->getAttribute('session').

setFormInputName(string $formInputName)

Set the form input name. If it's not provided, use csrf_token.

setSessionKey(string $sessionKey)

Set the session key. If it's not provided, use $session->get('csrf_token').

setLimit(int $limit)

Set limit the number of token to store in the session. If it's not provided, is 5.

embryo-csrf's People

Contributors

davidecesarano avatar

Stargazers

hack-club avatar

Watchers

James Cloos avatar hack-club avatar  avatar

Forkers

ncou

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.