Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
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
2 changes: 1 addition & 1 deletion api/src/routes/ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import APIRouter, Depends

from dependencies.jwt import jwt_bearer
from utils.IPV4 import (
from utils.ipv4 import (
bin_to_dec,
dec_to_bin,
dec_to_hex,
Expand Down
14 changes: 2 additions & 12 deletions api/src/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def verify_jwt(token: str) -> dict:
except JWTError as err:
raise HTTPException(status_code=403, detail="Invalid token") from err


def validate_password(password: str) -> bool:
"""Return True if the password is valid, False otherwise.

Expand All @@ -115,16 +114,7 @@ def validate_password(password: str) -> bool:

Returns:
bool: True if the password is valid, False otherwise

"""
if password is None:
return False
if len(password) < 8:
return False
if re.match(r"[A-Z]", password) is None:
return False
if re.match(r"[a-z]", password) is None:
return False
if re.match(r"[0-9]", password) is None:
if not password or len(password) < 8:
return False
return re.match("[^A-Za-z0-9]", password) is not None
return bool(re.match(r"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$", password))
94 changes: 57 additions & 37 deletions api/src/utils/ipv6.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Module for IPv6 utilities."""

def simplify(ipv6: str) -> str:
"""Simplify an IPv6 address.

Expand All @@ -10,28 +9,44 @@ def simplify(ipv6: str) -> str:
str: The simplified IPv6 address.

"""
parts = ipv6.split(":")
for part in parts:
if part == "0000":
parts[parts.index(part)] = "0"

is_zero = False
if ipv6.contains("::"):
is_zero = True
i = 0
while i < len(parts):
part = parts[i]
if part == "0":
parts.pop(i)
is_zero = True
elif is_zero:
parts.insert(i, "")
i = len(parts) # break
# Split the address into its groups
groups = ipv6.split(":")

# Step 1: Remove leading zeros in each group
groups = [group.lstrip("0") or "0" for group in groups]

# Step 2: Identify the longest run of consecutive "0" groups
zero_runs = []
current_start = None
for i, group in enumerate(groups):
if group == "0":
if current_start is None:
current_start = i
else:
i += 1

return ":".join(parts)

if current_start is not None:
zero_runs.append((current_start, i - 1))
current_start = None
if current_start is not None:
zero_runs.append((current_start, len(groups) - 1))

# Find the longest run of zeros
if zero_runs:
longest_run = max(zero_runs, key=lambda run: run[1] - run[0])
else:
longest_run = None

# Step 3: Replace the longest run of zeros with "::"
if longest_run:
start, end = longest_run
groups = groups[:start] + [""] + groups[end + 1:]

# Step 4: Reconstruct the compressed address
compressed_address = ":".join(groups)

# Ensure "::" is not accidentally replaced multiple times
compressed_address = compressed_address.replace(":::", "::")

return compressed_address
def extend(ipv6: str) -> str:
"""Extend an IPv6 address.

Expand All @@ -42,18 +57,23 @@ def extend(ipv6: str) -> str:
str: The extended IPv6 address.

"""
parts = ipv6.split(":")
i = 0
while i < len(parts):
if parts[i] == "":
parts[i] = "0000"
for _j in range(8 - len(parts)):
parts.insert(i, "0000")
i += 8 - len(parts)
elif parts[i] == "0":
parts[i] = "0000"
i += 1
else:
i += 1

return ":".join(parts)
if "::" in ipv6:
parts = ipv6.split("::")
left = parts[0].split(":") if parts[0] else []
right = parts[1].split(":") if len(parts) > 1 and parts[1] else []

# Calculate the number of groups needed to fill the gap
num_zeros_to_add = 8 - (len(left) + len(right))
middle = ["0000"] * num_zeros_to_add

# Combine all parts into a full address
full_address = left + middle + right
else:
# If no "::", simply split by ":" for expansion
full_address = ipv6.split(":")

# Step 2: Pad each group to 4 digits
expanded_address = [group.zfill(4) for group in full_address]

# Step 3: Join the groups with ":"
return ":".join(expanded_address)
Loading