flask-rebar-auth0是一个简单的flask-rebar身份验证器auth0
flask-rebar-auth0的Python项目详细描述
烧瓶-钢筋-认证0
简单的Flask-Rebar验证器,用于Auth0。 您的访问令牌必须是JWT tokens,此身份验证器才能工作。
初始化
# Configapp.config.from_mapping({"AUTH0_ENDPOINT":"perdu.auth0.com",# The Auth0 domain for your tenant"AUTH0_ALGORITHMS":["RS256"],# The authorized algorithms, you should not have to change it"AUTH0_AUDIENCE":"https://api.perdu.com"# The API Identifier as set on Auth0"AUTH0_HEADER_AUTHENTICATION":True# USe the authentication by header"AUTH0_HEADER_NAME":"Authorization"# (OPTIONAL) Change the header used"AUTH0_HEADER_PREFIX":"Bearer"# (OPTIONAL) Change the prefix used# OR"AUTH0_COOKIE_AUTHENTICATION":True# Use the authentication by cookie"AUTH0_COOKIE_NAME":"Some Cookie"# Name of the cookie containing the access token})# Createauthenticator=Auth0Authenticator(app)@authenticator.identity_handlerdefcreate_user(claims:Dict[str,Any])->Any:"""Built a user object from the claims"""return{"id":claims["sub"]}
用法
fromflask_rebar_auth0importget_authenticated_user@registry.handles(rule="/users/me",method="GET",marshal_schema=UserSchema(),authenticator=authenticator,# Use the authenticator)defget_user():returnget_authenticated_user()# Get the user data created by the identity_handler@registry.handles(rule="/users/me/location",method="GET",marshal_schema=UserLocationSchema(),authenticator=authenticator.with_scopes(["read:location"]),# Require some scopes to access the ressource)defget_user_location():user=get_authenticated_user()returnlocationService.get(user)
大摇大摆
如果要使用swagger,则需要注册自定义验证器。
这是rebar
所需要的,以便能够将它们转换为正确的招摇security definition。
如果使用Cookie
身份验证,请注意swagger 2.0确实支持这种身份验证方法。它将注册为Cookie
头。
fromflask_rebar_auth0importregister_authenticatorsregister_authenticators(registry)