def attach_oauth(app: fastapi.FastAPI):
    """
    Attaches OAuth functionality to a FastAPI application by adding OAuth routes and session middleware.

    Args:
        app (fastapi.FastAPI): The FastAPI application instance to which OAuth routes and middleware will be attached.

    Notes:
        - The session middleware requires a secret key to sign the cookies. A hash of the OAuth secret key is used to
          make it unique to the Space and to ensure it is updated if the OAuth configuration changes.
        - The session secret includes a version identifier ("-autotrain-v2") to allow for future changes in the session
          cookie format. If the format changes, the version can be bumped to invalidate old cookies and prevent HTTP 500 errors.
    """
    _add_oauth_routes(app)
    # Session Middleware requires a secret key to sign the cookies. Let's use a hash
    # of the OAuth secret key to make it unique to the Space + updated in case OAuth
    # config gets updated.
    session_secret = OAUTH_CLIENT_SECRET + "-autotrain-v2"
    # ^ if we change the session cookie format in the future, we can bump the version of the session secret to make
    #   sure cookies are invalidated. Otherwise some users with an old cookie format might get a HTTP 500 error.
    app.add_middleware(
        SessionMiddleware,
        secret_key=hashlib.sha256(session_secret.encode()).hexdigest(),
        https_only=True,
        same_site="none",
    )

    Attaches OAuth functionality to a FastAPI application by adding OAuth routes and session middleware.

    Args:
        app (fastapi.FastAPI): The FastAPI application instance to which OAuth routes and middleware will be attached.

    Notes:
        - The session middleware requires a secret key to sign the cookies. A hash of the OAuth secret key is used to
          make it unique to the Space and to ensure it is updated if the OAuth configuration changes.
        - The session secret includes a version identifier ("-autotrain-v2") to allow for future changes in the session
          cookie format. If the format changes, the version can be bumped to invalidate old cookies and prevent HTTP 500 errors.
    
src/autotrain/app/oauth.py