I want to try out Envoy JWT authentication with a local JSON Web Key Set as an inline string. As an algorithm I want to use HS256, because the key is only needed for my Service that generates the JWT and Envoy for enforcing rules, so not much sharing with more services.
Below is an excerpt of my Envoy config that configures the authentication rules:
http_filters:
- name: envoy.filters.http.jwt_authn
typed_config:
"@type": "type.googleapis.com/envoy.extensions.filters.http.jwt_authn.v3.JwtAuthentication"
providers:
main_auth:
forward_payload_header: auth_user
issuer: example.com
local_jwks:
inline_string: '{"keys":[{"kty": "oct","alg": "HS256", "use": "sig","kid": "Example Key","k": "nZeA0nwBUS-ma0oTtZfVnk8o1bjtaJZfuUMpzdEuXu4wEgeKou1klrGoWSPm4nRBNTAIwflteghMOsP9d0gtXOPNh-Fwd-0Ef1a06JGa5Zx3Uo1qmmKRLSxNGuCQY8M9qFWPyFsdMA5-1Y5EOEAnoqb02YqibGOUx6msH3uBcU-jMyY90ysEboN8s03gBl0FmhgWTAc25JNxF4eF4dt2oUF4Z48YHuk5U6lR6oNoJ38iP1wLBAz70d3kFgq_MtfojsKcuHk2HzBESx5fynUhc2ZFdXm-hg0zHFqFoHYd5wKva06bjIv61yd6lNe5YGi5wYbPNHO7bkg3vaWl0zhE5w"}]}'
rules:
# Not JWT verification is needed for the /auth route
- match:
prefix: /auth
# JWT verification is required for the /profile route
- match:
prefix: /profile
requires:
provider_name: main_auth
I'm not quite familiar with JSON web key sets and therefor I'm not sure what property of the set to use in my application as the secret to generate the key. I tried using the k
property of the set as my secret for the JWT but the request still fails with the message: Jwt verification fails
.
This is how I generate the key with the npm jsonwebtoken package, I have just copied the k
property of the Key Set as the secret:
jwt.sign(
{role: 'ADMIN'},
'nZeA0nwBUS-ma0oTtZfVnk8o1bjtaJZfuUMpzdEuXu4wEgeKou1klrGoWSPm4nRBNTAIwflteghMOsP9d0gtXOPNh-Fwd-0Ef1a06JGa5Zx3Uo1qmmKRLSxNGuCQY8M9qFWPyFsdMA5-1Y5EOEAnoqb02YqibGOUx6msH3uBcU-jMyY90ysEboN8s03gBl0FmhgWTAc25JNxF4eF4dt2oUF4Z48YHuk5U6lR6oNoJ38iP1wLBAz70d3kFgq_MtfojsKcuHk2HzBESx5fynUhc2ZFdXm-hg0zHFqFoHYd5wKva06bjIv61yd6lNe5YGi5wYbPNHO7bkg3vaWl0zhE5w',
{ issuer: 'example.com' }
);
What property of the set do I have to use as the secret in my application so that the JWT can get accepted by Envoy?
I generated my example Key set with this tool: https://mkjwk.org/
Edit
This is a JWK I generated and I'm not able to decode it either as a base64 string or base64URL string.
{
"keys": [
{
"kty": "oct",
"kid": "LqDTw3xmtazF_pIdZMDRfE2XRDR8MRD_yxLPWaZoyWA",
"use": "sig",
"alg": "HS256",
"k": "3D7zGRLWSumeweTpG6AkYAQ4KpLZVzv8FygYsZD7fTOYBj0XaI26P4lpZpE1EZPlVPcYOBOmzqRfnUrcdUh43H_wIYax7avrdgAFjD-69SWY7Yo4FkY4yOIpq4xzBA7yEb8_Y9SS3to5GBcZKygp3ybyK-6fLK5HNDAD8dR-VUDeg0DARyIQaBZjhXe2DW-2VjdE6llandUavn2Hd_pwUZNH6zYPW9ObJlk_CdOwMxlelH0brGc4Ja119ptgJTcqauUGQIYnizulbwYBgsQAXEyAChw07-HL7_FDQbqHPA_MxyoNk_ahNItD5GcKIoo1m5esQQeO7fbFkZhESY85Dw"
}
]
}
This is how I generate a JWK with Node.js
const fs = require("fs");
const jose = require("node-jose");
const main = () => {
const keyStore = jose.JWK.createKeyStore();
keyStore
.generate("oct", 2048, { alg: "HS256", use: "sig" })
.then((result) => {
fs.writeFileSync(
"keys.json",
JSON.stringify(keyStore.toJSON(true), null, " ")
);
});
};
main();