Authing SDK for Python
Project description
meta:
- name: description content: Python SDK
Authing - Python
Authing Python SDK 由两部分组成:ManagementClient 和 AuthenticationClient。
AuthenticationClient 以终端用户(End User)的身份进行请求,提供了登录、注册、登出、管理用户资料、获取授权资源等所有管理用户身份的方法;此模块还提供了各种身份协议的 SDK,如 OpenID Connect, OAuth 2.0, SAML 和 CAS。此模块适合用于纯后端交互的服务器环境。
ManagementClient 以管理员(Administrator)的身份进行请求,用于管理用户池资源和执行管理任务,提供了管理用户、角色、应用、资源等方法;一般来说,你在 Authing 控制台 中能做的所有操作,都能用此模块完成。此模块适合在后端服务器环境使用。
你应该将初始化过后的 ManagementClient 实例设置为一个全局变量(只初始化一次),而 AuthenticationClient 应该每次请求初始化一个。
Authing Python SDK 同时支持
python2和python3。
安装
pip install authing
使用管理模块
ManagementClient 以管理员(Administrator)的身份进行请求,用于管理用户池资源和执行管理任务,提供了管理用户、角色、应用、资源等方法;一般来说,你在 Authing 控制台 中能做的所有操作,都能用此模块完成。此模块适合在后端服务器环境使用。
初始化
ManagementClient 初始化需要传入用户池 ID userPoolId 和用户池密钥 secret:
你可以在此了解如何获取 UserPoolId 和 Secret .
你应该将初始化过后的 ManagementClient 实例设置为一个全局变量(只初始化一次)。
from authing.v2.management import ManagementClient, ManagementClientOptions
management_client = ManagementClient(
options=ManagementClientOptions(
user_pool_id='AUTHING_USERPOOL_ID',
secret='AUTHING_USERPOOL_SECRET',
))
现在 ManagementClient() 实例就可以使用了。例如可以获取用户池中的用户列表:
data = management_client.users.list()
返回的数据如下:
{
"totalCount": 1,
"list": [
{
"id": "5f7ddfe62ba819802422362e",
"arn": "arn:cn:authing:5f7a993eb9b49dcd5c021e40:user:5f7ddfe62ba819802422362e",
"userPoolId": "5f7a993eb9b49dcd5c021e40",
"username": "nhxcpzmklk",
"email": null,
"emailVerified": false,
"phone": null,
"phoneVerified": false,
"unionid": null,
"openid": null,
"nickname": null,
"registerSource": ["import:manual"],
"photo": "https://usercontents.authing.cn/authing-avatar.png",
"password": "a56f21e5659428f9b353be4ed667fc05",
"oauth": null,
"token": null,
"tokenExpiredAt": null,
"loginsCount": 0,
"lastLogin": null,
"lastIP": null,
"signedUp": "2020-10-07T23:33:58+08:00",
"blocked": false,
"isDeleted": false,
"device": null,
"browser": null,
"company": null,
"name": null,
"givenName": null,
"familyName": null,
"middleName": null,
"profile": null,
"preferredUsername": null,
"website": null,
"gender": "U",
"birthdate": null,
"zoneinfo": null,
"locale": null,
"address": null,
"formatted": null,
"streetAddress": null,
"locality": null,
"region": null,
"postalCode": null,
"country": null,
"createdAt": "2020-10-07T23:33:58+08:00",
"updatedAt": "2020-10-07T23:33:58+08:00"
}
]
}
使用认证模块
AuthenticationClient 以终端用户(End User)的身份进行请求,提供了登录、注册、登出、管理用户资料、获取授权资源等所有管理用户身份的方法;此模块还提供了各种身份协议的 SDK,如 OpenID Connect, OAuth 2.0, SAML 和 CAS。此模块适合用于纯后端交互的服务器环境。
初始化
初始化 AuthenticationClient 需要 app_id(应用 ID)和 app_host(应用端点,如 https://YOUR_DOMAIN.authing.cn):
你可以在此获取 app_id 和 app_host。
from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions
authentication_client = AuthenticationClient(
options=AuthenticationClientOptions(
app_id='AUTHING_APP_ID',
app_host='https://YOUR_DOMAIN.authing.cn'
))
完整参数如下:
app_id: Authing 应用 ID(必填);app_host: Authing 应用地址(必填),格式为https://YOUR_DOMAIN.authing.cn;token: 用户的 id_token(可选),你可以在前端 localStorage 中缓存用户id_token,然后使用id_token初始化 SDK,从而实现记住登录的目的;timeout: 请求超时时间(可选),位为毫秒,默认为 10000(10 秒);on_error: 错误处理函数(可选),你可以用其来全局捕捉 Authing 客户端请求的所有异常。完整的错误代码请见此文档。函数定义为:
def on_error(code, message):
raise AuthingException(code=code, errmsg=message)
enc_public_key: 密码非对称加密公钥(可选),如果你使用的是 Authing 公有云服务,可以忽略;如果你使用的是私有化部署的 Authing,请联系 Authing IDaaS 服务管理员。lang: 接口 Message 返回语言格式(可选),可选值为zh-CN和en-US,默认为zh-CN。
快速开始
我们推荐每次请求初始化一个新的 AuthenticationClient,保证不同请求之间完全隔离。
username = "bob"
password = "passw0rd"
user = authentication_client.login_by_username(
username=username,
password=password,
)
完成登录之后,update_profile 等要求用户登录的方法就可用了:
authentication_client.update_profile({
'nickname': 'Nick'
})
你也可以使用 token 参数来初始化 AuthenticationClient, 而不需要每次都调用 login 方法:
from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions
authentication_client = AuthenticationClient(
options=AuthenticationClientOptions(
app_id='AUTHING_APP_ID',
app_host='https://YOUR_DOMAIN.authing.cn',
token='ID_TOKEN'
))
再次执行 update_profile 方法,发现也成功了:
user = authentication_client.update_profile({
'nickname': 'Nick'
})
错误处理
如果函数执行失败会抛出异常,你需要使用 try/except 捕捉异常:
from authing.v2.exceptions import AuthingException
try:
authentication_client.login_by_username(
username='bob',
password='passw0rd',
)
except AuthingException as e:
print(e.code) # 2004
print(e.message) # 用户不存在
完整的错误代码请见此文档。
私有化部署
私有化部署场景需要指定你私有化的 Authing 服务的 GraphQL 端点(不带协议头和 Path)和密码加密公钥,如果你不清楚可以联系 Authing IDaaS 服务管理员。
from authing.v2.management import ManagementClient, ManagementClientOptions
management_client = ManagementClient(
options=ManagementClientOptions(
user_pool_id='AUTHING_USERPOOL_ID',
secret='AUTHING_USERPOOL_SECRET',
host="https://core.you-authing-service.com",
enc_public_key="YOUR_PUBLIC_KEY"
))
获取帮助
Join us on Gitter: #authing-chat
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file authing-4.4.0.tar.gz.
File metadata
- Download URL: authing-4.4.0.tar.gz
- Upload date:
- Size: 42.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a9164562cd3a7a6fe3a9ede1cc7d2c01587f56bdbe25e4e7981b43e2f247b9d
|
|
| MD5 |
354730c104d0ccec98e77ebec1ee235b
|
|
| BLAKE2b-256 |
87f6de9d2f08d9e730c171491b76055e7f7b85579a4f0be5fcbb306fd8a3499f
|
File details
Details for the file authing-4.4.0-py3-none-any.whl.
File metadata
- Download URL: authing-4.4.0-py3-none-any.whl
- Upload date:
- Size: 47.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e998046f0de89dbb90e223a85b0c01433c6b8ae744919840bbe8fe35217f29e6
|
|
| MD5 |
7b885b22fb578c245f532c9c944f4427
|
|
| BLAKE2b-256 |
2c8891009491f7afc994285612b22cbd9c18a01039f826ec6e52299cf1dc43b7
|