Session variables set from HTTP headers #9079
-
Is it possible to set session variables from HTTP headers? I realise these are not protected by signatures but I'd like to do something similar to what the So, e.g. I user has an allowed role of I realise that I can achieve all of this by just adding filters to query. However, I'd prefer not having to build the queries according to role, yet, allow injecting variable context via the header when a user has assumed a specific role. So, in the example above, the user would select the department once and then stay in "department X" mode until they make a new selection and start sending a different value in the header. Can something like this be done at all? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Hello @christian-detexian Absolutely , you can use that type of custom headers. But for Hasura to detect, you'll need to have a prefix of An example with a scenarioLet's say I am a user in two different organizations. So in my application, at a time I can only be present in one organization. So you must have seen there's a toggle option or "switch organization" setting which is provided to user if he/she wants to switch organization. Now upon login, they will select organization and then they will store that (this is example in javascript code, but you can directly pass header as "X-Hasura-Organization-Id" in your API explorer) return res.status(200).json({
'X-Hasura-Role': role,
'X-Hasura-Organization-Id': user.organizationId || null,
'X-Hasura-User-Id': user.id || null,
'X-Hasura-Auth0User-Id': user.auth0UserId || null
}) In permission view for user role in hasura consoleNow in Hasura console, you can use that header simply as "X-Hasura-Organization-Id" for applying checks/permissions for specific user role. In your Hasura GraphiQL API explorerAlso you can use them in GraphiQL API explorer which is provided in hasura console, see the attached screenshot here So in this case of example, whenever user switches their orgnanization, there will be different organization-id being passed to hasura auth webhook. Please refer to the following docs links here
Feel free to ask if you have any doubts here, and I hope this makes sense. |
Beta Was this translation helpful? Give feedback.
-
I realised that I do still have a question that may or may not be answered above (if it's the former, I might just be slow). Can the header be injected without using auth webhooks? I'd like to allow people to inject in the header whatever they want because I will do the sanity checking in my permissions. So, to use the organisation example above, a user can send any organisation they want but the permissions will check the sent organisation against an allow list. As far as I can tell, I cannot actually allow a custom header to turn into a session variable unless I use a webhook that modifies the session. Is that correct? |
Beta Was this translation helpful? Give feedback.
-
Is this true for v3? I searched the docs and found nothing. |
Beta Was this translation helpful? Give feedback.
Hello @christian-detexian
Absolutely , you can use that type of custom headers. But for Hasura to detect, you'll need to have a prefix of
x-hasura-*
for your header. So for e.g., it could be likeX-Hasura-Organization-Id
.An example with a scenario
Let's say I am a user in two different organizations. So in my application, at a time I can only be present in one organization. So you must have seen there's a toggle option or "switch organization" setting which is provided to user if he/she wants to switch organization.
Now upon login, they will select organization and then they will store that
organization_id
and pass it to hasura auth webhook. Now webhook can return the response such as(t…