| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Resource CORS options class definition.""" |
| 16 |
|
| 17 |
import collections |
| 18 |
import collections.abc |
| 19 |
import numbers |
| 20 |
|
| 21 |
__all__ = ("ResourceOptions",) |
| 22 |
|
| 23 |
|
| 24 |
def _is_proper_sequence(seq): |
| 25 |
"""Returns is seq is sequence and not string.""" |
| 26 |
return isinstance(seq, collections.abc.Sequence) and not isinstance(seq, str) |
| 27 |
|
| 28 |
|
| 29 |
class ResourceOptions( |
| 30 |
collections.namedtuple( |
| 31 |
"Base", |
| 32 |
( |
| 33 |
"allow_credentials", |
| 34 |
"expose_headers", |
| 35 |
"allow_headers", |
| 36 |
"max_age", |
| 37 |
"allow_methods", |
| 38 |
), |
| 39 |
) |
| 40 |
): |
| 41 |
"""Resource CORS options.""" |
| 42 |
|
| 43 |
__slots__ = () |
| 44 |
|
| 45 |
def __init__( |
| 46 |
self, |
| 47 |
*, |
| 48 |
allow_credentials=False, |
| 49 |
expose_headers=(), |
| 50 |
allow_headers=(), |
| 51 |
max_age=None, |
| 52 |
allow_methods=None |
| 53 |
): |
| 54 |
"""Construct resource CORS options. |
| 55 |
|
| 56 |
Options will be normalized. |
| 57 |
|
| 58 |
:param allow_credentials: |
| 59 |
Is passing client credentials to the resource from other origin |
| 60 |
is allowed. |
| 61 |
See <http://www.w3.org/TR/cors/#user-credentials> for |
| 62 |
the definition. |
| 63 |
:type allow_credentials: bool |
| 64 |
Is passing client credentials to the resource from other origin |
| 65 |
is allowed. |
| 66 |
|
| 67 |
:param expose_headers: |
| 68 |
Server headers that are allowed to be exposed to the client. |
| 69 |
Simple response headers are excluded from this set, see |
| 70 |
<http://www.w3.org/TR/cors/#list-of-exposed-headers>. |
| 71 |
:type expose_headers: sequence of strings or ``*`` string. |
| 72 |
|
| 73 |
:param allow_headers: |
| 74 |
Client headers that are allowed to be passed to the resource. |
| 75 |
See <http://www.w3.org/TR/cors/#list-of-headers>. |
| 76 |
:type allow_headers: sequence of strings or ``*`` string. |
| 77 |
|
| 78 |
:param max_age: |
| 79 |
How long the results of a preflight request can be cached in a |
| 80 |
preflight result cache (in seconds). |
| 81 |
See <http://www.w3.org/TR/cors/#http-access-control-max-age>. |
| 82 |
|
| 83 |
:param allow_methods: |
| 84 |
List of allowed methods or ``*``string. Can be used in resource or |
| 85 |
global defaults, but not in specific route. |
| 86 |
|
| 87 |
It's not required to specify all allowed methods for specific |
| 88 |
resource, routes that have explicit CORS configuration will be |
| 89 |
treated as if their methods are allowed. |
| 90 |
""" |
| 91 |
super().__init__() |
| 92 |
|
| 93 |
def __new__( |
| 94 |
cls, |
| 95 |
*, |
| 96 |
allow_credentials=False, |
| 97 |
expose_headers=(), |
| 98 |
allow_headers=(), |
| 99 |
max_age=None, |
| 100 |
allow_methods=None |
| 101 |
): |
| 102 |
"""Normalize source parameters and store them in namedtuple.""" |
| 103 |
|
| 104 |
if not isinstance(allow_credentials, bool): |
| 105 |
raise ValueError( |
| 106 |
"'allow_credentials' must be boolean, " |
| 107 |
"got '{!r}'".format(allow_credentials) |
| 108 |
) |
| 109 |
_allow_credentials = allow_credentials |
| 110 |
|
| 111 |
|
| 112 |
if expose_headers == "*": |
| 113 |
_expose_headers = expose_headers |
| 114 |
elif not _is_proper_sequence(expose_headers): |
| 115 |
raise ValueError( |
| 116 |
"'expose_headers' must be either '*', or sequence of strings, " |
| 117 |
"got '{!r}'".format(expose_headers) |
| 118 |
) |
| 119 |
elif expose_headers: |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
_expose_headers = frozenset(expose_headers) |
| 126 |
else: |
| 127 |
|
| 128 |
_expose_headers = frozenset() |
| 129 |
|
| 130 |
|
| 131 |
if allow_headers == "*": |
| 132 |
_allow_headers = allow_headers |
| 133 |
elif not _is_proper_sequence(allow_headers): |
| 134 |
raise ValueError( |
| 135 |
"'allow_headers' must be either '*', or sequence of strings, " |
| 136 |
"got '{!r}'".format(allow_headers) |
| 137 |
) |
| 138 |
else: |
| 139 |
|
| 140 |
_allow_headers = frozenset(h.upper() for h in allow_headers) |
| 141 |
|
| 142 |
if max_age is None: |
| 143 |
_max_age = None |
| 144 |
else: |
| 145 |
if not isinstance(max_age, numbers.Integral) or max_age < 0: |
| 146 |
raise ValueError( |
| 147 |
"'max_age' must be non-negative integer, " |
| 148 |
"got '{!r}'".format(max_age) |
| 149 |
) |
| 150 |
_max_age = max_age |
| 151 |
|
| 152 |
if allow_methods is None or allow_methods == "*": |
| 153 |
_allow_methods = allow_methods |
| 154 |
elif not _is_proper_sequence(allow_methods): |
| 155 |
raise ValueError( |
| 156 |
"'allow_methods' must be either '*', or sequence of strings, " |
| 157 |
"got '{!r}'".format(allow_methods) |
| 158 |
) |
| 159 |
else: |
| 160 |
|
| 161 |
_allow_methods = frozenset(m.upper() for m in allow_methods) |
| 162 |
|
| 163 |
return super().__new__( |
| 164 |
cls, |
| 165 |
allow_credentials=_allow_credentials, |
| 166 |
expose_headers=_expose_headers, |
| 167 |
allow_headers=_allow_headers, |
| 168 |
max_age=_max_age, |
| 169 |
allow_methods=_allow_methods, |
| 170 |
) |
| 171 |
|
| 172 |
def is_method_allowed(self, method): |
| 173 |
if self.allow_methods is None: |
| 174 |
return False |
| 175 |
|
| 176 |
if self.allow_methods == "*": |
| 177 |
return True |
| 178 |
|
| 179 |
return method.upper() in self.allow_methods |