了解在 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 可用作两个 python 文件,IdcsClient.pyConstants.py,您必须将其包括在 Web 应用程序中。这些 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 应用程序是使用 Python DJango框架开发的,它以函数形式实施 URL 路由。

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连接范围。此应用程序仅需要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)