Rescue Paperjet v1 implementation
This commit is contained in:
parent
7ff5c8130d
commit
db9e2ca51b
83 changed files with 6186 additions and 3894 deletions
|
|
@ -31,37 +31,37 @@ def get_auth_status(request: Request, db: Session = Depends(get_db)) -> AuthStat
|
|||
app_settings = Settings()
|
||||
db.add(app_settings)
|
||||
db.commit()
|
||||
|
||||
|
||||
setup_required = app_settings.password_hash is None
|
||||
|
||||
|
||||
token = request.cookies.get(settings.COOKIE_NAME)
|
||||
logged_in = False
|
||||
if token:
|
||||
user_id = verify_session(token)
|
||||
if user_id is not None:
|
||||
logged_in = True
|
||||
|
||||
|
||||
return AuthStatusResponse(setupRequired=setup_required, loggedIn=logged_in)
|
||||
|
||||
|
||||
@router.post("/setup", dependencies=[Depends(verify_csrf)])
|
||||
def setup_password(
|
||||
data: SetupRequest,
|
||||
response: Response,
|
||||
data: SetupRequest,
|
||||
response: Response,
|
||||
db: Session = Depends(get_db)
|
||||
) -> dict[str, str]:
|
||||
"""First-run setup."""
|
||||
app_settings = db.query(Settings).first()
|
||||
if app_settings and app_settings.password_hash:
|
||||
raise HTTPException(status_code=409, detail="Password already set")
|
||||
|
||||
|
||||
if not app_settings:
|
||||
app_settings = Settings()
|
||||
db.add(app_settings)
|
||||
|
||||
|
||||
app_settings.password_hash = hash_password(data.password)
|
||||
db.commit()
|
||||
|
||||
|
||||
# Log them in automatically
|
||||
create_session(response)
|
||||
return {"status": "ok"}
|
||||
|
|
@ -77,16 +77,16 @@ def login(
|
|||
"""Validate password and issue session cookie."""
|
||||
ip = get_client_ip(request)
|
||||
check_rate_limit(ip)
|
||||
|
||||
|
||||
app_settings = db.query(Settings).first()
|
||||
if not app_settings or not app_settings.password_hash:
|
||||
record_failed_attempt(ip)
|
||||
raise HTTPException(status_code=401, detail="Invalid credentials")
|
||||
|
||||
|
||||
if not verify_password(app_settings.password_hash, data.password):
|
||||
record_failed_attempt(ip)
|
||||
raise HTTPException(status_code=401, detail="Invalid credentials")
|
||||
|
||||
|
||||
clear_attempts(ip)
|
||||
create_session(response)
|
||||
return {"status": "ok"}
|
||||
|
|
@ -108,17 +108,17 @@ def change_password(
|
|||
"""Change an existing password."""
|
||||
ip = get_client_ip(request)
|
||||
check_rate_limit(ip)
|
||||
|
||||
|
||||
app_settings = db.query(Settings).first()
|
||||
if not app_settings or not app_settings.password_hash:
|
||||
raise HTTPException(status_code=400, detail="Setup required first")
|
||||
|
||||
|
||||
if not verify_password(app_settings.password_hash, data.current_password):
|
||||
record_failed_attempt(ip)
|
||||
raise HTTPException(status_code=401, detail="Invalid current password")
|
||||
|
||||
|
||||
clear_attempts(ip)
|
||||
app_settings.password_hash = hash_password(data.new_password)
|
||||
db.commit()
|
||||
|
||||
|
||||
return {"status": "ok"}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue