Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在连接到Azure AD时创建客户端断言JWT令牌?_Python_Django_Azure_Oauth 2.0_Openid - Fatal编程技术网

Python 如何在连接到Azure AD时创建客户端断言JWT令牌?

Python 如何在连接到Azure AD时创建客户端断言JWT令牌?,python,django,azure,oauth-2.0,openid,Python,Django,Azure,Oauth 2.0,Openid,我的问题是,在将授权代码发送回Azure AD以获取访问令牌时,我不确定如何对JWT令牌进行签名以进行客户端断言。支持的auth方法是“private_key_jwt”。提供的唯一内容是客户端id、租户id和清单文件端点。要完成整个过程,我们应该首先创建证书。我在这里使用自签名证书进行演示 步骤1:创建.cer和.key文件,我们将把.cer上传到Azure AD App,并使用.key文件签署我们的JWT代币 1) 通过Powershell创建密码为123456的自签名证书: $cert =

我的问题是,在将授权代码发送回Azure AD以获取访问令牌时,我不确定如何对JWT令牌进行签名以进行客户端断言。支持的auth方法是“private_key_jwt”。提供的唯一内容是客户端id、租户id和清单文件端点。

要完成整个过程,我们应该首先创建证书。我在这里使用自签名证书进行演示

步骤1:创建.cer和.key文件,我们将把.cer上传到Azure AD App,并使用.key文件签署我们的JWT代币

1) 通过Powershell创建密码为123456的自签名证书:

$cert = New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname stantest.com
$pwd = ConvertTo-SecureString -String '123456' -Force -AsPlainText
$path = 'cert:\localMachine\my\' + $cert.thumbprint 
Export-PfxCertificate -cert $path -FilePath <path of your pfx file> -Password $pwd
结果:

您需要证书,其公钥需要上传到AAD应用程序注册密钥。请注意,我没有使用python版本,但它可能会使您的过程更简单。谢谢,这非常有用。只有一件事,什么样的证书,是SSL还是针对这个特定情况生成的证书@我们为这个案子准备了一份证明。
openssl pkcs12 -in <path of .pfx file> -clcerts -nokeys -out <path of .cer> 
openssl pkcs12 -in <path of .pfx file> -nocerts -nodes  -out <path of .pem file>
openssl rsa -in <path of .pem file> -out <path of .key file>
import sys 
import json
import logging

import requests
import msal

config = {
    "client_id":"your application ID here",
    "authority":"https://login.microsoftonline.com/Your tenant name or ID",
    "thumbprint":"cert thumbprint value in step2",
    "private_key_file":r"the path of .pem file of private key",
    "scope": ["https://graph.microsoft.com/.default"],
    "endpoint":"https://graph.microsoft.com/v1.0/users?$top=1"
}


app = msal.ConfidentialClientApplication(
    config["client_id"], authority=config["authority"],
    client_credential={"thumbprint": config["thumbprint"], "private_key": open(config['private_key_file']).read()},
    )


result = app.acquire_token_for_client(scopes=config["scope"])

if "access_token" in result:
    print("Access Token value: " + result['access_token']);
    # Calling graph using the access token
    graph_data = requests.get(  # Use token to call downstream service
        config["endpoint"],
        headers={'Authorization': 'Bearer ' + result['access_token']},).json()
    print("Graph API call result: %s" % json.dumps(graph_data, indent=2))
else:
    print(result.get("error"))
    print(result.get("error_description"))
    print(result.get("correlation_id"))  # You may need this when reporting a bug