sanic路由的基本、摘要和承载令牌认证

Sanic-HTTPAuth的Python项目详细描述


Sanic HttpAuth

Build Status

这是萨尼克的Flask-HTTPAuth叉子。它是一个简单的扩展,为sanic路由提供基本和摘要http身份验证。

仍在进行中的工作,欢迎作出贡献。

安装

最简单的安装方法是通过pip。

pip install Sanic-HTTPAuth

基本身份验证示例

importhashlibfromsanicimportSanicfromsanic_httpauthimportHTTPBasicAuthapp=Sanic(__name__)auth=HTTPBasicAuth()defhash_password(salt,password):salted=password+saltreturnhashlib.sha512(salted.encode("utf8")).hexdigest()app_salt="APP_SECRET - don't do this in production"users={"john":hash_password(app_salt,"hello"),"susan":hash_password(app_salt,"bye"),}@auth.verify_passworddefverify_password(username,password):ifusernameinusers:returnusers.get(username)==hash_password(app_salt,password)returnFalse@app.route("/")@auth.login_requireddefindex(request):return"Hello, %s!"%auth.username()if__name__=="__main__":app.run()

注意:请参阅Flask-HTTPAuth documentation以获取涉及密码散列和自定义验证回调的更复杂示例。

摘要式身份验证示例

fromsanicimportSanicfromsanic_httpauthimportHTTPDigestAuthfromsanic_sessionimportSessionapp=Sanic(__name__)app.config["SECRET_KEY"]="secret key here"auth=HTTPDigestAuth()Session(app)users={"john":"hello","susan":"bye"}@auth.get_passworddefget_pw(username):ifusernameinusers:returnusers.get(username)returnNone@app.route("/")@auth.login_requireddefindex(request):return"Hello, %s!"%auth.username()if__name__=="__main__":app.run()

资源

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java错误:无法找到或加载主类   Base85又名ASCI85 java项目   在Java oracle过程中创建Clob对象时引发AbstractMethodError   访问布尔Java时出现NullPointerException   在Java中压缩和解压缩字符串的调试程序   java了解JavaMail下未读消息的不同方法   java Vaadin 10网格样式基于内容的单个行   java使用Sparql查询识别实体的类别/分类   java如何在组件的设计阶段防止内存泄漏?   java使用共享首选项在应用程序上保存更改的语言   Spring数据jpa中具有复合PK的表的java本机查询   java复选框节点树