How to get header values from jwt #547
Replies: 2 comments 2 replies
-
Please kindly read the disclaimer and use the appropriate medium for open-ended questions. We would like to use GitHub Issues for technical issues, not discussions on usage. For the time being I'm converting this to a discussion thread. You show very little information, so the following is a guess. I'm assuming from your text that you have a JWS-signed JWT, which you would like to verify using a key, whose location is written in the Assuming this is the case, some random observations:
If you really need to get at the JWS headers manually from a JWS-signed JWT, perhaps it is |
Beta Was this translation helpful? Give feedback.
-
@Edwin-Luijten Okay, this is now based on your second version of the question. Thanks for the rewrite. I still have some questions in my head, but here's what I would do. First, in order to retrieve the JWS headers, I would probably just handroll a function to type tmpJWSHeader struct {
Algorithm string `json:"alg"`
KeyID string `json:"kid"`
}
var signed []byte = ....; // the original JWS signed JWT
// Get the first part out of the JWS
i := bytes.Index(signed, []byte('.'))
encodedHdrs = signed[:i]
// decode base64
decodedHdrs := make([]byte, base64.RawURLEncoding.DecodedLen(i))
base64.RawURLEncoding.Decode(decodedHdrs, encodedHdrs)
// unmarshal into struct
var jwsHdr tmpJWSHeader
json.Unmarshal(decodedHdrs, &jwsHdr) At this point Next, we fetch the JWK using // Fetch the JWK (you can use jwk.AutoRefresh if this URL is repeatedly fetched)
ctx := context.Background() // or whatever
set, _ := jwk.Fetch(ctx, baseURL + jwsHdr.KeyID)
// We're assuming there's only one key in the set
// the key _should_ already have a key ID, but... sure, we'll take your cue
key := set.Get(0)
key.Set(jwk.KeyIDKey, jwsHdr.KeyID)
key.Set(jwk.AlgorithmKey, jwsHdr.Algorithm) And finally, just use that information to parse the JWT. Originally you wrote about token, err := jwt.Parse(signed, jwt.WithValidate(true), jws.WithKeySet(set)) And since you have already set the key ID and algorithm for the key, and the JWS headers also have the same information, you should be able to just shove the JWKS to I just have to wonder though, why would they make you fetch the JWK using the |
Beta Was this translation helpful? Give feedback.
-
Scenario:
I'm getting a third party jwt token which is signed and has the following headers:
The endpoint for the public key has this format: `https://domain.com/{{ kid }}.
When looking trough the code I would think the method
KeySetProvider
in combination withFetch
would be the way to go, but in order to use those I need a way to retrieve values from the headers eg:token.Header.Get("kid")
.The way I went with it now is by getting the encoded header part, base64 decode in and then unmarshal it.
Now I can retrieve the key by doing a http request and transform it into a
Set
.Then I use that key like this:
This works but I feel I'm not using the correct feature of the library.
Beta Was this translation helpful? Give feedback.
All reactions