瞭解 Python 應用程式與Oracle Identity Cloud Service之間的認證

您可以開始瞭解 Python Web 應用程式與Oracle Identity Cloud Service之間的認證。這包含了解下列各項:

  • 使用 SDK 搭配 Python 應用程式使用時,即可使用Oracle Identity Cloud Service進行認證。

  • Oracle Identity Cloud Service支援 Python SDK 的三方式認證流程

  • Python SDK 的方法和函數

瞭解三個認證流程

Oracle Identity Cloud Service支援 Python SDK 的三方式認證流程。在此流程中,使用者會直接與Oracle Identity Cloud Service互動。使用者登入之後,Oracle Identity Cloud Service會發出 SDK 為使用者存取記號交換的授權代碼。Python 應用程式可使用此存取記號授予使用者存取應用程式中受保護資源的權限。三方流程使用授權代碼授權類型.

為了提高安全性,Oracle建議您使用三方式流程將 Python Web 應用程式與Oracle Identity Cloud Service整合進行認證。透過使用授權代碼授權類型,您也可以存取Oracle Identity Cloud Service所保護的其他應用程式,而不需要重新驗證。

瞭解使用 SDK 搭配 Python 應用程式的主要使用案例

Python Web 應用程式實行兩個使用案例:一個用於認證使用者,另一個用於存取登入使用者的相關詳細資訊。

下列資料流程圖表說明Web瀏覽器、Web應用程式以及每個使用案例之Oracle Identity Cloud Service之間的事件、呼叫與回應流程。

圖-使用案例1:使用者認證

圖 - 的描述如下
「圖-使用案例1:使用者認證」描述

資料流程的方式如下:

  1. 使用者要求受保護的資源。

  2. 認證模組使用 SDK 產生Oracle Identity Cloud Service的要求-授權程式碼 URL,並將此 URL 作為 Web 瀏覽器的重導回應傳送。

  3. Web 瀏覽器會呼叫 URL。

  4. 就會顯示Oracle Identity Cloud Service登入頁面。

  5. 使用者送出Oracle Identity Cloud Service登入證明資料.

  6. 使用者順利登入之後,Oracle Identity Cloud Service會為使用者建立階段作業並發出授權代碼。

  7. Web 應用程式會呼叫後端 (或伺服器對伺服器) 來交換使用者存取記號的授權代碼。

  8. Oracle Identity Cloud Service會發出存取記號。

  9. 系統便會建立階段作業,並將使用者重導至「首頁」。

  10. Web 應用程式的「首頁」隨即顯示。

圖-使用案例2:取得使用者詳細資訊

圖 - 的描述如下
「圖-使用案例2:取得使用者詳細資訊」描述

資料流程的方式如下:

  1. 使用者要求/myProfile資源。

  2. Web 應用程式會使用 SDK 呼叫Oracle Identity Cloud Service,此 SDK 會使用儲存在使用者階段作業中的存取記號作為參數。

  3. 使用者的詳細資訊會以 JSON 物件的形式傳送至 Web 應用程式。

  4. 「我的設定檔」頁面會將 JSON 物件轉換為 HTML 內容。

瞭解 Python SDK 的方法和函數

Python SDK 可作為IdcsClient.pyConstants.py這兩個隨附在 Web 應用程式中的 python 檔案。這些 python 檔案取決於應用程式中必須包含的第三方程式庫。若要包括它們,請執行下列pip install命令:

pip install simplejson==3.13.2
pip install cryptography==2.1.4
pip install PyJWT==1.5.2
pip install requests==2.18.4
pip install six==1.10.0
pip install py3_lru_cache==0.1.6

此範例 Web 應用程式是使用會以函數形式實行 URL 路由的 Python DJango架構所開發。

Python SDK 需要使用Oracle Identity Cloud Service連線資訊載入的 JSON 變數。Python Web 應用程式會使用可提供下列資訊的config.json檔案。

//Oracle Identity Cloud Service connection parameters as a json var
{
   "ClientId" : "clientid",
   "ClientSecret" : "clientsecret",
   "BaseUrl" : "https://idcs-1234.identity.oraclecloud.com",
   "AudienceServiceUrl" : "https://idcs-1234.identity.oraclecloud.com",
   "TokenIssuer" : "https://identity.oraclecloud.com",
   "scope" : "urn:opc:idm:t.user.me openid",
   "redirectURL": "http://localhost:8000/callback",
   "logoutSufix":"/oauth2/v1/userlogout"
}

以下是此 SDK 之每個必要屬性的簡要說明:

表格- Python SDK 的必要屬性

屬性名稱 屬性描述
ClientId 在您使用Oracle Identity Cloud Service主控台註冊 Web 應用程式之後所產生的從屬端 ID 值。
ClientSecret 使用Oracle Identity Cloud Service主控台註冊 Web 應用程式之後所產生的從屬端密碼值。
BaseUrl 您Oracle Identity Cloud Service執行處理的網域 URL.
AdminServiceUrl 您Oracle Identity Cloud Service執行處理的網域名稱 URL.這通常與BaseUrl相同。
TokenIssuer 保留此處顯示的值。
scope 範圍會控制應用程式可代表使用者存取或處理的資料。由於應用程式使用 SDK 進行認證,因此範圍為openid。此應用程式也實行get user details使用案例,因此您必須同時使用範圍urn:opc:idm:t.user.me

