user_login_manager.userlogin#

Definition of the user management and login API endpoints (accessible through swagger UI).

Attributes#

Functions#

get_current_user(]) → scanhub_libraries.models.User)

Get current user from access_token. May be called as an endpoint or used in FastAPI with Depends.

get_current_user_admin(]) → 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.

loginfromcookie(] = None) → scanhub_libraries.models.User)

Login endpoint for login with cookie.

login(], response)

Login endpoint.

logout(], response)

Logout endpoint.

get_user_out(→ scanhub_libraries.models.User)

Convert UserSQL to User, replace token with empty string.

get_user_list(]) → list[scanhub_libraries.models.User])

Get all users endpoint (only admins).

check_no_users(→ bool)

Check if there are no users in the database.

create_user_internal(new_user)

Create user database entry (only admins).

create_user(current_user, new_user)

Create user database entry (only admins).

create_first_user(first_user)

Create first user.

user_delete(], username_to_delete)

Delete an existing user (requires admin priviledges).

update_user(], updated_user)

Update the first_name, last_name, email and role of an existing user.

change_password(], password_update_request, response)

Change password of a user. Only administrators may change passwords of other users.

Module Contents#

user_login_manager.userlogin.MAX_INACTIVITY_TIME_SECONDS = 3600#
user_login_manager.userlogin.MAX_SESSION_LENGTH_SECONDS = 39600#
user_login_manager.userlogin.LOG_CALL_DELIMITER = '-------------------------------------------------------------------------------'#
user_login_manager.userlogin.router#
async user_login_manager.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>”

Return type:

User pydantic model, the user data of the current user.

Raises:

HTTPException – 401: Unauthorized if the token is invalid or outdated.

async user_login_manager.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>”

Return type:

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 user_login_manager.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.

Return type:

User pydantic model, the user data in case of a successful login.

Raises:

HTTPException – 401: Unauthorized if the username or password is wrong.

async user_login_manager.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.

Return type:

User pydantic model, the user data in case of a successful login.

Raises:

HTTPException – 401: Unauthorized if the username or password is wrong.

async user_login_manager.userlogin.logout(user: Annotated[scanhub_libraries.models.User, Depends(get_current_user)], response: fastapi.Response) → None#

Logout endpoint.

async user_login_manager.userlogin.get_user_out(user_db: app.db.UserSQL) → scanhub_libraries.models.User#

Convert UserSQL to User, replace token with empty string.

async user_login_manager.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).

Return type:

List of all users. The access_token and token_type properties are set to “” for all of them.

async user_login_manager.userlogin.check_no_users() → bool#

Check if there are no users in the database.

Return type:

True, if there are no users in the database.

async user_login_manager.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 user_login_manager.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 user_login_manager.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 user_login_manager.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 user_login_manager.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.

Return type:

None

Raises:

HTTPException – 404: Not found if user not found.

async user_login_manager.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

Return type:

None

Raises:

HTTPException – 400: New Password must have at least 12 characters. Old Password must be correct.