瞭解 Python 應用程式與 Oracle Identity Cloud Service 之間的認證
您已經準備好瞭解 Python Web 應用程式與 Oracle Identity Cloud Service 之間的認證。其中包括瞭解下列各項:
-
使用具有 Python 應用程式的 SDK 與 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 之間事件、呼叫和回應的流程。
資料流程會發生下列情況:
-
使用者要求受保護的資源。
-
認證模組會使用 SDK 產生 Oracle Identity Cloud Service 的要求授權碼 URL,並將此 URL 作為 Web 瀏覽器的重新導向回應傳送。
-
Web 瀏覽器會呼叫 URL。
-
就會顯示 Oracle Identity Cloud Service 登入頁面。
-
使用者會送出其 Oracle Identity Cloud Service 登入證明資料。
-
使用者順利登入之後,Oracle Identity Cloud Service 會為使用者建立階段作業並發出授權碼。
-
Web 應用程式會進行後端 (或伺服器對伺服器) 呼叫,以交換使用者存取權杖的授權碼。
-
Oracle Identity Cloud Service 會發出存取權杖和 ID 權杖。
-
已建立階段作業,並將使用者重新導向至首頁。
-
Web 應用程式的首頁隨即顯示。
資料流程會發生下列情況:
-
使用者要求
/myProfile
資源。 -
Web 應用程式使用 Oracle Identity Cloud Service 的 SDK 來驗證 ID 權杖。
-
從 ID 權杖驗證傳回的資料包含以 JSON 物件格式的使用者詳細資訊。
-
我的設定檔頁面會將 JSON 物件轉譯為 HTML 內容。
瞭解 Python SDK 的方法和功能
Python SDK 提供兩個 python 檔案,IdcsClient.py
和 Constants.py
,您必須包括在 Web 應用程式中。
這些 python 檔案取決於應用程式中必須包含的協力廠商程式庫。若要包括它們,請開啟 SDK zip 檔案,將 README.txt
和 requirements.txt
檔案擷取至暫存資料夾,然後執行 pip install -r requirements.txt
命令。
範例 Web 應用程式是使用 Python DJango 架構開發的,此架構以函數形式實行 URL 路由。
Python SDK 需要載入 Oracle Identity Cloud Service 連線資訊的 JSON 變數。Python Web 應用程式使用提供下列資訊的 config.json
檔案。
{ "ClientId" : "123456789abcdefghij", "ClientSecret" : "abcde-12345-zyxvu-98765-qwerty", "BaseUrl" : "https://idcs-abcd1234.identity.oraclecloud.com", "AudienceServiceUrl" : "https://idcs-abcd1234.identity.oraclecloud.com", "scope" : "urn:opc:idm:t.user.me openid", "TokenIssuer" : "https://identity.oraclecloud.com/", "redirectURL": "http://localhost:8000/callback", "logoutSufix":"/oauth2/v1/userlogout", "LogLevel":"INFO", "ConsoleLog":"True" }
以下是此 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 。
|
ConsoleLog |
啟用 SDK 日誌。 |
LogLevel |
指示 SDK 的日誌層次。 |
logoutSufix
和 redirectURL
屬性都由應用程式使用。因此,SDK 不需要這些功能。
應用程式會實行 auth
函數定義,此定義會對應 /auth
URL。當使用者使用 Oracle Identity Cloud Service 進行認證時,瀏覽器會要求此 URL。
auth
函數會初始化 Authentication Manager、使用 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() print "config.json file = %s" % options #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 參數來要求存取權杖和 ID 權杖。兩個記號都儲存在使用者階段作業中供日後使用。
# 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() id_token = ar.getIdToken() #Validating id token to acquire information such as UserID, DisplayName, list of groups #and AppRoles assigned to the user id_token_verified = am.verifyIdToken(id_token) displayname = id_token_verified.getDisplayName() #The application then adds these information to the User Session. request.session['access_token'] = access_token request.session['id_token'] = id_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
函數會存取使用者的 ID 記號,呼叫 AuthenticationManager
verifyIdToken
函數以擷取使用者的資訊,例如以 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, validates the id token to acquire #information such as UserID, DisplayName, list of groups and AppRoles assigned to the user. #Authentication Manager loaded with the configurations. am = IdcsClient.AuthenticationManager(getOptions()) id_token = request.session.get('id_token', 'none') id_token_verified = am.verifyIdToken(id_token) #Getting the user details in json format. jsonProfile = id_token_verified.getIdToken() #Getting User information to send to the My Profile page. displayname = request.session.get('displayname', 'displayname') #Redenring json to be used in the html page. json_pretty = json.dumps(jsonProfile, sort_keys=True, indent=2) context = { 'displayname': displayname, "json_pretty": json_pretty, } #Rendering the content of the My Profile Page. return render(request, 'sampleapp/myProfile.html', context)
若要將使用者從應用程式與 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): #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: options = getOptions() url = options["BaseUrl"] url += options["logoutSufix"] url += '?post_logout_redirect_uri=http%3A//localhost%3A8000&id_token_hint=' url += request.session.get('id_token', 'none') #Clear session attributes del request.session['access_token'] del request.session['id_token'] del request.session['displayname'] #Redirect to Oracle Identity Cloud Service logout URL. return HttpResponseRedirect(url)