Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove user agent header from python requests #403

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions app/apps/authenticatie/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging
from urllib import parse

import requests
from apps.authenticatie.forms import (
GebruikerAanmakenForm,
GebruikerAanpassenForm,
Expand All @@ -10,7 +8,6 @@
)
from apps.instellingen.models import Instelling
from apps.services.meldingen import MeldingenService
from django.conf import settings
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required, permission_required
Expand All @@ -26,31 +23,6 @@
logger = logging.getLogger(__name__)


def provider_logout(request):
logout_url = settings.OIDC_OP_LOGOUT_ENDPOINT
oidc_id_token = request.session.get("oidc_id_token", None)
redirect_url = request.build_absolute_uri(
location=request.GET.get("next", settings.LOGOUT_REDIRECT_URL)
)
if oidc_id_token:
logout_url = (
settings.OIDC_OP_LOGOUT_ENDPOINT
+ "?"
+ parse.urlencode(
{
"id_token_hint": oidc_id_token,
"post_logout_redirect_uri": redirect_url,
}
)
)
logout_response = requests.get(logout_url)
if logout_response.status_code != 200:
logger.error(
f"provider_logout: status code: {logout_response.status_code}, logout_url: {logout_url}"
)
return redirect_url


@method_decorator(
permission_required("authorisatie.gebruiker_bekijken"), name="dispatch"
)
Expand Down
3 changes: 1 addition & 2 deletions app/apps/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,7 @@ def meldingen_bestand(request):
meldingen_service = MeldingenService(request=request)
modified_path = request.path.replace(settings.MOR_CORE_URL_PREFIX, "")
url = f"{instelling.mor_core_basis_url}{modified_path}"
headers = {"Authorization": f"Token {meldingen_service.haal_token()}"}
response = requests.get(url, stream=True, headers=headers)
response = requests.get(url, stream=True, headers=meldingen_service.get_headers())
return StreamingHttpResponse(
response.raw,
content_type=response.headers.get("content-type"),
Expand Down
3 changes: 2 additions & 1 deletion app/apps/services/basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from urllib.parse import urlencode

import requests
import urllib3
from django.contrib import messages
from django.core.cache import cache
from requests import Request, Response
Expand Down Expand Up @@ -35,7 +36,7 @@ def get_url(self, url):
return url

def get_headers(self):
return {}
return {"user-agent": urllib3.util.SKIP_HEADER}

def naar_json(self, response):
try:
Expand Down
2 changes: 1 addition & 1 deletion app/apps/services/meldingen.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def haal_token(self):
return meldingen_token

def get_headers(self):
headers = {}
headers = super().get_headers()
if self._use_token:
headers.update({"Authorization": f"Token {self.haal_token()}"})
return headers
Expand Down
2 changes: 2 additions & 0 deletions app/apps/services/mercure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import jwt
import requests
import urllib3
from django.conf import settings
from django.core.validators import URLValidator

Expand Down Expand Up @@ -47,6 +48,7 @@ def __init__(self):

def _get_headers(self, token):
return {
"user-agent": urllib3.util.SKIP_HEADER,
"Authorization": f"Bearer {token}",
"Content-Type": "application/x-www-form-urlencoded",
}
Expand Down
9 changes: 7 additions & 2 deletions app/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from os.path import join

import requests
import urllib3

locale.setlocale(locale.LC_ALL, "nl_NL.UTF-8")
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -426,7 +427,12 @@ def show_debug_toolbar(request):
)
OPENID_CONFIG = {}
try:
OPENID_CONFIG = requests.get(OPENID_CONFIG_URI).json()
OPENID_CONFIG = requests.get(
OPENID_CONFIG_URI,
headers={
"user-agent": urllib3.util.SKIP_HEADER,
},
).json()
except Exception as e:
logger.error(f"OPENID_CONFIG FOUT, url: {OPENID_CONFIG_URI}, error: {e}")

Expand Down Expand Up @@ -465,7 +471,6 @@ def show_debug_toolbar(request):
"apps.authenticatie.auth.OIDCAuthenticationBackend",
]

# OIDC_OP_LOGOUT_URL_METHOD = "apps.authenticatie.views.provider_logout"
ALLOW_LOGOUT_GET_METHOD = True
OIDC_STORE_ID_TOKEN = True
OIDC_RENEW_ID_TOKEN_EXPIRY_SECONDS = int(
Expand Down
Loading