0

I was able to build from a sample Starlette example a piece of code that gets Basic Auth username and password, reads a header, and grabs the json body. But it only does so if I use "GET" instead of post, and I have not been able to figure out how to change the accepted method to POST. (The application I am trying to host for only uses POST. Is it a simple thing to get the POST method to work, or is this a rewrite?

    from starlette.applications import Starlette
    from starlette.authentication import requires
    from starlette.authentication import (
        AuthCredentials, AuthenticationBackend, AuthenticationError, SimpleUser
    )
    from starlette.middleware import Middleware
    from starlette.middleware.authentication import AuthenticationMiddleware
    from starlette.responses import (PlainTextResponse, JSONResponse)
    from starlette.routing import Route
    import base64
    import binascii
    
    class BasicAuthBackend(AuthenticationBackend):
        async def authenticate(self, conn):
            if "Authorization" not in conn.headers:
                return
    
            auth = conn.headers["Authorization"]
            try:
                scheme, credentials = auth.split()
                if scheme.lower() != 'basic':
                    return
                decoded = base64.b64decode(credentials).decode("ascii")
            except (ValueError, UnicodeDecodeError, binascii.Error) as exc:
                raise AuthenticationError('Invalid basic auth credentials')
            username, _, password = decoded.partition(":")
            global my_user
            global my_pass
            my_user = username
            my_pass = password
            # TODO: You'd want to verify the username and password here.
            return AuthCredentials(["authenticated"]), SimpleUser(username)
    
    
    async def homepage(request):
        if request.user.is_authenticated:
            body = await request.json()
            return JSONResponse({"user": my_user, "password": my_pass, "header": request.headers['client_id']}, body )
             
        return PlainTextResponse('Hello, you')
    
    routes = [
        Route("/testpath", endpoint=homepage)
    ]

middleware = [
    Middleware(AuthenticationMiddleware, backend=BasicAuthBackend())
]

app = Starlette(debug=True, routes=routes, middleware=middleware)

1 Answers1

0

You need mention that your route accepts POST method.

async def homepage(request):
    if request.user.is_authenticated:
        body = await request.json()
        return JSONResponse({"user": my_user, "password": my_pass, "header": request.headers['client_id']})

    return PlainTextResponse('Hello, you')


routes = [
    Route("/testpath", endpoint=homepage, methods=["POST"])
]
Irfanuddin
  • 2,295
  • 1
  • 15
  • 29