Okta Python Management SDK

repository·master·Indexed 42 days ago

https://github.com/okta/okta-sdk-python

An auto-generated Python client based on Okta's OpenAPI specifications that allows developers to interact with the Okta Management API from server-side applications. Version 3.4.5 requires Python 3.9.0 or higher. The SDK features asynchronous API calls, support for OAuth 2.0 private key authentication, and extensible components for custom request executors, HTTP clients, and cache managers.

Tokens
335K
Snippets
781
Records
844
Agent score
52%

What's inside Okta

  1. Overview of okta.DeviceApi

    master

    The okta.DeviceApi class provides methods to manage devices within Okta. It allows you to perform lifecycle operations such as activating, deactivating, suspending, and deleting devices, as well as retrieving device details and listing users associated with a device.

    Available Methods:

    • activate_device(device_id): Sets device status to ACTIVE.
    • deactivate_device(device_id): Sets device status to DEACTIVATED.
    • delete_device(device_id): Permanently deletes a DEACTIVATED device.
    • get_device(device_id): Retrieves a specific device.
    • list_device_users(device_id): Lists all users for a specific device.
    • list_devices(): Lists all devices.
    • suspend_device(device_id): Suspends a device.
    • unsuspend_device(device_id): Unsuspends a device.
  2. Overview of okta.GroupApi

    master

    The okta.GroupApi class provides methods to manage groups within your Okta organization. It allows you to create, retrieve, delete, and manage group memberships (assigning/unassigning users).

    Available methods include:

    • add_group: Add a new group.
    • assign_user_to_group: Assign a user to a group.
    • delete_group: Delete a group.
    • get_group: Retrieve a specific group by ID.
    • list_assigned_applications_for_group: List all assigned apps for a group.
    • list_group_users: List all member users of a group.
    • list_groups: List all groups.
    • replace_group: Replace a group.
    • unassign_user_from_group: Unassign a user from a group.
  3. Overview of okta.CustomPagesApi

    master
    The okta.CustomPagesApi class provides methods to manage customized brand pages within Okta, including error pages, sign-in pages, and sign-out settings. It allows for retrieving, replacing, and deleting both live customized pages and preview pages (unpublished changes).
  4. Overview of WebAuthnPreregistrationApi

    master
    The WebAuthnPreregistrationApi class provides methods to manage WebAuthn preregistration factors. This includes activating enrollments, assigning fulfillment errors, deleting factors, enrolling new factors, generating fulfillment requests, listing factors, and sending PINs. All URIs are relative to your Okta host (e.g., https://subdomain.okta.com).
  5. Manage custom SMTP servers with okta.EmailServerApi

    master

    The okta.EmailServerApi class allows you to manage custom SMTP server configurations for your Okta organization. You can create, retrieve, list, delete, test, and update SMTP server settings. All requests are relative to your Okta host (e.g., https://subdomain.okta.com).

    import okta
    
    configuration = okta.Configuration(host="https://subdomain.okta.com")
    configuration.api_key['apiToken'] = 'YOUR_API_KEY'
    
    with okta.ApiClient(configuration) as api_client:
        api_instance = okta.EmailServerApi(api_client)
        # Use api_instance to call methods like create_email_server, list_email_servers, etc.
  6. Manage Application SSO Credential Keys with ApplicationSSOCredentialKeyApi

    master

    The okta.ApplicationSSOCredentialKeyApi class provides methods to manage X.509 certificate key credentials for Okta applications. You can use it to generate new keys, clone existing keys between applications, list current keys, and manage Certificate Signing Requests (CSRs).

    import okta
    
    configuration = okta.Configuration(host="https://subdomain.okta.com")
    configuration.api_key['apiToken'] = "YOUR_API_KEY"
    
    with okta.ApiClient(configuration) as api_client:
        api_instance = okta.ApplicationSSOCredentialKeyApi(api_client)
        # Use api_instance to call methods like generate_application_key, clone_application_key, etc.
  7. Manage API tokens with ApiTokenApi

    master

    The okta.ApiTokenApi class allows you to manage Okta API tokens. You can retrieve metadata for specific tokens, list all active tokens, revoke specific tokens, or revoke the token currently being used for authentication. All requests are relative to your Okta host (e.g., https://subdomain.okta.com).

    import okta
    
    configuration = okta.Configuration(host="https://subdomain.okta.com")
    configuration.api_key['apiToken'] = "YOUR_API_KEY"
    
    with okta.ApiClient(configuration) as api_client:
        api_instance = okta.ApiTokenApi(api_client)
        # Use api_instance to call methods like list_api_tokens(), get_api_token(id), etc.
  8. Refactor Model Usage: From OktaObject to Pydantic

    master

    In v3.0.0, all API models have been refactored to use Pydantic's BaseModel. This provides runtime data validation and better IDE support.

    Changes to Model Implementation:

    • Inheritance: Models now inherit from pydantic.BaseModel.
    • Initialization: The __init__ method for manual value assignment has been removed. Instead, properties are declared directly in the class with specific types, making them required or optional with default values. Type validation occurs upon object creation.
    • New Utility Methods: Models now include the following methods for data handling:
      • to_str()
      • to_json()
      • from_json()
      • to_dict()
      • from_dict()
  9. How to use the Okta Python Management SDK

    master

    The Okta Python Management SDK uses asynchronous methods for all API calls. To use the client, you must initialize an OktaClient and call its methods using await within an async def function. The function must then be executed using an event loop (e.g., via asyncio).

    Methods are organized by the API endpoint they correspond to. For example, methods related to the Users API are found under the User API module.

    from okta.client import Client as OktaClient
    import asyncio
    
    async def main():
        client = OktaClient()
        # Most methods return (data, response, error)
        users, resp, err = await client.list_users()
        print(len(users))
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
  10. Handle Rate Limiting and Retries

    master

    The Okta API returns a 429 status code when rate limits are exceeded. The SDK includes a built-in retry strategy that automatically handles these errors by looking at the X-Rate-Limit-Reset header.

    You can configure the retry behavior using these keys in the client configuration:

    • client.rateLimit.maxRetries: The number of times to retry the request (default is managed by the SDK).
    • client.requestTimeout: The waiting time in seconds for a request to be resolved. A value of 0 means no timeout.
  11. Refactor model usage to Pydantic in v3.0.0+

    master

    In v3.0.0+, models are Pydantic BaseModel objects. Instead of passing dictionaries to an __init__ method, you should instantiate nested models directly using keyword arguments and pass them into request models.

    from okta import UserProfile, CreateUserRequest
    
    # Instantiate nested models directly with keyword arguments
    profile = UserProfile(firstName="John", lastName="Doe")
    create_request = CreateUserRequest(profile=profile)
  12. Manage Hook Keys with HookKeyApi

    master

    The okta.HookKeyApi class provides methods to manage keys used for application features like inline hooks. You can create, retrieve, delete, list, and replace hook keys.

    Key Constraints & Details:

    • Limit: You can create a maximum of 50 keys per Okta organization.
    • ID vs keyId: The id property is the unique identifier for CRUD operations. The keyId is an alias for the public key used to retrieve public key details.
    • Safety: Only keys that are not currently in use can be deleted.
    import okta
    
    configuration = okta.Configuration(host="https://subdomain.okta.com")
    configuration.api_key['apiToken'] = "YOUR_API_KEY"
    
    with okta.ApiClient(configuration) as api_client:
        api_instance = okta.HookKeyApi(api_client)
        # Use api_instance to call methods like create_hook_key, delete_hook_key, etc.