This repository was archived by the owner on Aug 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add renew endpoint #27
Open
singingtelegram
wants to merge
6
commits into
master
Choose a base branch
from
renew
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e0e1452
feat: add renew endpoint
singingtelegram 9247894
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 09bd039
fix: remove unused imports
singingtelegram d8b41d9
fix: misc fixes
singingtelegram 17b5540
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c5e5063
Merge branch 'master' into renew
singingtelegram File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import pexpect | ||
|
|
||
| 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.
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") | ||
|
singingtelegram marked this conversation as resolved.
Outdated
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} | ||
|
singingtelegram marked this conversation as resolved.
Outdated
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.