@@ -18,15 +18,9 @@ def generate_nexus_identity_keys(algorithm=None):
1818
1919 import os
2020 import subprocess
21- import asyncio
2221 import sys
23-
24- from azure .identity import AzureCliCredential
25- from msgraph import GraphServiceClient
26- from msgraph .generated .models .open_type_extension import OpenTypeExtension
27- from msgraph .generated .models .extension import Extension
28- from azure .core .exceptions import ClientAuthenticationError , HttpResponseError
29- from msgraph .generated .models .o_data_errors .o_data_error import ODataError
22+ import requests
23+ import json
3024
3125 # Generate SSH key
3226 if sys .platform .startswith ("win" ) or sys .platform .startswith ("linux" ):
@@ -84,66 +78,86 @@ def generate_nexus_identity_keys(algorithm=None):
8478 raise CLIError (f"Unexpected error reading public key: { e } " ) from e
8579
8680 try :
87- credential = AzureCliCredential ()
88- scopes = ["https://graph.microsoft.com//.default" ]
89- graph_client = GraphServiceClient (credentials = credential , scopes = scopes )
90-
91- except ClientAuthenticationError as e :
92- logger .error ("Authentication failed: %s" , e )
93- raise CLIError (f"Authentication failed: { e } " ) from e
81+ # Get access token using Azure CLI
82+ if sys .platform .startswith ("win" ):
83+ az_cmd = "az account get-access-token --resource https://graph.microsoft.com --output json"
84+ token_result = subprocess .run (
85+ az_cmd ,
86+ capture_output = True ,
87+ text = True ,
88+ check = True ,
89+ shell = True ,
90+ )
91+ else :
92+ token_result = subprocess .run (
93+ ["az" , "account" , "get-access-token" , "--resource" , "https://graph.microsoft.com" , "--output" , "json" ],
94+ capture_output = True ,
95+ text = True ,
96+ check = True ,
97+ )
98+ token_json = json .loads (token_result .stdout )
99+ access_token = token_json ["accessToken" ]
94100 except Exception as e :
95- logger .error ("An unexpected error occurred: %s" , e )
96- raise CLIError (f"An unexpected error occurred: { e } " ) from e
97-
98- async def me ():
99- extension_id = "com.nexusidentity.keys"
100-
101- # Get user object
102- user = await graph_client .me .get ()
101+ print ("Exception to fetch bearer token:" , e )
102+ logger .error ("Failed to obtain access token: %s" , e )
103+ raise CLIError (f"Failed to obtain access token: { e } " ) from e
103104
104- # Get extensions associated with the user
105- extensions = await graph_client .me .extensions .get ()
105+ headers = {
106+ "Authorization" : f"Bearer { access_token } " ,
107+ "Content-Type" : "application/json"
108+ }
106109
107- extension_exists = any (
108- extension .id == extension_id for extension in extensions .value
109- )
110+ extension_id = "com.nexusidentity.keys"
111+ graph_base = "https://graph.microsoft.com/v1.0"
110112
111- try :
112- # Update or create extension
113- if extension_exists :
114- request_body = Extension (
115- odata_type = "microsoft.graph.openTypeExtension" ,
116- additional_data = {
117- "extension_name" : extension_id ,
118- "publicKey" : public_key ,
119- },
120- )
121- await graph_client .me .extensions .by_extension_id (
122- extension_id
123- ).patch (request_body )
124-
125- print (
126- f"Successfully updated public key to Microsoft Entra Id account { user .mail } "
127- )
128- else :
129- request_body = OpenTypeExtension (
130- odata_type = "microsoft.graph.openTypeExtension" ,
131- extension_name = extension_id ,
132- additional_data = {"publicKey" : public_key },
133- )
134- await graph_client .me .extensions .post (request_body )
135-
136- print (
137- f"Successfully uploaded public key to Microsoft Entra Id account { user .mail } "
138- )
139- except ODataError as e :
140- logger .error ("Error updating extension: %s" , e )
141- raise CLIError (f"Error updating extension: { e } " ) from e
142- except HttpResponseError as e :
143- logger .error ("Failed to update or create extension: %s" , e )
144- raise CLIError (f"Failed to update or create extension: { e } " ) from e
145-
146- asyncio .run (me ())
113+ try :
114+ # Get user info
115+ user_resp = requests .get (f"{ graph_base } /me" , headers = headers )
116+ user_resp .raise_for_status ()
117+ user = user_resp .json ()
118+ user_mail = user .get ("mail" ) or user .get ("userPrincipalName" )
119+
120+ # Get extensions
121+ ext_resp = requests .get (f"{ graph_base } /me/extensions" , headers = headers )
122+ ext_resp .raise_for_status ()
123+ extensions = ext_resp .json ().get ("value" , [])
124+ extension_exists = any (ext .get ("id" ) == extension_id for ext in extensions )
125+
126+ if extension_exists :
127+ # Update extension
128+ patch_body = {
129+ "@odata.type" : "microsoft.graph.openTypeExtension" ,
130+ "extensionName" : extension_id ,
131+ "publicKey" : public_key
132+ }
133+ patch_resp = requests .patch (
134+ f"{ graph_base } /me/extensions/{ extension_id } " ,
135+ headers = headers ,
136+ data = json .dumps (patch_body )
137+ )
138+ patch_resp .raise_for_status ()
139+ print (f"Successfully updated public key to Microsoft Entra Id account { user_mail } " )
140+ else :
141+ # Create extension
142+ post_body = {
143+ "@odata.type" : "microsoft.graph.openTypeExtension" ,
144+ "extensionName" : extension_id ,
145+ "publicKey" : public_key
146+ }
147+ post_resp = requests .post (
148+ f"{ graph_base } /me/extensions" ,
149+ headers = headers ,
150+ data = json .dumps (post_body )
151+ )
152+ post_resp .raise_for_status ()
153+ print (f"Successfully uploaded public key to Microsoft Entra Id account { user_mail } " )
154+
155+ except requests .HTTPError as e :
156+ logger .error ("HTTP error: %s" , e )
157+ raise CLIError (f"HTTP error: { e } " ) from e
158+ except Exception as e :
159+ logger .error ("Unexpected error: %s" , e )
160+ raise CLIError (f"Unexpected error: { e } " ) from e
147161 else :
148162 logger .warning (
149163 "This command is currently supported only on Windows and linux platforms"
0 commit comments