Skip to content

Commit

Permalink
Update readme and exemples
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrousse committed Nov 4, 2015
1 parent 5590df2 commit 4e1d9bf
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
84 changes: 83 additions & 1 deletion README.md
Expand Up @@ -5,4 +5,86 @@

Secure your web ressources (files, images, streams, ...) with simple token system.

!!! En développement !!!
## Installation

Install the latest version with

```bash
$ composer require gbrousse/php-token-manager
```

## Basic usage

### get a token
```php
<?php

use TokenManager\TokenManager;

// Setup
$options = array(
'dir' => 'directory/where/stock/tokens',
'prefix' => 'prefix_of_tokens_files',
'salt' => 'salt',
'hash' => 'md5', // hash use to generate token
'maxTimeout' => 7200, //max lifetime for a token
'maxTimeout' => 600, //min lifetime for a token
);
$TokenMgr = new TokenManager($options);

// Get token
$token = $TokenMgr->get();

```
If you use a single configuration for the tokens, modify the attributes of the class instead of using options array.

### Verify a token
```php
<?php

use TokenManager\TokenManager;


// Setup
$options = array(
'dir' => 'directory/where/stock/tokens',
'prefix' => 'prefix_of_tokens_files',
'salt' => 'salt',
'hash' => 'md5', // hash use to generate token
'maxTimeout' => 7200, //max lifetime for a token
'maxTimeout' => 600, //min lifetime for a token
);
$TokenMgr = new TokenManager($options);

// Verify token validity
if($TokenMgr->isValid($token)){
// action to do if token is OK
}

```
If you use a single configuration for the tokens, modify the attributes of the class instead of using options array.


## Examples

- [Get a token](examples/usage-get.php)
- [Verify a token](examples/usage-isvalid.php)


## About

### Requirements

- php-token-manager works with PHP 5.3 or above.

### Submitting bugs and feature requests

Bugs and feature request are tracked on [GitHub](https://github.com/gbrousse/php-token-manager/issues)

### Author

Gregory Brousse - <pro@gregory-brousse.fr> - <http://gregory-brousse.fr>

### License

php-token-manager is licensed under the LGPL-3.0 License - see the `LICENSE` file for details
1 change: 1 addition & 0 deletions examples/test.txt
@@ -0,0 +1 @@
test
Empty file.
40 changes: 40 additions & 0 deletions examples/usage-get.php
@@ -0,0 +1,40 @@
<?php
require '../vendor/autoload.php';

use TokenManager\TokenManager;

// Setup
$options = array(
'dir' => './tokens/',
'prefix' => 'token',
'salt' => 'salt',
'hash' => 'sha256', // hash use to generate token
'maxTimeout' => 120, //max lifetime for a token
'maxTimeout' => 30, //min lifetime for a token
);
$TokenMgr = new TokenManager($options);

// Get token
$token = $TokenMgr->get();
?>

<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8">

<title>php-token-manager : get</title>
<meta name="description" content="php-token-manager : get">
<meta name="author" content="Gregory Brousse">
</head>

<body>
<a href="./usage-verify.php?token=<?php echo $token ?>">Test with right token</a><br/>
<a href="./usage-verify.php?token=123456">Test with wrong token</a><br/>
<a href="./usage-verify.php">Test with no token</a><br/>
<br/>
Last error : <?php echo $TokenMgr->getLastError(); ?>

</body>
</html>
29 changes: 29 additions & 0 deletions examples/usage-verify.php
@@ -0,0 +1,29 @@
<?php
require '../vendor/autoload.php';

use TokenManager\TokenManager;

if(isset($_GET['token'])){
// Setup
$options = array(
'dir' => './tokens/',
'prefix' => 'token',
'salt' => 'salt',
'hash' => 'sha256', // hash use to generate token
'maxTimeout' => '120', //max lifetime for a token
'maxTimeout' => '30', //min lifetime for a token
);
$TokenMgr = new TokenManager($options);

// Verify token
if($TokenMgr->isValid($_GET['token'])){
header('Content-type: text/txt');
header('Content-Disposition: attachment; filename="test.txt"');
echo file_get_contents('./test.txt');
}else{
echo 'Error token not valid';
}
}else{
echo 'Error token missing';
}
?>

0 comments on commit 4e1d9bf

Please sign in to comment.