Can I be in charge of verifying JWT, no setting of PGRST_JWT_SECRET (jwt-secret)? #2725
-
For example, I have the following code which works (and it uses the latest public signing keys: CREATE OR REPLACE FUNCTION auth.decode_role (jwt text)
RETURNS text
AS $$
import asyncio
from guardpost.jwts import JWTValidator
async def main():
validator = JWTValidator(
authority="https://login.microsoftonline.com/{TENANT_ID}/",
valid_issuers=["https://login.microsoftonline.com/{TENANT_ID}/v2.0"],
valid_audiences=["{CLIENT_ID}"]
)
return await validator.validate_jwt(jwt)
decoded = asyncio.run(main())
return decoded["preferred_username"]
$$ LANGUAGE plpython3u; Running this in psql:
Returns:
I want to know if I can then be in charge of having appropriate functions run a variabilized form of :
To create an authenticated request, I would just pass -H "Authorization: Bearer {ID_TOKEN}" in a curl request. Is it possible to define a flow such as this without setting jwt-secret ? P.S. Additionally in the future I might like to also include an additional header with the access token passed through to Microsoft Graph. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
No, such a manual verification of the JWT is not possible. You will need to provide PostgREST with the key somehow to validate the JWT itself. I looked at guardpost, which you use in your snippet, for a moment and I realized they are downloading the required JWKS automatically: https://github.com/Neoteroi/GuardPost#functions-to-validate-jwts That's actually quite nice. I wonder whether that could be implemented in PostgREST, too... |
Beta Was this translation helpful? Give feedback.
-
Yes currently I am dreading the task of key rotation and am putting it off (potentially it isn’t as big a task as I have bigged up). but if postgREST could be in charge of syncing the latest keys (it has all the access in order to do so), or to at least allow deferring to a function that can interact with the internet and update postgrest’s configuration (without environment variables taking effect again in the case containers being recreated) that would be great. |
Beta Was this translation helpful? Give feedback.
-
@dnk8n, checkout https://github.com/edgeflare/pgo. It's not yet as robust and reliable (please do give your feedback to make it so) as PostgREST, but enhances postgrest in a few ways:
rest:
listenAddr: ":8080"
pg:
connString: "host=localhost port=5432 user=postgrest password=secret dbname=testdb"
oidc:
issuer: https://iam.example.org
clientID: example-client-id
clientSecret: example-client-secret
roleClaimKey: .policies.pgrole
basicAuth:
admin: adminpw
user1: user1pw
anonRole: anon |
Beta Was this translation helpful? Give feedback.
Hm. Maybe it's already possible to do this right now. You can set the JWKS via in-database-configuration. That means you can update it at will as well and then trigger a config reload for PostgREST. So a simple function to load the keys from the external source and do just that, which is then run periodically via pg_cron, could work?