Home / File/ tutorial003_py39.py — fastapi Source File

tutorial003_py39.py — fastapi Source File

Architecture documentation for tutorial003_py39.py, a python file in the fastapi codebase. 4 imports, 0 dependents.

File python FastAPI Routing 4 imports 7 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  75cfd1a5_0803_efc1_bd73_03fd35666bbc["tutorial003_py39.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  75cfd1a5_0803_efc1_bd73_03fd35666bbc --> 0dda2280_3359_8460_301c_e98c77e78185
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  75cfd1a5_0803_efc1_bd73_03fd35666bbc --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  35c4ea20_c454_5afd_6cda_f0818bbcc650["__init__.py"]
  75cfd1a5_0803_efc1_bd73_03fd35666bbc --> 35c4ea20_c454_5afd_6cda_f0818bbcc650
  6913fbd4_39df_d14b_44bb_522e99b65b90["pydantic"]
  75cfd1a5_0803_efc1_bd73_03fd35666bbc --> 6913fbd4_39df_d14b_44bb_522e99b65b90
  style 75cfd1a5_0803_efc1_bd73_03fd35666bbc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Union

from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic import BaseModel

fake_users_db = {
    "johndoe": {
        "username": "johndoe",
        "full_name": "John Doe",
        "email": "johndoe@example.com",
        "hashed_password": "fakehashedsecret",
        "disabled": False,
    },
    "alice": {
        "username": "alice",
        "full_name": "Alice Wonderson",
        "email": "alice@example.com",
        "hashed_password": "fakehashedsecret2",
        "disabled": True,
    },
}

app = FastAPI()


def fake_hash_password(password: str):
    return "fakehashed" + password


oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: Union[str, None] = None
    full_name: Union[str, None] = None
    disabled: Union[bool, None] = None


class UserInDB(User):
    hashed_password: str


def get_user(db, username: str):
    if username in db:
        user_dict = db[username]
        return UserInDB(**user_dict)


def fake_decode_token(token):
    # This doesn't provide any security at all
    # Check the next version
    user = get_user(fake_users_db, token)
    return user


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Not authenticated",
            headers={"WWW-Authenticate": "Bearer"},
        )
    return user


async def get_current_active_user(current_user: User = Depends(get_current_user)):
    if current_user.disabled:
        raise HTTPException(status_code=400, detail="Inactive user")
    return current_user


@app.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    user_dict = fake_users_db.get(form_data.username)
    if not user_dict:
        raise HTTPException(status_code=400, detail="Incorrect username or password")
    user = UserInDB(**user_dict)
    hashed_password = fake_hash_password(form_data.password)
    if not hashed_password == user.hashed_password:
        raise HTTPException(status_code=400, detail="Incorrect username or password")

    return {"access_token": user.username, "token_type": "bearer"}


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_active_user)):
    return current_user

Domain

Subdomains

Classes

Dependencies

Frequently Asked Questions

What does tutorial003_py39.py do?
tutorial003_py39.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Routing subdomain.
What functions are defined in tutorial003_py39.py?
tutorial003_py39.py defines 7 function(s): fake_decode_token, fake_hash_password, get_current_active_user, get_current_user, get_user, login, read_users_me.
What does tutorial003_py39.py depend on?
tutorial003_py39.py imports 4 module(s): __init__.py, __init__.py, pydantic, typing.
Where is tutorial003_py39.py in the architecture?
tutorial003_py39.py is located at docs_src/security/tutorial003_py39.py (domain: FastAPI, subdomain: Routing, directory: docs_src/security).

Analyze Your Own Codebase

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

Try Supermodel Free