-
-
Notifications
You must be signed in to change notification settings - Fork 566
/
auth.py
39 lines (32 loc) · 1.25 KB
/
auth.py
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
# -*- coding: utf-8 -*-
"""
proxy.py
~~~~~~~~
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
Network monitoring, controls & Application development, testing, debugging.
:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
.. spelling::
auth
http
"""
from typing import Optional
from ...http import httpHeaders
from ..exception import ProxyAuthenticationFailed
from ...http.proxy import HttpProxyBasePlugin
from ...http.parser import HttpParser
class AuthPlugin(HttpProxyBasePlugin):
"""Performs proxy authentication."""
def before_upstream_connection(
self, request: HttpParser,
) -> Optional[HttpParser]:
if self.flags.auth_code:
request.headers = request.headers or {}
if httpHeaders.PROXY_AUTHORIZATION not in request.headers:
raise ProxyAuthenticationFailed()
parts = request.headers[httpHeaders.PROXY_AUTHORIZATION][1].split()
if len(parts) != 2 \
or parts[0].lower() != b'basic' \
or parts[1] != self.flags.auth_code:
raise ProxyAuthenticationFailed()
return request