I am working with jwt tokens coming from Microsoft to a client to authenticate requests from it to an web API (server). I have control over the code of both the client (js) and the server (Python).
At the client, I am using the following request to get the token (which the user claims through password/2FA on the tenant):
`https://login.microsoftonline.com/${TENANT_ID}/oauth2/v2.0/authorize
?response_type=id_token+token
&client_id=${CLIENT_ID}
&redirect_uri=${redirect_uri}
&scope=openid+email+profile
&state=${guid()}
&nonce=${guid()}`
here guid
is a unique value, TENANT_ID
is the tenant, and CLIENT_ID
the client.
After I get this token, I send it as an authorization header, like this:
init = {
headers: {
'Authorization': `Bearer ${token}`,
}
}
return fetch(url, init).then(response => {
return response.json()
})
On the server, I then retrieve the token and validate it:
if 'Authorization' in request.headers and request.headers['Authorization'].startswith('Bearer '):
token = request.headers['Authorization'][len('Bearer '):]
from authlib.jose import jwt
claims = jwt.decode(token, jwk)
where jwk
is the contents of https://login.microsoftonline.com/{TENANT_ID}/discovery/v2.0/keys
.
The whole flow works until the validation, which fails with the following error:
authlib.jose.errors.InvalidHeaderParameterName: invalid_header_parameter_name: Invalid Header Parameter Names: nonce
which indicates that the header of the token contains a key nonce
(I validated it).
Looking at the Microsoft's documentation on this, here, there is no reference to a nonce
on the header -- just on the payload.
Q1: what am I doing wrong here?
Q2: assuming that Microsoft is the one putting the nonce in the wrong place (header instead of payload), is it possible to just remove the nonce from the header (on the server side) before passing it to the jose's authentication library? Is this safe to do?