應用程式同時使用logoutSufixredirectURL屬性。因此,SDK 不需要它們。

此應用程式實行對映/auth URL 的auth函數定義。當使用者向Oracle Identity Cloud Service進行認證時,瀏覽器會對此 URL 提出要求。

auth函數會初始化「認證管理程式」,使用 JSON 組態屬性作為參數,使用 SDK 產生Oracle Identity Cloud Service授權程式碼 URL,然後將瀏覽器重導至此 URL。

#Loading the SDK Python file.
from . import IdcsClient
 
#Function used to load the configurations from the config.json file
def getOptions():
    fo = open("config.json", "r")
    config = fo.read()
    options = json.loads(config)
    return options
 
# Definition of the /auth route
def auth(request):
    #Loading the configurations
    options = getOptions()
    #Authentication Manager loaded with the configurations.
    am = IdcsClient.AuthenticationManager(options)
    #Using Authentication Manager to generate the Authorization Code URL, passing the 
    #application's callback URL as parameter, along with code value and code parameter.
    url = am.getAuthorizationCodeUrl(options["redirectURL"], options["scope"], "1234", "code")
    #Redirecting the browser to the Oracle Identity Cloud Service Authorization URL.
    return HttpResponseRedirect(url)

下列參數是用來產生授權代碼 URL:

表格-用來產生授權代碼 URL 的參數

參數名稱 參數描述
options["redirectURL"] 成功登入之後,Oracle Identity Cloud Service會將使用者的 Web 瀏覽器重導至此 URL。此 URL 必須符合您在Oracle Identity Cloud Service主控台中為信任應用程式設定的 URL。
options["scope"] 認證的OAuth或OpenID Connect範圍。此應用程式僅需要openid認證。
state Web 應用程式會使用此代碼來檢查是否可以向Oracle Identity Cloud Service建立通訊。此參數由OAuth協定定義。
response_type 授權代碼授權類型所需的值 (例如,code)。

callback函數使用授權代碼 URL 參數來要求存取記號。存取記號會儲存為 Cookie,然後傳送給瀏覽器供日後使用。

# Definition of the /callback route
def callback(request):
   code = request.GET.get('code')
   #Authentication Manager loaded with the configurations.
   am = IdcsClient.AuthenticationManager(getOptions())
   #Using the Authentication Manager to exchange the Authorization Code to an Access Token.
   ar = am.authorizationCode(code)
   #Get the access token as a variable
   access_token = ar.getAccessToken()
   #User Manager loaded with the configurations.
   um = IdcsClient.UserManager(getOptions())
   #Using the access_token value to get an object instance representing the User Profile.
   u = um.getAuthenticatedUser(access_token)
   #Getting the user details in json object format.
   displayname = u.getDisplayName()
   #The application then adds these information to the User Session.
   request.session['access_token'] = access_token
   request.session['displayname'] = displayname
   #Rendering the home page and adding displayname to be printed in the page.
   return render(request, 'sampleapp/home.html', {'displayname': displayname})

myProfile函數會存取使用者的存取記號、呼叫getAuthenticatedUser函數以將使用者資訊以 JSON 文字的形式擷取,然後將資訊傳送至myProfile.html以進行轉換。

# Definition of the /myProfile route
def myProfile(request):
   #Getting the Access Token value from the session
   access_token = request.session.get('access_token', 'none')
   if access_token == 'none':
       #If the access token isn't present redirects to login page.
       return render(request, 'sampleapp/login.html')
   else:
       #If the access token is present, then loads the User Manager with the configurations.
       am = IdcsClient.UserManager(getOptions())
       #Using the access_token value to get an object instance representing the User Profile.
       u = am.getAuthenticatedUser(access_token)
       #Getting the user details in json format.
       jsonProfile = json.dumps(u.getUser())
       #Getting User information to send to the My Profile page.
       displayname = request.session.get('displayname', 'displayname')
       #Rendering the content of the My Profile Page.
       return render(request, 'sampleapp/myProfile.html', {'displayname': displayname, 'jsonProfile':jsonProfile})

若要從應用程式與Oracle Identity Cloud Service之間的單一登入來登出使用者,Python Web 應用程式會實行logout函數。此函數會讓使用者階段作業失效,然後將使用者重導至Oracle Identity Cloud Service的OAuth登出 URL。此 URL 在config.json檔案中設定為logoutSufix參數。

# Definition of the /logout route
def logout(request):
    options = getOptions()
    url = options["BaseUrl"]
    url += options["logoutSufix"]
    del request.session['access_token']
    del request.session['displayname']
    return HttpResponseRedirect(url)