【问题标题】:Simple HTML Python CGI for MSAL用于 MSAL 的简单 HTML Python CGI
【发布时间】:2019-10-17 03:05:27
【问题描述】:

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 的输入以获取 Token。

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 外观的帮助?

提前致谢。

和平, 埃里克

【问题讨论】:

    标签: python msal


    【解决方案1】:

    如果您的 cgi 脚本已经与 ADAL Python 一起工作,那么您至少可以将其用作基线,然后像这样针对 MSAL Python 开始故障排除过程。

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

      • 哦,等等,实际上,我认为这可能是问题所在。您使用的是适合您正在使用的 ADFS 服务器的手工制作的授权 URL,可能是 ADFS 2016 或更低版本(因为您使用 cgi 脚本的第二半发送的“资源”参数提供给它)。 MSAL Python 可以直接使用较新版本的 ADFS(它接受“范围”参数),或者当它们在 Azure AD 后面联合时间接使用较旧版本的 ADFS(这意味着,MSAL Python 与 AAD 对话,AAD 与您公司的本地对话) ADFS)。

      • 尽管如此,尝试在 MSAL Python 中完成 get_authorization_request_url() 和 acquire_token_by_authorization_code() 的两条腿,而不是混合和匹配,这并没有什么坏处。如果这对您不起作用,也许您将不得不在 ADAL Python 中停留更长时间。不用担心。 ADAL Python 不会消失。 ADAL Python 中的现有功能将继续有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-09
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多