# Authing-Python

The Authing Python SDK consists of two parts: ManagementClient and AuthenticationClient. All operations performed in ManagementClient are performed as an administrator, including modules such as managing users, managing roles, managing authority policies, and managing user pool configuration. All operations in AuthenticationClient are performed as the current terminal user, including methods such as login, registration, modification of user information, and logout.

You should set the initialized ManagementClient instance to a global variable (initialize only once), and AuthenticationClient should be initialized one for every request.

Authing Python SDK supports both python2 and python3.

# Installation

pip install authing

# Using the management module

Initializing ManagementClient requires user_pool_id (user pool ID) and secret (user pool key):

You can click here Learn how to get UserPoolId and Secret (opens new window).

from authing.v2.management import ManagementClient, ManagementClientOptions

management_client = ManagementClient(
  options=ManagementClientOptions(
    user_pool_id='AUTHING_USERPOOL_ID',
    secret='AUTHING_USERPOOL_SECRET',
))

Now the ManagementClient() instance is ready to use. For example, you can get the list of users in the user pool:

data = management_client.users.list()

The returned data is as follows:

{
  "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",
    }
  ]
}

# Use authentication module

Initializing the AuthenticationClient requires app_id (application ID):

You can view your own application list in Applications in the console.

from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions

authentication_client = AuthenticationClient(
  options=AuthenticationClientOptions(
    app_id='AUTHING_APP_ID'
))

Next, you can perform operations such as registration and login:

username = get_random_string(10)
password = get_random_string(10)
user = authentication_client.login_by_username(
    username=username,
    password=password,
)

After logging in, methods such as update_profile that require users to log in are available:

authentication_client.update_profile({
  'nickname':'Nick'
})

You can also use the access_token parameter to initialize the AuthenticationClient instead of calling the login method every time:

from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions

authentication_client = AuthenticationClient(
  options=AuthenticationClientOptions(
    app_id='AUTHING_APP_ID',
    access_token='AUTHING_USER_TOKEN'
))

Execute the update_profile method again and find that it is also successful:

user = authentication_client.update_profile({
  'nickname':'Nick'
})

# Error handling

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) # user does not exist

For the complete error code, please see this document (opens new window).

# Get help

Join us on Gitter: #authing-chat (opens new window)

# Interface Index

The authentication module contains the following methods:

  • Get the user information of the current user: get_current_user
  • Register with email: register_by_email
  • Register with username: register_by_username
  • Use mobile phone number verification code to register: register_by_phone_code
  • Login with email: login_by_email
  • Login with username: login_by_username
  • Use the phone number verification code to log in to login_by_phone_code
  • Use mobile phone number and password to log in: login_by_phone_password
  • Send email: send_email
  • Send SMS verification code: send_sms_code
  • Check the valid status of the token: check_login_status
  • Use mobile phone number verification code to reset password: reset_password_by_phone_code
  • Use email verification code to reset password: reset_password_by_email_code
  • Update user profile: update_profile
  • Update password: update_password
  • Update phone number: update_phone
  • Update email: update_email
  • Refresh token: refresh_token
  • Bind mobile phone number: bind_phone
  • Unbind mobile phone number: unbind_phone
  • Add current user-defined field value: set_udv
  • Get the current user's custom field value: udv
  • Delete the current user-defined field value: remove_udv

For details, see:

Core Authentication Module

The management module contains the following sub-modules:

Manage users Manage role Manage Strategy Manage Authority & Access Control Manage custom field metadata