Reputation: 81
Postgrest APIs can be secured through inbuilt JWT tokens or through a third party service like Auth0, Okta or Keycloak (http://postgrest.org/en/v5.0/install.html)
We want to consume JWT provided by a Keycloak only , but there is very limited document available.
Can anybody guide me how can I secure postgrest apis through keycloak ?
thanks
Upvotes: 5
Views: 5102
Reputation: 432
UPDATE: Simple method with curl examples
curl https://$KEYCLOAK_URL/auth/realms/$REALM/protocol/openid-connect/certs
copy the first element of the keys
array and use it in the postgrest configuration for the jwt-secret
variable
.preferred_username
as the value of role-claim-key
in postgrest configurationAssuming you 've done the above you can test your installation:
curl -X POST https://$KEYCLOAK_URL/auth/realms/$REALM/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=$USERNAME" \
-d "password=$PASSWORD" \
-d 'grant_type=password' \
-d "client_id=$CLIENT"
curl -H "Authorization: Bearer $ACCESS_TOKEN" $POSTGREST_URL/your_table
There are several ways to do it, I'll just describe one:
"role": "username"
)Details
/#/realms/<realm name>/clients/<client id>/mappers
and set the claims you want#/realms/<realm name>/keys
you can get your rsa public key, translate it to jwk format and save it to the postgrest configuration. In order to translate it to jwk-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----
There are infinite options. A node js example: https://github.com/keycloak/keycloak-nodejs-connect/tree/master/example that uses the keycloak-connect package
You can read the token at req.kauth.grant
and place it at a hidden field of the html you send to the browser
From the browser read the token and place it at the authentication header with the bearer prefix. If you use axios:
axios({
method: 'get',
url: 'your url',
headers: { 'authorization': `Bearer ${token}` }
})
Upvotes: 2