zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
from __future__ import annotations
 
import base64
import binascii
import typing as t
import warnings
from functools import wraps
 
from ..http import dump_header
from ..http import parse_dict_header
from ..http import parse_set_header
from ..http import quote_header_value
from .structures import CallbackDict
from .structures import HeaderSet
 
if t.TYPE_CHECKING:
    import typing_extensions as te
 
 
class Authorization:
    """Represents the parts of an ``Authorization`` request header.
 
    :attr:`.Request.authorization` returns an instance if the header is set.
 
    An instance can be used with the test :class:`.Client` request methods' ``auth``
    parameter to send the header in test requests.
 
    Depending on the auth scheme, either :attr:`parameters` or :attr:`token` will be
    set. The ``Basic`` scheme's token is decoded into the ``username`` and ``password``
    parameters.
 
    For convenience, ``auth["key"]`` and ``auth.key`` both access the key in the
    :attr:`parameters` dict, along with ``auth.get("key")`` and ``"key" in auth``.
 
    .. versionchanged:: 2.3
        The ``token`` parameter and attribute was added to support auth schemes that use
        a token instead of parameters, such as ``Bearer``.
 
    .. versionchanged:: 2.3
        The object is no longer a ``dict``.
 
    .. versionchanged:: 0.5
        The object is an immutable dict.
    """
 
    def __init__(
        self,
        auth_type: str,
        data: dict[str, str] | None = None,
        token: str | None = None,
    ) -> None:
        self.type = auth_type
        """The authorization scheme, like ``Basic``, ``Digest``, or ``Bearer``."""
 
        if data is None:
            data = {}
 
        self.parameters = data
        """A dict of parameters parsed from the header. Either this or :attr:`token`
        will have a value for a give scheme.
        """
 
        self.token = token
        """A token parsed from the header. Either this or :attr:`parameters` will have a
        value for a given scheme.
 
        .. versionadded:: 2.3
        """
 
    def __getattr__(self, name: str) -> str | None:
        return self.parameters.get(name)
 
    def __getitem__(self, name: str) -> str | None:
        return self.parameters.get(name)
 
    def get(self, key: str, default: str | None = None) -> str | None:
        return self.parameters.get(key, default)
 
    def __contains__(self, key: str) -> bool:
        return key in self.parameters
 
    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Authorization):
            return NotImplemented
 
        return (
            other.type == self.type
            and other.token == self.token
            and other.parameters == self.parameters
        )
 
    @classmethod
    def from_header(cls, value: str | None) -> te.Self | None:
        """Parse an ``Authorization`` header value and return an instance, or ``None``
        if the value is empty.
 
        :param value: The header value to parse.
 
        .. versionadded:: 2.3
        """
        if not value:
            return None
 
        scheme, _, rest = value.partition(" ")
        scheme = scheme.lower()
        rest = rest.strip()
 
        if scheme == "basic":
            try:
                username, _, password = base64.b64decode(rest).decode().partition(":")
            except (binascii.Error, UnicodeError):
                return None
 
            return cls(scheme, {"username": username, "password": password})
 
        if "=" in rest.rstrip("="):
            # = that is not trailing, this is parameters.
            return cls(scheme, parse_dict_header(rest), None)
 
        # No = or only trailing =, this is a token.
        return cls(scheme, None, rest)
 
    def to_header(self) -> str:
        """Produce an ``Authorization`` header value representing this data.
 
        .. versionadded:: 2.0
        """
        if self.type == "basic":
            value = base64.b64encode(
                f"{self.username}:{self.password}".encode()
            ).decode("utf8")
            return f"Basic {value}"
 
        if self.token is not None:
            return f"{self.type.title()} {self.token}"
 
        return f"{self.type.title()} {dump_header(self.parameters)}"
 
    def __str__(self) -> str:
        return self.to_header()
 
    def __repr__(self) -> str:
        return f"<{type(self).__name__} {self.to_header()}>"
 
 
def auth_property(name: str, doc: str | None = None) -> property:
    """A static helper function for Authentication subclasses to add
    extra authentication system properties onto a class::
 
        class FooAuthenticate(WWWAuthenticate):
            special_realm = auth_property('special_realm')
 
    .. deprecated:: 2.3
        Will be removed in Werkzeug 3.0.
    """
    warnings.warn(
        "'auth_property' is deprecated and will be removed in Werkzeug 3.0.",
        DeprecationWarning,
        stacklevel=2,
    )
 
    def _set_value(self, value):  # type: ignore[no-untyped-def]
        if value is None:
            self.pop(name, None)
        else:
            self[name] = str(value)
 
    return property(lambda x: x.get(name), _set_value, doc=doc)
 
 
class WWWAuthenticate:
    """Represents the parts of a ``WWW-Authenticate`` response header.
 
    Set :attr:`.Response.www_authenticate` to an instance of list of instances to set
    values for this header in the response. Modifying this instance will modify the
    header value.
 
    Depending on the auth scheme, either :attr:`parameters` or :attr:`token` should be
    set. The ``Basic`` scheme will encode ``username`` and ``password`` parameters to a
    token.
 
    For convenience, ``auth["key"]`` and ``auth.key`` both act on the :attr:`parameters`
    dict, and can be used to get, set, or delete parameters. ``auth.get("key")`` and
    ``"key" in auth`` are also provided.
 
    .. versionchanged:: 2.3
        The ``token`` parameter and attribute was added to support auth schemes that use
        a token instead of parameters, such as ``Bearer``.
 
    .. versionchanged:: 2.3
        The object is no longer a ``dict``.
 
    .. versionchanged:: 2.3
        The ``on_update`` parameter was removed.
    """
 
    def __init__(
        self,
        auth_type: str | None = None,
        values: dict[str, str] | None = None,
        token: str | None = None,
    ):
        if auth_type is None:
            warnings.warn(
                "An auth type must be given as the first parameter. Assuming 'basic' is"
                " deprecated and will be removed in Werkzeug 3.0.",
                DeprecationWarning,
                stacklevel=2,
            )
            auth_type = "basic"
 
        self._type = auth_type.lower()
        self._parameters: dict[str, str] = CallbackDict(  # type: ignore[misc]
            values, lambda _: self._trigger_on_update()
        )
        self._token = token
        self._on_update: t.Callable[[WWWAuthenticate], None] | None = None
 
    def _trigger_on_update(self) -> None:
        if self._on_update is not None:
            self._on_update(self)
 
    @property
    def type(self) -> str:
        """The authorization scheme, like ``Basic``, ``Digest``, or ``Bearer``."""
        return self._type
 
    @type.setter
    def type(self, value: str) -> None:
        self._type = value
        self._trigger_on_update()
 
    @property
    def parameters(self) -> dict[str, str]:
        """A dict of parameters for the header. Only one of this or :attr:`token` should
        have a value for a give scheme.
        """
        return self._parameters
 
    @parameters.setter
    def parameters(self, value: dict[str, str]) -> None:
        self._parameters = CallbackDict(  # type: ignore[misc]
            value, lambda _: self._trigger_on_update()
        )
        self._trigger_on_update()
 
    @property
    def token(self) -> str | None:
        """A dict of parameters for the header. Only one of this or :attr:`token` should
        have a value for a give scheme.
        """
        return self._token
 
    @token.setter
    def token(self, value: str | None) -> None:
        """A token for the header. Only one of this or :attr:`parameters` should have a
        value for a given scheme.
 
        .. versionadded:: 2.3
        """
        self._token = value
        self._trigger_on_update()
 
    def set_basic(self, realm: str = "authentication required") -> None:
        """Clear any existing data and set a ``Basic`` challenge.
 
        .. deprecated:: 2.3
            Will be removed in Werkzeug 3.0. Create and assign an instance instead.
        """
        warnings.warn(
            "The 'set_basic' method is deprecated and will be removed in Werkzeug 3.0."
            " Create and assign an instance instead."
        )
        self._type = "basic"
        dict.clear(self.parameters)  # type: ignore[arg-type]
        dict.update(
            self.parameters,  # type: ignore[arg-type]
            {"realm": realm},  # type: ignore[dict-item]
        )
        self._token = None
        self._trigger_on_update()
 
    def set_digest(
        self,
        realm: str,
        nonce: str,
        qop: t.Sequence[str] = ("auth",),
        opaque: str | None = None,
        algorithm: str | None = None,
        stale: bool = False,
    ) -> None:
        """Clear any existing data and set a ``Digest`` challenge.
 
        .. deprecated:: 2.3
            Will be removed in Werkzeug 3.0. Create and assign an instance instead.
        """
        warnings.warn(
            "The 'set_digest' method is deprecated and will be removed in Werkzeug 3.0."
            " Create and assign an instance instead."
        )
        self._type = "digest"
        dict.clear(self.parameters)  # type: ignore[arg-type]
        parameters = {
            "realm": realm,
            "nonce": nonce,
            "qop": ", ".join(qop),
            "stale": "TRUE" if stale else "FALSE",
        }
 
        if opaque is not None:
            parameters["opaque"] = opaque
 
        if algorithm is not None:
            parameters["algorithm"] = algorithm
 
        dict.update(self.parameters, parameters)  # type: ignore[arg-type]
        self._token = None
        self._trigger_on_update()
 
    def __getitem__(self, key: str) -> str | None:
        return self.parameters.get(key)
 
    def __setitem__(self, key: str, value: str | None) -> None:
        if value is None:
            if key in self.parameters:
                del self.parameters[key]
        else:
            self.parameters[key] = value
 
        self._trigger_on_update()
 
    def __delitem__(self, key: str) -> None:
        if key in self.parameters:
            del self.parameters[key]
            self._trigger_on_update()
 
    def __getattr__(self, name: str) -> str | None:
        return self[name]
 
    def __setattr__(self, name: str, value: str | None) -> None:
        if name in {"_type", "_parameters", "_token", "_on_update"}:
            super().__setattr__(name, value)
        else:
            self[name] = value
 
    def __delattr__(self, name: str) -> None:
        del self[name]
 
    def __contains__(self, key: str) -> bool:
        return key in self.parameters
 
    def __eq__(self, other: object) -> bool:
        if not isinstance(other, WWWAuthenticate):
            return NotImplemented
 
        return (
            other.type == self.type
            and other.token == self.token
            and other.parameters == self.parameters
        )
 
    def get(self, key: str, default: str | None = None) -> str | None:
        return self.parameters.get(key, default)
 
    @classmethod
    def from_header(cls, value: str | None) -> te.Self | None:
        """Parse a ``WWW-Authenticate`` header value and return an instance, or ``None``
        if the value is empty.
 
        :param value: The header value to parse.
 
        .. versionadded:: 2.3
        """
        if not value:
            return None
 
        scheme, _, rest = value.partition(" ")
        scheme = scheme.lower()
        rest = rest.strip()
 
        if "=" in rest.rstrip("="):
            # = that is not trailing, this is parameters.
            return cls(scheme, parse_dict_header(rest), None)
 
        # No = or only trailing =, this is a token.
        return cls(scheme, None, rest)
 
    def to_header(self) -> str:
        """Produce a ``WWW-Authenticate`` header value representing this data."""
        if self.token is not None:
            return f"{self.type.title()} {self.token}"
 
        if self.type == "digest":
            items = []
 
            for key, value in self.parameters.items():
                if key in {"realm", "domain", "nonce", "opaque", "qop"}:
                    value = quote_header_value(value, allow_token=False)
                else:
                    value = quote_header_value(value)
 
                items.append(f"{key}={value}")
 
            return f"Digest {', '.join(items)}"
 
        return f"{self.type.title()} {dump_header(self.parameters)}"
 
    def __str__(self) -> str:
        return self.to_header()
 
    def __repr__(self) -> str:
        return f"<{type(self).__name__} {self.to_header()}>"
 
    @property
    def qop(self) -> set[str]:
        """The ``qop`` parameter as a set.
 
        .. deprecated:: 2.3
            Will be removed in Werkzeug 3.0. It will become the same as other
            parameters, returning a string.
        """
        warnings.warn(
            "The 'qop' property is deprecated and will be removed in Werkzeug 3.0."
            " It will become the same as other parameters, returning a string.",
            DeprecationWarning,
            stacklevel=2,
        )
 
        def on_update(value: HeaderSet) -> None:
            if not value:
                if "qop" in self:
                    del self["qop"]
 
                return
 
            self.parameters["qop"] = value.to_header()
 
        return parse_set_header(self.parameters.get("qop"), on_update)
 
    @property
    def stale(self) -> bool | None:
        """The ``stale`` parameter as a boolean.
 
        .. deprecated:: 2.3
            Will be removed in Werkzeug 3.0. It will become the same as other
            parameters, returning a string.
        """
        warnings.warn(
            "The 'stale' property is deprecated and will be removed in Werkzeug 3.0."
            " It will become the same as other parameters, returning a string.",
            DeprecationWarning,
            stacklevel=2,
        )
 
        if "stale" in self.parameters:
            return self.parameters["stale"].lower() == "true"
 
        return None
 
    @stale.setter
    def stale(self, value: bool | str | None) -> None:
        if value is None:
            if "stale" in self.parameters:
                del self.parameters["stale"]
 
            return
 
        if isinstance(value, bool):
            warnings.warn(
                "Setting the 'stale' property to a boolean is deprecated and will be"
                " removed in Werkzeug 3.0.",
                DeprecationWarning,
                stacklevel=2,
            )
            self.parameters["stale"] = "TRUE" if value else "FALSE"
        else:
            self.parameters["stale"] = value
 
    auth_property = staticmethod(auth_property)
 
 
def _deprecated_dict_method(f):  # type: ignore[no-untyped-def]
    @wraps(f)
    def wrapper(*args, **kwargs):  # type: ignore[no-untyped-def]
        warnings.warn(
            "Treating 'Authorization' and 'WWWAuthenticate' as a dict is deprecated and"
            " will be removed in Werkzeug 3.0. Use the 'parameters' attribute instead.",
            DeprecationWarning,
            stacklevel=2,
        )
        return f(*args, **kwargs)
 
    return wrapper
 
 
for name in (
    "__iter__",
    "clear",
    "copy",
    "items",
    "keys",
    "pop",
    "popitem",
    "setdefault",
    "update",
    "values",
):
    f = _deprecated_dict_method(getattr(dict, name))
    setattr(Authorization, name, f)
    setattr(WWWAuthenticate, name, f)