Authorization header in HTTP endpoints

We are using Convex for the backend, and we are exposing a couple of http endpoints. We need to be able to support sending the Authorization header (in the format of Authorization: Bearer <api token>) to our backend. We did not have any success with sending/extracting the <api token> header in our backend. Please see the below code snippet; the authHeader is null.
const myEndpoint = httpEndpoint(async ({ runAction, runQuery }, request) => {
const authHeader = request.headers.get("Authorization"); //authHeader is null
...
}
const myEndpoint = httpEndpoint(async ({ runAction, runQuery }, request) => {
const authHeader = request.headers.get("Authorization"); //authHeader is null
...
}
Is there any way to enable sending the auth header in a request?
14 Replies
sshader
sshader2y ago
What you're describing sounds like it should work, and nothing jumps out to me from looking at the code snippets. We support setting Authorization: Bearer <token>, where <token> is a JWT-encoded OpenID Connect identity token. This should populate auth.getUserIdentity() in the httpEndpoint with the appropriate user (and also the header should not be null). Is this what you're trying to do? Are you able to set and read other headers from your HTTP endpoints?
samira.barouti
samira.baroutiOP2y ago
The API token is something that we manage; they are the API keys that we generate and give to the end user to access our application.
jamwt
jamwt2y ago
@samira.barouti are you able to get the User-Agent header, for example?
samira.barouti
samira.baroutiOP2y ago
That I am able to get:
User-Agent: ' 'python-requests/2.28.2'
User-Agent: ' 'python-requests/2.28.2'
On a side note: we are using clerk for auth for actions/mutations/etc.
ballingt
ballingt2y ago
Can you print the request as it is sent from your Python script using requests, to confirm the Authorization header is there?
sshader
sshader2y ago
I think currently we only support sending something like a Clerk token in the Authorization header for an HTTP endpoint. As a workaround, using another header name for this API token should hopefully unblock you?
tylerkohn
tylerkohn2y ago
@ballingt to follow up on this. If I make a request like: curl -X POST https://hearty-crocodile-ABC.convex.site/test -d '{"name": "bob"}' -H "Content-Type: application/json" -H "X-API-key: apikeyvalue" my http endpoint get the request and I can process it. If I call: curl -X POST https://hearty-crocodile-ABC.convex.site/test -d '{"name": "bob"}' -H "Content-Type: application/json" -H "Authorization: Bearer apikeyvalue" I get: {"code":"InvalidHeader","message":"Invalid Authorization header"}%
and my http endpoint never gets the request
ballingt
ballingt2y ago
Ah ok, that makes sense
tylerkohn
tylerkohn2y ago
@sshader unfortunately this does not unblock us since some of our user are using clients that can only set the bearer token (we don't always control the client library, just the api endpoint) there might have been some misunderstanding during an earlier conversation that led us to believe that if we used an http endpoint we could be in control of the authorization but I don't think we explicitly said we'd need to use bearer tokens.
sshader
sshader2y ago
Yeah -- Convex is currently special casing the Authorization header, which means we're not supporting your use case of passing the api key in the Authorization header (but would allow any other header name as your example demonstrates). Your use case seems pretty reasonable, so we're brainstorming on our end some ways to support this.
tylerkohn
tylerkohn2y ago
thanks, makes sense. Auth is always a challenge!
ian
ian2y ago
After discussing internally, we are confident we can make this work, and that allowing the Authorization header to pass through is the right behavior. Will follow up when it's live.
tylerkohn
tylerkohn2y ago
ok, thanks!
ian
ian2y ago
@tylerkohn @samira.barouti you should be able to pass Authorization: Bearer blah in the header and have it pass through. Thanks for your patience!

Did you find this page helpful?