MSAL的简单HTML Python CGI

2024-05-16 04:47:16 发布

您现在位置:Python中文网/ 问答频道 /正文

ADAL已贬值,取而代之的是MSAL。 尝试在简单PythonCGI中实现MSAL(没有烧瓶)。你知道吗

https://msal-python.readthedocs.io/en/latest/

https://github.com/Azure-Samples/ms-identity-python-webapp

下面是ADAL的工作代码。你知道吗

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import cgi
import cgitb; cgitb.enable()  # for troubleshooting
from msal import PublicClientApplication
import adal


form = cgi.FieldStorage()
code = form.getvalue('code')
token = form.getvalue('token')
redirect_uri  = 'http://************'
resource      = '************'
client_id     = '************'
header = ''
message=''

if not code and not token:
    auth_template = 'https://adfs.blah.com/adfs/oauth2/authorize?response_type=code&client_id={}&redirect_uri={}'
    authorization_url = (auth_template).format(client_id, redirect_uri)
    header = '<meta http-equiv="refresh" content="0;url=' + authorization_url + '"/>'
elif code != "" and not token:
    authority_url = 'https://adfs.blah.com/adfs'
    context = adal.AuthenticationContext(authority_url, validate_authority=False,)
    token = context.acquire_token_with_authorization_code(code, redirect_uri, resource, client_id)
    refresh_token = token['refreshToken']
    token = context.acquire_token_with_refresh_token(refresh_token, client_id, resource,)
    token_userid = token["userId"].split("@",1)[0].upper()
    message=token_userid

print("Content-Type: text/html;charset=utf-8")
print()

#    put css link? the header section vs using <style> in the page <link rel="stylesheet" href="report.css" />
print("""
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
{header}
</head>
<body>
{message}
</body>
</html>
 """.format(header=header,message=message))

我想我遇到的问题是使用MSAL获取一个有效的代码。你知道吗

我不认为我的服务器是设置给客户机的秘密。你知道吗

所以我尝试使用ADAL获取一个代码,然后使用ADAL的代码作为MSAL的输入来获取一个令牌。你知道吗

if not code and not token:
    auth_template = 'https://adfs.blah.com/adfs/oauth2/authorize?response_type=code&client_id={}&redirect_uri={}'
    authorization_url = (auth_template).format(client_id, redirect_uri)
    header = '<meta http-equiv="refresh" content="0;url=' + authorization_url + '"/>'
elif code != "" and not token:
    result=msal.ConfidentialClientApplication(client_id, client_credential=None, authority=None, validate_authority=True, token_cache=None, verify=True, proxies=None, timeout=None, client_claims=None)
    message=result.acquire_token_by_authorization_code(code, ["User.ReadBasic.All"], redirect_uri=None,)

但基于此错误,输入数据结构的格式似乎不正确。你知道吗

{'error': 'invalid_grant', 'error_description': 'AADSTS9002313: Invalid request. Request is malformed or invalid.\r\nTrace ID: 9fdb3fe4-23b0-4779-9e65-3e3e13951500\r\nCorrelation ID: 51d1d399-7732-4c99-8266-ef89c0f74b2c\r\nTimestamp: 2019-10-16 05:32:05Z', 'error_codes': [9002313], 'timestamp': '2019-10-16 05:32:05Z', 'trace_id': '9fdb3fe4-23b0-4779-9e65-3e3e13951500', 'correlation_id': '51d1d399-7732-4c99-8266-ef89c0f74b2c', 'error_uri': 'https://login.microsoftonline.com/error?code=9002313', 'suberror': 'bad_token'}

想知道一个简单的Python CGI MSAL是什么样子的吗?你知道吗

提前谢谢。你知道吗

和平, 埃里克


Tags: httpsclienttokennoneidurlmessagenot
1条回答
网友
1楼 · 发布于 2024-05-16 04:47:16

如果您已经有了使用adalpython的cgi脚本,那么您至少可以将其用作基线,并像这样开始msalpython的故障排除过程。你知道吗

  • 创建一个PublicClientApplication(你不必创建一个ConfidentialClientApplication,除非你的应用被分配了一个秘密或证书)。用相关参数调用它的get_authorization_request_url()。现在可以将其返回的url与脚本的前半部分中使用的url进行比较https://adfs.blah.com/adfs/oauth2/authorize?response_type=code&client_id={}&redirect_uri={}。你知道吗

    • 哦,等等,事实上,我想这可能就是问题所在。您使用的是一个手工制作的authorize URL,它适合您正在使用的ADFS服务器,可能是ADFS 2016或更低版本(因为您使用了cgi脚本的后半部分发送的“resource”参数)。MSAL Python可以直接与ADFS的较新版本(它接受一个“scope”参数)一起工作,也可以间接地与ADFS的较旧版本一起工作(这意味着,MSAL Python与AAD对话,AAD与您公司的on prem ADFS对话)。

    • 尽管如此,尝试在MSAL Python中完成get\u authorization\u request\u url()和acquire\u token\u by\u authorization\u code()这两段代码并不有害,而不是混合和匹配。如果这对您不起作用,那么您可能需要在adalpython上呆更长的时间。别担心。阿达尔Python不会消失。adalpython中现有的特性将继续工作。

相关问题 更多 >