这是lazr包的模板。开始你自己的lazr包,
lazr.authentication的Python项目详细描述
.BR/>此文件是LZR的一部分。身份验证。BR/> BR/> LAZR.Audio是免费软件:您可以在GNU小通用公共许可证的条款下重新发布和修改它。BR/<自由软件基金会,许可证第3版。
BR/>lazr.authentication发布的目的是希望它会有用,但是
没有任何保证;甚至没有对适销性
或特定用途的适用性的隐含保证。有关详细信息,请参阅gnu less general public
许可证。
您应该已经收到gnu less general public许可证的副本
以及lazr.authentication。如果没有,请参阅<;http://www.gnu.org/licenses/>;
===
>;>;来自未来的导入打印功能r/>>;>gt;导入lazr.authentication
>;>print('version:%s%lazr.authentication.\uu version\uu)
版本:…
basicauthmidler软件实现http基本auth。它的构造函数
接受许多参数,包括执行实际身份验证的回调函数。此函数返回一个对象
,标识试图进行身份验证的用户。如果
身份验证凭据无效,则应该不返回任何凭据。
首先,让我们创建一个真正简单的wsgi应用程序,用200响应代码响应任何请求。
>;def dummy_application(environ,start_response):
…开始响应('200',[('content-type','text/plain')])
…return[b'success']
现在让我们保护这个应用程序。这是一个身份验证回调函数。
>;>def authenticate(用户名、密码):
…""接受"用户/密码",拒绝所有其他内容。
…
…:return:如果凭据有效,则返回用户名。
…没有,否则。
…"
…如果username=="user"和password=="password":
…返回用户名
…return none
>;>print(authenticate("user","password"))
user
>;>print(authenticate("notuser","password"))
none
lazr.authentication.wsgi导入Basicauthmiddleware
>>gt;def protected_application():
…返回基本中间件(
…dummy_application,realm="wsgi中间件测试",
…保护路径模式=".*protected.*",
…authenticate_with=authenticate)
>;>import wsgi_intercept
>;>from wsgi_intercept.httplib2_intercept import install
>;>install()
>;>wsgi_intercept.add_wsgi_intercept(
…'BasicTest',80,protected_application)
大多数应用程序的URL不受
中间件的保护。您可以在不提供凭据的情况下访问它们。
>;>import httplib2
>;>client=httplib2.http()
>;>response,body=client.request('http://basictest/')
>;>print(response['status'])
200
>;>print(body.decode())
成功
任何包含字符串"protected"的URL都受
中间件保护,没有凭据无法访问。
>;>;响应,body=client.request('http://basictest/protected/')
>;>;打印(响应['status'])
401
>>>打印(回复['www-authenticate'])
basic realm="wsgi中间件测试"
>;>response,body=client.request(
…)http://basictest/this is protected as/')
>;>print(response['status'])
401
d您将获得401。
>;>client.add_credentials("baduser","baspassword")
>;>response,body=client.request("http://basictest/protected/")
>;>print(response['status'])
401
,即使对于
受保护的uri。
>;>>客户端。添加凭据("用户"、"密码")
>;>response,body=client.request('http://basictest/protected/')
>;>print(response['status'])
200
teardown。
>;
stacking
*****
basicauthmiddleware实例可以堆叠,每个实例保护不同的路径模式。在这里,我们将使用堆栈来保护
这两个regex".*protected.*"和".*different.*",而不将它们组合成一个复杂的regex。
>>>;def return_user_application(environ,start_response):
…开始响应('200',[('content-type','text/plain')])
…返回[str(environ['authenticated_user'].encode('utf-8')]
>>>def protected_application():
…受保护=基本中间件(
…return_user_application,realm="wsgi中间件测试",
…保护路径模式=".*protected.*",
…authenticate_with=authenticate)
…返回基本中间件(
…受保护,realm="另一个中间件",
…保护路径模式=".*different.*",
…authenticate_with=authenticate)
设置。
>>wsgi_intercept.add_wsgi_intercept(
…)堆叠,80,受保护的磁盘应用程序)
>;>client=httplib2.http()
两种路径模式都受保护:
>;>response,body=client.request('http://stacked/protected')
>;>print(response['status'])
401
>;>responsee,body=client.request('http://stacked/different')
>;>print(response['status'])
401
.request('http://stacked/protected resource')
>;>print(response['status'])
200
>;>print(body.decode())
user
>;>response,body=client.request('http://stacked/different resource')
>;>print(response['status'])
200
>;>print(body.decode())
user
>;>wsgi_intercept.remove_wsgi_intercept('stacked',80)
oauthmiddleware
----
oauthmiddleware实现oauth speci的第7节("访问受保护的
资源")。化。也就是说,它确保
传入的使用者密钥和访问令牌通过一些应用程序定义的
测试。它不会帮助您提供请求令牌或交换访问令牌的请求
令牌。
但是,由于我们使用的是
oauth,我们将检查使用者密钥和访问令牌,而不是
用户名和密码。
>;>/>>>>有效的令牌=OAuthToken("令牌","机密")
>>>定义身份验证(使用者、令牌、参数):
…""接受有效的使用者和令牌,拒绝所有其他内容。
…
…:return:消费者,如果凭据有效。
…没有,否则。
…"
…如果consumer==valid_consumer和token==valid_token:
…退货消费者
…将none
>;>print(authenticate(有效的消费者,有效的消费者,无)。key)
consumer
>;>invalid\u consumer=oauthcummer("其他消费者","其他消费者")
>;>print(authenticate(无效的消费者,有效的消费者,无))
none
为了测试oauthmiddleware的安全特性,我们还需要创建一个数据存储。在实际的应用程序中,数据存储可能是一个包含注册的使用者密钥和
令牌的数据库。我们正在使用一个为测试而设计的简单数据存储,并告诉它一个有效的使用者和令牌。
>;>;来自lazr.authentication.testing.oauth import simpleoauthdatastore
>;>;data\u store=simpleoauthdatastore(
…{有效的消费者.密钥:有效的消费者},
…{valid_token.key:valid_token})
>;>print(data_store.lookup_consumer("consumer").key)
consumer
>;>print(data_store.lookup_consumer("badcummer")
none
如果使用相同的值调用数据
store的lookup_nce()两次,则第一次调用将
返回false,第二次调用将返回true。
>;>;print(data戋store.lookup_nce("consumer","token","nonce"))
false
>;>;print(data戋store.lookup戋nonce("consumer","token","nonce"))
true
>;>;打印(数据存储查找nonce("newconsumer","token","nonce"))
false
从lazr.authentication.wsgi import oauthmiddleware中存储。
>;>返回OAuthmiddleware(
…虚拟应用程序,realm="oauth test",
…authenticate_with=authenticate,data_store=data_store)
>wsgi_intercept.add_wsgi_intercept(
…)OAuthTest',80,受保护的应用程序)
>;>client=httplib2.http()
OAuthrequest,OAuthSignatureMethod_明文)
>;>def sign_request(url,consumer=valid_consumer,token=valid_token):
…request=oauthrequest()。来自消费者和令牌(
…消费者,令牌,http_url=url)
…请求。签署请求(
…OAuthSignatureMethod_PlaintText(),使用者,令牌)
…headers=request.to_header('oauth test')
…返回头
>;>;url='http://oauthtest/'
>;>;头=签名请求(url)
>;>;响应,正文=客户端。请求(url,头=头)
>;>;打印(响应["状态])
200
>;>;打印(正文.解码())
成功
如果您重放第一次工作的已签名http请求,它将在第二次失败,因为您将发送一个已使用的nonce。
>;响应,body=client.request(url,headers=headers)
>;>;打印(响应状态s'])
401
未签名的请求将失败。
>;>response,body=client.request('http://oauthtest/')
>;>>打印(响应["状态])
401
>;>>打印(响应["www-authenticate"])
oauth realm="oauth test"
ers=签名请求(url,token=错误的令牌)
>;>response,body=client.request(url,headers=headers)
>;>print(response['status'])
401
teardown.
>;>wsgi-u intercept.remove\u wsgi-u intercept('oauthtest',80)
==================
关于lazr.身份验证的新闻
关于lazr.身份验证的新闻
=================
==========
=>0.1.3(2014-11-08)0.1.3(2014-11-08)0.1.3(2014-11-08)0.br/>============
==
=>
=>
=>0.1.2(2010-02-09)0.1.2(2010-02-09)0.1.2(2010-02-09)0.1==
-修复了阻止middlewar的错误e来自堆栈的实例。
0.1.1(2009-10-19)
===
-修复了导致OAuth中间件将
身份验证领域指定为基本域的错误。
0.1.0(2009-10-07)
===
-初始版本
BR/>lazr.authentication发布的目的是希望它会有用,但是
没有任何保证;甚至没有对适销性
或特定用途的适用性的隐含保证。有关详细信息,请参阅gnu less general public
许可证。
您应该已经收到gnu less general public许可证的副本
以及lazr.authentication。如果没有,请参阅<;http://www.gnu.org/licenses/>;
>;>;来自未来的导入打印功能r/>>;>gt;导入lazr.authentication
>;>print('version:%s%lazr.authentication.\uu version\uu)
版本:…
basicauthmidler软件实现http基本auth。它的构造函数
接受许多参数,包括执行实际身份验证的回调函数。此函数返回一个对象
,标识试图进行身份验证的用户。如果
身份验证凭据无效,则应该不返回任何凭据。
首先,让我们创建一个真正简单的wsgi应用程序,用200响应代码响应任何请求。
>;def dummy_application(environ,start_response):
…开始响应('200',[('content-type','text/plain')])
…return[b'success']
现在让我们保护这个应用程序。这是一个身份验证回调函数。
>;>def authenticate(用户名、密码):
…""接受"用户/密码",拒绝所有其他内容。
…
…:return:如果凭据有效,则返回用户名。
…没有,否则。
…"
…如果username=="user"和password=="password":
…返回用户名
…return none
>;>print(authenticate("user","password"))
user
>;>print(authenticate("notuser","password"))
none
lazr.authentication.wsgi导入Basicauthmiddleware
>>gt;def protected_application():
…返回基本中间件(
…dummy_application,realm="wsgi中间件测试",
…保护路径模式=".*protected.*",
…authenticate_with=authenticate)
>;>import wsgi_intercept
>;>from wsgi_intercept.httplib2_intercept import install
>;>install()
>;>wsgi_intercept.add_wsgi_intercept(
…'BasicTest',80,protected_application)
大多数应用程序的URL不受
中间件的保护。您可以在不提供凭据的情况下访问它们。
>;>import httplib2
>;>client=httplib2.http()
>;>response,body=client.request('http://basictest/')
>;>print(response['status'])
200
>;>print(body.decode())
成功
任何包含字符串"protected"的URL都受
中间件保护,没有凭据无法访问。
>;>;响应,body=client.request('http://basictest/protected/')
>;>;打印(响应['status'])
401
>>>打印(回复['www-authenticate'])
basic realm="wsgi中间件测试"
>;>response,body=client.request(
…)http://basictest/this is protected as/')
>;>print(response['status'])
401
d您将获得401。
>;>client.add_credentials("baduser","baspassword")
>;>response,body=client.request("http://basictest/protected/")
>;>print(response['status'])
401
,即使对于
受保护的uri。
>;>>客户端。添加凭据("用户"、"密码")
>;>response,body=client.request('http://basictest/protected/')
>;>print(response['status'])
200
teardown。
>;
stacking
*****
basicauthmiddleware实例可以堆叠,每个实例保护不同的路径模式。在这里,我们将使用堆栈来保护
这两个regex".*protected.*"和".*different.*",而不将它们组合成一个复杂的regex。
>>>;def return_user_application(environ,start_response):
…开始响应('200',[('content-type','text/plain')])
…返回[str(environ['authenticated_user'].encode('utf-8')]
>>>def protected_application():
…受保护=基本中间件(
…return_user_application,realm="wsgi中间件测试",
…保护路径模式=".*protected.*",
…authenticate_with=authenticate)
…返回基本中间件(
…受保护,realm="另一个中间件",
…保护路径模式=".*different.*",
…authenticate_with=authenticate)
设置。
>>wsgi_intercept.add_wsgi_intercept(
…)堆叠,80,受保护的磁盘应用程序)
>;>client=httplib2.http()
两种路径模式都受保护:
>;>response,body=client.request('http://stacked/protected')
>;>print(response['status'])
401
>;>responsee,body=client.request('http://stacked/different')
>;>print(response['status'])
401
.request('http://stacked/protected resource')
>;>print(response['status'])
200
>;>print(body.decode())
user
>;>response,body=client.request('http://stacked/different resource')
>;>print(response['status'])
200
>;>print(body.decode())
user
>;>wsgi_intercept.remove_wsgi_intercept('stacked',80)
oauthmiddleware
----
oauthmiddleware实现oauth speci的第7节("访问受保护的
资源")。化。也就是说,它确保
传入的使用者密钥和访问令牌通过一些应用程序定义的
测试。它不会帮助您提供请求令牌或交换访问令牌的请求
令牌。
但是,由于我们使用的是
oauth,我们将检查使用者密钥和访问令牌,而不是
用户名和密码。
>;>/>>>>有效的令牌=OAuthToken("令牌","机密")
>>>定义身份验证(使用者、令牌、参数):
…""接受有效的使用者和令牌,拒绝所有其他内容。
…
…:return:消费者,如果凭据有效。
…没有,否则。
…"
…如果consumer==valid_consumer和token==valid_token:
…退货消费者
…将none
>;>print(authenticate(有效的消费者,有效的消费者,无)。key)
consumer
>;>invalid\u consumer=oauthcummer("其他消费者","其他消费者")
>;>print(authenticate(无效的消费者,有效的消费者,无))
none
为了测试oauthmiddleware的安全特性,我们还需要创建一个数据存储。在实际的应用程序中,数据存储可能是一个包含注册的使用者密钥和
令牌的数据库。我们正在使用一个为测试而设计的简单数据存储,并告诉它一个有效的使用者和令牌。
>;>;来自lazr.authentication.testing.oauth import simpleoauthdatastore
>;>;data\u store=simpleoauthdatastore(
…{有效的消费者.密钥:有效的消费者},
…{valid_token.key:valid_token})
>;>print(data_store.lookup_consumer("consumer").key)
consumer
>;>print(data_store.lookup_consumer("badcummer")
none
如果使用相同的值调用数据
store的lookup_nce()两次,则第一次调用将
返回false,第二次调用将返回true。
>;>;print(data戋store.lookup_nce("consumer","token","nonce"))
false
>;>;print(data戋store.lookup戋nonce("consumer","token","nonce"))
true
>;>;打印(数据存储查找nonce("newconsumer","token","nonce"))
false
从lazr.authentication.wsgi import oauthmiddleware中存储。
>;>返回OAuthmiddleware(
…虚拟应用程序,realm="oauth test",
…authenticate_with=authenticate,data_store=data_store)
>wsgi_intercept.add_wsgi_intercept(
…)OAuthTest',80,受保护的应用程序)
>;>client=httplib2.http()
OAuthrequest,OAuthSignatureMethod_明文)
>;>def sign_request(url,consumer=valid_consumer,token=valid_token):
…request=oauthrequest()。来自消费者和令牌(
…消费者,令牌,http_url=url)
…请求。签署请求(
…OAuthSignatureMethod_PlaintText(),使用者,令牌)
…headers=request.to_header('oauth test')
…返回头
>;>;url='http://oauthtest/'
>;>;头=签名请求(url)
>;>;响应,正文=客户端。请求(url,头=头)
>;>;打印(响应["状态])
200
>;>;打印(正文.解码())
成功
如果您重放第一次工作的已签名http请求,它将在第二次失败,因为您将发送一个已使用的nonce。
>;响应,body=client.request(url,headers=headers)
>;>;打印(响应状态s'])
401
未签名的请求将失败。
>;>response,body=client.request('http://oauthtest/')
>;>>打印(响应["状态])
401
>;>>打印(响应["www-authenticate"])
oauth realm="oauth test"
ers=签名请求(url,token=错误的令牌)
>;>response,body=client.request(url,headers=headers)
>;>print(response['status'])
401
teardown.
>;>wsgi-u intercept.remove\u wsgi-u intercept('oauthtest',80)
==================
关于lazr.身份验证的新闻
关于lazr.身份验证的新闻
=================
==========
=>0.1.3(2014-11-08)0.1.3(2014-11-08)0.1.3(2014-11-08)0.br/>============
==
=>
=>
=>0.1.2(2010-02-09)0.1.2(2010-02-09)0.1.2(2010-02-09)0.1==
-修复了阻止middlewar的错误e来自堆栈的实例。
0.1.1(2009-10-19)
===
-修复了导致OAuth中间件将
身份验证领域指定为基本域的错误。
0.1.0(2009-10-07)
===
-初始版本