PHP code example of tigerwill90 / xsrf-middleware
1. Go to this page and download the library: Download tigerwill90/xsrf-middleware library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tigerwill90 / xsrf-middleware example snippets
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([]));
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => "/api" /* or ["/api", "/admin"]*/
]));
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => ["/api", "/admin"],
"passthrough" => "/api/orders"
]));
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => ["/api", "/admin"],
"anticsrf" => "xCsrf"
]));
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => ["/api", "/admin"],
"token" => "jwt"
]));
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => ["/api", "/admin"],
"payload" => $container["decoded"]
]));
[
"uid" => 1,
"iat" => "1428819941",
"exp" => "1744352741",
"aud" => "www.example.com",
"roles" => [1,0,1,1,1],
"xsrf" => "thepseudorandomvaluegeneratedforbothcookieandtoken"
]
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => ["/api", "/admin"],
"claim" => "xsrf"
]));
use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Formatter\LineFormatter;
$app = new Slim\App
$logger = new Logger("slim");
$formatter = new LineFormatter(
"[%datetime%] [%level_name%]: %message% %context%\n",
null,
true,
true
);
$rotating = new RotatingFileHandler(__DIR__ . "/logs/xsrf.log", 0, Logger::DEBUG);
$rotating->setFormatter($formatter);
$logger->pushHandler($rotating);
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => ["/api", "/admin"],
"claim" => "xsrf",
"logger" => $logger
]));
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => ["/api", "/admin"],
"claim" => "xsrf",
"error" => function ($response, $arguments) {
$data["message"] = $arguments["message];
return $response
->withHeader("Content-Type", "application/json")
->write(json_encode($data));
}
]));
$app = new Slim\App
$app->add(new Tigerwill90\Middleware\XsrfProtection([
"path" => ["/api", "/admin"],
"payload" => $container["decoded"]
"msgpack" => true
]));
$container = $app->getContainer();
$container["XsrfProtection"] = function($c) {
function new \Tigerwill90\Middleware\XsrfProtection([
"path" => "/api",
"passthrough" => ["/api/users/signin", "/api/users/token"],
"anticsrf" => "xCsrf",
"token" => "jwt",
"claim" => "xsrf"
]);
};
$container["JwtAuthentication"] = function($c) {
return new \Slim\Middleware\JwtAuthentication([
"secure" => true,
"path" => "/api",
"passthrough" => ["/api/users/signin", "/api/users/token"],
"attribute" => "jwt",
"secret" => getenv("JWT_SECRET")
]);
};
$app->add("XsrfProtection");
$app->add("JwtAuthentication");