Skip to content

Commit

Permalink
Extend README
Browse files Browse the repository at this point in the history
  • Loading branch information
BMeu committed Sep 24, 2019
1 parent c14fd19 commit 8898a3c
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,117 @@ package manager.

## Usage

Flask-EasyJWT is used exactly as [EasyJWT](https://github.com/BMeu/EasyJWT). Therefore, this section only describes the
specific features of Flask-EasyJWT and the basic usage. For detailed explanations on how to use EasyJWT (for example,
optional claims, registered claims such as `aud`, `iat`, and `sub`, or verifying third-party tokens), see
[its documentation](https://easyjwt.readthedocs.org/en/latest/#usage).

### Application Setup

You do not need to initialize Flask-EasyJWT with your Flask application. All you have to do (although even this is,
strictly speaking, not required), is to specify some default settings for all of your tokens in the configuration of
your Flask application. These settings are:


| Configuration Key | Description |
|--------------------------|-------------|
| `EASYJWT_KEY` | The key that will be used for encoding and decoding all tokens. If `EASYJWT_KEY` is not specified, Flask-EasyJWT will fall back to Flask's `SECRET_KEY` configuration value. |
| `EASYJWT_TOKEN_VALIDITY` | The validity of each token after its creation. This value can be given as a string (that is parsable to an integer), an integer, or a `timedelta` object. The former two are interpreted in seconds. |

You can specify these configuration values as any other configuration values in your Flask application, for example,
using a mapping in your code:

```python
from datetime import timedelta
from flask import Flask

app = Flask(__name__)
app.config.update(
EASYJWT_KEY='Super secret key',
EASYJWT_TOKEN_VALIDITY=timedelta(minutes=7)
)
```

In this example, all tokens will (by default) be encoded using the (not so secure) string `Super secret key` and will
be valid for seven minutes after they have been created (i.e., after the `create()` method has been called on the token
object).

Of course, any other way of specifying the configuration values will work as well (see
[Flask's documentation](https://flask.palletsprojects.com/en/1.1.x/config/)).

### Token Specification & Usage

Tokens are specified and used exactly as with [EasyJWT](https://easyjwt.readthedocs.org/en/latest/#usage):

```python
from flask_easyjwt import FlaskEasyJWT

# Define the claims of your token.
class MySuperSimpleJWT(FlaskEasyJWT):

def __init__(self, key):
super().__init__(key)

# Define a claim `name`.
self.name = None

# Assuming we are within a Flask app context.

# Create a token with some values.
token_object = MySuperSimpleJWT()
token_object.name = 'Zaphod Beeblebrox'
token = token_object.create()

# Verify the created token.
verified_token_object = MySuperSimpleJWT.verify(token)
assert verified_token_object.name == 'Zaphod Beeblebrox'
```

The only difference is that you do not have to pass the key for encoding or decoding the token to the constructor and
`verify()` method, respectively (you still can do so if you do not want to use the default key defined in your
application's configuration).

Additionally, if the configuration value `EASYJWT_TOKEN_VALIDITY` is set, the token will
be valid for the amount specified in this configuration value after it has been created with `create()`. If this
configuration value is not set tokens will not expire. If you explicitly set the expiration date on a token object
this value will always take precedence (if it is not `None`):

```python
import datetime

from flask_easyjwt import FlaskEasyJWT
from flask import Flask

# Define the claims of your token.
class MySuperSimpleJWT(FlaskEasyJWT):

def __init__(self, key):
super().__init__(key)

# Define a claim `name`.
self.name = None

# Define the default configuration options for FlaskEasyJWT
# in the configuration of your Flask app.
app = Flask(__name__)
app.config.from_mapping(
EASYJWT_KEY='Super secret key',
EASYJWT_TOKEN_VALIDITY=datetime.timedelta(minutes=7)
)

# Assuming we are within a Flask app context.

token_object = MySuperSimpleJWT()
token_object.name = 'Zaphod Beeblebrox'

# This token will expire in 15 minutes, even though the default token validity is set to 7 minutes.
token_object.expiration_date = datetime.datetime.utcnow() + datetime.timedelta(minutes=15)
```

Initializing token objects and creating and verifying tokens must be executed within a
[Flask application context](https://flask.palletsprojects.com/en/1.1.x/appcontext/) if you want to use the configuration
values from the application's configuration.

## Acknowledgements

Flask-EasyJWT is just an easy-to-use abstraction layer around José Padilla's
Expand Down

0 comments on commit 8898a3c

Please sign in to comment.