app.userlogin#
Definition of the user management and login API endpoints (accessible through swagger UI).
Attributes#
Functions#
|
Get current user from access_token. May be called as an endpoint or used in FastAPI with Depends. |
|
Check if the current_user has the role admin. May be (called as an endpoint or) used in FastAPI with Depends. |
|
Login endpoint for login with cookie. |
|
Login endpoint. |
|
Logout endpoint. |
|
Convert UserSQL to User, replace token with empty string. |
|
Get all users endpoint (only admins). |
|
Check if there are no users in the database. |
|
Create user database entry (only admins). |
|
Create user database entry (only admins). |
|
Create first user. |
|
Delete an existing user (requires admin priviledges). |
|
Update the first_name, last_name, email and role of an existing user. |
|
Change password of a user. Only administrators may change passwords of other users. |
Module Contents#
- app.userlogin.MAX_INACTIVITY_TIME_SECONDS = 3600#
- app.userlogin.MAX_SESSION_LENGTH_SECONDS = 39600#
- app.userlogin.LOG_CALL_DELIMITER = '-------------------------------------------------------------------------------'#
- app.userlogin.router#
- async app.userlogin.get_current_user(access_token: Annotated[str, Depends(oauth2_scheme)]) scanhub_libraries.models.User#
Get current user from access_token. May be called as an endpoint or used in FastAPI with Depends.
Parameters#
- access_token
User token as previously obtained trough a call to /login Submit via HTTP header “Authorization: Bearer <access_token>”
Returns#
User pydantic model, the user data of the current user.
Raises#
- HTTPException
401: Unauthorized if the token is invalid or outdated.
- async app.userlogin.get_current_user_admin(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user)]) scanhub_libraries.models.User#
Check if the current_user has the role admin. May be (called as an endpoint or) used in FastAPI with Depends.
Parameters#
- current_user
The current_user as determined by get_current_user from the access_token. Submit the access_token via HTTP header “Authorization: Bearer <access_token>”
Returns#
User pydantic model, the user data of the current user, who needs to have the role admin.
Raises#
- HTTPException
401: Unauthorized if the token is invalid or user is not admin.
- async app.userlogin.loginfromcookie(response: fastapi.Response, access_token: Annotated[str | None, Cookie()] = None) scanhub_libraries.models.User#
Login endpoint for login with cookie.
Parameters#
- access_token
User token as previously obtained trough a call to /login Submit via HTTP cookie.
Returns#
User pydantic model, the user data in case of a successful login.
Raises#
- HTTPException
401: Unauthorized if the username or password is wrong.
- async app.userlogin.login(form_data: Annotated[fastapi.security.OAuth2PasswordRequestForm, Depends()], response: fastapi.Response) scanhub_libraries.models.User#
Login endpoint.
Parameters#
- form_data
Http form data for OAuth2 compliant login with username and password.
Returns#
User pydantic model, the user data in case of a successful login.
Raises#
- HTTPException
401: Unauthorized if the username or password is wrong.
- async app.userlogin.logout(user: Annotated[scanhub_libraries.models.User, Depends(get_current_user)], response: fastapi.Response) None#
Logout endpoint.
- async app.userlogin.get_user_out(user_db: app.db.UserSQL) scanhub_libraries.models.User#
Convert UserSQL to User, replace token with empty string.
- async app.userlogin.get_user_list(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user_admin)]) list[scanhub_libraries.models.User]#
Get all users endpoint (only admins).
Returns#
List of all users. The access_token and token_type properties are set to “” for all of them.
- async app.userlogin.check_no_users() bool#
Check if there are no users in the database.
Returns#
True, if there are no users in the database.
- async app.userlogin.create_user_internal(new_user: scanhub_libraries.models.User)#
Create user database entry (only admins).
Parameters#
- new_user
pydantic base model of new user, token_type should be “password” and access_token should contain the password of the new user. The password of the new user should at least be 12 characters long.
- async app.userlogin.create_user(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user_admin)], new_user: scanhub_libraries.models.User)#
Create user database entry (only admins).
Parameters#
- new_user
pydantic base model of new user, token_type should be “password” and access_token should contain the password of the new user. The password of the new user should at least be 12 characters long.
- async app.userlogin.create_first_user(first_user: scanhub_libraries.models.User)#
Create first user.
Parameters#
- first_user
pydantic base model of the first user, token_type should be “password” and access_token should contain the password of the new user. The password of the new user should at least be 12 characters long. The role should be admin.
- async app.userlogin.user_delete(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user_admin)], username_to_delete: str) None#
Delete an existing user (requires admin priviledges).
Parameters#
- username_to_delete
Name of the user to delete.
Raises#
- HTTPException
404: Not found
- async app.userlogin.update_user(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user_admin)], updated_user: scanhub_libraries.models.User) None#
Update the first_name, last_name, email and role of an existing user.
Parameters#
- updated_user
The attribute username identifies the user to modify. The attributes first_name, last_name, email and role are set for this user.
Returns#
None
Raises#
- HTTPException
404: Not found if user not found.
- async app.userlogin.change_password(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user)], password_update_request: scanhub_libraries.models.PasswordUpdateRequest, response: fastapi.Response) None#
Change password of a user. Only administrators may change passwords of other users.
Parameters#
- password_update_request
.password_of_requester: the password of the requester .username_to_change_password_for: the username for whom to change the password .newpassword: the new password
Returns#
None
Raises#
- HTTPException
400: New Password must have at least 12 characters. Old Password must be correct.