-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How to protect API routes without UI login #16
Comments
Ah yes - repro'd, will try and fix tonight. |
The fix in #17 means that this should work correctly, I'd missed something off in a refactoring! |
@djpugh thanks for fixing this. @soderluk I am just wondering if you get it working or not . Cause I still have some issues. I am using the example for here, with an import logging
logging.basicConfig(level="DEBUG")
from fastapi import APIRouter, Depends
from fastapi import FastAPI
from starlette.responses import HTMLResponse, PlainTextResponse, RedirectResponse
from starlette.requests import Request
from starlette.middleware.cors import CORSMiddleware
from starlette.routing import request_response, Route
import uvicorn
from fastapi_aad_auth import __version__, AADAuth, AuthenticationState
auth_provider = AADAuth()
router = APIRouter()
@router.get("/hello")
async def hello_world(
auth_state: AuthenticationState = Depends(auth_provider.api_auth_scheme),
):
print(auth_state)
return {"hello": "world"}
if "untagged" in __version__ or "unknown":
API_VERSION = 0
else:
API_VERSION = __version__.split(".")[0]
async def homepage(request):
if request.user.is_authenticated:
return PlainTextResponse("Hello, " + request.user.display_name)
return HTMLResponse(f"<html><body><h1>Hello, you</h1><br></body></html>")
@auth_provider.auth_required()
async def test(request):
if request.user.is_authenticated:
return PlainTextResponse("Hello, " + request.user.display_name)
routes = [Route("/", endpoint=homepage), Route("/test", endpoint=test)]
app = FastAPI(
title="fastapi_aad_auth test app",
description="Testapp for Adding Azure Active Directory Authentication for FastAPI",
version=__version__,
openapi_url=f"/api/v{API_VERSION}/openapi.json",
docs_url="/api/docs",
swagger_ui_init_oauth=auth_provider.api_auth_scheme.init_oauth,
redoc_url="/api/redoc",
routes=routes,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:8000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(router)
# comment out cause I want anonymous user to access the swagger UI
# auth_provider.configure_app(app)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", debug=True, port=8000, log_level="debug") I got this error but don't know where I can dig the root cause. Update: this is caused by CORS. Also another question, as you can see it ask users to input any help from you guys would be highly appreciated. |
@tricosmo: No I haven't tested the fix yet. I'm also just providing a pure API for any frontend to use, so the HTML things are not working out for me anyway. I need to figure out the endpoints how to initialize the auth flow to Azure from the frontend, instead of using the UI provided by fastapi_aad_auth. Also I find it quite frustrating, that when I enable the authentication for the docs, I have to log in every 3rd refresh or so... I think I just have to dig deeper into the module at some point. Right now I'm focusing on getting everything else ready in the API and focus on the auth later. |
That is provided by fastapi and the swaggerui component that's being used there - the values can/should be preconfigured on the UI if they're loaded in via the environement |
@soderluk - in terms of needing to login again, I'm assuming you mean when you are restarting the development server? You can set (as env variables or in a .env file):
Which will ensure restarting the server doesn't reset the cookie sessions |
thanks @djpugh. I got it working now. I have the same login again problem. It happens for every refresh of the swagger page. (server was running always) |
Released in 0.1.4 |
@djpugh I'm still struggling a bit how to do this whole thing without a UI... I mean I only have my API, and use Insomnia (or Postman) to interact with the API. Could you give some pointers, as to how I should go about to actually initialize the auth flow to Azure, then use a token in the headers to be able to use the endpoints? And as a bonus, does this module provide a callback, so that the user model could be persisted in the DB? |
First of all, thanks for your work on the library!
I'm struggling a bit trying to understand the usage of the library though.
How does one protect an API route without the need to have any UI?
I'm reading the docs, but cannot for the life of me figure out how to use this for a plain API.
The authentication works well when using the
/docs
endpoint, but e.g. using insomnia and reading endpoints, I just can't get the authentication to work.Having the dependency in the route
auth_state: AuthenticationState = Depends(auth_provider.api_auth_scheme)
shows theauth_state
asNone
.In my
main.py
I usein my routes:
Any help appreciated!
The text was updated successfully, but these errors were encountered: