ArcGIS API For Python 03 - 使用不同的身份驗證方案

ArcGIS API For Python中的GIS對象提供了對於幾類用戶的多種驗證方案:

  • 匿名用戶
  • 內置用戶
  • Enterprise標識存儲
    • Web-tier authentication with LDAP
    • Portal-tier authentication with Active Directory
    • Portal-tier authentication with LDAP
    • Web-tier authentication secured with PKI
    • Web-tier authentication secured with IWA
  • 基於OAuth 2.0的用戶身份驗證
  • 通過ArcGIS Pro連接

GIS類支持通過向ArcGIS Online或者ArcGIS Enterprise實例提供URL或者用戶憑據的方式構建GIS對象。用戶憑據內容可包括一對用戶名和用戶密碼,或者在使用PKI的情況下,使用一個密鑰文件/文件證書對作爲用戶憑據內容。

如果不提供URL地址,默認使用ArcGIS Online;如果不提供用戶憑據,則默認以匿名用戶訪問。另外,腳本可通過ArcGIS Pro登錄Portal,並且會選擇使用當前激活的Portal。

匿名用戶訪問

作爲匿名用戶訪問此API,你可完成的任務比較少,可以查詢或者查看公開分享的地圖和圖層,但是,向創建或者修改內容、執行分析任務等情況是沒有權限的。

  • 作爲匿名用戶連接ArcGIS Online
print("ArcGIS Online as anonymous user")
gis = GIS()
print("Logged in as anonymous user to " + gis.properties.portalName)
  • 作爲匿名用戶連接ArcGIS Enterprise

如果ArcGIS Enterprise允許匿名用戶訪問,只傳入Enterprise的URL地址,不傳入用戶名和密碼,即可以匿名用戶的方式連接Enterprise。

值得注意的一點,如果Enterprise配置了IWA認證,並且在Windows系統中登錄Enterprise,可能連接時會自動登錄。

print("ArcGIS Enterprise as anonymous user")
gis = GIS("https://singlega.jiaoxn.local/arcgis")
print("Logged in as anonymous user to " + gis.properties.portalName)

內置用戶

無論ArcGIS Online還是ArcGIS Enterprise都使用內置的身份存儲進行了預配置,所以可以很輕鬆的創建用戶和創建組。當使用這種情況登錄時,需提供用戶名和密碼。

  • 作爲內置用戶連接ArcGIS Online
print("ArcGIS Online Org account")    
gis = GIS("https://www.arcgis.com", "arcgis_python", "P@ssword123")
print("Logged in as " + str(gis.properties.user.username))
  • 作爲內置用戶連接Enterprise
print("Portal for ArcGIS as a built in user")
gis = GIS("https://portalname.domain.com/webadapter_name", "sharinguser", "password")
print("Logged in as: " + gis.properties.user.username)

Enterprise 標識存儲

ArcGIS Online和ArcGIS Enterprise的設計允許您通過Enterprise賬號和羣組來控制對ArcGIS組織的訪問。

  • Web-tier authentication with LDAP
print("\n\nBasic Authentication with LDAP")    
ldapbasic = GIS("https://portalname.domain.com/webadapter_name", "amy", "password")
print("Logged in as: " + ldapbasic.properties.user.username)
  • Portal-tier authentication with Active Directory

當使用來自當前激活目錄的Enterprise用戶連接時,以domain\\username的方式指定用戶名。

print("\n\nPortal-tier Authentication with LDAP - enterprise user")
gisldap = GIS("https://portalname.domain.com/webadapter_name", "AVWORLD\\Publisher", "password")
print("Logged in as: " + gisldap.properties.user.username)
  • Portal-tier authentication with LDAP

當使用來自LDAP的Enterprise用戶賬號連接時,向平常一樣指定用戶名和密碼即可。

print("\n\nPortal-tier Authentication with LDAP - builtin user")    
gisldap = GIS("https://portalname.domain.com/webadapter_name", "sharing1", "password")
print("Logged in as: " + gisldap.properties.user.username)
  • Web-tier authentication secured with PKI

使用PKI連接時,有兩種方式:一種是使用key_filecert_file,另一種是使用pkcs12格式化的證書文件和密碼

# 使用PEM編碼的證書和密鑰文件
print("\n\nPKI with key and cert files")  
gis = GIS("https://portalname.domain.com/webcontext", 
          key_file="C:\\path\\to\\key.pem",
          cert_file="C:\\path\\to\\cert.pem")
print("Logged in as: " + gis.properties.user.username)

# 使用PKCS12格式化的證書文件和密碼
print("\n\nPKI with PFX file and password")  
gis = GIS("https://portalname.domain.com/webcontext", 
          cert_file="C:\\path\\to\\mycert.pfx",
          password="secret.password")
print("Logged in as: " + gis.properties.user.username)
  • Web-tier authentication secured with IWA

基於OAuth 2.0的用戶身份驗證

使用此方式連接時,需要一個client.id

