Skip to content

Commit

Permalink
Changed documentation to the new middleware. Upgraded version to 0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
amitrip committed Dec 8, 2018
1 parent 5f2528d commit e1b268d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# HISTORY

## 0.1.4

Supports now starlette>=0.9.3 and uses the AuthenticationMiddleware.
This package must use starletter>=0.9.3

## 0.1.3

Release for Starlette 0.9.x
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ Register the Middleware with your app.

```python
from starlette.applications import Starlette
from starlette_jwt import JWTAuthenticationMiddleware, authentication_required, anonymous_allowed

from starlette_jwt import JWTAuthenticationBackend
from starlette.middleware.authentication import AuthenticationMiddleware

app = Starlette()
app.add_middleware(JWTAuthenticationMiddleware, secret_key='secret', prefix='JWT')
app.add_middleware(AuthenticationMiddleware, backend=JWTAuthenticationBackend(secret_key='secret', prefix='JWT'))

```

Access the JWT payload in a request,
Expand All @@ -42,17 +43,18 @@ The `@authentication_required` decorator will enforce the user to be logged in f
The default behavior is `@anonymous_allowed` so your code be explicit.

```python
from starlette.authentication import requires

def my_handler(request):
@app.route('/noauth')
@authentication_required
@requires('authenticated')
async def homepage(request):
return JSONResponse({'payload': request.session})
```

Not all handlers must be with authentication
```python
@app.route('/noauth')
@anonymous_allowed
async def homepage(request):
return JSONResponse({'payload': None})
```
Expand All @@ -63,15 +65,23 @@ async def homepage(request):

Store your secret key in this setting while creating the middleware:
```python
app.add_middleware(JWTAuthenticationMiddleware, secret_key='MY SECRET KEY')
app.add_middleware(AuthenticationMiddleware, backend=JWTAuthenticationBackend(secret_key='MY SECRET KEY'))
```

*prefix*

Change the Authorization header prefix string (defualts to "JWT"):
```python
# Example: changes the prefix to Bearer
app.add_middleware(JWTAuthenticationMiddleware, secret_key='secret', prefix='Bearer')
app.add_middleware(AuthenticationMiddleware, backend=JWTAuthenticationBackend(secret_key='secret', prefix='Bearer'))
```

*username_field*

The user name field in the JWT token payload:
```python
# Example: changes the username field to "user"
app.add_middleware(AuthenticationMiddleware, backend=JWTAuthenticationBackend(secret_key='secret', username_field='user'))
```

## Todo
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

setup(
name='starlette_jwt',
version='0.1.3',
version='0.1.4',
description="A JSON Web Token Middleware for Starlette",
long_description=readme,
author="Amit Ripshtos",
Expand Down

0 comments on commit e1b268d

Please sign in to comment.