Cisco Secure Access Identities Registration sample script

Identities Registration API Guide

This guide provides Python client samples for the Cisco Secure Access Identities Registration API.

Note: Your Secure Access API key must have the permissions to read and write on the deployments.identities key scope. For more information about the API key scopes, see Secure Access OAuth 2.0 Scopes.

First get your Secure Access API key, set up your environment, and install the Secure Access API client. For more information, see Samples Overview.

Run the Script

  1. Copy the script to a local file called main.py. Locate the script in your environment in a directory above the cisco directory.
  2. Run python3 main.py.

main.py

"""
Copyright (c) 2025 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at

https://developer.cisco.com/docs/licenses

All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""

import requests
from requests_toolbelt import MultipartEncoder
import json
import os
from dotenv import load_dotenv

from cisco.secure_access import API
from cisco.secure_access import deployments
from cisco.secure_access import GET
from cisco.secure_access import PUT
from cisco.secure_access import token_url
from cisco.secure_access import client_id
from cisco.secure_access import client_secret

# Identity Registration API endpoints
endpoint_devices_endpoint = "identities/registrations/{}"

load_dotenv()

def get_endpoint_devices(api, type):
    ''' List the properties of the identity devices. '''
    try:
        if type is None:
            raise ValueError("type is required to get the identity devices.")
        url = endpoint_devices_endpoint.format(type)

        # Get the properties for the active and inactive identity devices
        response = api.Query(deployments, url, GET)

        # Check if the API request was successful
        if response.status_code == 200:
            print(f"Success. GET {url}, {response.json()}.")
            return response.json()
        else:
            print(f"Failed to get the identity devices of {type}. Status code: {response.status_code}, Response: {response.text}.")
            return
    except Exception as e:
        print(f"An error occurred: {e}.")

def put_endpoint_device(api, type, key, auth_name, label, status):
    ''' Update Identity Devices. '''
    try:
        if type is None or key is None or auth_name is None or label is None or status is None:
            raise ValueError("type, key, auth_name, label, and status are required to update the identity device.")

        url = endpoint_devices_endpoint.format(type)

        # Prepare the payload
        # Adapt method to handle multiple updates
        payload = [
            {
                "key": key,
                "authName": auth_name,
                "label": label,
                "status": status
            }
        ]

        # Update Endpoint Devices
        response = api.Query(deployments, url, PUT, payload)

        # Check the response status
        if response.status_code == 200:
            print(f"Success. PUT {url}, {response.json()}")
            return response.json()
        else:
            print(f"Failed to update the identity devices of {type}. Status code: {response.status_code}, Response: {response.text}.")
            return
    except Exception as e:
        print(f"An error occurred: {e}.")

def main():
    # Exit out if the required client_id or client_secret is not set
    for var in ['API_KEY', 'API_SECRET', 'OUTPUT_DIR']:
        if os.environ.get(var) == None:
            print("Required environment variable: {} not set".format(var))
            exit()

    # Get an API token
    api = API(token_url, client_id, client_secret)

    try:
        type = "device"

        # list the endpoint devices in the organization
        json_data = get_endpoint_devices(api, type)

        # add an identity device in the organization with the PUT operation
        key = "123e4567"
        auth_name = "auth_device_300"
        label = "labelabc123label"
        status = "active"
        json_data = put_endpoint_device(api, type, key, auth_name, label, status)

        # list the endpoint devices in the organization
        json_data = get_endpoint_devices(api, type)

        # update an identity device in the organization with the PUT operation
        # modify to update several identity devices
        key = "123e4567"
        auth_name = "auth_device_300"
        label = "labelupdated"
        status = "inactive"
        json_data = put_endpoint_device(api, type, key, auth_name, label, status)

        # list the endpoint devices in the organization
        json_data = get_endpoint_devices(api, type)
    except Exception as e:
        print(e)

if __name__ == "__main__":
    main()