Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.
Open
Changes from 3 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
67 changes: 67 additions & 0 deletions app/routes/account/renew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pexpect
Comment thread
singingtelegram marked this conversation as resolved.

import ocflib.account.validators as validators

from fastapi import HTTPException
from pydantic import BaseModel, Field

from routes import router


class ExpiredPasswordInput(BaseModel):
username: str = Field(
min_length=3,
max_length=16,
)
old_password: str = Field(
min_length=5,
max_length=256,
)
new_password: str = Field(
min_length=12,
max_length=256,
)


class ExpiredPasswordOutput(BaseModel):
output: str
error: str


@router.post("/account/renew", tags=["account"], response_model=ExpiredPasswordOutput)
def reset_password(data: ExpiredPasswordInput):
Comment thread
singingtelegram marked this conversation as resolved.
Outdated
try:
validators.validate_username(data.username)
validators.validate_password(
data.username, data.old_password, strength_check=False
)
validators.validate_password(
data.username, data.new_password, strength_check=True
)
except ValueError as ex:
raise HTTPException(status_code=400, detail=str(ex))
cmd = "kinit --no-forwardable -l0 {}@OCF.BERKELEY.EDU".format(data.username)
child = pexpect.spawn(cmd, timeout=10)
child.expect("{}@OCF.BERKELEY.EDU's Password:".format(data.username))
child.sendline(data.old_password)
try:
result = child.expect(["incorrect", "unknown", pexpect.EOF, "expired"])
if result == 0:
raise HTTPException(status_code=403, detail="Authentication failed")
Comment thread
singingtelegram marked this conversation as resolved.
Outdated
Comment thread
singingtelegram marked this conversation as resolved.
Outdated
elif result == 1:
raise HTTPException(status_code=400, detail="Unknown user")
elif result == 2:
raise HTTPException(status_code=400, detail="Password not expired")
else:
child.sendline(data.new_password)
child.expect("\r\nRepeat new password:")
child.sendline(data.new_password)
child.expect("\r\nSuccess: Password changed\r\n")
output = "Password successfully updated!"
error = ""
except pexpect.exceptions.TIMEOUT:
raise HTTPException(
status_code=400, detail="Please double check your credentials"
)

return {"output": output, "error": error}
Comment thread
singingtelegram marked this conversation as resolved.
Outdated