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
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
singingtelegram marked this conversation as resolved.
Show resolved Hide 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):
singingtelegram marked this conversation as resolved.
Show resolved Hide resolved
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")
singingtelegram marked this conversation as resolved.
Show resolved Hide resolved
singingtelegram marked this conversation as resolved.
Show resolved Hide resolved
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}
singingtelegram marked this conversation as resolved.
Show resolved Hide resolved