Skip to content

Commit

Permalink
docs: update config and example
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpy committed Dec 19, 2018
1 parent 04867d0 commit 9dd3434
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,34 @@ exports.middleware = ['token'];

exports.token = {
type: 'md5',
secret: 'your-magic-secret-key'

apps: {
felixpy: {
secret: 'XnMib79vzwP01gtr',
expires: 30000
},
codetrial: {
secret: 'mi9yNGT6zwrqMv8z',
expires: 30000
}
}
};
```

`type` is the algorithm that can be used to generate hash digests.

See [crypto.createHash](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options) for more detail.

Each key of `apps` is the application's code, `secret` is used to generate token and `expires` is the validity period of the token.

The way to generate tokens is as follows:

```js
const ts = Date.now();
const md5Value = md5(`${APP_CODE}:${ts}:${APP_SECRET}`);
const token = base64Encode(`${APP_CODE}:${ts}:${md5Value}`);
```

## Example

This is an example of using axios to request an api:
Expand All @@ -52,7 +72,7 @@ const axios = require('axios');
const hash = crypto.createHash('md5');

const APP_CODE = 'felixpy';
const APP_SECRET = 'your-magic-secret-key';
const APP_SECRET = 'XnMib79vzwP01gtr';

const ts = Date.now();
const md5Value = hash.update(`${APP_CODE}:${ts}:${APP_SECRET}`).digest('hex');
Expand Down

0 comments on commit 9dd3434

Please sign in to comment.