Skip to content

Refactor: replace manual auth checks with annotated dependency injection (as per fast api docos) - #66

Merged
seanboyce merged 11 commits into
seanboyce:devfrom
singhgarima:dev
Apr 27, 2026
Merged

Refactor: replace manual auth checks with annotated dependency injection (as per fast api docos)#66
seanboyce merged 11 commits into
seanboyce:devfrom
singhgarima:dev

Conversation

@singhgarima

Copy link
Copy Markdown
Contributor

Refactors authentication and authorisation across all routers to use FastAPI's annotated dependency
injection pattern, replacing manual if user.isAdmin checks and Depends(get_current_user_from_token)
calls throughout.

Other Minor changes:

  • Fixed database session leaks across federation.py and files.py — all routes now use db =
    SessionLocal() before try with finally: db.close()
  • Added tests for the new auth dependency module (tests/dependencies/test_auth.py)

@singhgarima

Copy link
Copy Markdown
Contributor Author

I apologise that it is a bigger pull request but mainly it is all just DRYing up user annotation and admin logic everywhere 🙏

Comment thread ubiblio/routers/admin.py Outdated
Comment thread ubiblio/routers/admin.py
with open('export/csvBookExport' + date_time + '.csv', 'a') as f:
writer = csv.writer(f)
writer.writerows(bookData)
conn.close()

@seanboyce seanboyce Apr 26, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the f.close() that I had previously is necessary here. Otherwise there could be some changes that don't get written to the file properly.

(in contexts where we use aiofiles, sometimes we don't need to worry about closing files though, like further down on line 225)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked that f.close is not required within context i.e. under with

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah! You're right!

Comment thread ubiblio/dependencies/auth.py Outdated
Comment thread ubiblio/routers/admin.py
"user": user,
"request": request,
}
return templates.TemplateResponse(request, "userManagement.html", context)

@seanboyce seanboyce Apr 26, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest sticking to the try/except format here, but this one should never fail unless I've made a mistake in the jinjia2 template. So actually this is better, because I'll just get the raw jinjia2 error.

(no change needed here)

Comment thread ubiblio/routers/admin.py
print(e)
return "Could not promote user."
finally:
db.close()

@seanboyce seanboyce Apr 26, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally missed closing a few db sessions around here, good catch.

(no change needed here)


@router.post("/fedsearch", dependencies=[get_rate_limiter(times=1, seconds=5)], response_class=HTMLResponse)
async def fed_search_books(body: bytes = Depends(get_body)):
db = SessionLocal()

@seanboyce seanboyce Apr 26, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're forgetting to close this one, needs a:

finally:
db.close()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my mistake, ignore this one.


@router.post("/fedBookDetails/", dependencies=[get_rate_limiter(times=1, seconds=5)], response_class=HTMLResponse)
async def fed_book_details(body: bytes = Depends(get_body)):
db = SessionLocal()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, needs a:

finally:
db.close()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already there 😄 Does not show up in diff view L127-128

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same -- can ignore this one.

Comment thread ubiblio/routers/files.py Outdated
# --------------------------------------------------------------------------
@router.get("/downloadEbook/{filename}", dependencies=[get_rate_limiter(times=2, seconds=1)], response_class=HTMLResponse)
def download_ebook(request: Request, filename: str, user: schemas.User = Depends(get_current_user_from_token)):
def download_ebook(request: Request, filename: str, user: admin_user):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be current_user. Non-admin users should be able to download ebooks stored in ubiblio.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call 😄 I added it due to the existing exception message. I will update the exception message 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

db = SessionLocal()
books = []
withdrawnList = crud.browseWithdrawn(db)
for i in withdrawnList:

@seanboyce seanboyce Apr 26, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah! What was I thinking?

Your way is correct.

(you've already fixed this silly thing I did)

@seanboyce

Copy link
Copy Markdown
Owner

No worries about the big PR, that's what long weekends are for, and it legitimately needed a refactor.

I think I found a couple of bugs, which I've raised above. I figured you'd like to fix them yourself, but I can do instead if you're busy.

@singhgarima

Copy link
Copy Markdown
Contributor Author

Fixed most comments and replied to others :) Thank you so much for the review

@seanboyce

Copy link
Copy Markdown
Owner

OK, I think we're good. I'll merge this into dev, and mess with it a bit today. If OK, I might push to release later today.

@seanboyce
seanboyce merged commit de9e19c into seanboyce:dev Apr 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants