aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/addons/stickyauth.py
blob: ab0599eef604f01580ccd201ff9d8a196633c17a (plain)
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
import typing

from mitmproxy import exceptions
from mitmproxy import flowfilter
from mitmproxy import ctx


class StickyAuth:
    def __init__(self):
        self.flt = None
        self.hosts = {}

    def load(self, loader):
        loader.add_option(
            "stickyauth", typing.Optional[str], None,
            "Set sticky auth filter. Matched against requests."
        )

    def configure(self, updated):
        if "stickyauth" in updated:
            if ctx.options.stickyauth:
                flt = flowfilter.parse(ctx.options.stickyauth)
                if not flt:
                    raise exceptions.OptionsError(
                        "stickyauth: invalid filter expression: %s" % ctx.options.stickyauth
                    )
                self.flt = flt
            else:
                self.flt = None

    def request(self, flow):
        if self.flt:
            host = flow.request.host
            if "authorization" in flow.request.headers:
                self.hosts[host] = flow.request.headers["authorization"]
            elif flowfilter.match(self.flt, flow):
                if host in self.hosts:
                    flow.request.headers["authorization"] = self.hosts[host]