feat(dgsi/isp): allow ISP to read the vlan of a user #17

Closed
lbailly wants to merge 1 commit from direct_vlan_acces into main
3 changed files with 32 additions and 0 deletions

View file

@ -244,6 +244,9 @@ VLAN_ID_MAX = 4094
VLAN_ID_MIN = (VLAN_ID_MAX - 850) + 1 VLAN_ID_MIN = (VLAN_ID_MAX - 850) + 1
VLAN_AUTOCONNECT = credentials.get("VLAN_AUTOCONNECT", False) VLAN_AUTOCONNECT = credentials.get("VLAN_AUTOCONNECT", False)
ISP_USERNAME = credentials["ISP_USERNAME"]
ISP_PASSWORD = credentials["ISP_PASSWORD"]
### ###
# Internationalization configuration # Internationalization configuration

View file

@ -59,6 +59,10 @@ urlpatterns = [
views.ServiceRedirectView.as_view(), views.ServiceRedirectView.as_view(),
name="service_redirect", name="service_redirect",
), ),
path(
"isp/vlan/<slug:username>/",
views.UserVlanISPView.as_view(),
),
### ###
# Profile views # Profile views
path( path(

View file

@ -1,3 +1,4 @@
import base64
from mimetypes import guess_type from mimetypes import guess_type
from typing import Any, NamedTuple from typing import Any, NamedTuple
@ -300,6 +301,30 @@ class ServiceRedirectView(LoginRequiredMixin, SingleObjectMixin, RedirectView):
return self.get_object().url return self.get_object().url
class UserVlanISPView(View):
def get(self, request: HttpRequest, **kwargs: Any):
if "HTTP_AUTHORIZATION" in request.META:
auth = request.META["HTTP_AUTHORIZATION"].split()
if len(auth) == 2 and auth[0].lower() == "basic":
name, passwd = base64.b64decode(auth[1]).split(b":")
if name == settings.ISP_USERNAME and passwd == settings.ISP_PASSWORD:
try:
user = User.get(
username=kwargs["username"], vlan_id__isnull=False
)
return HttpResponse(
str(user.vlan_id), content_type="text/plain"
)
except User.DoesNotExist:
return HttpResponse(status=400)
return HttpResponse(
status=401, headers={"WWW-Authenticate": 'Basic realm="DGNum ISP"'}
)
## ##
# INFO: Below are views related to the administration of DGSI # INFO: Below are views related to the administration of DGSI