app.userlogin ============= .. py:module:: app.userlogin .. autoapi-nested-parse:: Definition of the user management and login API endpoints (accessible through swagger UI). Attributes ---------- .. autoapisummary:: app.userlogin.MAX_INACTIVITY_TIME_SECONDS app.userlogin.MAX_SESSION_LENGTH_SECONDS app.userlogin.LOG_CALL_DELIMITER app.userlogin.router Functions --------- .. autoapisummary:: app.userlogin.get_current_user app.userlogin.get_current_user_admin app.userlogin.loginfromcookie app.userlogin.login app.userlogin.logout app.userlogin.get_user_out app.userlogin.get_user_list app.userlogin.check_no_users app.userlogin.create_user_internal app.userlogin.create_user app.userlogin.create_first_user app.userlogin.user_delete app.userlogin.update_user app.userlogin.change_password Module Contents --------------- .. py:data:: MAX_INACTIVITY_TIME_SECONDS :value: 3600 .. py:data:: MAX_SESSION_LENGTH_SECONDS :value: 39600 .. py:data:: LOG_CALL_DELIMITER :value: '-------------------------------------------------------------------------------' .. py:data:: router .. py:function:: get_current_user(access_token: Annotated[str, Depends(oauth2_scheme)]) -> scanhub_libraries.models.User :async: 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 " Returns ------- User pydantic model, the user data of the current user. Raises ------ HTTPException 401: Unauthorized if the token is invalid or outdated. .. py:function:: get_current_user_admin(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user)]) -> scanhub_libraries.models.User :async: 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 " 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. .. py:function:: loginfromcookie(response: fastapi.Response, access_token: Annotated[Optional[str], Cookie()] = None) -> scanhub_libraries.models.User :async: 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. .. py:function:: login(form_data: Annotated[fastapi.security.OAuth2PasswordRequestForm, Depends()], response: fastapi.Response) -> scanhub_libraries.models.User :async: 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. .. py:function:: logout(user: Annotated[scanhub_libraries.models.User, Depends(get_current_user)], response: fastapi.Response) -> None :async: Logout endpoint. .. py:function:: get_user_out(user_db: app.db.UserSQL) -> scanhub_libraries.models.User :async: Convert UserSQL to User, replace token with empty string. .. py:function:: get_user_list(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user_admin)]) -> list[scanhub_libraries.models.User] :async: 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. .. py:function:: check_no_users() -> bool :async: Check if there are no users in the database. Returns ------- True, if there are no users in the database. .. py:function:: create_user_internal(new_user: scanhub_libraries.models.User) :async: 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. .. py:function:: create_user(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user_admin)], new_user: scanhub_libraries.models.User) :async: 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. .. py:function:: create_first_user(first_user: scanhub_libraries.models.User) :async: 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. .. py:function:: user_delete(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user_admin)], username_to_delete: str) -> None :async: Delete an existing user (requires admin priviledges). Parameters ---------- username_to_delete Name of the user to delete. Raises ------ HTTPException 404: Not found .. py:function:: update_user(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user_admin)], updated_user: scanhub_libraries.models.User) -> None :async: 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. .. py:function:: change_password(current_user: Annotated[scanhub_libraries.models.User, Depends(get_current_user)], password_update_request: scanhub_libraries.models.PasswordUpdateRequest, response: fastapi.Response) -> None :async: 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.