簡體   English   中英

用於 MSAL 的簡單 HTML Python CGI

[英]Simple HTML Python CGI for MSAL

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 外觀的幫助?

提前致謝。

和平,埃里克

好吧,如果您的 cgi 腳本已經與 ADAL Python 一起使用,您至少可以將其用作基線,然后像這樣針對 MSAL Python 開始故障排除過程。

  • 創建一個PublicClientApplication (你不必創建一個ConfidentialClientApplication ,除非你的應用被分配了一個秘密或證書)。 使用相關參數調用它的get_authorization_request_url() 現在您可以將其返回的 url 與您在腳本前半部分使用的https://adfs.blah.com/adfs/oauth2/authorize?response_type=code&client_id={}&redirect_uri={}進行比較

    • 哦,等等,實際上,我認為這可能是問題所在。 您使用的是手工制作的授權 URL,它適用於您正在使用的 ADFS 服務器,可能是 ADFS 2016 或更低版本(因為您使用 cgi 腳本的第二半發送的“資源”參數提供給它)。 MSAL Python can work with newer versions of ADFS directly (which accepts a "scope" parameter), or older versions of ADFS indirectly when they are federated behind Azure AD (meaning, MSAL Python talks to AAD, AAD talks to your corp's on-prem ADFS)。

    • 盡管如此,嘗試在 MSAL Python 中完成 get_authorization_request_url() 和 acquire_token_by_authorization_code() 的兩條腿,而不是混合和匹配,這並沒有什么壞處。 如果這對您不起作用,也許您將不得不與 ADAL Python 待更長時間。 不用擔心。 ADAL Python 不會離開 go。 ADAL Python 中的現有功能將繼續有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM