Home / Function/ get_current_user() — fastapi Function Reference

get_current_user() — fastapi Function Reference

Architecture documentation for the get_current_user() function in tutorial005_an_py39.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  9eae426a_5644_51f5_d589_ea188aea573e["get_current_user()"]
  874d355e_5626_0709_a216_de8189208c14["tutorial005_an_py39.py"]
  9eae426a_5644_51f5_d589_ea188aea573e -->|defined in| 874d355e_5626_0709_a216_de8189208c14
  ded6e2ed_6bb8_97a6_b77b_a772071f956d["get_user()"]
  9eae426a_5644_51f5_d589_ea188aea573e -->|calls| ded6e2ed_6bb8_97a6_b77b_a772071f956d
  style 9eae426a_5644_51f5_d589_ea188aea573e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

docs_src/security/tutorial005_an_py39.py lines 105–137

async def get_current_user(
    security_scopes: SecurityScopes, token: Annotated[str, Depends(oauth2_scheme)]
):
    if security_scopes.scopes:
        authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
    else:
        authenticate_value = "Bearer"
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": authenticate_value},
    )
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        username = payload.get("sub")
        if username is None:
            raise credentials_exception
        scope: str = payload.get("scope", "")
        token_scopes = scope.split(" ")
        token_data = TokenData(scopes=token_scopes, username=username)
    except (InvalidTokenError, ValidationError):
        raise credentials_exception
    user = get_user(fake_users_db, username=token_data.username)
    if user is None:
        raise credentials_exception
    for scope in security_scopes.scopes:
        if scope not in token_data.scopes:
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="Not enough permissions",
                headers={"WWW-Authenticate": authenticate_value},
            )
    return user

Domain

Subdomains

Calls

Frequently Asked Questions

What does get_current_user() do?
get_current_user() is a function in the fastapi codebase, defined in docs_src/security/tutorial005_an_py39.py.
Where is get_current_user() defined?
get_current_user() is defined in docs_src/security/tutorial005_an_py39.py at line 105.
What does get_current_user() call?
get_current_user() calls 1 function(s): get_user.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free