3
3
import os
4
4
import random
5
5
import subprocess
6
+ import uuid
6
7
from typing import Tuple
7
8
8
9
from azure .identity .aio import AzureDeveloperCliCredential
27
28
28
29
async def create_application (graph_client : GraphServiceClient , request_app : Application ) -> Tuple [str , str ]:
29
30
app = await graph_client .applications .post (request_app )
31
+ if app is None :
32
+ raise ValueError ("Failed to create application" )
30
33
object_id = app .id
31
34
client_id = app .app_id
35
+ if object_id is None or client_id is None :
36
+ raise ValueError ("Created application has no ID or client ID" )
32
37
33
38
# Create a service principal
34
39
request_principal = ServicePrincipal (app_id = client_id , display_name = app .display_name )
@@ -40,8 +45,12 @@ async def add_client_secret(graph_client: GraphServiceClient, app_id: str) -> st
40
45
request_password = AddPasswordPostRequestBody (
41
46
password_credential = PasswordCredential (display_name = "WebAppSecret" ),
42
47
)
43
- result = await graph_client .applications .by_application_id (app_id ).add_password .post (request_password )
44
- return result .secret_text
48
+ password_credential = await graph_client .applications .by_application_id (app_id ).add_password .post (request_password )
49
+ if password_credential is None :
50
+ raise ValueError ("Failed to create client secret" )
51
+ if password_credential .secret_text is None :
52
+ raise ValueError ("Created client secret has no secret text" )
53
+ return password_credential .secret_text
45
54
46
55
47
56
async def create_or_update_application_with_secret (
@@ -94,7 +103,7 @@ def server_app_permission_setup(server_app_id: str) -> Application:
94
103
known_client_applications = [],
95
104
oauth2_permission_scopes = [
96
105
PermissionScope (
97
- id = " 7b207263-0c4a-4127-a6fe-38ea8c8cd1a7" ,
106
+ id = uuid . UUID ( "{ 7b207263-0c4a-4127-a6fe-38ea8c8cd1a7}" ) ,
98
107
admin_consent_display_name = "Access Azure Search OpenAI Chat API" ,
99
108
admin_consent_description = "Allows the app to access Azure Search OpenAI Chat API as the signed-in user." ,
100
109
user_consent_display_name = "Access Azure Search OpenAI Chat API" ,
@@ -111,15 +120,15 @@ def server_app_permission_setup(server_app_id: str) -> Application:
111
120
resource_app_id = "00000003-0000-0000-c000-000000000000" ,
112
121
resource_access = [
113
122
# Graph User.Read
114
- ResourceAccess (id = " e1fe6dd8-ba31-4d61-89e7-88639da4683d" , type = "Scope" ),
123
+ ResourceAccess (id = uuid . UUID ( "{ e1fe6dd8-ba31-4d61-89e7-88639da4683d}" ) , type = "Scope" ),
115
124
# Graph email
116
- ResourceAccess (id = " 64a6cdd6-aab1-4aaf-94b8-3cc8405e90d0" , type = "Scope" ),
125
+ ResourceAccess (id = uuid . UUID ( "{ 64a6cdd6-aab1-4aaf-94b8-3cc8405e90d0}" ) , type = "Scope" ),
117
126
# Graph offline_access
118
- ResourceAccess (id = " 7427e0e9-2fba-42fe-b0c0-848c9e6a8182" , type = "Scope" ),
127
+ ResourceAccess (id = uuid . UUID ( "{ 7427e0e9-2fba-42fe-b0c0-848c9e6a8182}" ) , type = "Scope" ),
119
128
# Graph openid
120
- ResourceAccess (id = " 37f7f235-527c-4136-accd-4a02d197296e" , type = "Scope" ),
129
+ ResourceAccess (id = uuid . UUID ( "{ 37f7f235-527c-4136-accd-4a02d197296e}" ) , type = "Scope" ),
121
130
# Graph profile
122
- ResourceAccess (id = " 14dad69e-099b-42c9-810b-d002981feec1" , type = "Scope" ),
131
+ ResourceAccess (id = uuid . UUID ( "{ 14dad69e-099b-42c9-810b-d002981feec1}" ) , type = "Scope" ),
123
132
],
124
133
)
125
134
],
@@ -128,6 +137,10 @@ def server_app_permission_setup(server_app_id: str) -> Application:
128
137
129
138
130
139
def client_app (server_app_id : str , server_app : Application , identifier : int ) -> Application :
140
+ if server_app .api is None :
141
+ raise ValueError ("Server app does not have an API" )
142
+ if server_app .api .oauth2_permission_scopes is None or len (server_app .api .oauth2_permission_scopes ) == 0 :
143
+ raise ValueError ("Server app does not have any permission scopes" )
131
144
return Application (
132
145
display_name = f"Azure Search OpenAI Chat Client App { identifier } " ,
133
146
sign_in_audience = "AzureADMyOrg" ,
@@ -150,7 +163,7 @@ def client_app(server_app_id: str, server_app: Application, identifier: int) ->
150
163
RequiredResourceAccess (
151
164
resource_app_id = "00000003-0000-0000-c000-000000000000" ,
152
165
resource_access = [
153
- ResourceAccess (id = "e1fe6dd8-ba31-4d61-89e7-88639da4683d" , type = "Scope" ),
166
+ ResourceAccess (id = uuid . UUID ( "e1fe6dd8-ba31-4d61-89e7-88639da4683d" ) , type = "Scope" ),
154
167
],
155
168
),
156
169
],
@@ -160,7 +173,7 @@ def client_app(server_app_id: str, server_app: Application, identifier: int) ->
160
173
def server_app_known_client_application (client_app_id : str ) -> Application :
161
174
return Application (
162
175
api = ApiApplication (
163
- known_client_applications = [client_app_id ],
176
+ known_client_applications = [uuid . UUID ( f" { client_app_id } " ) ],
164
177
)
165
178
)
166
179
0 commit comments