獲取一個client id

  1. 登錄ArcGIS Online或者ArcGIS Enterprise
  2. 選擇Content
  3. 點擊+ Add ItemAn Application按鈕
  4. 添加一個應用:類型爲Application,標題爲Python,標籤爲Python
  5. 在剛剛創建的應用的詳細頁面,導航至Settings
  6. 點擊頁面底部的Registered Info
  7. 點擊後,當前頁面會顯示一個應用ID,這個就是連接GIS對象時需要的client.id。

此方式連接時,如果不提供用戶名和密碼,將會顯示交互式的登錄頁面,登錄後將會獲得一個代碼,可以粘貼爲該代碼已完成登錄過程;提供用戶名和密碼,可屏蔽此過程。

gis = GIS("https://pythonapi.playground.esri.com/portal", username="arcgis_python", password="amazing_arcgis_123",
          client_id='f8cRxbP3NO8bf9ag')
print("Successfully logged in as: " + gis.properties.user.username)

通過ArcGIS Pro連接

基於pro的授權模式,Python代碼腳本可以在不輸入用戶名和密碼的情況下,創建代表當前激活的Portal的GIS類實例。

值得注意的是,如果使用Named User license這樣的授權方式授權Pro,如果在登錄時沒有勾選Signe me in automatically或者沒有設置Pro可離線使用,則在使用該模式連接時,需確保ArcGIS Pro在運行該代碼的機器上已安裝並且Pro已運行。如果勾選Signe me in automatically,默認情況下,在Portal許可到期之前,ArcGIS Pro可以連續關閉2周。

print("\n\nActive Portal in ArcGIS Pro")  
gis = GIS("pro")

保護密碼

如果向其他用戶分享你的Notebook代碼或者在你的代碼存儲在公共位置時,有時會不想直接暴露密碼在代碼中,可以不輸入代碼來解決這個問題。當Python API運行代碼時,會調用內置模塊getpass,提示用戶輸入代碼,如下代碼所示。

gis = GIS("https://pythonapi.playground.esri.com/portal", username='arcgis_python')
print("Successfully logged in as: " + gis.properties.user.username)

# Output
Enter password: ········
Successfully logged in as: arcgis_python

當提示輸入密碼時,代碼將會暫停運行,直到用戶輸入正確的代碼。輸入的代碼會用*號掩蓋。

在需要輸入密碼的連接模式中,均可使用這種模式保護你的密碼。如果在單獨的Python腳本中使用這種方式時,該腳本需要以交互式的方式運行,因爲在腳本運行時,需要用戶提供密碼。

本地保存資格證書

如果經常連接一個GIS實例,並且考慮將連接證書保存到本地,那麼GIS實例化時傳入profile參數可解決。

通過向GIS類傳遞授權憑據,並指定一個證書名稱,即可創建持久化的配置文件。這個配置文件在用戶主目錄下,將除密碼以外的授權憑據存儲在一個未加密的名爲.arcgisprofile配置文件中。資格證書會通過keyring模塊以安全的方式將密碼存放在操作系統的密碼管理器中,如果是Linux系統可能需要安裝額外的軟件來保證適當的安全性。

保存配置文件後,可通過profile參數傳遞證書文件。多個證書文件可以被平行的創建和調用。

# 創建
playground_gis = GIS(url="https://pythonapi.playground.esri.com/portal", username='arcgis_python', password='amazing_arcgis_123',
                     profile='python_playground_prof')
agol_gis = GIS(url="https://arcgis.com/", username='arcgis_python', password="P@ssword123",
    profile="AGOL_prof")
print("profile defined for {}".format(playground_gis))
print("profile defined for {}".format(agol_gis))

# 使用

def print_profile_info(gis):
    print("Successfully logged into '{}' via the '{}' user".format(
           gis.properties.portalHostname,
           gis.properties.user.username)) 

playground_gis = GIS(profile='python_playground_prof')
print_profile_info(playground_gis)

agol_gis = GIS(profile="AGOL_prof")
print_profile_info(agol_gis)

持久配置文件詳細內容

持久配置文件將在未加密的.arcgisprofile文件中存儲以下GIS參數的任意組合:

  • url
  • username
  • key_file
  • cert_file
  • cliend_id

密碼將會通過keyring模塊以安全的方式的存儲,也就是說,在運行代碼時,密碼將會從操作系統的密碼存儲管理器中檢索。

Windows

Window系統中,密碼將會存放在Windows憑據管理器。其查看方式爲:

Start > Control Panel > User Accounts > Credential Manager > Windows Credentials > Generic Credentials
Mac

在Mac系統中,密碼將會存放在Keychain Access,查看方式爲:

Applications > Utilities > Keychain Access > Passwords > "arcgis_python_api_profile_passwords"
Linux

在Linux上,可能需要安裝並配置其他軟件,以安全地存儲具有持久配置文件的密碼。其配置過程可通過另外一篇博客來了解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章