Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 38bf10d3d2 | |||
| cb9c6339e5 | |||
| 264a9ad369 | |||
| ff41a72222 | |||
| 8daae3a673 | |||
| 004871c495 | |||
| 6d9ebbb264 | |||
| e136ab0101 | |||
| 51668878df | |||
| 45b8147b0a | |||
| b52e896ef0 | |||
| 4012836eee | |||
| ad2a1db6c5 | |||
| f05c49105e | |||
| 0f9ba5052c | |||
| fb5678afbb |
@@ -0,0 +1,19 @@
|
|||||||
|
### 1.0.3 2021-08-31
|
||||||
|
|
||||||
|
- Migrated to aioredis 2
|
||||||
|
- SameSite support https://github.com/sanderfoobar/quart-session/commit/8daae3a6734e8f7da13954d5a1a5da8f5fc5a49a
|
||||||
|
- Memcached stuff https://github.com/filak/quart-session/commit/004871c495a069784e57e604b69f65af1b7e645a
|
||||||
|
|
||||||
|
### 1.0.0 2020-01-15
|
||||||
|
|
||||||
|
- Added support for arbitrary usage of caching backends.
|
||||||
|
- Exposed `get`, `set`, `delete` on the session interface for direct usage.
|
||||||
|
- Renamed `SESSION_HIJACK_REVERSE_PROXY` to `SESSION_REVERSE_PROXY`.
|
||||||
|
- Renamed `SESSION_HIJACK_PROTECTION` to `SESSION_PROTECTION`.
|
||||||
|
- Removed fallback when `X-Forwarded-For` is not present whilst USING `SESSION_REVERSE_PROXY`, emit error instead.
|
||||||
|
- Fixed a bug where session timeouts would default to 600 seconds.
|
||||||
|
- Deprecated/disabled the `dirty()` method.
|
||||||
|
|
||||||
|
### 0.0.1 2020-01-04
|
||||||
|
|
||||||
|
- Released initial pre alpha version.
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
Copyright (c) 2014 by Shipeng Feng.
|
Copyright (c) 2014 by Shipeng Feng.
|
||||||
Copyright (c) 2020 by dsc.
|
Copyright (c) 2020 by Sander.
|
||||||
|
|
||||||
Some rights reserved.
|
Some rights reserved.
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
include LICENSE
|
||||||
|
include CHANGELOG.md
|
||||||
|
include README.md
|
||||||
|
include setup.cfg
|
||||||
|
recursive-include quart_session *.py
|
||||||
|
recursive-include quart_session *.md
|
||||||
|
exclude .gitlab-ci.yml
|
||||||
|
exclude examples
|
||||||
|
exclude docs
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
# Quart-session
|
# Quart-Session
|
||||||
|
|
||||||
Quart-Session is an extension for Quart that adds support for
|
 [](https://pypi.org/project/Quart-Session/) 
|
||||||
|
|
||||||
|
Quart-Session is an extension for [Quart](https://gitlab.com/pgjones/quart/blob/master/README.rst) that adds support for
|
||||||
server-side sessions to your application.
|
server-side sessions to your application.
|
||||||
|
|
||||||
Based on [flask-session](https://pypi.org/project/Flask-Session/).
|
Based on [flask-session](https://pypi.org/project/Flask-Session/).
|
||||||
|
|
||||||
## Quick start
|
## Quick start
|
||||||
|
|
||||||
Quart-Session can be installed via pipenv or
|
Quart-Session can be installed via pipenv or pip,
|
||||||
pip,
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ pipenv install quart-session
|
$ pipenv install quart-session
|
||||||
@@ -55,13 +56,13 @@ app.config['SESSION_TYPE'] = 'redis'
|
|||||||
|
|
||||||
@app.before_serving
|
@app.before_serving
|
||||||
async def setup():
|
async def setup():
|
||||||
cache = await aioredis.create_redis_pool({"address": "..."})
|
cache = await aioredis.create_redis_pool(...)
|
||||||
app.config['SESSION_REDIS'] = cache
|
app.config['SESSION_REDIS'] = cache
|
||||||
Session(app)
|
Session(app)
|
||||||
```
|
```
|
||||||
|
|
||||||
By default, Quart-session creates a single connection to Redis, while
|
By default, Quart-session creates a single connection to Redis, while
|
||||||
the example above creates a connection pool.
|
the example above sets up a connection pool.
|
||||||
|
|
||||||
#### Trio support
|
#### Trio support
|
||||||
|
|
||||||
@@ -89,8 +90,8 @@ Session(app)
|
|||||||
### JSON serializer
|
### JSON serializer
|
||||||
|
|
||||||
[flask-session](https://pypi.org/project/Flask-Session/) uses `pickle`
|
[flask-session](https://pypi.org/project/Flask-Session/) uses `pickle`
|
||||||
for session data, Quart-Session opts for a JSON serializer capable of
|
for session data while Quart-Session uses [a JSON serializer](https://gitlab.com/pgjones/quart/blob/37e249b9b146824a8668eaa1daa12392aeb00256/src/quart/json/tag.py#L141)
|
||||||
(de)serializing the usual JSON types, as well as: `Tuple`, `Bytes`,
|
capable of serializing the usual JSON types, as well as: `Tuple`, `Bytes`,
|
||||||
`Markup`, `UUID`, and `DateTime`.
|
`Markup`, `UUID`, and `DateTime`.
|
||||||
|
|
||||||
JSON as session data allows for greater interoperability with other
|
JSON as session data allows for greater interoperability with other
|
||||||
@@ -112,86 +113,69 @@ except ImportError:
|
|||||||
app.session_interface.serialize = pickle
|
app.session_interface.serialize = pickle
|
||||||
```
|
```
|
||||||
|
|
||||||
### Session control
|
### Back-end usage
|
||||||
|
|
||||||
By default, [flask-session](https://pypi.org/project/Flask-Session/) sets a
|
At any point you may interface with the session back-end directly:
|
||||||
|
|
||||||
|
```python3
|
||||||
|
@app.route("/")
|
||||||
|
async def hello():
|
||||||
|
cache = app.session_interface
|
||||||
|
await cache.set("random_key", "val", expiry=3600)
|
||||||
|
data = await cache.get("random_key")
|
||||||
|
```
|
||||||
|
|
||||||
|
The interface will have the `get`, `set`, and `delete` methods available (regardless of
|
||||||
|
back-end - similar to how [aiocache](https://github.com/argaen/aiocache) works).
|
||||||
|
|
||||||
|
### Performance
|
||||||
|
|
||||||
|
[flask-session](https://pypi.org/project/Flask-Session/) sets a
|
||||||
session for each incoming request, including static files. From experience,
|
session for each incoming request, including static files. From experience,
|
||||||
this approach can put unneeded load on underlying session infrastructure,
|
this often puts unneeded load on underlying session infrastructure,
|
||||||
especially in high-traffic environments.
|
especially in high-traffic environments.
|
||||||
|
|
||||||
Quart-Session offers control over the session creation. For example, often you'll only need to create a session when
|
Quart-Session only contacts the back-end when a session changed (or created). In addition,
|
||||||
a user successfully logs in.
|
static file serves never emit a `Set-Cookie` header. If you'd like to enable
|
||||||
|
this though, set `SESSION_STATIC_FILE` to `True`.
|
||||||
|
|
||||||
To enable this behaviour, set `SESSION_EXPLICIT` to `True`.
|
|
||||||
|
### Session pinning
|
||||||
|
|
||||||
|
Associates an user's session to his/her IP address. This mitigates cookie stealing via XSS etc, and is handy
|
||||||
|
for web applications that require extra security.
|
||||||
|
|
||||||
```python3
|
```python3
|
||||||
app = Quart(__name__)
|
app = Quart(__name__)
|
||||||
app.config['SESSION_TYPE'] = 'redis'
|
app.config['SESSION_TYPE'] = 'redis'
|
||||||
app.config['SESSION_EXPLICIT'] = True
|
app.config['SESSION_PROTECTION'] = True
|
||||||
Session(app)
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
async def root():
|
|
||||||
if session.get('authenticated'):
|
|
||||||
return "Welcome back!"
|
|
||||||
return "Welcome anonymous!"
|
|
||||||
|
|
||||||
@app.route('/login')
|
|
||||||
async def login():
|
|
||||||
session["authenticated"] = True
|
|
||||||
session.dirty() # mark session for saving
|
|
||||||
return 'Logged in!'
|
|
||||||
|
|
||||||
app.run()
|
|
||||||
```
|
|
||||||
|
|
||||||
To re-gain the old behaviour of always emitting a `Set-Cookie` header on static file serves,
|
|
||||||
set `SESSION_STATIC_FILE` to `True`.
|
|
||||||
|
|
||||||
|
|
||||||
### Session hijack prevention
|
|
||||||
|
|
||||||
(Optionally) pins an user's session to his/her IP address. This mitigates cookie stealing via XSS etc, and is handy
|
|
||||||
for paranoid web applications.
|
|
||||||
|
|
||||||
```python3
|
|
||||||
app = Quart(__name__)
|
|
||||||
app.config['SESSION_TYPE'] = 'redis'
|
|
||||||
app.config['SESSION_HIJACK_PROTECTION'] = True
|
|
||||||
Session(app)
|
Session(app)
|
||||||
```
|
```
|
||||||
|
|
||||||
With this option, session reuse from a different IP will result in the
|
Session reuse from a different IP will now result in the creation of a new session, and the deletion of the old.
|
||||||
creation of a new session, and the deletion of the old.
|
|
||||||
|
|
||||||
**Important:** If your application is behind a reverse proxy, it most
|
**Important:** If your application is behind a reverse proxy, it most
|
||||||
likely provides the `X-Forwarded-For` header which you **must** make use of
|
likely provides the `X-Forwarded-For` header which you **must** make use of
|
||||||
by explicitly setting `SESSION_HIJACK_REVERSE_PROXY` to `True`.
|
by explicitly setting `SESSION_REVERSE_PROXY` to `True`.
|
||||||
|
|
||||||
## Future development
|
## Future development
|
||||||
|
|
||||||
The following session interfaces would be nice to have:
|
|
||||||
|
|
||||||
- `MongoDBSessionInterface`
|
- `MongoDBSessionInterface`
|
||||||
- `FileSystemSessionInterface`
|
- `FileSystemSessionInterface`
|
||||||
- `GoogleCloudDatastoreSessionInterface`
|
- `GoogleCloudDatastoreSessionInterface`
|
||||||
|
- Pytest
|
||||||
|
|
||||||
Other to-do's:
|
## Flask-Session
|
||||||
|
|
||||||
- Unit testing
|
|
||||||
- Documentation (Sphinx)
|
|
||||||
|
|
||||||
## Migrating from Flask
|
|
||||||
|
|
||||||
This library works very similarly to [flask-session](https://pypi.org/project/Flask-Session/).
|
This library works very similarly to [flask-session](https://pypi.org/project/Flask-Session/).
|
||||||
The `quart_session.sessions` APIs are not 100% the same, but unless you
|
The changes are specified below:
|
||||||
are embedded in Flask-Session's internals, a migration should be fairly
|
|
||||||
straightforward. The distinct changes are specified below:
|
|
||||||
|
|
||||||
- Quart-Session does not `Set-Cookie` on (static) files by default.
|
- Quart-Session does not emit a `Set-Cookie` on every request.
|
||||||
- Quart-Session might not have all the back-end interfaces implemented (yet), such as "filesystem".
|
- Quart-Session does not emit a `Set-Cookie` on static file serves.
|
||||||
- Quart-Session uses a different serializer: `quart.json.tag.TaggedJSONSerializer` instead of `pickle`.
|
- Quart-Session uses a different serializer: `quart.json.tag.TaggedJSONSerializer` instead of `pickle`.
|
||||||
- Quart-Session disallows the client to supply their own made up `sid` cookie value.
|
- Quart-Session disallows the client to supply their own made up `sid` cookie value.
|
||||||
|
- Quart-Session can do session protection.
|
||||||
|
- Quart-Session might not have all the back-end interfaces implemented (yet), such as "filesystem".
|
||||||
|
|
||||||
## Help
|
## Help
|
||||||
|
|
||||||
@@ -199,4 +183,4 @@ Find the Quart folk on [gitter](https://gitter.im/python-quart/lobby) or open an
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
BSD
|
BSD
|
||||||
|
|||||||
+3
-3
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
Quart-Session demo.
|
Quart-Session demo.
|
||||||
|
|
||||||
:copyright: (c) 2020 by dsc.
|
:copyright: (c) 2020 by Sander.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
from quart import Quart, session
|
from quart import Quart, session
|
||||||
@@ -21,13 +21,13 @@ Session(app)
|
|||||||
|
|
||||||
|
|
||||||
@app.route('/set/')
|
@app.route('/set/')
|
||||||
def set():
|
async def set():
|
||||||
session['key'] = 'value'
|
session['key'] = 'value'
|
||||||
return 'ok'
|
return 'ok'
|
||||||
|
|
||||||
|
|
||||||
@app.route('/get/')
|
@app.route('/get/')
|
||||||
def get():
|
async def get():
|
||||||
return session.get('key', 'not set')
|
return session.get('key', 'not set')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,15 +6,13 @@
|
|||||||
Adds server session support to your application.
|
Adds server session support to your application.
|
||||||
|
|
||||||
:copyright: (c) 2014 by Shipeng Feng.
|
:copyright: (c) 2014 by Shipeng Feng.
|
||||||
:copyright: (c) 2020 by dsc.
|
:copyright: (c) 2020 by Sander.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = '0.0.1'
|
__version__ = '1.0.3'
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sniffio
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from quart import Quart
|
from quart import Quart
|
||||||
|
|
||||||
@@ -80,8 +78,8 @@ class Session(object):
|
|||||||
config.setdefault('SESSION_PERMANENT', True)
|
config.setdefault('SESSION_PERMANENT', True)
|
||||||
config.setdefault('SESSION_USE_SIGNER', False)
|
config.setdefault('SESSION_USE_SIGNER', False)
|
||||||
config.setdefault('SESSION_KEY_PREFIX', 'session:')
|
config.setdefault('SESSION_KEY_PREFIX', 'session:')
|
||||||
config.setdefault('SESSION_HIJACK_PROTECTION', False)
|
config.setdefault('SESSION_PROTECTION', False)
|
||||||
config.setdefault('SESSION_HIJACK_REVERSE_PROXY', False)
|
config.setdefault('SESSION_REVERSE_PROXY', False)
|
||||||
config.setdefault('SESSION_STATIC_FILE', False)
|
config.setdefault('SESSION_STATIC_FILE', False)
|
||||||
config.setdefault('SESSION_EXPLICIT', False)
|
config.setdefault('SESSION_EXPLICIT', False)
|
||||||
config.setdefault('SESSION_REDIS', None)
|
config.setdefault('SESSION_REDIS', None)
|
||||||
@@ -92,6 +90,18 @@ class Session(object):
|
|||||||
config.setdefault('SESSION_FILE_MODE', 384)
|
config.setdefault('SESSION_FILE_MODE', 384)
|
||||||
config = {k: v for k, v in config.items() if k.startswith('SESSION_')}
|
config = {k: v for k, v in config.items() if k.startswith('SESSION_')}
|
||||||
|
|
||||||
|
if isinstance(config.get("SESSION_HIJACK_PROTECTION"), bool):
|
||||||
|
app.logger.warning("Deprecation: `SESSION_HIJACK_PROTECTION` "
|
||||||
|
"has been renamed to `SESSION_PROTECTION`")
|
||||||
|
|
||||||
|
if isinstance(config.get("SESSION_HIJACK_REVERSE_PROXY"), str):
|
||||||
|
app.logger.warning("Deprecation: `SESSION_HIJACK_REVERSE_PROXY` "
|
||||||
|
"has been renamed to `SESSION_REVERSE_PROXY`")
|
||||||
|
|
||||||
|
backend_warning = f"Please specify a session backend. " \
|
||||||
|
f"Available interfaces: redis, redis+trio, " \
|
||||||
|
f"memcached, null. e.g: app.config['SESSION_TYPE'] = 'redis'"
|
||||||
|
|
||||||
if config['SESSION_TYPE'] == 'redis':
|
if config['SESSION_TYPE'] == 'redis':
|
||||||
options = {
|
options = {
|
||||||
"redis": config['SESSION_REDIS'],
|
"redis": config['SESSION_REDIS'],
|
||||||
@@ -123,7 +133,15 @@ class Session(object):
|
|||||||
use_signer=config['SESSION_USE_SIGNER'],
|
use_signer=config['SESSION_USE_SIGNER'],
|
||||||
permanent=config['SESSION_PERMANENT'],
|
permanent=config['SESSION_PERMANENT'],
|
||||||
**config)
|
**config)
|
||||||
|
elif config['SESSION_TYPE'] == 'null':
|
||||||
|
app.logger.warning(f"{backend_warning}. Currently using: null")
|
||||||
|
session_interface = NullSessionInterface(
|
||||||
|
key_prefix=config['SESSION_KEY_PREFIX'],
|
||||||
|
use_signer=config['SESSION_USE_SIGNER'],
|
||||||
|
permanent=config['SESSION_PERMANENT'],
|
||||||
|
**config)
|
||||||
else:
|
else:
|
||||||
session_interface = NullSessionInterface()
|
raise NotImplementedError(f"No such session interface "
|
||||||
|
f"\"{config['SESSION_TYPE']}\". {backend_warning}")
|
||||||
|
|
||||||
return session_interface
|
return session_interface
|
||||||
|
|||||||
+104
-86
@@ -6,15 +6,15 @@
|
|||||||
Server-side Sessions and SessionInterfaces.
|
Server-side Sessions and SessionInterfaces.
|
||||||
|
|
||||||
:copyright: (c) 2014 by Shipeng Feng.
|
:copyright: (c) 2014 by Shipeng Feng.
|
||||||
:copyright: (c) 2020 by dsc.
|
:copyright: (c) 2020 by Sander.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
import time
|
import time
|
||||||
from typing import Any, Callable, Optional, TYPE_CHECKING
|
from typing import Optional
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from quart import Quart
|
from quart import Quart, current_app
|
||||||
from quart.wrappers import BaseRequestWebsocket, Response
|
from quart.wrappers import BaseRequestWebsocket, Response
|
||||||
from quart.wrappers.response import FileBody
|
from quart.wrappers.response import FileBody
|
||||||
from quart.sessions import SessionInterface as QuartSessionInterface, SecureCookieSession
|
from quart.sessions import SessionInterface as QuartSessionInterface, SecureCookieSession
|
||||||
@@ -36,7 +36,12 @@ class ServerSideSession(SecureCookieSession):
|
|||||||
self.permanent = permanent
|
self.permanent = permanent
|
||||||
if addr:
|
if addr:
|
||||||
self.addr = addr
|
self.addr = addr
|
||||||
self._dirty = False
|
self.modified = False
|
||||||
|
|
||||||
|
def dirty(self):
|
||||||
|
current_app.logger.warning("Deprecation: `dirty()` has "
|
||||||
|
"been made obsolete. Will be "
|
||||||
|
"removed soon^tm.")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def addr(self) -> str:
|
def addr(self) -> str:
|
||||||
@@ -46,28 +51,6 @@ class ServerSideSession(SecureCookieSession):
|
|||||||
def addr(self, value: str) -> None:
|
def addr(self, value: str) -> None:
|
||||||
self['_addr'] = value # type: ignore
|
self['_addr'] = value # type: ignore
|
||||||
|
|
||||||
def dirty(self):
|
|
||||||
"""Marks the session to be written/saved.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
This feature only works if you have set ``SESSION_EXPLICIT``
|
|
||||||
to ``True``, at which point you'll have to explicitly mark
|
|
||||||
each session before they'll get processed and saved.
|
|
||||||
|
|
||||||
Example::
|
|
||||||
|
|
||||||
app.config['SESSION_EXPLICIT'] = True
|
|
||||||
Session(app)
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def root():
|
|
||||||
session['foo'] = 'bar'
|
|
||||||
session.dirty()
|
|
||||||
return "Hello World!"
|
|
||||||
"""
|
|
||||||
self._dirty = True
|
|
||||||
|
|
||||||
|
|
||||||
class RedisSession(ServerSideSession):
|
class RedisSession(ServerSideSession):
|
||||||
pass
|
pass
|
||||||
@@ -105,8 +88,15 @@ class SessionInterface(QuartSessionInterface):
|
|||||||
request: BaseRequestWebsocket
|
request: BaseRequestWebsocket
|
||||||
) -> Optional[SecureCookieSession]:
|
) -> Optional[SecureCookieSession]:
|
||||||
sid = request.cookies.get(app.session_cookie_name)
|
sid = request.cookies.get(app.session_cookie_name)
|
||||||
addr = request.headers.get('X-Forwarded-For', request.remote_addr) if \
|
if self._config['SESSION_REVERSE_PROXY'] is True:
|
||||||
self._config['SESSION_HIJACK_PROTECTION'] else None
|
# and no, you cannot define your own incoming
|
||||||
|
# header, stick to standards :-)
|
||||||
|
addr = request.headers.get('X-Forwarded-For')
|
||||||
|
if not addr:
|
||||||
|
app.logger.error("Could not grab IP from reverse proxy, "
|
||||||
|
"session protection is DISABLED!")
|
||||||
|
else:
|
||||||
|
addr = request.remote_addr
|
||||||
options = {"sid": sid, "permanent": self.permanent, "addr": addr}
|
options = {"sid": sid, "permanent": self.permanent, "addr": addr}
|
||||||
|
|
||||||
if not sid:
|
if not sid:
|
||||||
@@ -125,7 +115,7 @@ class SessionInterface(QuartSessionInterface):
|
|||||||
options['sid'] = self._generate_sid()
|
options['sid'] = self._generate_sid()
|
||||||
return self.session_class(**options)
|
return self.session_class(**options)
|
||||||
|
|
||||||
val = await self._backend_get(app, self.key_prefix + sid)
|
val = await self.get(key=self.key_prefix + sid, app=app)
|
||||||
if val is None:
|
if val is None:
|
||||||
options['sid'] = self._generate_sid()
|
options['sid'] = self._generate_sid()
|
||||||
return self.session_class(**options)
|
return self.session_class(**options)
|
||||||
@@ -135,19 +125,16 @@ class SessionInterface(QuartSessionInterface):
|
|||||||
except:
|
except:
|
||||||
app.logger.warning(f"Failed to deserialize session "
|
app.logger.warning(f"Failed to deserialize session "
|
||||||
f"data for sid: {sid}. Generating new sid.")
|
f"data for sid: {sid}. Generating new sid.")
|
||||||
|
app.logger.debug(f"data: {val}")
|
||||||
options['sid'] = self._generate_sid()
|
options['sid'] = self._generate_sid()
|
||||||
return self.session_class(**options)
|
return self.session_class(**options)
|
||||||
|
|
||||||
prevent_hijack = self._config['SESSION_HIJACK_PROTECTION']
|
protection = self._config['SESSION_PROTECTION']
|
||||||
if prevent_hijack is True:
|
if protection is True and addr is not None and \
|
||||||
if self._config['SESSION_HIJACK_REVERSE_PROXY'] is True:
|
data.get('_addr', addr) != addr:
|
||||||
addr = request.headers.get('X-Forwarded-For', request.remote_addr)
|
await self.delete(key=self.key_prefix + sid, app=app)
|
||||||
else:
|
options['sid'] = self._generate_sid()
|
||||||
addr = request.remote_addr
|
return self.session_class(**options)
|
||||||
if data.get('_addr', addr) != addr:
|
|
||||||
await self._backend_delete(app, self.key_prefix + sid)
|
|
||||||
options['sid'] = self._generate_sid()
|
|
||||||
return self.session_class(**options)
|
|
||||||
|
|
||||||
res = self.session_class(data, sid)
|
res = self.session_class(data, sid)
|
||||||
return res
|
return res
|
||||||
@@ -158,9 +145,8 @@ class SessionInterface(QuartSessionInterface):
|
|||||||
session: SecureCookieSession,
|
session: SecureCookieSession,
|
||||||
response: Response
|
response: Response
|
||||||
) -> None:
|
) -> None:
|
||||||
# prevent set-cookie
|
# prevent set-cookie on unmodified session objects
|
||||||
if self._config['SESSION_EXPLICIT'] is True and \
|
if not session.modified:
|
||||||
not session._dirty:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# prevent set-cookie on (static) file responses
|
# prevent set-cookie on (static) file responses
|
||||||
@@ -168,39 +154,42 @@ class SessionInterface(QuartSessionInterface):
|
|||||||
if self._config['SESSION_STATIC_FILE'] is False and \
|
if self._config['SESSION_STATIC_FILE'] is False and \
|
||||||
isinstance(response.response, FileBody):
|
isinstance(response.response, FileBody):
|
||||||
return
|
return
|
||||||
|
|
||||||
session_key = self.key_prefix + session.sid
|
session_key = self.key_prefix + session.sid
|
||||||
domain = self.get_cookie_domain(app)
|
domain = self.get_cookie_domain(app)
|
||||||
path = self.get_cookie_path(app)
|
path = self.get_cookie_path(app)
|
||||||
if not session:
|
if not session:
|
||||||
if session.modified:
|
if session.modified:
|
||||||
await self._backend_delete(app=app, key=session_key)
|
await self.delete(key=session_key, app=app)
|
||||||
response.delete_cookie(app.session_cookie_name,
|
response.delete_cookie(app.session_cookie_name,
|
||||||
domain=domain, path=path)
|
domain=domain, path=path)
|
||||||
return
|
return
|
||||||
httponly = self.get_cookie_httponly(app)
|
httponly = self.get_cookie_httponly(app)
|
||||||
|
samesite = self.get_cookie_samesite(app)
|
||||||
secure = self.get_cookie_secure(app)
|
secure = self.get_cookie_secure(app)
|
||||||
expires = self.get_expiration_time(app, session)
|
expires = self.get_expiration_time(app, session)
|
||||||
|
|
||||||
val = self.serializer.dumps(dict(session))
|
val = self.serializer.dumps(dict(session))
|
||||||
await self._backend_set(app=app, key=session_key, value=val)
|
await self.set(key=session_key, value=val, app=app)
|
||||||
if self.use_signer:
|
if self.use_signer:
|
||||||
session_id = self._get_signer(app).sign(want_bytes(session.sid))
|
session_id = self._get_signer(app).sign(want_bytes(session.sid))
|
||||||
else:
|
else:
|
||||||
session_id = session.sid
|
session_id = session.sid
|
||||||
response.set_cookie(app.session_cookie_name, session_id,
|
response.set_cookie(app.session_cookie_name, session_id,
|
||||||
expires=expires, httponly=httponly,
|
expires=expires, httponly=httponly,
|
||||||
domain=domain, path=path, secure=secure)
|
domain=domain, path=path, secure=secure, samesite=samesite)
|
||||||
|
|
||||||
async def create(self, app: Quart):
|
async def create(self, app: Quart):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def _backend_get(self, app: Quart, key: str):
|
async def get(self, app: Quart, key: str):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def _backend_set(self, app: Quart, key: str, value):
|
async def set(self, key: str, value, expiry: int = None,
|
||||||
|
app: Quart = None):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def _backend_delete(self, app: Quart, key: str):
|
async def delete(self, key: str, app: Quart = None):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def _generate_sid(self) -> str:
|
def _generate_sid(self) -> str:
|
||||||
@@ -227,7 +216,7 @@ class RedisSessionInterface(SessionInterface):
|
|||||||
|
|
||||||
def __init__(self, redis, **kwargs):
|
def __init__(self, redis, **kwargs):
|
||||||
super(RedisSessionInterface, self).__init__(**kwargs)
|
super(RedisSessionInterface, self).__init__(**kwargs)
|
||||||
self.redis = redis
|
self.backend = redis
|
||||||
|
|
||||||
async def create(self, app: Quart) -> None:
|
async def create(self, app: Quart) -> None:
|
||||||
"""Creates ``aioredis.Redis`` instance.
|
"""Creates ``aioredis.Redis`` instance.
|
||||||
@@ -237,20 +226,25 @@ class RedisSessionInterface(SessionInterface):
|
|||||||
Creates a single Redis connection, you might prefer
|
Creates a single Redis connection, you might prefer
|
||||||
pooling instead (see ``aioredis.Redis.create_redis_pool``)
|
pooling instead (see ``aioredis.Redis.create_redis_pool``)
|
||||||
"""
|
"""
|
||||||
if self.redis is None:
|
if self.backend is None:
|
||||||
import aioredis
|
import aioredis
|
||||||
self.redis = await aioredis.create_redis("redis://localhost")
|
self.backend = await aioredis.from_url(
|
||||||
|
"redis://localhost", encoding="utf-8", decode_responses=True
|
||||||
|
)
|
||||||
|
|
||||||
async def _backend_get(self, app: Quart, key: str):
|
async def get(self, key: str, app: Quart = None):
|
||||||
return await self.redis.get(key)
|
return await self.backend.get(key)
|
||||||
|
|
||||||
async def _backend_set(self, app: Quart, key: str, value):
|
async def set(self, key: str, value, expiry: int = None,
|
||||||
return await self.redis.setex(
|
app: Quart = None):
|
||||||
key=key, value=value,
|
if app and not expiry:
|
||||||
seconds=total_seconds(app.permanent_session_lifetime))
|
expiry = total_seconds(app.permanent_session_lifetime)
|
||||||
|
return await self.backend.setex(
|
||||||
|
name=key, value=value,
|
||||||
|
time=expiry)
|
||||||
|
|
||||||
async def _backend_delete(self, app: Quart, key: str):
|
async def delete(self, key: str, app: Quart = None):
|
||||||
return await self.redis.delete(key)
|
return await self.backend.delete(key)
|
||||||
|
|
||||||
|
|
||||||
class RedisTrioSessionInterface(SessionInterface):
|
class RedisTrioSessionInterface(SessionInterface):
|
||||||
@@ -267,7 +261,7 @@ class RedisTrioSessionInterface(SessionInterface):
|
|||||||
|
|
||||||
def __init__(self, redis, **kwargs):
|
def __init__(self, redis, **kwargs):
|
||||||
super(RedisTrioSessionInterface, self).__init__(**kwargs)
|
super(RedisTrioSessionInterface, self).__init__(**kwargs)
|
||||||
self.redis_trio = redis
|
self.backend = redis
|
||||||
|
|
||||||
async def create(self, app: Quart) -> None:
|
async def create(self, app: Quart) -> None:
|
||||||
"""Creates ``aioredis.Redis`` instance.
|
"""Creates ``aioredis.Redis`` instance.
|
||||||
@@ -277,23 +271,26 @@ class RedisTrioSessionInterface(SessionInterface):
|
|||||||
Creates a single Redis connection. Pooling not
|
Creates a single Redis connection. Pooling not
|
||||||
supported yet for ``RedisTrio``.
|
supported yet for ``RedisTrio``.
|
||||||
"""
|
"""
|
||||||
if self.redis_trio is None:
|
if self.backend is None:
|
||||||
from quart_session.redis_trio import RedisTrio
|
from quart_session.redis_trio import RedisTrio
|
||||||
self.redis_trio = RedisTrio()
|
self.backend = RedisTrio()
|
||||||
await self.redis_trio.connect()
|
await self.backend.connect()
|
||||||
|
|
||||||
async def _backend_get(self, app: Quart, key: str):
|
async def get(self, key: str, app: Quart = None):
|
||||||
data = await self.redis_trio.get(key)
|
data = await self.backend.get(key)
|
||||||
if data:
|
if data:
|
||||||
return data.decode()
|
return data.decode()
|
||||||
|
|
||||||
async def _backend_set(self, app: Quart, key: str, value):
|
async def set(self, key: str, value, expiry: int = None,
|
||||||
return await self.redis_trio.setex(
|
app: Quart = None):
|
||||||
|
if app and not expiry:
|
||||||
|
expiry = total_seconds(app.permanent_session_lifetime)
|
||||||
|
return await self.backend.setex(
|
||||||
key=key, value=value,
|
key=key, value=value,
|
||||||
seconds=total_seconds(app.permanent_session_lifetime))
|
seconds=expiry)
|
||||||
|
|
||||||
async def _backend_delete(self, app: Quart, key: str):
|
async def delete(self, key: str, app: Quart = None):
|
||||||
return await self.redis_trio.delete(key)
|
return await self.backend.delete(key)
|
||||||
|
|
||||||
|
|
||||||
class MemcachedSessionInterface(SessionInterface):
|
class MemcachedSessionInterface(SessionInterface):
|
||||||
@@ -314,14 +311,17 @@ class MemcachedSessionInterface(SessionInterface):
|
|||||||
super(MemcachedSessionInterface, self).__init__(
|
super(MemcachedSessionInterface, self).__init__(
|
||||||
key_prefix=key_prefix, use_signer=use_signer,
|
key_prefix=key_prefix, use_signer=use_signer,
|
||||||
permanent=permanent, **kwargs)
|
permanent=permanent, **kwargs)
|
||||||
self.memcached = memcached
|
self.backend = memcached
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def create(self, app: Quart) -> None:
|
def create(self, app: Quart) -> None:
|
||||||
if self.memcached is None:
|
if self.backend is None:
|
||||||
import aiomcache
|
import aiomcache
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
self.memcached = aiomcache.Client("127.0.0.1", 11211, loop=loop)
|
#self.backend = aiomcache.Client("127.0.0.1", 11211, loop=loop)
|
||||||
|
self.backend = aiomcache.Client(self._config.get('SESSION_MEMCACHED_HOST', '127.0.0.1'),
|
||||||
|
self._config.get('SESSION_MEMCACHED_PORT', 11211),
|
||||||
|
loop=loop)
|
||||||
|
|
||||||
def _get_memcache_timeout(self, timeout):
|
def _get_memcache_timeout(self, timeout):
|
||||||
"""
|
"""
|
||||||
@@ -338,26 +338,44 @@ class MemcachedSessionInterface(SessionInterface):
|
|||||||
timeout += int(time.time())
|
timeout += int(time.time())
|
||||||
return timeout
|
return timeout
|
||||||
|
|
||||||
async def _backend_get(self, app: Quart, key: str):
|
async def get(self, key: str, app: Quart = None):
|
||||||
key = key.encode()
|
key = key.encode()
|
||||||
return await self.memcached.get(key)
|
return await self.backend.get(key)
|
||||||
|
|
||||||
|
async def set(self, key: str, value, expiry: int = None,
|
||||||
|
app: Quart = None):
|
||||||
|
if app and not expiry:
|
||||||
|
expiry = self._get_memcache_timeout(
|
||||||
|
total_seconds(app.permanent_session_lifetime))
|
||||||
|
|
||||||
async def _backend_set(self, app: Quart, key: str, value):
|
|
||||||
key = key.encode()
|
key = key.encode()
|
||||||
value = value.encode()
|
value = value.encode()
|
||||||
expiry = self._get_memcache_timeout(total_seconds(
|
return await self.backend.set(key=key, value=value,
|
||||||
app.permanent_session_lifetime))
|
exptime=expiry)
|
||||||
return await self.memcached.set(key=key, value=value,
|
|
||||||
exptime=expiry)
|
|
||||||
|
|
||||||
async def _backend_delete(self, app: Quart, key: str):
|
async def delete(self, key: str, app: Quart = None):
|
||||||
key = key.encode()
|
key = key.encode()
|
||||||
return await self.memcached.delete(key)
|
return await self.backend.delete(key)
|
||||||
|
|
||||||
|
|
||||||
class NullSessionInterface(SessionInterface):
|
class NullSessionInterface(SessionInterface):
|
||||||
"""Used to open a :class:`quart.sessions.NullSession` instance.
|
"""This class does absolutely nothing"""
|
||||||
"""
|
session_class = NullSession
|
||||||
|
|
||||||
def open_session(self, app: Quart, request: BaseRequestWebsocket):
|
def __init__(
|
||||||
return None
|
self, key_prefix: str, use_signer: bool = False,
|
||||||
|
permanent: bool = True, **kwargs):
|
||||||
|
super(NullSessionInterface, self).__init__(
|
||||||
|
key_prefix=key_prefix, use_signer=use_signer,
|
||||||
|
permanent=permanent, **kwargs)
|
||||||
|
self.backend = None
|
||||||
|
|
||||||
|
async def create(self, app: Quart) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def get(self, key: str, app: Quart = None) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def set(self, key: str, value, expiry: int = None,
|
||||||
|
app: Quart = None) -> None:
|
||||||
|
pass
|
||||||
|
|||||||
@@ -9,11 +9,14 @@ Links
|
|||||||
`````
|
`````
|
||||||
|
|
||||||
* `Github
|
* `Github
|
||||||
<https://github.com/xmrdsc/quart-session>`_
|
<https://github.com/sferdi0/quart-session>`_
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from setuptools import setup
|
from setuptools import setup
|
||||||
|
|
||||||
|
with open('README.md') as f:
|
||||||
|
long_description = f.read()
|
||||||
|
|
||||||
|
|
||||||
INSTALL_REQUIRES = [
|
INSTALL_REQUIRES = [
|
||||||
"Quart>=0.10.0"
|
"Quart>=0.10.0"
|
||||||
@@ -21,20 +24,24 @@ INSTALL_REQUIRES = [
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='Quart-Session',
|
name='Quart-Session',
|
||||||
version='0.0.1',
|
version='1.0.3',
|
||||||
url='https://github.com/xmrdsc/quart-session',
|
url='https://github.com/sferdi0/quart-session',
|
||||||
license='BSD',
|
license='BSD',
|
||||||
author='dsc',
|
author='Sander',
|
||||||
author_email='dsc@xmr.pm',
|
author_email='sander@sanderf.nl',
|
||||||
description='Adds server-side session support to your Quart application',
|
description='Adds server-side session support to your Quart application',
|
||||||
long_description=__doc__,
|
long_description=long_description,
|
||||||
|
long_description_content_type='text/markdown',
|
||||||
packages=['quart_session'],
|
packages=['quart_session'],
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
platforms='any',
|
platforms='any',
|
||||||
install_requires=INSTALL_REQUIRES,
|
install_requires=INSTALL_REQUIRES,
|
||||||
tests_require=INSTALL_REQUIRES + ["asynctest", "hypothesis", "pytest", "pytest-asyncio"],
|
tests_require=INSTALL_REQUIRES + ["asynctest", "hypothesis", "pytest", "pytest-asyncio"],
|
||||||
extras_require={"dotenv": ["python-dotenv"]},
|
extras_require={
|
||||||
|
"dotenv": ["python-dotenv"],
|
||||||
|
"redis": ["aioredis>=2.0.0"]
|
||||||
|
},
|
||||||
classifiers=[
|
classifiers=[
|
||||||
'Environment :: Web Environment',
|
'Environment :: Web Environment',
|
||||||
'Intended Audience :: Developers',
|
'Intended Audience :: Developers',
|
||||||
|
|||||||
Reference in New Issue
Block a user