gpt4 book ai didi

jwt - 如何验证 Microsoft jwt id_token?

转载 作者:行者123 更新时间:2023-12-02 00:10:58 24 4
gpt4 key购买 nike

我正在使用从 Microsoft 到客户端的 jwt token 来验证从它到 Web API(服务器)的请求。我可以控制客户端 (js) 和服务器 (Python) 的代码。

在客户端,我使用以下请求获取 token (用户通过租户上的密码/2FA 声明):

`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()}`

这里的guid是一个唯一值,TENANT_ID是租户,CLIENT_ID是客户端。

获得此 token 后,我将其作为授权 header 发送,如下所示:

init = {
headers: {
'Authorization': `Bearer ${token}`,
}
}

return fetch(url, init).then(response => {
return response.json()
})

然后在服务器上,我检索 token 并验证它:

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)

其中 jwkhttps://login.microsoftonline.com/{TENANT_ID}/discovery/v2.0/keys 的内容。

整个流程一直有效,直到验证失败,并出现以下错误:

authlib.jose.errors.InvalidHeaderParameterName: invalid_header_parameter_name: Invalid Header Parameter Names: nonce

这表明 token 的 header 包含一个 key nonce(我验证了它)。

查看 Microsoft 关于此的文档,here , header 上没有对 nonce 的引用 - 仅在有效负载上。

Q1:我做错了什么?

问题 2:假设 Microsoft 将 nonce 放在错误的位置( header 而不是负载),是否可以在将 nonce 传递给 jose 的身份验证库之前从 header (在服务器端)删除 nonce ?这样做安全吗?

最佳答案

参见:https://robertoprevato.github.io/Validating-JWT-Bearer-tokens-from-Azure-AD-in-Python/

以下是我在我的 API 中验证 Azure AD Id_tokens 的方法:

import base64
import jwt
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization


def ensure_bytes(key):
if isinstance(key, str):
key = key.encode('utf-8')
return key


def decode_value(val):
decoded = base64.urlsafe_b64decode(ensure_bytes(val) + b'==')
return int.from_bytes(decoded, 'big')


def rsa_pem_from_jwk(jwk):
return RSAPublicNumbers(
n=decode_value(jwk['n']),
e=decode_value(jwk['e'])
).public_key(default_backend()).public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)


# obtain jwks as you wish: configuration file, HTTP GET request to the endpoint returning them;
jwks = {
"keys": [
{
"kty": "RSA",
"use": "sig",
"kid": "piVlloQDSMKx...",
"x5t": "piVlloQDSMKx...",
"n": "0XhhwpmEpN-jDBapnzhF...",
"e": "AQAB",
"x5c": [
"MIIDBTCCAe2gAwIBAgIQMCJcg...."
],
"issuer": "https://login.microsoftonline.com/{tenant}/v2.0"
}
]
}

# configuration, these can be seen in valid JWTs from Azure B2C:
valid_audiences = ['dd050a67-ebfd-xxx-xxxx-xxxxxxxx'] # id of the application prepared previously

class InvalidAuthorizationToken(Exception):
def __init__(self, details):
super().__init__('Invalid authorization token: ' + details)


def get_kid(token):
headers = jwt.get_unverified_header(token)
if not headers:
raise InvalidAuthorizationToken('missing headers')
try:
return headers['kid']
except KeyError:
raise InvalidAuthorizationToken('missing kid')



def get_jwk(kid):
for jwk in jwks.get('keys'):
if jwk.get('kid') == kid:
return jwk
raise InvalidAuthorizationToken('kid not recognized')

def get_issuer(kid):
for jwk in jwks.get('keys'):
if jwk.get('kid') == kid:
return jwk.get('issuer')
raise InvalidAuthorizationToken('kid not recognized')

def get_public_key(token):
return rsa_pem_from_jwk(get_jwk(get_kid(token)))


def validate_jwt(jwt_to_validate):
try:
public_key = get_public_key(jwt_to_validate)
issuer = get_issuer(kid)


options = {
'verify_signature': True,
'verify_exp': True, # Skipping expiration date check
'verify_nbf': False,
'verify_iat': False,
'verify_aud': True # Skipping audience check
}

decoded = jwt.decode(jwt_to_validate,
public_key,
options=options,
algorithms=['RS256'],
audience=valid_audiences,
issuer=issuer)

# do what you wish with decoded token:
# if we get here, the JWT is validated
print(decoded)

except Exception as ex:
print('The JWT is not valid!')
return False
else:
return decoded
print('The JWT is valid!')

关于jwt - 如何验证 Microsoft jwt id_token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59151559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com