From a5a6180cccda2ea3eb26fb83b48dff96b920f57a Mon Sep 17 00:00:00 2001 From: Will Munslow Date: Thu, 8 May 2025 11:16:20 -0700 Subject: [PATCH 1/2] fix: add example for create_auth_claim & delete_auth_claim --- sample_script.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/sample_script.py b/sample_script.py index bff0c53..542c915 100644 --- a/sample_script.py +++ b/sample_script.py @@ -56,4 +56,76 @@ print("The response of PrivilegesApi->list_privileges:\n") pprint(api_response) except Exception as e: - print("Exception when calling PrivilegesApi->list_privileges: %s\n" % e) \ No newline at end of file + print("Exception when calling PrivilegesApi->list_privileges: %s\n" % e) + + # Create an instance of the APIAuthClaimsApi + auth_claims_api = onelogin.APIAuthClaimsApi(api_client) + content_type = "application/json" + + # First, list available auth servers to get an ID + auth_server_api = onelogin.APIAuthorizationServerApi(api_client) + try: + # Get list of auth servers + auth_servers = auth_server_api.get_auth_servers(content_type=content_type) + print("Available Auth Servers:\n") + pprint(auth_servers) + + # For testing, use the first auth server ID if available + if auth_servers and len(auth_servers) > 0: + auth_server_id = str(auth_servers[0].id) + + # Create an auth claim + auth_claim = onelogin.AuthClaim( + name="test_claim", + user_attribute_mappings="email" + ) + + try: + # Create Auth Claim + create_response = auth_claims_api.create_auth_claim( + api_auth_id=auth_server_id, + content_type=content_type, + auth_claim=auth_claim + ) + print("Created Auth Claim with ID:\n") + pprint(create_response) + + # Get the created claim ID + claim_id = create_response + + # List all claims to verify creation + try: + all_claims = auth_claims_api.get_authclaims( + api_auth_id=auth_server_id, + content_type=content_type + ) + print("All Auth Claims:\n") + pprint(all_claims) + except Exception as e: + print(f"Exception when listing auth claims: {e}") + + # Delete the auth claim + try: + auth_claims_api.delete_auth_claim( + api_auth_id=auth_server_id, + claim_id=claim_id, + content_type=content_type + ) + print(f"Successfully deleted auth claim with ID: {claim_id}") + + # Verify deletion by listing claims again + all_claims_after = auth_claims_api.get_authclaims( + api_auth_id=auth_server_id, + content_type=content_type + ) + print("Auth Claims after deletion:\n") + pprint(all_claims_after) + except Exception as e: + print(f"Exception when deleting auth claim: {e}") + + except Exception as e: + print(f"Exception when creating auth claim: {e}") + else: + print("No authorization servers found to test with.") + except Exception as e: + print(f"Exception when getting auth servers: {e}") \ No newline at end of file From 74722922a186a7138081d8f9e965cee10df1ebfd Mon Sep 17 00:00:00 2001 From: Sumit Kumar Date: Fri, 9 May 2025 22:31:25 +0530 Subject: [PATCH 2/2] fix: updated CRUD ops for auth_claims and auth_server apis --- app_script.py | 70 --------------------- sample_script.py | 160 +++++++++++++++++++++++++++-------------------- user_script.py | 82 ------------------------ 3 files changed, 93 insertions(+), 219 deletions(-) delete mode 100644 app_script.py delete mode 100644 user_script.py diff --git a/app_script.py b/app_script.py deleted file mode 100644 index 59ae4c0..0000000 --- a/app_script.py +++ /dev/null @@ -1,70 +0,0 @@ -import os -import onelogin -from onelogin.rest import ApiException -from onelogin.models.generic_app import GenericApp -from pprint import pprint - -# Change host to your domain or it will default to https://your-api-subdomain.onelogin.com -configuration = onelogin.Configuration( - host = "https://your-api-subdomain.onelogin.com", - username = "", - password = "" -) - -# Enter a context with an instance of the API client -with onelogin.ApiClient(configuration) as api_client: - # Create an instance of the API class - token_instance = onelogin.OAuth2Api(api_client) - generate_token_request = {"grant_type":"client_credentials"} # GenerateTokenRequest | Request Body to Generate OAuth Token - content_type="application/json" - - try: - # Generate and Save Access Token - api_response = token_instance.generate_token(generate_token_request, content_type=content_type) - configuration.access_token = api_response.access_token - pprint(configuration.access_token) - except Exception as e: - print("Exception when generating access token: %s\n" % e) - - app_id = "" # Replace with your app ID - app_instance = onelogin.AppsApi(api_client) - - try: - # List Apps - api_response = app_instance.list_apps() - print("The response of AppsApi->get_app:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling AppsApi->get_app: %s\n" % e) - - try: - # Get App by ID - api_response = app_instance.get_app(app_id=app_id) - print("The response of AppsApi->get_app:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling AppsApi->get_app: %s\n" % e) - - try: - # Update App - api_response = app_instance.update_app(app_id=app_id, request_body={"visible": False}) - print("The response of AppsApi->update_app:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling AppsApi->update_app: %s\n" % e) - - # try: - # # Delete App - # api_response = app_instance.delete_app(app_id=app_id) - # print("The response of AppsApi->delete_app:\n") - # pprint(api_response) - # except Exception as e: - # print("Exception when calling AppsApi->delete_app: %s\n" % e) - - try: - # Get App Users - api_response = app_instance.get_app_users(app_id=app_id) - print("The response of AppsApi->get_app_users:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling AppsApi->get_app_users: %s\n" % e) \ No newline at end of file diff --git a/sample_script.py b/sample_script.py index 542c915..978eb07 100644 --- a/sample_script.py +++ b/sample_script.py @@ -20,6 +20,7 @@ token_instance = onelogin.OAuth2Api(api_client) generate_token_request = {"grant_type":"client_credentials"} # GenerateTokenRequest | Request Body to Generate OAuth Token content_type="application/json" + try: # Generate and Save Access Token api_response = token_instance.generate_token(generate_token_request, content_type=content_type) @@ -58,74 +59,99 @@ except Exception as e: print("Exception when calling PrivilegesApi->list_privileges: %s\n" % e) - # Create an instance of the APIAuthClaimsApi - auth_claims_api = onelogin.APIAuthClaimsApi(api_client) - content_type = "application/json" - - # First, list available auth servers to get an ID + + auth_servers = None auth_server_api = onelogin.APIAuthorizationServerApi(api_client) + try: - # Get list of auth servers - auth_servers = auth_server_api.get_auth_servers(content_type=content_type) - print("Available Auth Servers:\n") - pprint(auth_servers) - - # For testing, use the first auth server ID if available - if auth_servers and len(auth_servers) > 0: - auth_server_id = str(auth_servers[0].id) - - # Create an auth claim - auth_claim = onelogin.AuthClaim( - name="test_claim", - user_attribute_mappings="email" + # Create a dummy auth server for testing + auth_server = onelogin.AuthServer( + name="Test Auth Server", + description="This is a dummy auth server", + configuration= { + "resource_identifier": "http://myapi.com/contacts2", + "audiences": ["http://myapi.com/contacts2"] + } ) - - try: - # Create Auth Claim - create_response = auth_claims_api.create_auth_claim( - api_auth_id=auth_server_id, - content_type=content_type, - auth_claim=auth_claim - ) - print("Created Auth Claim with ID:\n") - pprint(create_response) - - # Get the created claim ID - claim_id = create_response - - # List all claims to verify creation - try: - all_claims = auth_claims_api.get_authclaims( - api_auth_id=auth_server_id, - content_type=content_type - ) - print("All Auth Claims:\n") - pprint(all_claims) - except Exception as e: - print(f"Exception when listing auth claims: {e}") - - # Delete the auth claim - try: - auth_claims_api.delete_auth_claim( - api_auth_id=auth_server_id, - claim_id=claim_id, - content_type=content_type - ) - print(f"Successfully deleted auth claim with ID: {claim_id}") - - # Verify deletion by listing claims again - all_claims_after = auth_claims_api.get_authclaims( - api_auth_id=auth_server_id, - content_type=content_type - ) - print("Auth Claims after deletion:\n") - pprint(all_claims_after) - except Exception as e: - print(f"Exception when deleting auth claim: {e}") - - except Exception as e: - print(f"Exception when creating auth claim: {e}") - else: - print("No authorization servers found to test with.") + # auth_server = onelogin.AuthServer( + auth_server_response = auth_server_api.create_auth_server( + auth_server=auth_server + ) + + pprint("The response of APIAuthorizationServerApi->create_auth_server:\n") + pprint(auth_server_response) + except Exception as e: + print("Exception when calling APIAuthorizationServerApi->create_auth_server: %s\n" % e) + + try: + # List auth servers + auth_servers_response = auth_server_api.list_auth_servers() + auth_servers = auth_servers_response + print("The response of APIAuthorizationServerApi->list_auth_servers:\n") + pprint(auth_servers_response) + except Exception as e: + print("Exception when calling APIAuthorizationServerApi->list_auth_servers: %s\n" % e) + + try: + # Update Auth Server + auth_server = onelogin.AuthServer( + id=auth_servers[0].id, + name="Test Auth Server v3", + description="This is a dummy auth server v3", + ) + + auth_server_response = auth_server_api.update_auth_server(api_auth_id=str(auth_servers[0].id), auth_server=auth_server) + print("The response of APIAuthorizationServerApi->update_auth_server:\n") + pprint(auth_server_response) + except Exception as e: + print("Exception when calling APIAuthorizationServerApi->update_auth_server: %s\n" % e) + + try: + # Delete Auth Server + auth_server_response = auth_server_api.delete_auth_server(api_auth_id=str(auth_servers[-1].id)) + print("The response of APIAuthorizationServerApi->delete_auth_server:\n") + pprint(auth_server_response) + except Exception as e: + print("Exception when calling APIAuthorizationServerApi->delete_auth_server: %s\n" % e) + + + auth_claims = None + auth_claims_api = onelogin.APIAuthClaimsApi(api_client) + + try: + # Create a new claim + claim = onelogin.AuthClaim( + name="Dummy Claim v2", + user_attribute_mappings="firstname" + ) + + claim_response = auth_claims_api.create_auth_claim(api_auth_id=str(auth_servers[0].id), auth_claim=claim) + print("The response of APIAuthClaimsApi->create_auth_claim:\n") + pprint(claim_response) + except Exception as e: + print("Exception when calling APIAuthClaimsApi->create_auth_claim: %s\n" % e) + + try: + #List Auth Claim by ID + auth_claims_response = auth_claims_api.get_authclaims(api_auth_id=str(auth_servers[0].id)) + auth_claims = auth_claims_response + print("The response of APIAuthClaimsApi->get_authclaims:\n") + pprint(auth_claims_response) + except Exception as e: + print("Exception when calling APIAuthClaimsApi->get_authclaims: %s\n" % e) + + try: + # Update claim + auth_claims_response = auth_claims_api.update_claim(api_auth_id=str(auth_servers[0].id), claim_id=auth_claims[1].id, auth_claim=onelogin.AuthClaim(name="Test Claim v1")) + print("The response of APIAuthClaimsApi->update_claim:\n") + pprint(auth_claims_response) + except Exception as e: + print("Exception when calling APIAuthClaimsApi->update_claim: %s\n" % e) + + try: + # Delete Claim + auth_claims_response = auth_claims_api.delete_auth_claim(api_auth_id=str(auth_servers[0].id), claim_id=auth_claims[1].id) + print("The response of APIAuthClaimsApi->delete_claim:\n") + pprint(auth_claims_response) except Exception as e: - print(f"Exception when getting auth servers: {e}") \ No newline at end of file + print("Exception when calling APIAuthClaimsApi->delete_claim: %s\n" % e) \ No newline at end of file diff --git a/user_script.py b/user_script.py deleted file mode 100644 index 45cca52..0000000 --- a/user_script.py +++ /dev/null @@ -1,82 +0,0 @@ -import os -import onelogin -from onelogin.rest import ApiException -from pprint import pprint - -# Change host to your domain or it will default to https://your-api-subdomain.onelogin.com -configuration = onelogin.Configuration( - host = "https://your-api-subdomain.onelogin.com", - username = "", - password = "" -) - -# Enter a context with an instance of the API client -with onelogin.ApiClient(configuration) as api_client: - # Create an instance of the API class - token_instance = onelogin.OAuth2Api(api_client) - generate_token_request = {"grant_type":"client_credentials"} # GenerateTokenRequest | Request Body to Generate OAuth Token - content_type="application/json" - - try: - # Generate and Save Access Token - api_response = token_instance.generate_token(generate_token_request, content_type=content_type) - configuration.access_token = api_response.access_token - pprint(configuration.access_token) - except Exception as e: - print("Exception when generating access token: %s\n" % e) - - users = None - user_instance = onelogin.UsersV2Api(api_client) - - try: - # List All Users - api_response = user_instance.list_users2() - print("The response of UsersV2Api->list_users:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling UsersV2Api->list_users: %s\n" % e) - - try: - # Create User - dummy_user = { - "email": "dummy@quest.com", - } - api_response = user_instance.create_user2(user=dummy_user, _headers={"Authorization": "Bearer " + configuration.access_token}) - users = api_response - print("The response of UsersV2Api->create_user:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling UsersV2Api->create_user: %s\n" % e) - - try: - # Get User by ID - api_response = user_instance.get_user2(user_id=users.id) - pprint("The response of UsersV2Api->get_user:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling UsersV2Api->get_user: %s\n" % e) - - try: - # List User Apps - api_response = user_instance.get_user_apps2(user_id=users.id) - print("The response of UsersV2Api->list_user_apps:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling UsersV2Api->list_user_apps: %s\n" % e) - - try: - # Update User - user_details = {"firstname": 'dummy', "lastname": 'user'} - api_response = user_instance.update_user2(user_id=users.id, user=user_details, _headers={"Authorization": "Bearer " + configuration.access_token}) - print("The response of UsersV2Api->update_user:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling UsersV2Api->update_user: %s\n" % e) - - try: - # Delete User - api_response = user_instance.delete_user2(user_id=users.id) - print("The response of UsersV2Api->delete_user:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling UsersV2Api->delete_user: %s\n" % e) \ No newline at end of file