Name | quart-cors JSON |
Version |
0.7.0
JSON |
| download |
home_page | https://github.com/pgjones/quart-cors/ |
Summary | A Quart extension to provide Cross Origin Resource Sharing, access control, support |
upload_time | 2023-09-23 12:28:40 |
maintainer | |
docs_url | None |
author | pgjones |
requires_python | >=3.7 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
Quart-CORS
==========
|Build Status| |pypi| |python| |license|
Quart-CORS is an extension for `Quart
<https://github.com/pgjones/quart>`_ to enable and control `Cross
Origin Resource Sharing <http://www.w3.org/TR/cors/>`_, CORS (also
known as access control).
CORS is required to share resources in browsers due to the `Same
Origin Policy <https://en.wikipedia.org/wiki/Same-origin_policy>`_
which prevents resources being used from a different origin. An origin
in this case is defined as the scheme, host and port combined and a
resource corresponds to a path.
In practice the Same Origin Policy means that a browser visiting
``http://quart.com`` will prevent the response of ``GET
http://api.com`` being read. It will also prevent requests such as
``POST http://api.com``. Note that CORS applies to browser initiated
requests, non-browser clients such as ``requests`` are not subject to
CORS restrictions.
CORS allows a server to indicate to a browser that certain resources
can be used, contrary to the Same Origin Policy. It does so via
access-control headers that inform the browser how the resource can be
used. For GET requests these headers are sent in the response. For
non-GET requests the browser must ask the server for the
access-control headers before sending the actual request, it does so
via a preflight OPTIONS request.
The Same Origin Policy does not apply to WebSockets, and hence there
is no need for CORS. Instead the server alone is responsible for
deciding if the WebSocket is allowed and it should do so by inspecting
the WebSocket-request origin header.
Simple (GET) requests should return CORS headers specifying the
origins that are allowed to use the resource (response). This can be
any origin, ``*`` (wildcard), or a list of specific origins. The
response should also include a CORS header specifying whether
response-credentials e.g. cookies can be used. Note that if credential
sharing is allowed the allowed origins must be specific and not a
wildcard.
Preflight requests should return CORS headers specifying the origins
allowed to use the resource, the methods and headers allowed to be
sent in a request to the resource, whether response credentials can be
used, and finally which response headers can be used.
Note that certain actions are allowed in the Same Origin Policy such
as embedding e.g. ``<img src="http://api.com/img.gif">`` and simple
POSTs. For the purposes of this readme though these complications are
ignored.
The CORS access control response headers are,
================================ ===========================================================
Header name Meaning
-------------------------------- -----------------------------------------------------------
Access-Control-Allow-Origin Origins that are allowed to use the resource.
Access-Control-Allow-Credentials Can credentials be shared.
Access-Control-Allow-Methods Methods that may be used in requests to the resource.
Access-Control-Allow-Headers Headers that may be sent in requests to the resource.
Access-Control-Expose-Headers Headers that may be read in the response from the resource.
Access-Control-Max-Age Maximum age to cache the CORS headers for the resource.
================================ ===========================================================
Quart-CORS uses the same naming (without the Access-Control prefix)
for it's arguments and settings when they relate to the same meaning.
Usage
-----
To add CORS access control headers to all of the routes in the
application, simply apply the ``cors`` function to the application, or
to a specific blueprint,
.. code-block:: python
app = Quart(__name__)
app = cors(app, **settings)
blueprint = Blueprint(__name__)
blueprint = cors(blueprint, **settings)
alternatively if you wish to add CORS selectively by resource, apply
the ``route_cors`` function to a route, or the ``websocket_cors``
function to a WebSocket,
.. code-block:: python
@app.route('/')
@route_cors(**settings)
async def handler():
...
@app.websocket('/')
@websocket_cors(allow_origin=...)
async def handler():
...
The ``settings`` are these arguments,
================= ====================================================
Argument type
----------------- ----------------------------------------------------
allow_origin Union[Set[Union[Pattern, str]], Union[Pattern, str]]
allow_credentials bool
allow_methods Union[Set[str], str]
allow_headers Union[Set[str], str]
expose_headers Union[Set[str], str]
max_age Union[int, flot, timedelta]
================= ====================================================
which correspond to the CORS headers noted above. Note that all
settings are optional and defaults can be specified in the application
configuration,
============================ ========================
Configuration key type
---------------------------- ------------------------
QUART_CORS_ALLOW_ORIGIN Set[Union[Pattern, str]]
QUART_CORS_ALLOW_CREDENTIALS bool
QUART_CORS_ALLOW_METHODS Set[str]
QUART_CORS_ALLOW_HEADERS Set[str]
QUART_CORS_EXPOSE_HEADERS Set[str]
QUART_CORS_MAX_AGE float
============================ ========================
The ``websocket_cors`` decorator only takes an ``allow_origin``
argument which defines the origins that are allowed to use the
WebSocket. A WebSocket request from a disallowed origin will be
responded to with a 400 response.
The ``allow_origin`` origins should be the origin only (no path, query
strings or fragments) i.e. ``https://quart.com`` not
``https://quart.com/``.
The ``cors_exempt`` decorator can be used in conjunction with ``cors``
to exempt a websocket handler or view function from cors.
Simple examples
~~~~~~~~~~~~~~~
To allow an app to be used from any origin (not recommended as it is
too permissive),
.. code-block:: python
app = Quart(__name__)
app = cors(app, allow_origin="*")
To allow a route or WebSocket to be used from another specific domain,
``https://quart.com``,
.. code-block:: python
@app.route('/')
@route_cors(allow_origin="https://quart.com")
async def handler():
...
@app.websocket('/')
@websocket_cors(allow_origin="https://quart.com")
async def handler():
...
To allow a route or WebSocket to be used from any subdomain (but not
the domain itself) of ``quart.com``,
.. code-block:: python
@app.route('/')
@route_cors(allow_origin=re.compile(r"https:\/\/.*\.quart\.com"))
async def handler():
...
@app.websocket('/')
@websocket_cors(allow_origin=re.compile(r"https:\/\/.*\.quart\.com"))
async def handler():
...
To allow a JSON POST request to an API route, from ``https://quart.com``,
.. code-block:: python
@app.route('/', methods=["POST"])
@route_cors(
allow_headers=["content-type"],
allow_methods=["POST"],
allow_origin=["https://quart.com"],
)
async def handler():
data = await request.get_json()
...
Contributing
------------
Quart-CORS is developed on `GitHub
<https://github.com/pgjones/quart-cors>`_. You are very welcome to
open `issues <https://github.com/pgjones/quart-cors/issues>`_ or
propose `merge requests
<https://github.com/pgjones/quart-cors/merge_requests>`_.
Testing
~~~~~~~
The best way to test Quart-CORS is with Tox,
.. code-block:: console
$ pip install tox
$ tox
this will check the code style and run the tests.
Help
----
This README is the best place to start, after that try opening an
`issue <https://github.com/pgjones/quart-cors/issues>`_.
.. |Build Status| image:: https://github.com/pgjones/quart-cors/actions/workflows/ci.yml/badge.svg
:target: https://github.com/pgjones/quart-cors/commits/main
.. |pypi| image:: https://img.shields.io/pypi/v/quart-cors.svg
:target: https://pypi.python.org/pypi/Quart-CORS/
.. |python| image:: https://img.shields.io/pypi/pyversions/quart-cors.svg
:target: https://pypi.python.org/pypi/Quart-CORS/
.. |license| image:: https://img.shields.io/badge/license-MIT-blue.svg
:target: https://github.com/pgjones/quart-cors/blob/main/LICENSE
Raw data
{
"_id": null,
"home_page": "https://github.com/pgjones/quart-cors/",
"name": "quart-cors",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "",
"author": "pgjones",
"author_email": "philip.graham.jones@googlemail.com",
"download_url": "https://files.pythonhosted.org/packages/20/b1/a5f6dd757496a8b29ac7ed41e581736b2b86327facb5f1c5ccba9e0513ee/quart_cors-0.7.0.tar.gz",
"platform": null,
"description": "Quart-CORS\n==========\n\n|Build Status| |pypi| |python| |license|\n\nQuart-CORS is an extension for `Quart\n<https://github.com/pgjones/quart>`_ to enable and control `Cross\nOrigin Resource Sharing <http://www.w3.org/TR/cors/>`_, CORS (also\nknown as access control).\n\nCORS is required to share resources in browsers due to the `Same\nOrigin Policy <https://en.wikipedia.org/wiki/Same-origin_policy>`_\nwhich prevents resources being used from a different origin. An origin\nin this case is defined as the scheme, host and port combined and a\nresource corresponds to a path.\n\nIn practice the Same Origin Policy means that a browser visiting\n``http://quart.com`` will prevent the response of ``GET\nhttp://api.com`` being read. It will also prevent requests such as\n``POST http://api.com``. Note that CORS applies to browser initiated\nrequests, non-browser clients such as ``requests`` are not subject to\nCORS restrictions.\n\nCORS allows a server to indicate to a browser that certain resources\ncan be used, contrary to the Same Origin Policy. It does so via\naccess-control headers that inform the browser how the resource can be\nused. For GET requests these headers are sent in the response. For\nnon-GET requests the browser must ask the server for the\naccess-control headers before sending the actual request, it does so\nvia a preflight OPTIONS request.\n\nThe Same Origin Policy does not apply to WebSockets, and hence there\nis no need for CORS. Instead the server alone is responsible for\ndeciding if the WebSocket is allowed and it should do so by inspecting\nthe WebSocket-request origin header.\n\nSimple (GET) requests should return CORS headers specifying the\norigins that are allowed to use the resource (response). This can be\nany origin, ``*`` (wildcard), or a list of specific origins. The\nresponse should also include a CORS header specifying whether\nresponse-credentials e.g. cookies can be used. Note that if credential\nsharing is allowed the allowed origins must be specific and not a\nwildcard.\n\nPreflight requests should return CORS headers specifying the origins\nallowed to use the resource, the methods and headers allowed to be\nsent in a request to the resource, whether response credentials can be\nused, and finally which response headers can be used.\n\nNote that certain actions are allowed in the Same Origin Policy such\nas embedding e.g. ``<img src=\"http://api.com/img.gif\">`` and simple\nPOSTs. For the purposes of this readme though these complications are\nignored.\n\nThe CORS access control response headers are,\n\n================================ ===========================================================\nHeader name Meaning\n-------------------------------- -----------------------------------------------------------\nAccess-Control-Allow-Origin Origins that are allowed to use the resource.\nAccess-Control-Allow-Credentials Can credentials be shared.\nAccess-Control-Allow-Methods Methods that may be used in requests to the resource.\nAccess-Control-Allow-Headers Headers that may be sent in requests to the resource.\nAccess-Control-Expose-Headers Headers that may be read in the response from the resource.\nAccess-Control-Max-Age Maximum age to cache the CORS headers for the resource.\n================================ ===========================================================\n\nQuart-CORS uses the same naming (without the Access-Control prefix)\nfor it's arguments and settings when they relate to the same meaning.\n\nUsage\n-----\n\nTo add CORS access control headers to all of the routes in the\napplication, simply apply the ``cors`` function to the application, or\nto a specific blueprint,\n\n.. code-block:: python\n\n app = Quart(__name__)\n app = cors(app, **settings)\n\n blueprint = Blueprint(__name__)\n blueprint = cors(blueprint, **settings)\n\nalternatively if you wish to add CORS selectively by resource, apply\nthe ``route_cors`` function to a route, or the ``websocket_cors``\nfunction to a WebSocket,\n\n.. code-block:: python\n\n @app.route('/')\n @route_cors(**settings)\n async def handler():\n ...\n\n @app.websocket('/')\n @websocket_cors(allow_origin=...)\n async def handler():\n ...\n\nThe ``settings`` are these arguments,\n\n================= ====================================================\nArgument type\n----------------- ----------------------------------------------------\nallow_origin Union[Set[Union[Pattern, str]], Union[Pattern, str]]\nallow_credentials bool\nallow_methods Union[Set[str], str]\nallow_headers Union[Set[str], str]\nexpose_headers Union[Set[str], str]\nmax_age Union[int, flot, timedelta]\n================= ====================================================\n\nwhich correspond to the CORS headers noted above. Note that all\nsettings are optional and defaults can be specified in the application\nconfiguration,\n\n============================ ========================\nConfiguration key type\n---------------------------- ------------------------\nQUART_CORS_ALLOW_ORIGIN Set[Union[Pattern, str]]\nQUART_CORS_ALLOW_CREDENTIALS bool\nQUART_CORS_ALLOW_METHODS Set[str]\nQUART_CORS_ALLOW_HEADERS Set[str]\nQUART_CORS_EXPOSE_HEADERS Set[str]\nQUART_CORS_MAX_AGE float\n============================ ========================\n\nThe ``websocket_cors`` decorator only takes an ``allow_origin``\nargument which defines the origins that are allowed to use the\nWebSocket. A WebSocket request from a disallowed origin will be\nresponded to with a 400 response.\n\nThe ``allow_origin`` origins should be the origin only (no path, query\nstrings or fragments) i.e. ``https://quart.com`` not\n``https://quart.com/``.\n\nThe ``cors_exempt`` decorator can be used in conjunction with ``cors``\nto exempt a websocket handler or view function from cors.\n\nSimple examples\n~~~~~~~~~~~~~~~\n\nTo allow an app to be used from any origin (not recommended as it is\ntoo permissive),\n\n.. code-block:: python\n\n app = Quart(__name__)\n app = cors(app, allow_origin=\"*\")\n\nTo allow a route or WebSocket to be used from another specific domain,\n``https://quart.com``,\n\n.. code-block:: python\n\n @app.route('/')\n @route_cors(allow_origin=\"https://quart.com\")\n async def handler():\n ...\n\n @app.websocket('/')\n @websocket_cors(allow_origin=\"https://quart.com\")\n async def handler():\n ...\n\nTo allow a route or WebSocket to be used from any subdomain (but not\nthe domain itself) of ``quart.com``,\n\n.. code-block:: python\n\n @app.route('/')\n @route_cors(allow_origin=re.compile(r\"https:\\/\\/.*\\.quart\\.com\"))\n async def handler():\n ...\n\n @app.websocket('/')\n @websocket_cors(allow_origin=re.compile(r\"https:\\/\\/.*\\.quart\\.com\"))\n async def handler():\n ...\n\nTo allow a JSON POST request to an API route, from ``https://quart.com``,\n\n.. code-block:: python\n\n @app.route('/', methods=[\"POST\"])\n @route_cors(\n allow_headers=[\"content-type\"],\n allow_methods=[\"POST\"],\n allow_origin=[\"https://quart.com\"],\n )\n async def handler():\n data = await request.get_json()\n ...\n\nContributing\n------------\n\nQuart-CORS is developed on `GitHub\n<https://github.com/pgjones/quart-cors>`_. You are very welcome to\nopen `issues <https://github.com/pgjones/quart-cors/issues>`_ or\npropose `merge requests\n<https://github.com/pgjones/quart-cors/merge_requests>`_.\n\nTesting\n~~~~~~~\n\nThe best way to test Quart-CORS is with Tox,\n\n.. code-block:: console\n\n $ pip install tox\n $ tox\n\nthis will check the code style and run the tests.\n\nHelp\n----\n\nThis README is the best place to start, after that try opening an\n`issue <https://github.com/pgjones/quart-cors/issues>`_.\n\n\n.. |Build Status| image:: https://github.com/pgjones/quart-cors/actions/workflows/ci.yml/badge.svg\n :target: https://github.com/pgjones/quart-cors/commits/main\n\n.. |pypi| image:: https://img.shields.io/pypi/v/quart-cors.svg\n :target: https://pypi.python.org/pypi/Quart-CORS/\n\n.. |python| image:: https://img.shields.io/pypi/pyversions/quart-cors.svg\n :target: https://pypi.python.org/pypi/Quart-CORS/\n\n.. |license| image:: https://img.shields.io/badge/license-MIT-blue.svg\n :target: https://github.com/pgjones/quart-cors/blob/main/LICENSE\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Quart extension to provide Cross Origin Resource Sharing, access control, support",
"version": "0.7.0",
"project_urls": {
"Homepage": "https://github.com/pgjones/quart-cors/",
"Repository": "https://github.com/pgjones/quart-cors/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "60fc1ffe9042df05d48f5eaac4116708fee3f7bb18b696380cc4e3797c8fd510",
"md5": "6511fc4330f08b1595fe8278d38193f3",
"sha256": "fa872cc94a2ae6b51a35b028ebca65c14069d7121d63a4caa3526ebbfb7c5a99"
},
"downloads": -1,
"filename": "quart_cors-0.7.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6511fc4330f08b1595fe8278d38193f3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8034,
"upload_time": "2023-09-23T12:28:38",
"upload_time_iso_8601": "2023-09-23T12:28:38.814720Z",
"url": "https://files.pythonhosted.org/packages/60/fc/1ffe9042df05d48f5eaac4116708fee3f7bb18b696380cc4e3797c8fd510/quart_cors-0.7.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "20b1a5f6dd757496a8b29ac7ed41e581736b2b86327facb5f1c5ccba9e0513ee",
"md5": "314cc79d05f8c134620cacf09bd988b9",
"sha256": "d667a0f13b4ce6d9e926489de5d819780844fbff5b2cdea156bd8867dd426a37"
},
"downloads": -1,
"filename": "quart_cors-0.7.0.tar.gz",
"has_sig": false,
"md5_digest": "314cc79d05f8c134620cacf09bd988b9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 9782,
"upload_time": "2023-09-23T12:28:40",
"upload_time_iso_8601": "2023-09-23T12:28:40.373302Z",
"url": "https://files.pythonhosted.org/packages/20/b1/a5f6dd757496a8b29ac7ed41e581736b2b86327facb5f1c5ccba9e0513ee/quart_cors-0.7.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-23 12:28:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pgjones",
"github_project": "quart-cors",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "quart-cors"
}