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

feat: add renew endpoint #27

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
73 changes: 73 additions & 0 deletions app/routes/account/renew_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pexpect

import ocflib.account.validators as validators

from fastapi import HTTPException, status
from pydantic import BaseModel, Field

from routes import router


class RenewPasswordInput(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 RenewPasswordOutput(BaseModel):
output: str


@router.post(
"/account/renew-password", tags=["account"], response_model=RenewPasswordOutput
)
def renew_password(data: RenewPasswordInput):
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.HTTP_400_BAD_REQUEST, 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=status.HTTP_401_UNAUTHORIZED, detail="Authentication failed"
)
elif result == 1:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Unknown user"
)
elif result == 2:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, 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!"
except pexpect.exceptions.TIMEOUT:
raise HTTPException(
status_code=400, detail="Please double check your credentials"
)

return {"output": output}
1 change: 1 addition & 0 deletions requirements-minimal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ celery[redis]==5.2.6
fastapi==0.75.1
ocflib
paramiko==2.10.1
pexpect==4.8.0
python-dateutil==2.8.2
python-dotenv==0.19.2
python-jose==3.3.0
Expand Down