@@ -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,97 @@ 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+ token_result = subprocess .run (
84+ "az account get-access-token --resource https://graph.microsoft.com --output json" ,
85+ capture_output = True ,
86+ text = True ,
87+ check = True ,
88+ shell = True ,
89+ )
90+ else :
91+ token_result = subprocess .run (
92+ [
93+ "az" ,
94+ "account" ,
95+ "get-access-token" ,
96+ "--resource" ,
97+ "https://graph.microsoft.com" ,
98+ "--output" ,
99+ "json" ,
100+ ],
101+ capture_output = True ,
102+ text = True ,
103+ check = True ,
104+ )
105+ access_token = json .loads (token_result .stdout )["accessToken" ]
94106 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 ()
107+ print ("Exception to fetch bearer token:" , e )
108+ logger .error ("Failed to obtain access token: %s" , e )
109+ raise CLIError (f"Failed to obtain access token: { e } " ) from e
103110
104- # Get extensions associated with the user
105- extensions = await graph_client .me .extensions .get ()
111+ headers = {
112+ "Authorization" : f"Bearer { access_token } " ,
113+ "Content-Type" : "application/json" ,
114+ }
106115
107- extension_exists = any (
108- extension .id == extension_id for extension in extensions .value
109- )
116+ extension_id = "com.nexusidentity.keys"
117+ graph_base = "https://graph.microsoft.com/v1.0"
110118
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 ())
119+ try :
120+ # Get user info
121+ user = requests .get (f"{ graph_base } /me" , headers = headers )
122+ user .raise_for_status ()
123+ user = user .json ()
124+
125+ # Get extensions
126+ ext_resp = requests .get (f"{ graph_base } /me/extensions" , headers = headers )
127+ ext_resp .raise_for_status ()
128+ ext_resp = ext_resp .json ().get ("value" , [])
129+ extension_exists = any (ext .get ("id" ) == extension_id for ext in ext_resp )
130+
131+ if extension_exists :
132+ # Update extension
133+ patch_body = {
134+ "@odata.type" : "microsoft.graph.openTypeExtension" ,
135+ "extensionName" : extension_id ,
136+ "publicKey" : public_key ,
137+ }
138+ patch_resp = requests .patch (
139+ f"{ graph_base } /me/extensions/{ extension_id } " ,
140+ headers = headers ,
141+ data = json .dumps (patch_body ),
142+ )
143+ patch_resp .raise_for_status ()
144+ print (
145+ f"Successfully updated public key to Microsoft Entra Id account "
146+ f"{ user .get ('mail' ) or user .get ('userPrincipalName' )} "
147+ )
148+ else :
149+ # Create extension
150+ post_body = {
151+ "@odata.type" : "microsoft.graph.openTypeExtension" ,
152+ "extensionName" : extension_id ,
153+ "publicKey" : public_key ,
154+ }
155+ post_resp = requests .post (
156+ f"{ graph_base } /me/extensions" ,
157+ headers = headers ,
158+ data = json .dumps (post_body ),
159+ )
160+ post_resp .raise_for_status ()
161+ print (
162+ f"Successfully uploaded public key to Microsoft Entra Id account "
163+ f"{ user .get ('mail' ) or user .get ('userPrincipalName' )} "
164+ )
165+
166+ except requests .HTTPError as e :
167+ logger .error ("HTTP error: %s" , e )
168+ raise CLIError (f"HTTP error: { e } " ) from e
169+ except Exception as e :
170+ logger .error ("Unexpected error: %s" , e )
171+ raise CLIError (f"Unexpected error: { e } " ) from e
147172 else :
148173 logger .warning (
149174 "This command is currently supported only on Windows and linux platforms"
0 commit comments