Manage Identity
How to set up Identity to sign in and use the Python SDK.
Read time 3 minutesLast updated a year ago
Unity Cloud services require the caller to be properly authorized before you can perform any operation or access other modules such as Assets.
There are two modules available to retrieve such authorization:
unity_cloud.identity.user_loginunity_cloud.identity.service_account
Only one module can be used at all time. Declaring prevents any call to the module by throwing an exception.
unity_cloud.identity.user_login.use()unity_cloud.identity.service_accountHow do I...?
Authenticate with a user login
The user sign-in process requires to:
- Manually request a sign-in (or a sign-out) from the integration.
- Enter your Unity account credentials to authenticate.
The user login implements the OAuth 2.0 PKCE standard flow to retrieve an access token, and relies on the default OS browser as an intermediary to authenticate.
Initialize and sign in to the Unity Dashboard
To use standard user login, follow these steps:
- Call to initialize the
unity_cloud.identity.user_login.use()module. This action automatically checks if any refresh token is currently cached. If so, you are authenticated.user_login - If there is no token, call to trigger your browser to open and sign in to Unity Dashboard.
login
After you have signed in, a refresh token is automatically cached.
def initialize_user_login(): unity_cloud.initialize() unity_cloud.identity.user_login.use() auth_state = unity_cloud.identity.user_login.get_authentication_state() if auth_state != unity_cloud.identity.user_login.Authentication_State.LOGGED_IN: unity_cloud.identity.user_login.login()
Sign out
To sign out, follow these steps:
- Initialize the following modules in this order:
unity_cloudunity_cloud.identity.user_login.use()- (Optional) . Read more about initializing and signing in the user through Unity Dashboard.
unity_cloud.identity.user_login.login
- Call the method with the
logoutargument set toclear_cache.TRUE
Calling the method signs you out from the browser. - (Optional) Set the argument to
clear_cacheto allow automatic re-login next timeFALSEis called.unity_cloud.identity.user_login.use()
unity_cloud.identity.user_login.logout( clear_cache = True )
Get information about the current user
To retrieve all information on the current user, including their access token, call the method.
get_current_user_infouser_infos = unity_cloud.identity.user_login.get_current_user_info()
Use a callback to track the authentication state during a user sign-in
Subscribing to the authentication state change allows you to get a callback every step of the authentication flow.
The states tracked are: , , , , and .
AWAITING_INITIALIZATIONAWAITING_LOGINLOGGED_INAWAITING_LOGOUTLOGGED_OUTdef auth_state_callback(state): print(state) unity_cloud.identity.user_login.subscribe( event_type = unity_cloud.identity.user_login.AUTHENTICATION_STATE_CHANGED_EVENT, callback = auth_state_callback ) unity_cloud.identity.user_login.login() unity_cloud.identity.user_login.logout(clear_cache = True) unity_cloud.identity.user_login.unsubscribe( event_type = unity_cloud.identity.user_login.AUTHENTICATION_STATE_CHANGED_EVENT, callback = auth_state_callback )
Use a service account to perform app-oriented authorization
The service account allows authorization for internal headless scenarios (automated scripts, tests, etc.).
Generate a service account
- Create a service account.
- Make sure to store the service account credentials (and
key ID) in a secure and private location.secret key
Refer to these credentials in your integration to authorize your calls to Unity Cloud services. - Attach the service account to an organization or a project.
Set up your app to use a service account
After the required initialization, pass the and of your service account generated via the Dashboard.
unity_cloudkey idsecret keykey_id = "1234abcd-ab12-cd34-ef56-123456abcdef" key = "12345678ABCDefgh-1234AbC-5678eFg" unity_cloud.identity.service_account.use(key_id=key_id, key=key)
Get a list of the accessible organizations or projects
To get a list of accessible organizations or projects, follow these steps:
- Use the method to retrieve a list of all accessible organizations.
get_organization_list - Call with an organization id to retrieve the accessible projects contained in it.
get_project_list- (Optional) Add the parameter to limit the amount of returned results. Leave it untouched or set it to 0 to return everything.
limit_to - (Optional) Add the parameter to specify the number of collection to skip in the results. Use it in combination with limit_to to page through results.
skip
- (Optional) Add the parameter
orgs = unity_cloud.identity.get_organization_list() if len(orgs) != 0: projects = unity_cloud.identity.get_project_list(orgs[0].id) permissions = unity_cloud.identity.get_organization_permissions(orgs[0].id) roles = unity_cloud.identity.get_organization_roles(orgs[0].id)
The , , and methods provide more granular information about roles and permissions that are inherited from whichever authorization flow you chose.
get_organization_permissionsget_organization_rolesget_project_permissionget_project_rolesCreate a project
To create a project, follow these steps:
- Create a object with the name of the new project and optionally metadata.
ProjectCreation - Call with the
create_projectobject and the organization ID.ProjectCreation
project_creation = ProjectCreation(name="myNewProject")unity_cloud.identity.create_project(project_creation, org_id)
Get a project
To get a single project, call with the organization and project IDs.
get_projectproject = unity_cloud.identity.get_project(org_id, project_id)
List users in an organization
To list all users associated to an organization, call with the organization ID.
get_organization_user_info_listusers = unity_cloud.identity.get_organization_user_info_list(org_id)
List users in a project
To list all users associated to a project, call with the organization and project IDs.
get_project_user_info_listusers = unity_cloud.identity.get_project_user_info_list(org_id, project_id)
Get a single user
To get the info for a user, call with the organization and user IDs.
get_user_infouser = unity_cloud.identity.get_user_info(org_id, user_id)