Download the PHP package mmdm/sim-csrf without Composer
On this page you can find all versions of the php package mmdm/sim-csrf. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mmdm/sim-csrf
More information about mmdm/sim-csrf
Files in mmdm/sim-csrf
Informations about the package sim-csrf
Simplicity CSRF
A library for CSRF token management.
Install
composer
Or you can simply download zip file from github and extract it, then put file to your project library and use it like other libraries.
Just add line below to autoload files:
and you are good to go.
How to use
__construct(ICsrfStorage $storage = null)
You can pass custom storage if you want. Just implement ICsrfStorage
class and pass it through constructor and you are good to go.
Go to Custom Csrf Storage section for more information about how to create custom storage.
Available functions
- setStorage(ICsrfStorage $storage): ICsrf
You can set storage even after construction with this method.
- getStorage(): ICsrfStorage
Get csrf storage.
- setExpiration(int $timeout): ICsrf
This method set expiration from now to a csrf token. Default expiration is 300 seconds.
Note: If you plan to get token continuously if a code snipped then you should specify expiration each time before getting field or token.
exp. If you don't specify expiration in any of them, it has no problem and all of them will be default expiration, but if you specify in one of them, you should speficy expiration after that in each of usage
No problem example:
Problematic example:
- getExpiration(): int
Get expiration time.
- extendExpiration(bool $answer = true)
By default it'll extend timeout of csrf if implemented in storage
class or if you want to prevent this behavior you can send false
to this method.
Important Note: If you set expiration to 7200 seconds(2 hours) and extend functionality is on, after each successful csrf validation, it'll extend the expiration. It means you have 3600 seconds left for example and a form has been submitted, now you have 7200 seconds again because you did not off extend functionality but if you turn this functionality off or you have a custom csrf stroge that did not implement any functionality in extend method, now if you have 3600 seconds expiration time and submit a form, you have 3600 seconds left and time not extended.
Added from v1.2.0
- getField(string $name = null, string $input_name = null): string
This method will return input with type hidden and value of token. The $name is an ID to generated token and $input_name is the name of hidden input.
- getToken(string $name = null): string
This method will return token value only.
Note: If we had token with $name before and it is valid, returns it.
- regenerateToken(string $name = null): string
This method unlike getToken, returns a new token every time.
- validate($token, $name = null): bool
This method validate a token.
- clear(): ICsrf
This method clears all generated token.
Custom Csrf Storage
Just implement ICsrfStorage
interface
Important Note: All $key
parameters has an extra prefix that is
concatenated with a dot to token's hashed name.
License
Under MIT license.
All versions of sim-csrf with dependencies
ext-openssl Version *