mirror of
https://github.com/fusion44/blitz_api.git
synced 2026-08-17 12:26:11 +02:00
Add endpoint to change passwords (#85)
This commit is contained in:
parent
ce7f82c106
commit
8fa239f135
2 changed files with 49 additions and 4 deletions
|
|
@ -77,7 +77,6 @@ def parse_key_value_lines(lines: list) -> dict:
|
|||
def parse_key_value_text(text: str) -> dict:
|
||||
return parse_key_value_lines(text.splitlines())
|
||||
|
||||
|
||||
def password_valid(password: str):
|
||||
if len(password) < 8:
|
||||
return False
|
||||
|
|
@ -85,7 +84,6 @@ def password_valid(password: str):
|
|||
return False
|
||||
return re.match("^[a-zA-Z0-9]*$", password)
|
||||
|
||||
|
||||
def name_valid(password: str):
|
||||
if len(password) < 3:
|
||||
return False
|
||||
|
|
@ -93,6 +91,43 @@ def name_valid(password: str):
|
|||
return False
|
||||
return re.match("^[\.a-zA-Z0-9-_]*$", password)
|
||||
|
||||
async def password_change(type: str, old_password: str, new_password: str):
|
||||
|
||||
# check just allowed type values
|
||||
type = type.lower()
|
||||
if not type in ["a","b","c"]:
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="unkown password type")
|
||||
|
||||
# check password formattings
|
||||
if not password_valid(old_password):
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="old password format unvalid")
|
||||
if not password_valid(new_password):
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="new password format unvalid")
|
||||
|
||||
if PLATFORM == APIPlatform.RASPIBLITZ:
|
||||
|
||||
# first check if old password is correct
|
||||
result = await call_script(
|
||||
f"/home/admin/config.scripts/blitz.passwords.sh check {type} \"{old_password}\""
|
||||
)
|
||||
data = parse_key_value_text(result)
|
||||
if not data["correct"] == "1":
|
||||
raise HTTPException(status.HTTP_406_NOT_ACCEPTABLE, detail="old password not correct")
|
||||
|
||||
# second set new password
|
||||
scriptcall=f"/home/admin/config.scripts/blitz.passwords.sh set {type} \"{new_password}\""
|
||||
if type == "c":
|
||||
# will set password c of both lnd & core lightning if installed/activated
|
||||
scriptcall=f"/home/admin/config.scripts/blitz.passwords.sh set c \"{old_password}\" \"{new_password}\""
|
||||
result = await call_script(scriptcall)
|
||||
data = parse_key_value_text(result)
|
||||
print(str(data))
|
||||
if "error" in data.keys() and len(data["error"])>0:
|
||||
raise HTTPException(status.HTTP_501_NOT_IMPLEMENTED, detail=data["error"])
|
||||
return
|
||||
|
||||
else:
|
||||
raise HTTPException(status.HTTP_501_NOT_IMPLEMENTED, detail="endpoint just works on raspiblitz so far")
|
||||
|
||||
async def get_system_info() -> SystemInfo:
|
||||
try:
|
||||
|
|
@ -102,7 +137,6 @@ async def get_system_info() -> SystemInfo:
|
|||
except NotImplementedError as r:
|
||||
raise HTTPException(status.HTTP_501_NOT_IMPLEMENTED, detail=r.args[0])
|
||||
|
||||
|
||||
async def get_hardware_info() -> map:
|
||||
return await get_hardware_info_impl()
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ from app.repositories.system import (
|
|||
get_system_info,
|
||||
parse_key_value_text,
|
||||
password_valid,
|
||||
password_change,
|
||||
shutdown,
|
||||
subscribe_hardware_info,
|
||||
)
|
||||
|
|
@ -52,7 +53,7 @@ async def login(i: LoginInput):
|
|||
# script does not work when called from api yet
|
||||
if password_valid(i.password):
|
||||
result = await call_script(
|
||||
f"/home/admin/config.scripts/blitz.passwords.sh check a {i.password}"
|
||||
f"/home/admin/config.scripts/blitz.passwords.sh check a \"{i.password}\""
|
||||
)
|
||||
data = parse_key_value_text(result)
|
||||
if data["correct"] == "1":
|
||||
|
|
@ -76,6 +77,16 @@ def refresh_token():
|
|||
return sign_jwt()
|
||||
|
||||
|
||||
@router.post(
|
||||
"/change-password",
|
||||
name=f"{_PREFIX}.change-password",
|
||||
summary="Endpoint to change your password a, b or c",
|
||||
response_description="if 200 OK - password change worked",
|
||||
dependencies=[Depends(JWTBearer())],
|
||||
)
|
||||
async def change_password(type: str, old_password: str, new_password: str):
|
||||
return await password_change(type, old_password, new_password)
|
||||
|
||||
@router.get(
|
||||
"/get-system-info",
|
||||
name=f"{_PREFIX}.get-system-info",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue