Authress SDK作为服务进行授权,并与Authress API交互。
authress-sdk的Python项目详细描述
authress sdk
Authress SDK for Python。在
这是Authress SDK,用于与位于https://authress.io的服务提供者身份验证集成。在
使用
pip install authress-sdk
(您可能需要使用根权限运行pip
:sudo pip install authress-sdk
)
然后导入包:
^{pr2}$入门
使用用户令牌授权
fromauthress_sdkimportApiClient# create an instance of the API class during service initialization# Replace DOMAIN with the Authress domain for your accounthost="https://DOMAIN.api.authress.io"authress_client=ApiClient(host)# on api routefromflaskimportrequestfromauthress_sdkimportUserPermissionsApi,ApiException@app.route('/resources/<resourceId>')defget_resource(resourceId):# Get the user token and pass it to authressauthorization_token=request.headers.get('authorization')authress_client.set_token(authorization_token)# Check Authress to authorize the usertryapi_instance=UserPermissionsApi(authress_client)api_instance.authorize_user(None,f'resources/{resourceId}','READ')exceptApiExceptionasapi_exception:# Will throw except if the user is not authorized to read the resourceifapi_exception.statusis403:return403raiseapi_exception# On success, continue with the route code to load resource and return itreturn'Resource',200
向服务客户端授权
fromauthress_sdkimportApiClient# create an instance of the API class during service initialization# Replace DOMAIN with the Authress domain for your accounthost="https://DOMAIN.api.authress.io"# Create a service client in the Authress management portal and past the access token hereaccess_token='eyJrZXlJ....'authress_client=ApiClient(host,access_token)# on api routefromflaskimportrequestfromauthress_sdkimportUserPermissionsApi,ApiException@app.route('/resources/<resourceId>')defget_resource(resourceId):# Get the useruser_id=get_user_id(request)# Check Authress to authorize the usertryapi_instance=UserPermissionsApi(authress_client)api_instance.authorize_user(user_id,f'resources/{resourceId}','READ')exceptApiExceptionasapi_exception:# Will throw except if the user is not authorized to read the resourceifapi_exception.statusis403:return403raiseapi_exception# On success, continue with the route code to load resource and return itreturn'Resource',200
- 项目
标签: