Skip to content

Redirect URI while behind a nginx proxy #134

@leshravnya

Description

@leshravnya

I am trying to deploy my app using nginx as a proxy. My fastapi app is running on http://localhost:8000 and the nginx is listening to the public IP on port 443. I have configured the redirect_uri for the login as http://localhost:8000/auth/callback. However, after authentication from the SSO server, it redirects to the http://localhost:8000/auth/callback instead of the https://domain.example.com/auth/callback.

I tried to change the auth URI from http://localhost:8000/auth/callback to https://domain.example.com/auth/callback and I get the below error in the callback. I need help in resolving this issue.

00 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/middleware/cors.py", line 83, in __call__
    await self.app(scope, receive, send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/routing.py", line 758, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/routing.py", line 778, in app
    await route.handle(scope, receive, send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/routing.py", line 299, in handle
    await self.app(scope, receive, send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/routing.py", line 79, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/starlette/routing.py", line 74, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 278, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/resource-management/backend_app/main.py", line 138, in login_callback
    openid = await microsoft_sso.verify_and_process(request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/fastapi_sso/sso/base.py", line 294, in verify_and_process
    return await self.process_login(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/fastapi_sso/sso/base.py", line 387, in process_login
    self.oauth_client.parse_request_body_response(json.dumps(content))
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 427, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 441, in parse_token_response
    validate_token_parameters(params)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 448, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "/home/ubuntu/resource-management/.venv/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 399, in raise_from_error
    raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.InvalidClientError: (invalid_client) AADSTS500112: The reply address 'http://localhost:8000/auth/callback' does not match the reply address 'https://domain.example.com/auth/callback' provided when requesting Authorization code. Trace ID: 811d0b87-c6ce-4407-acd5-050e443aa800 Correlation ID: d663ad03-3c0b-4dd3-bf73-20b11373edcb Timestamp: 2024-03-07 03:57:25Z

Metadata

Metadata

Assignees

No one assigned

    Labels

    more info neededMore info is needed in order to address the issue

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions