documentation
This commit is contained in:
16
docs/api.rst
16
docs/api.rst
@@ -44,6 +44,22 @@ User Sessions
|
||||
.. automodule:: microdot.session
|
||||
:members:
|
||||
|
||||
Authentication
|
||||
--------------
|
||||
|
||||
.. automodule:: microdot.auth
|
||||
:inherited-members:
|
||||
:special-members: __call__
|
||||
:members:
|
||||
|
||||
User Logins
|
||||
-----------
|
||||
|
||||
.. automodule:: microdot.login
|
||||
:inherited-members:
|
||||
:special-members: __call__
|
||||
:members:
|
||||
|
||||
Cross-Origin Resource Sharing (CORS)
|
||||
------------------------------------
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@ Microdot is a highly extensible web application framework. The extensions
|
||||
described in this section are maintained as part of the Microdot project in
|
||||
the same source code repository.
|
||||
|
||||
WebSocket Support
|
||||
~~~~~~~~~~~~~~~~~
|
||||
WebSocket
|
||||
~~~~~~~~~
|
||||
|
||||
.. list-table::
|
||||
:align: left
|
||||
@@ -39,8 +39,8 @@ Example::
|
||||
message = await ws.receive()
|
||||
await ws.send(message)
|
||||
|
||||
Server-Sent Events Support
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Server-Sent Events
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. list-table::
|
||||
:align: left
|
||||
@@ -78,8 +78,8 @@ Example::
|
||||
the SSE object. For bidirectional communication with the client, use the
|
||||
WebSocket extension.
|
||||
|
||||
Rendering Templates
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
Templates
|
||||
~~~~~~~~~
|
||||
|
||||
Many web applications use HTML templates for rendering content to clients.
|
||||
Microdot includes extensions to render templates with the
|
||||
@@ -202,8 +202,8 @@ must be used.
|
||||
.. note::
|
||||
The Jinja extension is not compatible with MicroPython.
|
||||
|
||||
Maintaining Secure User Sessions
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Secure User Sessions
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. list-table::
|
||||
:align: left
|
||||
@@ -270,6 +270,67 @@ The :func:`save() <microdot.session.SessionDict.save>` and
|
||||
:func:`delete() <microdot.session.SessionDict.delete>` methods are used to update
|
||||
and destroy the user session respectively.
|
||||
|
||||
Authentication
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
.. list-table::
|
||||
:align: left
|
||||
|
||||
* - Compatibility
|
||||
- | CPython & MicroPython
|
||||
|
||||
* - Required Microdot source files
|
||||
- | `auth.py <https://github.com/miguelgrinberg/microdot/tree/main/src/microdot/auth.py>`_
|
||||
|
||||
* - Required external dependencies
|
||||
- | None
|
||||
|
||||
* - Examples
|
||||
- | `basic_auth.py <https://github.com/miguelgrinberg/microdot/blob/main/examples/auth/basic_auth.py>`_
|
||||
| `token_auth.py <https://github.com/miguelgrinberg/microdot/blob/main/examples/auth/token_auth.py>`_
|
||||
|
||||
The authentication extension provides helper classes for two commonly used
|
||||
authentication patterns, described below.
|
||||
|
||||
Basic Authentication
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
`Basic Authentication <https://en.wikipedia.org/wiki/Basic_access_authentication>`_
|
||||
is a method of authentication that is part of the HTTP specification. It allows
|
||||
clients to authenticate to a server using a username and a password.
|
||||
|
||||
To use Basic Authentication, create an instance of the :class:`BasicAuth <microdot.auth.BasicAuth>`
|
||||
class::
|
||||
|
||||
from microdot.auth import BasicAuth
|
||||
|
||||
auth = BasicAuth(app)
|
||||
|
||||
Token Authentication
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
User Logins
|
||||
~~~~~~~~~~~
|
||||
|
||||
.. list-table::
|
||||
:align: left
|
||||
|
||||
* - Compatibility
|
||||
- | CPython & MicroPython
|
||||
|
||||
* - Required Microdot source files
|
||||
- | `login.py <https://github.com/miguelgrinberg/microdot/tree/main/src/microdot/auth.py>`_
|
||||
| `session.py <https://github.com/miguelgrinberg/microdot/tree/main/src/microdot/session.py>`_
|
||||
|
||||
* - Required external dependencies
|
||||
- | CPython: `PyJWT <https://pyjwt.readthedocs.io/>`_
|
||||
| MicroPython: `jwt.py <https://github.com/micropython/micropython-lib/blob/master/python-ecosys/pyjwt/jwt.py>`_,
|
||||
`hmac.py <https://github.com/micropython/micropython-lib/blob/master/python-stdlib/hmac/hmac.py>`_
|
||||
|
||||
* - Examples
|
||||
- | `login.py <https://github.com/miguelgrinberg/microdot/blob/main/examples/login/login.py>`_
|
||||
|
||||
|
||||
Cross-Origin Resource Sharing (CORS)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -305,8 +366,8 @@ Example::
|
||||
cors = CORS(app, allowed_origins=['https://example.com'],
|
||||
allow_credentials=True)
|
||||
|
||||
Testing with the Test Client
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Test Client
|
||||
~~~~~~~~~~~
|
||||
|
||||
.. list-table::
|
||||
:align: left
|
||||
@@ -342,8 +403,8 @@ Example::
|
||||
See the documentation for the :class:`TestClient <microdot.test_client.TestClient>`
|
||||
class for more details.
|
||||
|
||||
Deploying on a Production Web Server
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Production Deployments
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The ``Microdot`` class creates its own simple web server. This is enough for an
|
||||
application deployed with MicroPython, but when using CPython it may be useful
|
||||
|
||||
@@ -6,14 +6,25 @@ from microdot.microdot import urlencode, invoke_handler
|
||||
class BaseAuth:
|
||||
def __init__(self):
|
||||
self.auth_callback = None
|
||||
self.error_callback = lambda request: abort(401)
|
||||
self.error_callback = None
|
||||
|
||||
def __call__(self, f):
|
||||
"""Decorator to protect a route with authentication.
|
||||
|
||||
Microdot will only call the route if the authentication callback
|
||||
returns a valid user object, otherwise it will call the error
|
||||
callback."""
|
||||
An instance of this class must be used as a decorator on the routes
|
||||
that need to be protected. Example::
|
||||
|
||||
auth = BasicAuth() # or TokenAuth()
|
||||
|
||||
@app.route('/protected')
|
||||
@auth
|
||||
def protected(request):
|
||||
# ...
|
||||
|
||||
Routes that are decorated in this way will only be invoked if the
|
||||
authentication callback returned a valid user object, otherwise the
|
||||
error callback will be executed.
|
||||
"""
|
||||
async def wrapper(request, *args, **kwargs):
|
||||
auth = self._get_auth(request)
|
||||
if not auth:
|
||||
@@ -27,17 +38,16 @@ class BaseAuth:
|
||||
return wrapper
|
||||
|
||||
|
||||
class HTTPAuth(BaseAuth):
|
||||
def authenticate(self, f):
|
||||
"""Decorator to configure the authentication callback.
|
||||
class BasicAuth(BaseAuth):
|
||||
"""Basic Authentication.
|
||||
|
||||
Microdot calls the authentication callback to allow the application to
|
||||
check user credentials.
|
||||
"""
|
||||
self.auth_callback = f
|
||||
|
||||
|
||||
class BasicAuth(HTTPAuth):
|
||||
:param realm: The realm that is displayed when the user is prompted to
|
||||
authenticate in the browser.
|
||||
:param charset: The charset that is used to encode the realm.
|
||||
:param scheme: The authentication scheme. Defaults to 'Basic'.
|
||||
:param error_status: The error status code to return when authentication
|
||||
fails. Defaults to 401.
|
||||
"""
|
||||
def __init__(self, realm='Please login', charset='UTF-8', scheme='Basic',
|
||||
error_status=401):
|
||||
super().__init__()
|
||||
@@ -63,12 +73,38 @@ class BasicAuth(HTTPAuth):
|
||||
'WWW-Authenticate': '{} realm="{}", charset="{}"'.format(
|
||||
self.scheme, self.realm, self.charset)}
|
||||
|
||||
def authenticate(self, f):
|
||||
"""Decorator to configure the authentication callback.
|
||||
|
||||
class TokenAuth(HTTPAuth):
|
||||
def __init__(self, header='Authorization', scheme='Bearer'):
|
||||
This decorator must be used with a function that accepts the request
|
||||
object, a username and a password and returns a user object if the
|
||||
credentials are valid, or ``None`` if they are not. Example::
|
||||
|
||||
@auth.authenticate
|
||||
async def check_credentials(request, username, password):
|
||||
user = get_user(username)
|
||||
if user and user.check_password(password):
|
||||
return get_user(username)
|
||||
"""
|
||||
self.auth_callback = f
|
||||
|
||||
|
||||
class TokenAuth(BaseAuth):
|
||||
"""Token based authentication.
|
||||
|
||||
:param header: The name of the header that will contain the token. Defaults
|
||||
to 'Authorization'.
|
||||
:param scheme: The authentication scheme. Defaults to 'Bearer'.
|
||||
:param error_status: The error status code to return when authentication
|
||||
fails. Defaults to 401.
|
||||
"""
|
||||
def __init__(self, header='Authorization', scheme='Bearer',
|
||||
error_status=401):
|
||||
super().__init__()
|
||||
self.header = header
|
||||
self.scheme = scheme.lower()
|
||||
self.error_status = error_status
|
||||
self.error_callback = self.authentication_error
|
||||
|
||||
def _get_auth(self, request):
|
||||
auth = request.headers.get(self.header)
|
||||
@@ -83,6 +119,19 @@ class TokenAuth(HTTPAuth):
|
||||
else:
|
||||
return (auth,)
|
||||
|
||||
def authenticate(self, f):
|
||||
"""Decorator to configure the authentication callback.
|
||||
|
||||
This decorator must be used with a function that accepts the request
|
||||
object, a username and a password and returns a user object if the
|
||||
credentials are valid, or ``None`` if they are not. Example::
|
||||
|
||||
@auth.authenticate
|
||||
async def check_credentials(request, token):
|
||||
return get_user(token)
|
||||
"""
|
||||
self.auth_callback = f
|
||||
|
||||
def errorhandler(self, f):
|
||||
"""Decorator to configure the error callback.
|
||||
|
||||
@@ -92,144 +141,5 @@ class TokenAuth(HTTPAuth):
|
||||
"""
|
||||
self.error_callback = f
|
||||
|
||||
|
||||
class Login(BaseAuth):
|
||||
def __init__(self, login_url='/login'):
|
||||
super().__init__()
|
||||
self.login_url = login_url
|
||||
self.user_callback = None
|
||||
self.user_id_callback = None
|
||||
self.auth_callback = self._authenticate
|
||||
self.error_callback = self._redirect_to_login
|
||||
|
||||
def id_to_user(self, f):
|
||||
"""Decorator to configure the user callback.
|
||||
|
||||
Microdot calls the user callback to load the user object from the
|
||||
user ID stored in the user session.
|
||||
"""
|
||||
self.user_callback = f
|
||||
|
||||
def user_to_id(self, f):
|
||||
"""Decorator to configure the user ID callback.
|
||||
|
||||
Microdot calls the user ID callback to load the user ID from the
|
||||
user session.
|
||||
"""
|
||||
self.user_id_callback = f
|
||||
|
||||
def _get_session(self, request):
|
||||
return request.app._session.get(request)
|
||||
|
||||
def _update_remember_cookie(self, request, days, user_id=None):
|
||||
remember_payload = request.app._session.encode({
|
||||
'user_id': user_id,
|
||||
'days': days,
|
||||
'exp': time() + days * 24 * 60 * 60
|
||||
})
|
||||
|
||||
@request.after_request
|
||||
async def _set_remember_cookie(request, response):
|
||||
response.set_cookie('_remember', remember_payload,
|
||||
max_age=days * 24 * 60 * 60)
|
||||
print(response.headers)
|
||||
return response
|
||||
|
||||
def _get_auth(self, request):
|
||||
session = self._get_session(request)
|
||||
if session and '_user_id' in session:
|
||||
return (session['_user_id'],)
|
||||
if '_remember' in request.cookies:
|
||||
remember_payload = request.app._session.decode(
|
||||
request.cookies['_remember'])
|
||||
user_id = remember_payload.get('user_id')
|
||||
if user_id: # pragma: no branch
|
||||
self._update_remember_cookie(
|
||||
request, remember_payload.get('_days', 30), user_id)
|
||||
session['_user_id'] = user_id
|
||||
session['_fresh'] = False
|
||||
session.save()
|
||||
return (user_id,)
|
||||
|
||||
async def _authenticate(self, request, user_id):
|
||||
return await invoke_handler(self.user_callback, user_id)
|
||||
|
||||
async def _redirect_to_login(self, request):
|
||||
return '', 302, {'Location': self.login_url + '?next=' + urlencode(
|
||||
request.url)}
|
||||
|
||||
async def login_user(self, request, user, remember=False,
|
||||
redirect_url='/'):
|
||||
"""Log a user in.
|
||||
|
||||
:param request: the request object
|
||||
:param user: the user object
|
||||
:param remember: if the user's logged in state should be remembered
|
||||
with a cookie after the session ends. Set to the
|
||||
number of days the remember cookie should last, or to
|
||||
``True`` to use a default duration of 30 days.
|
||||
:param redirect_url: the URL to redirect to after login
|
||||
|
||||
This call marks the user as logged in by storing their user ID in the
|
||||
user session. The application must call this method to log a user in
|
||||
after their credentials have been validated.
|
||||
|
||||
The method returns a redirect response, either to the URL the user
|
||||
originally intended to visit, or if there is no original URL to the URL
|
||||
specified by the `redirect_url`.
|
||||
"""
|
||||
session = self._get_session(request)
|
||||
session['_user_id'] = await invoke_handler(self.user_id_callback, user)
|
||||
session['_fresh'] = True
|
||||
session.save()
|
||||
|
||||
if remember:
|
||||
days = 30 if remember is True else int(remember)
|
||||
self._update_remember_cookie(request, days, session['_user_id'])
|
||||
|
||||
next_url = request.args.get('next', redirect_url)
|
||||
if not next_url.startswith('/'):
|
||||
next_url = redirect_url
|
||||
return redirect(next_url)
|
||||
|
||||
async def logout_user(self, request):
|
||||
"""Log a user out.
|
||||
|
||||
:param request: the request object
|
||||
|
||||
This call removes information about the user's log in from the user
|
||||
session. If a remember cookie exists, it is removed as well.
|
||||
"""
|
||||
session = self._get_session(request)
|
||||
session.pop('_user_id', None)
|
||||
session.pop('_fresh', None)
|
||||
session.save()
|
||||
if '_remember' in request.cookies:
|
||||
self._update_remember_cookie(request, 0)
|
||||
|
||||
def __call__(self, f):
|
||||
"""Decorator to protect a route with authentication.
|
||||
|
||||
If the user is not logged in, Microdot will redirect to the login page
|
||||
first. The decorated route will only run after successful login by the
|
||||
user. If the user is already logged in, the route will run immediately.
|
||||
"""
|
||||
return super().__call__(f)
|
||||
|
||||
def fresh(self, f):
|
||||
"""Decorator to protect a route with "fresh" authentication.
|
||||
|
||||
This decorator prevents the route from running when the login session
|
||||
is not fresh. A fresh session is a session that has been created from
|
||||
direct user interaction with the login page, as opposite to a session
|
||||
that was restored from a "remember me" cookie.
|
||||
"""
|
||||
base_wrapper = super().__call__(f)
|
||||
|
||||
async def wrapper(request, *args, **kwargs):
|
||||
session = self._get_session(request)
|
||||
if session.get('_fresh'):
|
||||
return await base_wrapper(request, *args, **kwargs)
|
||||
return await invoke_handler(self.error_callback, request)
|
||||
|
||||
return wrapper
|
||||
def authentication_error(self, request):
|
||||
abort(self.error_status)
|
||||
|
||||
@@ -2,8 +2,7 @@ import asyncio
|
||||
import binascii
|
||||
import unittest
|
||||
from microdot import Microdot
|
||||
from microdot.auth import BasicAuth, TokenAuth, Login
|
||||
from microdot.session import Session
|
||||
from microdot.auth import BasicAuth, TokenAuth
|
||||
from microdot.test_client import TestClient
|
||||
|
||||
|
||||
@@ -124,141 +123,3 @@ class TestAuth(unittest.TestCase):
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.status_code, 403)
|
||||
self.assertEqual(res.json, {'status_code': 403})
|
||||
|
||||
def test_login_auth(self):
|
||||
app = Microdot()
|
||||
Session(app, secret_key='secret')
|
||||
login_auth = Login()
|
||||
|
||||
@login_auth.id_to_user
|
||||
def id_to_user(user_id):
|
||||
return {'id': int(user_id), 'name': f'user{user_id}'}
|
||||
|
||||
@login_auth.user_to_id
|
||||
def user_to_id(user):
|
||||
return str(user['id'])
|
||||
|
||||
@app.get('/')
|
||||
@login_auth
|
||||
def index(request):
|
||||
return request.g.current_user['name']
|
||||
|
||||
@app.post('/login')
|
||||
async def login(request):
|
||||
return await login_auth.login_user(
|
||||
request, {'id': 123, 'name': 'user123'})
|
||||
|
||||
@app.post('/logout')
|
||||
async def logout(request):
|
||||
await login_auth.logout_user(request)
|
||||
return 'ok'
|
||||
|
||||
client = TestClient(app)
|
||||
res = self._run(client.get('/?foo=bar'))
|
||||
self.assertEqual(res.status_code, 302)
|
||||
self.assertEqual(res.headers['Location'], '/login?next=/%3Ffoo%3Dbar')
|
||||
|
||||
res = self._run(client.post('/login?next=/%3Ffoo=bar'))
|
||||
self.assertEqual(res.status_code, 302)
|
||||
self.assertEqual(res.headers['Location'], '/?foo=bar')
|
||||
self.assertEqual(len(res.headers['Set-Cookie']), 1)
|
||||
self.assertIn('session', client.cookies)
|
||||
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.text, 'user123')
|
||||
|
||||
res = self._run(client.post('/logout'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.status_code, 302)
|
||||
|
||||
def test_login_auth_bad_redirect(self):
|
||||
app = Microdot()
|
||||
Session(app, secret_key='secret')
|
||||
login_auth = Login()
|
||||
|
||||
@login_auth.id_to_user
|
||||
def id_to_user(user_id):
|
||||
return user_id
|
||||
|
||||
@login_auth.user_to_id
|
||||
def user_to_id(user):
|
||||
return user
|
||||
|
||||
@app.get('/')
|
||||
@login_auth
|
||||
async def index(request):
|
||||
return 'ok'
|
||||
|
||||
@app.post('/login')
|
||||
async def login(request):
|
||||
return await login_auth.login_user(request, 'user')
|
||||
|
||||
client = TestClient(app)
|
||||
res = self._run(client.post('/login?next=http://example.com'))
|
||||
self.assertEqual(res.status_code, 302)
|
||||
self.assertEqual(res.headers['Location'], '/')
|
||||
|
||||
def test_login_remember(self):
|
||||
app = Microdot()
|
||||
Session(app, secret_key='secret')
|
||||
login_auth = Login()
|
||||
|
||||
@login_auth.id_to_user
|
||||
def id_to_user(user_id):
|
||||
return user_id
|
||||
|
||||
@login_auth.user_to_id
|
||||
def user_to_id(user):
|
||||
return user
|
||||
|
||||
@app.get('/')
|
||||
@login_auth
|
||||
def index(request):
|
||||
return request.g.current_user
|
||||
|
||||
@app.post('/login')
|
||||
async def login(request):
|
||||
return await login_auth.login_user(request, 'user', remember=True)
|
||||
|
||||
@app.post('/logout')
|
||||
async def logout(request):
|
||||
await login_auth.logout_user(request)
|
||||
return 'ok'
|
||||
|
||||
@app.get('/fresh')
|
||||
@login_auth.fresh
|
||||
async def fresh(request):
|
||||
return f'fresh {request.g.current_user}'
|
||||
|
||||
client = TestClient(app)
|
||||
res = self._run(client.post('/login?next=/%3Ffoo=bar'))
|
||||
self.assertEqual(res.status_code, 302)
|
||||
self.assertEqual(res.headers['Location'], '/?foo=bar')
|
||||
self.assertEqual(len(res.headers['Set-Cookie']), 2)
|
||||
self.assertIn('session', client.cookies)
|
||||
self.assertIn('_remember', client.cookies)
|
||||
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.text, 'user')
|
||||
res = self._run(client.get('/fresh'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.text, 'fresh user')
|
||||
|
||||
del client.cookies['session']
|
||||
print(client.cookies)
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
res = self._run(client.get('/fresh'))
|
||||
self.assertEqual(res.status_code, 302)
|
||||
self.assertEqual(res.headers['Location'], '/login?next=/fresh')
|
||||
|
||||
res = self._run(client.post('/logout'))
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertFalse('_remember' in client.cookies)
|
||||
|
||||
res = self._run(client.get('/'))
|
||||
self.assertEqual(res.status_code, 302)
|
||||
|
||||
Reference in New Issue
Block a user