-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
custom.py
1034 lines (876 loc) · 59.1 KB
/
custom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import json
from re import A
import re
from knack.prompting import prompt_y_n
from knack.util import CLIError
from azure.cli.core.util import send_raw_request
from azure.cli.command_modules.appservice._appservice_utils import _generic_site_operation
from azure.cli.command_modules.appservice.custom import update_app_settings
from azure.cli.core.azclierror import ArgumentUsageError
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.command_modules.appservice._params import AUTH_TYPES
from azure.cli.core.cloud import AZURE_PUBLIC_CLOUD, AZURE_CHINA_CLOUD, AZURE_US_GOV_CLOUD, AZURE_GERMAN_CLOUD
MICROSOFT_SECRET_SETTING_NAME = "MICROSOFT_PROVIDER_AUTHENTICATION_SECRET"
FACEBOOK_SECRET_SETTING_NAME = "FACEBOOK_PROVIDER_AUTHENTICATION_SECRET"
GITHUB_SECRET_SETTING_NAME = "GITHUB_PROVIDER_AUTHENTICATION_SECRET"
GOOGLE_SECRET_SETTING_NAME = "GOOGLE_PROVIDER_AUTHENTICATION_SECRET"
MSA_SECRET_SETTING_NAME = "MSA_PROVIDER_AUTHENTICATION_SECRET"
TWITTER_SECRET_SETTING_NAME = "TWITTER_PROVIDER_AUTHENTICATION_SECRET"
TRUE_STRING = "true"
FALSE_STRING = "false"
# region rest calls
def get_resource_id(cmd, resource_group_name, name, slot):
sub_id = get_subscription_id(cmd.cli_ctx)
# TODO: Replace ARM call with SDK API after fixing swagger issues
resource_id = "/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Web/sites/{}".format(
sub_id,
resource_group_name,
name)
if slot is not None:
resource_id = resource_id + "/slots/" + slot
return resource_id
def get_auth_settings_v2(cmd, resource_group_name, name, slot=None):
resource_id = get_resource_id(cmd, resource_group_name, name, slot)
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
request_url = "{}/{}/{}?api-version={}".format(
management_hostname.strip('/'),
resource_id,
"config/authSettingsV2/list",
"2020-12-01")
# TODO: Replace ARM call with SDK API after fixing swagger issues
r = send_raw_request(cmd.cli_ctx, "GET", request_url)
return r.json()
def update_auth_settings_v2_rest_call(cmd, resource_group_name, name, site_auth_settings_v2,
slot=None, overwrite_settings=False, is_upgrade=False): # pylint: disable=unused-argument
is_using_v1 = get_config_version(cmd, resource_group_name, name, slot)["configVersion"] == 'v1'
is_new_auth_app = is_app_new_to_auth(cmd, resource_group_name, name, slot)
if not is_upgrade and is_using_v1 and not is_new_auth_app:
msg = 'Usage Error: Cannot use auth v2 commands when the app is using auth v1. ' \
'Update the auth settings using the az webapp auth-classic command group.'
raise CLIError(msg)
if not overwrite_settings: # if no auth v2 settings set, then default token store to true
if is_new_auth_app:
if "login" not in site_auth_settings_v2.keys():
site_auth_settings_v2["login"] = {}
if "tokenStore" not in site_auth_settings_v2["login"].keys():
site_auth_settings_v2["login"]["tokenStore"] = {}
site_auth_settings_v2["login"]["tokenStore"]["enabled"] = True
final_json = {
"properties": site_auth_settings_v2
}
resource_id = get_resource_id(cmd, resource_group_name, name, slot)
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
request_url = "{}/{}/{}?api-version={}".format(
management_hostname.strip('/'),
resource_id,
"config/authSettingsV2",
"2020-12-01")
# TODO: Replace ARM call with SDK API after fixing swagger issues
r = send_raw_request(cmd.cli_ctx, "PUT", request_url, None, None, json.dumps(final_json))
return r.json()["properties"]
def is_auth_v2_app(cmd, resource_group_name, name, slot=None):
resource_id = get_resource_id(cmd, resource_group_name, name, slot)
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
request_url = "{}/{}/{}?api-version={}".format(
management_hostname.strip('/'),
resource_id,
"config/authSettings/list",
"2020-12-01")
# TODO: Replace ARM call with SDK API after fixing swagger issues
r = send_raw_request(cmd.cli_ctx, "POST", request_url)
return r.json()["properties"]["configVersion"] == "v2"
# endregion
# region webapp auth
def set_auth_settings_v2(cmd, resource_group_name, name, body=None, slot=None): # pylint: disable=unused-argument
if body is None:
json_object = None
else:
json_object = json.loads(body)
return update_auth_settings_v2_rest_call(cmd, resource_group_name, name, json_object,
slot, overwrite_settings=True)
def update_auth_settings_v2(cmd, resource_group_name, name, set_string=None, enabled=None, # pylint: disable=unused-argument
runtime_version=None, config_file_path=None, unauthenticated_client_action=None, # pylint: disable=unused-argument
redirect_provider=None, enable_token_store=None, require_https=None, # pylint: disable=unused-argument
proxy_convention=None, proxy_custom_host_header=None, # pylint: disable=unused-argument
proxy_custom_proto_header=None, excluded_paths=None, slot=None): # pylint: disable=unused-argument
existing_auth = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
existing_auth = set_field_in_auth_settings(existing_auth, set_string)
if enabled is not None:
if "platform" not in existing_auth.keys():
existing_auth["platform"] = {}
existing_auth["platform"]["enabled"] = enabled
if runtime_version is not None:
if "platform" not in existing_auth.keys():
existing_auth["platform"] = {}
existing_auth["platform"]["runtimeVersion"] = runtime_version
if config_file_path is not None:
if "platform" not in existing_auth.keys():
existing_auth["platform"] = {}
existing_auth["platform"]["configFilePath"] = config_file_path
if unauthenticated_client_action is not None:
if "globalValidation" not in existing_auth.keys():
existing_auth["globalValidation"] = {}
existing_auth["globalValidation"]["unauthenticatedClientAction"] = unauthenticated_client_action
if redirect_provider is not None:
if "globalValidation" not in existing_auth.keys():
existing_auth["globalValidation"] = {}
existing_auth["globalValidation"]["redirectToProvider"] = redirect_provider
if enable_token_store is not None:
if "login" not in existing_auth.keys():
existing_auth["login"] = {}
if "tokenStore" not in existing_auth["login"].keys():
existing_auth["login"]["tokenStore"] = {}
existing_auth["login"]["tokenStore"]["enabled"] = enable_token_store
if excluded_paths is not None:
if "globalValidation" not in existing_auth.keys():
existing_auth["globalValidation"] = {}
excluded_paths_list_string = excluded_paths[1:-1]
existing_auth["globalValidation"]["excludedPaths"] = excluded_paths_list_string.split(",")
existing_auth = update_http_settings_in_auth_settings(existing_auth, require_https,
proxy_convention, proxy_custom_host_header,
proxy_custom_proto_header)
json_object = existing_auth
return update_auth_settings_v2_rest_call(cmd, resource_group_name, name, json_object, slot)
# endregion
# region webapp auth config-version
def upgrade_to_auth_settings_v2(cmd, resource_group_name, name, slot=None): # pylint: disable=unused-argument
if is_auth_v2_app(cmd, resource_group_name, name, slot):
raise CLIError('Usage Error: Cannot use command az webapp auth upgrade when the app is using auth v2.')
prep_auth_settings_for_v2(cmd, resource_group_name, name, slot)
site_auth_settings_v2 = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
return update_auth_settings_v2_rest_call(cmd, resource_group_name, name,
site_auth_settings_v2, slot, is_upgrade=True)
def get_config_version(cmd, resource_group_name, name, slot=None): # pylint: disable=unused-argument
isV2 = is_auth_v2_app(cmd, resource_group_name, name, slot)
config_version = "v1"
if isV2:
config_version = "v2"
return {
"configVersion": config_version
}
def revert_to_auth_settings(cmd, resource_group_name, name, slot=None): # pylint: disable=unused-argument
if not is_auth_v2_app(cmd, resource_group_name, name, slot):
raise CLIError('Usage Error: Cannot use command az webapp auth revert when the app is using auth v1.')
site_auth_settings = get_auth_settings(cmd, resource_group_name, name, slot)
set_auth_settings_v2(cmd, resource_group_name, name, None, slot)
site_auth_settings.enabled = TRUE_STRING if site_auth_settings.enabled else FALSE_STRING
site_auth_settings.token_store_enabled = TRUE_STRING if site_auth_settings.token_store_enabled else FALSE_STRING
action = None
if site_auth_settings.unauthenticated_client_action == "AllowAnonymous":
action = "AllowAnonymous"
elif site_auth_settings.unauthenticated_client_action == "RedirectToLoginPage":
if site_auth_settings.default_provider == "AzureActiveDirectory":
action = "LoginWithAzureActiveDirectory"
elif site_auth_settings.default_provider == "Facebook":
action = "LoginWithFacebook"
elif site_auth_settings.default_provider == "Google":
action = "LoginWithGoogle"
elif site_auth_settings.default_provider == "MicrosoftAccount":
action = "LoginWithMicrosoftAccount"
elif site_auth_settings.default_provider == "Twitter":
action = "LoginWithTwitter"
update_auth_classic_settings(cmd, resource_group_name, name, site_auth_settings.enabled, action,
site_auth_settings.client_id, site_auth_settings.token_store_enabled,
site_auth_settings.runtime_version,
site_auth_settings.token_refresh_extension_hours,
site_auth_settings.allowed_external_redirect_urls, site_auth_settings.client_secret,
site_auth_settings.client_secret_certificate_thumbprint,
site_auth_settings.allowed_audiences, site_auth_settings.issuer,
site_auth_settings.facebook_app_id,
site_auth_settings.facebook_app_secret, site_auth_settings.facebook_o_auth_scopes,
site_auth_settings.twitter_consumer_key, site_auth_settings.twitter_consumer_secret,
site_auth_settings.google_client_id, site_auth_settings.google_client_secret,
site_auth_settings.google_o_auth_scopes,
site_auth_settings.microsoft_account_client_id,
site_auth_settings.microsoft_account_client_secret,
site_auth_settings.microsoft_account_o_auth_scopes, slot,
site_auth_settings.git_hub_client_id, site_auth_settings.git_hub_client_secret,
site_auth_settings.git_hub_o_auth_scopes,
site_auth_settings.client_secret_setting_name,
site_auth_settings.facebook_app_secret_setting_name,
site_auth_settings.google_client_secret_setting_name,
site_auth_settings.microsoft_account_client_secret_setting_name,
site_auth_settings.twitter_consumer_secret_setting_name,
site_auth_settings.git_hub_client_secret_setting_name)
# endregion
# region helper methods
def is_app_new_to_auth(cmd, resource_group_name, name, slot):
existing_site_auth_settings_v2 = get_auth_settings_v2(cmd, resource_group_name, name, slot)
return json.dumps(existing_site_auth_settings_v2["properties"]) == "{}"
def set_field_in_auth_settings_recursive(field_name_split, field_value, auth_settings):
if len(field_name_split) == 1:
if not field_value.startswith('[') or not field_value.endswith(']'):
auth_settings[field_name_split[0]] = field_value
else:
field_value_list_string = field_value[1:-1]
auth_settings[field_name_split[0]] = field_value_list_string.split(",")
return auth_settings
remaining_field_names = field_name_split[1:]
if field_name_split[0] not in auth_settings.keys():
auth_settings[field_name_split[0]] = {}
auth_settings[field_name_split[0]] = set_field_in_auth_settings_recursive(remaining_field_names,
field_value,
auth_settings[field_name_split[0]])
return auth_settings
def set_field_in_auth_settings(auth_settings, set_string):
if set_string is not None:
split1 = set_string.split("=")
fieldName = split1[0]
fieldValue = split1[1]
split2 = fieldName.split(".")
auth_settings = set_field_in_auth_settings_recursive(split2, fieldValue, auth_settings)
return auth_settings
def update_http_settings_in_auth_settings(auth_settings, require_https, proxy_convention,
proxy_custom_host_header, proxy_custom_proto_header):
if require_https is not None:
if "httpSettings" not in auth_settings.keys():
auth_settings["httpSettings"] = {}
auth_settings["httpSettings"]["requireHttps"] = require_https
if proxy_convention is not None:
if "httpSettings" not in auth_settings.keys():
auth_settings["httpSettings"] = {}
if "forwardProxy" not in auth_settings["httpSettings"].keys():
auth_settings["httpSettings"]["forwardProxy"] = {}
auth_settings["httpSettings"]["forwardProxy"]["convention"] = proxy_convention
if proxy_custom_host_header is not None:
if "httpSettings" not in auth_settings.keys():
auth_settings["httpSettings"] = {}
if "forwardProxy" not in auth_settings["httpSettings"].keys():
auth_settings["httpSettings"]["forwardProxy"] = {}
auth_settings["httpSettings"]["forwardProxy"]["customHostHeaderName"] = proxy_custom_host_header
if proxy_custom_proto_header is not None:
if "httpSettings" not in auth_settings.keys():
auth_settings["httpSettings"] = {}
if "forwardProxy" not in auth_settings["httpSettings"].keys():
auth_settings["httpSettings"]["forwardProxy"] = {}
auth_settings["httpSettings"]["forwardProxy"]["customProtoHeaderName"] = proxy_custom_proto_header
return auth_settings
def is_auth_runtime_version_valid(runtime_version=None):
if runtime_version is None:
return True
if runtime_version.startswith("~") and len(runtime_version) > 1:
try:
int(runtime_version[1:])
except ValueError:
return False
return True
split_versions = runtime_version.split('.')
if len(split_versions) != 3:
return False
for version in split_versions:
try:
int(version)
except ValueError:
return False
return True
def prep_auth_settings_for_v2(cmd, resource_group_name, name, slot=None): # pylint: disable=unused-argument
site_auth_settings = get_auth_settings(cmd, resource_group_name, name, slot)
settings = []
if site_auth_settings.client_secret is not None:
settings.append(MICROSOFT_SECRET_SETTING_NAME + '=' + site_auth_settings.client_secret)
site_auth_settings.client_secret_setting_name = MICROSOFT_SECRET_SETTING_NAME
if site_auth_settings.facebook_app_secret is not None:
settings.append(FACEBOOK_SECRET_SETTING_NAME + '=' + site_auth_settings.facebook_app_secret)
site_auth_settings.facebook_app_secret_setting_name = FACEBOOK_SECRET_SETTING_NAME
if site_auth_settings.git_hub_client_secret is not None:
settings.append(GITHUB_SECRET_SETTING_NAME + '=' + site_auth_settings.git_hub_client_secret)
site_auth_settings.git_hub_client_secret_setting_name = GITHUB_SECRET_SETTING_NAME
if site_auth_settings.google_client_secret is not None:
settings.append(GOOGLE_SECRET_SETTING_NAME + '=' + site_auth_settings.google_client_secret)
site_auth_settings.google_client_secret_setting_name = GOOGLE_SECRET_SETTING_NAME
if site_auth_settings.microsoft_account_client_secret is not None:
settings.append(MSA_SECRET_SETTING_NAME + '=' + site_auth_settings.microsoft_account_client_secret)
site_auth_settings.microsoft_account_client_secret_setting_name = MSA_SECRET_SETTING_NAME
if site_auth_settings.twitter_consumer_secret is not None:
settings.append(TWITTER_SECRET_SETTING_NAME + '=' + site_auth_settings.twitter_consumer_secret)
site_auth_settings.twitter_consumer_secret_setting_name = TWITTER_SECRET_SETTING_NAME
if len(settings) > 0:
update_app_settings(cmd, resource_group_name, name, slot=slot, slot_settings=settings)
remove_all_auth_settings_secrets(cmd, resource_group_name, name, slot)
update_auth_classic_settings(cmd, resource_group_name, name, site_auth_settings.enabled, None,
site_auth_settings.client_id, site_auth_settings.token_store_enabled,
site_auth_settings.runtime_version, site_auth_settings.token_refresh_extension_hours,
site_auth_settings.allowed_external_redirect_urls, None,
site_auth_settings.client_secret_certificate_thumbprint,
site_auth_settings.allowed_audiences, site_auth_settings.issuer,
site_auth_settings.facebook_app_id, None,
site_auth_settings.facebook_o_auth_scopes,
site_auth_settings.twitter_consumer_key, None,
site_auth_settings.google_client_id, None,
site_auth_settings.google_o_auth_scopes,
site_auth_settings.microsoft_account_client_id,
None,
site_auth_settings.microsoft_account_o_auth_scopes, slot,
site_auth_settings.git_hub_client_id, None,
site_auth_settings.git_hub_o_auth_scopes,
site_auth_settings.client_secret_setting_name,
site_auth_settings.facebook_app_secret_setting_name,
site_auth_settings.google_client_secret_setting_name,
site_auth_settings.microsoft_account_client_secret_setting_name,
site_auth_settings.twitter_consumer_secret_setting_name,
site_auth_settings.git_hub_client_secret_setting_name)
def remove_all_auth_settings_secrets(cmd, resource_group_name, name, slot=None): # pylint: disable=unused-argument
auth_settings = get_auth_settings(cmd, resource_group_name, name, slot)
auth_settings.client_secret = ""
auth_settings.facebook_app_secret = ""
auth_settings.git_hub_client_secret = ""
auth_settings.google_client_secret = ""
auth_settings.microsoft_account_client_secret = ""
auth_settings.twitter_consumer_secret_setting_name = ""
return _generic_site_operation(cmd.cli_ctx, resource_group_name, name,
'update_auth_settings', slot, auth_settings)
def get_oidc_client_setting_app_setting_name(provider_name):
provider_name_prefix = provider_name.upper()
# an appsetting name can be up to 64 characters, and the suffix _PROVIDER_AUTHENTICATION_SECRET is 31 characters so limitting this to 32
if len(provider_name_prefix) > 32:
provider_name_prefix = provider_name_prefix[0:31]
return provider_name_prefix + "_PROVIDER_AUTHENTICATION_SECRET"
# endregion
# region webapp auth-classic
def get_auth_settings(cmd, resource_group_name, name, slot=None):
return _generic_site_operation(cmd.cli_ctx, resource_group_name, name, 'get_auth_settings', slot)
def update_auth_classic_settings(cmd, resource_group_name, name, enabled=None, action=None, # pylint: disable=unused-argument
client_id=None, token_store_enabled=None, runtime_version=None, # pylint: disable=unused-argument
token_refresh_extension_hours=None, # pylint: disable=unused-argument
allowed_external_redirect_urls=None, client_secret=None, # pylint: disable=unused-argument
client_secret_certificate_thumbprint=None, # pylint: disable=unused-argument
allowed_audiences=None, issuer=None, facebook_app_id=None, # pylint: disable=unused-argument
facebook_app_secret=None, facebook_oauth_scopes=None, # pylint: disable=unused-argument
twitter_consumer_key=None, twitter_consumer_secret=None, # pylint: disable=unused-argument
google_client_id=None, google_client_secret=None, # pylint: disable=unused-argument
google_oauth_scopes=None, microsoft_account_client_id=None, # pylint: disable=unused-argument
microsoft_account_client_secret=None, # pylint: disable=unused-argument
microsoft_account_oauth_scopes=None, slot=None, # pylint: disable=unused-argument
git_hub_client_id=None, git_hub_client_secret=None, # pylint: disable=unused-argument
git_hub_o_auth_scopes=None, # pylint: disable=unused-argument
client_secret_setting_name=None, facebook_app_secret_setting_name=None, # pylint: disable=unused-argument
google_client_secret_setting_name=None, # pylint: disable=unused-argument
microsoft_account_client_secret_setting_name=None, # pylint: disable=unused-argument
twitter_consumer_secret_setting_name=None, git_hub_client_secret_setting_name=None): # pylint: disable=unused-argument
if is_auth_v2_app(cmd, resource_group_name, name, slot):
raise CLIError('Usage Error: Cannot use command az webapp auth-classic update when the app '
'is using auth v2. If you wish to revert the app to v1, run az webapp auth revert')
auth_settings = get_auth_settings(cmd, resource_group_name, name, slot)
if action == 'AllowAnonymous':
auth_settings.unauthenticated_client_action = 'AllowAnonymous'
elif action:
auth_settings.unauthenticated_client_action = 'RedirectToLoginPage'
auth_settings.default_provider = AUTH_TYPES[action]
# validate runtime version
if not is_auth_runtime_version_valid(runtime_version):
raise CLIError('Usage Error: --runtime-version set to invalid value')
import inspect
frame = inspect.currentframe()
bool_flags = ['enabled', 'token_store_enabled']
# note: getargvalues is used already in azure.cli.core.commands.
# and no simple functional replacement for this deprecating method for 3.5
args, _, _, values = inspect.getargvalues(frame) # pylint: disable=deprecated-method
for arg in args[2:]:
if values.get(arg, None):
setattr(auth_settings, arg, values[arg] if arg not in bool_flags else values[arg] == 'true')
_generic_site_operation(cmd.cli_ctx, resource_group_name, name, 'update_auth_settings', slot, auth_settings)
# endregion
# region webapp auth microsoft
def get_aad_settings(cmd, resource_group_name, name, slot=None):
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
return {}
if "azureActiveDirectory" not in auth_settings["identityProviders"].keys():
return {}
return auth_settings["identityProviders"]["azureActiveDirectory"]
def update_aad_settings(cmd, resource_group_name, name, slot=None, # pylint: disable=unused-argument
client_id=None, client_secret_setting_name=None, # pylint: disable=unused-argument
issuer=None, allowed_token_audiences=None, client_secret=None, # pylint: disable=unused-argument
client_secret_certificate_thumbprint=None, # pylint: disable=unused-argument
client_secret_certificate_san=None, # pylint: disable=unused-argument
client_secret_certificate_issuer=None, # pylint: disable=unused-argument
yes=False, tenant_id=None): # pylint: disable=unused-argument
if client_secret is not None and client_secret_setting_name is not None:
raise ArgumentUsageError('Usage Error: --client-secret and --client-secret-setting-name cannot both be '
'configured to non empty strings')
if client_secret_setting_name is not None and client_secret_certificate_thumbprint is not None:
raise ArgumentUsageError('Usage Error: --client-secret-setting-name and --thumbprint cannot both be '
'configured to non empty strings')
if client_secret is not None and client_secret_certificate_thumbprint is not None:
raise ArgumentUsageError('Usage Error: --client-secret and --thumbprint cannot both be '
'configured to non empty strings')
if client_secret is not None and client_secret_certificate_san is not None:
raise ArgumentUsageError('Usage Error: --client-secret and --san cannot both be '
'configured to non empty strings')
if client_secret_setting_name is not None and client_secret_certificate_san is not None:
raise ArgumentUsageError('Usage Error: --client-secret-setting-name and --san cannot both be '
'configured to non empty strings')
if client_secret_certificate_thumbprint is not None and client_secret_certificate_san is not None:
raise ArgumentUsageError('Usage Error: --thumbprint and --san cannot both be '
'configured to non empty strings')
if ((client_secret_certificate_san is not None and client_secret_certificate_issuer is None) or
(client_secret_certificate_san is None and client_secret_certificate_issuer is not None)):
raise ArgumentUsageError('Usage Error: --san and --certificate-issuer must both be '
'configured to non empty strings')
if issuer is not None and (tenant_id is not None):
raise ArgumentUsageError('Usage Error: --issuer and --tenant-id cannot be configured '
'to non empty strings at the same time.')
is_new_aad_app = False
existing_auth = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
registration = {}
validation = {}
if "identityProviders" not in existing_auth.keys():
existing_auth["identityProviders"] = {}
if "azureActiveDirectory" not in existing_auth["identityProviders"].keys():
existing_auth["identityProviders"]["azureActiveDirectory"] = {}
is_new_aad_app = True
if is_new_aad_app and issuer is None and tenant_id is None:
raise CLIError('Usage Error: Either --issuer or --tenant-id must be specified when configuring the '
'Microsoft auth registration.')
if client_secret is not None and not yes:
msg = 'Configuring --client-secret will add app settings to the web app. Are you sure you want to continue?'
if not prompt_y_n(msg, default="n"):
raise CLIError('Usage Error: --client-secret cannot be used without agreeing to add app settings '
'to the web app.')
openid_issuer = issuer
if openid_issuer is None:
# cmd.cli_ctx.cloud resolves to whichever cloud the customer is currently logged into
authority = cmd.cli_ctx.cloud.endpoints.active_directory
if tenant_id is not None:
openid_issuer = authority + "/" + tenant_id + "/v2.0"
existing_auth = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
registration = {}
validation = {}
if "identityProviders" not in existing_auth.keys():
existing_auth["identityProviders"] = {}
if "azureActiveDirectory" not in existing_auth["identityProviders"].keys():
existing_auth["identityProviders"]["azureActiveDirectory"] = {}
if (client_id is not None or client_secret is not None or
client_secret_setting_name is not None or openid_issuer is not None or
client_secret_certificate_thumbprint is not None or
client_secret_certificate_san is not None or
client_secret_certificate_issuer is not None):
if "registration" not in existing_auth["identityProviders"]["azureActiveDirectory"].keys():
existing_auth["identityProviders"]["azureActiveDirectory"]["registration"] = {}
registration = existing_auth["identityProviders"]["azureActiveDirectory"]["registration"]
if allowed_token_audiences is not None:
if "validation" not in existing_auth["identityProviders"]["azureActiveDirectory"].keys():
existing_auth["identityProviders"]["azureActiveDirectory"]["validation"] = {}
validation = existing_auth["identityProviders"]["azureActiveDirectory"]["validation"]
if client_id is not None:
registration["clientId"] = client_id
if client_secret_setting_name is not None:
registration["clientSecretSettingName"] = client_secret_setting_name
if client_secret is not None:
registration["clientSecretSettingName"] = MICROSOFT_SECRET_SETTING_NAME
settings = []
settings.append(MICROSOFT_SECRET_SETTING_NAME + '=' + client_secret)
update_app_settings(cmd, resource_group_name, name, slot=slot, slot_settings=settings)
if client_secret_setting_name is not None or client_secret is not None:
if "clientSecretCertificateThumbprint" in registration.keys() and registration["clientSecretCertificateThumbprint"] is not None:
registration["clientSecretCertificateThumbprint"] = None
if "clientSecretCertificateSubjectAlternativeName" in registration.keys() and registration["clientSecretCertificateSubjectAlternativeName"] is not None:
registration["clientSecretCertificateSubjectAlternativeName"] = None
if "clientSecretCertificateIssuer" in registration.keys() and registration["clientSecretCertificateIssuer"] is not None:
registration["clientSecretCertificateIssuer"] = None
if client_secret_certificate_thumbprint is not None:
registration["clientSecretCertificateThumbprint"] = client_secret_certificate_thumbprint
if "clientSecretSettingName" in registration.keys() and registration["clientSecretSettingName"] is not None:
registration["clientSecretSettingName"] = None
if "clientSecretCertificateSubjectAlternativeName" in registration.keys() and registration["clientSecretCertificateSubjectAlternativeName"] is not None:
registration["clientSecretCertificateSubjectAlternativeName"] = None
if "clientSecretCertificateIssuer" in registration.keys() and registration["clientSecretCertificateIssuer"] is not None:
registration["clientSecretCertificateIssuer"] = None
if client_secret_certificate_san is not None:
registration["clientSecretCertificateSubjectAlternativeName"] = client_secret_certificate_san
if client_secret_certificate_issuer is not None:
registration["clientSecretCertificateIssuer"] = client_secret_certificate_issuer
if client_secret_certificate_san is not None and client_secret_certificate_issuer is not None:
if "clientSecretSettingName" in registration.keys() and registration["clientSecretSettingName"] is not None:
registration["clientSecretSettingName"] = None
if "clientSecretCertificateThumbprint" in registration.keys() and registration["clientSecretCertificateThumbprint"] is not None:
registration["clientSecretCertificateThumbprint"] = None
if openid_issuer is not None:
registration["openIdIssuer"] = openid_issuer
if allowed_token_audiences is not None:
validation["allowedAudiences"] = allowed_token_audiences.split(",")
existing_auth["identityProviders"]["azureActiveDirectory"]["validation"] = validation
if (client_id is not None or client_secret is not None or
client_secret_setting_name is not None or issuer is not None or
client_secret_certificate_thumbprint is not None or
client_secret_certificate_san is not None or
client_secret_certificate_issuer is not None):
existing_auth["identityProviders"]["azureActiveDirectory"]["registration"] = registration
updated_auth_settings = update_auth_settings_v2_rest_call(cmd, resource_group_name, name, existing_auth, slot)
return updated_auth_settings["identityProviders"]["azureActiveDirectory"]
# endregion
# region webapp auth facebook
def get_facebook_settings(cmd, resource_group_name, name, slot=None):
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
return {}
if "facebook" not in auth_settings["identityProviders"].keys():
return {}
return auth_settings["identityProviders"]["facebook"]
def update_facebook_settings(cmd, resource_group_name, name, slot=None, # pylint: disable=unused-argument
app_id=None, app_secret_setting_name=None, # pylint: disable=unused-argument
graph_api_version=None, scopes=None, app_secret=None, yes=False): # pylint: disable=unused-argument
if app_secret is not None and app_secret_setting_name is not None:
raise CLIError('Usage Error: --app-secret and --app-secret-setting-name cannot both be configured '
'to non empty strings')
if app_secret is not None and not yes:
msg = 'Configuring --app-secret will add app settings to the web app. Are you sure you want to continue?'
if not prompt_y_n(msg, default="n"):
raise CLIError('Usage Error: --app-secret cannot be used without agreeing to add app '
'settings to the web app.')
existing_auth = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
registration = {}
if "identityProviders" not in existing_auth.keys():
existing_auth["identityProviders"] = {}
if "facebook" not in existing_auth["identityProviders"].keys():
existing_auth["identityProviders"]["facebook"] = {}
if app_id is not None or app_secret is not None or app_secret_setting_name is not None:
if "registration" not in existing_auth["identityProviders"]["facebook"].keys():
existing_auth["identityProviders"]["facebook"]["registration"] = {}
registration = existing_auth["identityProviders"]["facebook"]["registration"]
if scopes is not None:
if "login" not in existing_auth["identityProviders"]["facebook"].keys():
existing_auth["identityProviders"]["facebook"]["login"] = {}
if app_id is not None:
registration["appId"] = app_id
if app_secret_setting_name is not None:
registration["appSecretSettingName"] = app_secret_setting_name
if app_secret is not None:
registration["appSecretSettingName"] = FACEBOOK_SECRET_SETTING_NAME
settings = []
settings.append(FACEBOOK_SECRET_SETTING_NAME + '=' + app_secret)
update_app_settings(cmd, resource_group_name, name, slot=slot, slot_settings=settings)
if graph_api_version is not None:
existing_auth["identityProviders"]["facebook"]["graphApiVersion"] = graph_api_version
if scopes is not None:
existing_auth["identityProviders"]["facebook"]["login"]["scopes"] = scopes.split(",")
if app_id is not None or app_secret is not None or app_secret_setting_name is not None:
existing_auth["identityProviders"]["facebook"]["registration"] = registration
updated_auth_settings = update_auth_settings_v2_rest_call(cmd, resource_group_name, name, existing_auth, slot)
return updated_auth_settings["identityProviders"]["facebook"]
# endregion
# region webapp auth github
def get_github_settings(cmd, resource_group_name, name, slot=None):
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
return {}
if "gitHub" not in auth_settings["identityProviders"].keys():
return {}
return auth_settings["identityProviders"]["gitHub"]
def update_github_settings(cmd, resource_group_name, name, slot=None, # pylint: disable=unused-argument
client_id=None, client_secret_setting_name=None, # pylint: disable=unused-argument
scopes=None, client_secret=None, yes=False): # pylint: disable=unused-argument
if client_secret is not None and client_secret_setting_name is not None:
raise CLIError('Usage Error: --client-secret and --client-secret-setting-name cannot '
'both be configured to non empty strings')
if client_secret is not None and not yes:
msg = 'Configuring --client-secret will add app settings to the web app. Are you sure you want to continue?'
if not prompt_y_n(msg, default="n"):
raise CLIError('Usage Error: --client-secret cannot be used without agreeing to add '
'app settings to the web app.')
existing_auth = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
registration = {}
if "identityProviders" not in existing_auth.keys():
existing_auth["identityProviders"] = {}
if "gitHub" not in existing_auth["identityProviders"].keys():
existing_auth["identityProviders"]["gitHub"] = {}
if client_id is not None or client_secret is not None or client_secret_setting_name is not None:
if "registration" not in existing_auth["identityProviders"]["gitHub"].keys():
existing_auth["identityProviders"]["gitHub"]["registration"] = {}
registration = existing_auth["identityProviders"]["gitHub"]["registration"]
if scopes is not None:
if "login" not in existing_auth["identityProviders"]["gitHub"].keys():
existing_auth["identityProviders"]["gitHub"]["login"] = {}
if client_id is not None:
registration["clientId"] = client_id
if client_secret_setting_name is not None:
registration["clientSecretSettingName"] = client_secret_setting_name
if client_secret is not None:
registration["clientSecretSettingName"] = GITHUB_SECRET_SETTING_NAME
settings = []
settings.append(GITHUB_SECRET_SETTING_NAME + '=' + client_secret)
update_app_settings(cmd, resource_group_name, name, slot=slot, slot_settings=settings)
if scopes is not None:
existing_auth["identityProviders"]["gitHub"]["login"]["scopes"] = scopes.split(",")
if client_id is not None or client_secret is not None or client_secret_setting_name is not None:
existing_auth["identityProviders"]["gitHub"]["registration"] = registration
updated_auth_settings = update_auth_settings_v2_rest_call(cmd, resource_group_name, name, existing_auth, slot)
return updated_auth_settings["identityProviders"]["gitHub"]
# endregion
# region webapp auth google
def get_google_settings(cmd, resource_group_name, name, slot=None):
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
return {}
if "google" not in auth_settings["identityProviders"].keys():
return {}
return auth_settings["identityProviders"]["google"]
def update_google_settings(cmd, resource_group_name, name, slot=None, # pylint: disable=unused-argument
client_id=None, client_secret_setting_name=None, # pylint: disable=unused-argument
scopes=None, allowed_token_audiences=None, client_secret=None, yes=False): # pylint: disable=unused-argument
if client_secret is not None and client_secret_setting_name is not None:
raise CLIError('Usage Error: --client-secret and --client-secret-setting-name cannot '
'both be configured to non empty strings')
if client_secret is not None and not yes:
msg = 'Configuring --client-secret will add app settings to the web app. Are you sure you want to continue?'
if not prompt_y_n(msg, default="n"):
raise CLIError('Usage Error: --client-secret cannot be used without agreeing to add '
'app settings to the web app.')
existing_auth = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
registration = {}
validation = {}
if "identityProviders" not in existing_auth.keys():
existing_auth["identityProviders"] = {}
if "google" not in existing_auth["identityProviders"].keys():
existing_auth["identityProviders"]["google"] = {}
if client_id is not None or client_secret is not None or client_secret_setting_name is not None:
if "registration" not in existing_auth["identityProviders"]["google"].keys():
existing_auth["identityProviders"]["google"]["registration"] = {}
registration = existing_auth["identityProviders"]["google"]["registration"]
if scopes is not None:
if "login" not in existing_auth["identityProviders"]["google"].keys():
existing_auth["identityProviders"]["google"]["login"] = {}
if allowed_token_audiences is not None:
if "validation" not in existing_auth["identityProviders"]["google"].keys():
existing_auth["identityProviders"]["google"]["validation"] = {}
if client_id is not None:
registration["clientId"] = client_id
if client_secret_setting_name is not None:
registration["clientSecretSettingName"] = client_secret_setting_name
if client_secret is not None:
registration["clientSecretSettingName"] = GOOGLE_SECRET_SETTING_NAME
settings = []
settings.append(GOOGLE_SECRET_SETTING_NAME + '=' + client_secret)
update_app_settings(cmd, resource_group_name, name, slot=slot, slot_settings=settings)
if scopes is not None:
existing_auth["identityProviders"]["google"]["login"]["scopes"] = scopes.split(",")
if allowed_token_audiences is not None:
validation["allowedAudiences"] = allowed_token_audiences.split(",")
existing_auth["identityProviders"]["google"]["validation"] = validation
if client_id is not None or client_secret is not None or client_secret_setting_name is not None:
existing_auth["identityProviders"]["google"]["registration"] = registration
updated_auth_settings = update_auth_settings_v2_rest_call(cmd, resource_group_name, name, existing_auth, slot)
return updated_auth_settings["identityProviders"]["google"]
# endregion
# region webapp auth twitter
def get_twitter_settings(cmd, resource_group_name, name, slot=None):
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
return {}
if "twitter" not in auth_settings["identityProviders"].keys():
return {}
return auth_settings["identityProviders"]["twitter"]
def update_twitter_settings(cmd, resource_group_name, name, slot=None, # pylint: disable=unused-argument
consumer_key=None, consumer_secret_setting_name=None, # pylint: disable=unused-argument
consumer_secret=None, yes=False): # pylint: disable=unused-argument
if consumer_secret is not None and consumer_secret_setting_name is not None:
raise CLIError('Usage Error: --consumer-secret and --consumer-secret-setting-name cannot '
'both be configured to non empty strings')
if consumer_secret is not None and not yes:
msg = 'Configuring --consumer-secret will add app settings to the web app. Are you sure you want to continue?'
if not prompt_y_n(msg, default="n"):
raise CLIError('Usage Error: --consumer-secret cannot be used without agreeing '
'to add app settings to the web app.')
registration = {}
existing_auth = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in existing_auth.keys():
existing_auth["identityProviders"] = {}
if "twitter" not in existing_auth["identityProviders"].keys():
existing_auth["identityProviders"]["twitter"] = {}
if consumer_key is not None or consumer_secret is not None or consumer_secret_setting_name is not None:
if "registration" not in existing_auth["identityProviders"]["twitter"].keys():
existing_auth["identityProviders"]["twitter"]["registration"] = {}
registration = existing_auth["identityProviders"]["twitter"]["registration"]
if consumer_key is not None:
registration["consumerKey"] = consumer_key
if consumer_secret_setting_name is not None:
registration["consumerSecretSettingName"] = consumer_secret_setting_name
if consumer_secret is not None:
registration["consumerSecretSettingName"] = TWITTER_SECRET_SETTING_NAME
settings = []
settings.append(TWITTER_SECRET_SETTING_NAME + '=' + consumer_secret)
update_app_settings(cmd, resource_group_name, name, slot=slot, slot_settings=settings)
if consumer_key is not None or consumer_secret is not None or consumer_secret_setting_name is not None:
existing_auth["identityProviders"]["twitter"]["registration"] = registration
updated_auth_settings = update_auth_settings_v2_rest_call(cmd, resource_group_name, name, existing_auth, slot)
return updated_auth_settings["identityProviders"]["twitter"]
# endregion
# region webapp auth apple
def get_apple_settings(cmd, resource_group_name, name, slot=None):
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
return {}
if "apple" not in auth_settings["identityProviders"].keys():
return {}
return auth_settings["identityProviders"]["apple"]
def update_apple_settings(cmd, resource_group_name, name, slot=None, # pylint: disable=unused-argument
client_id=None, client_secret_setting_name=None, # pylint: disable=unused-argument
scopes=None, client_secret=None, yes=False): # pylint: disable=unused-argument
if client_secret is not None and client_secret_setting_name is not None:
raise CLIError('Usage Error: --client-secret and --client-secret-setting-name '
'cannot both be configured to non empty strings')
if client_secret is not None and not yes:
msg = 'Configuring --client-secret will add app settings to the web app. ' \
'Are you sure you want to continue?'
if not prompt_y_n(msg, default="n"):
raise CLIError('Usage Error: --client-secret cannot be used without agreeing '
'to add app settings to the web app.')
existing_auth = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
registration = {}
if "identityProviders" not in existing_auth.keys():
existing_auth["identityProviders"] = {}
if "apple" not in existing_auth["identityProviders"].keys():
existing_auth["identityProviders"]["apple"] = {}
if client_id is not None or client_secret is not None or client_secret_setting_name is not None:
if "registration" not in existing_auth["identityProviders"]["apple"].keys():
existing_auth["identityProviders"]["apple"]["registration"] = {}
registration = existing_auth["identityProviders"]["apple"]["registration"]
if scopes is not None:
if "login" not in existing_auth["identityProviders"]["apple"].keys():
existing_auth["identityProviders"]["apple"]["login"] = {}
if client_id is not None:
registration["clientId"] = client_id
if client_secret_setting_name is not None:
registration["clientSecretSettingName"] = client_secret_setting_name
if client_secret is not None:
registration["clientSecretSettingName"] = 'APPLE_PROVIDER_AUTHENTICATION_SECRET'
settings = []
settings.append('APPLE_PROVIDER_AUTHENTICATION_SECRET=' + client_secret)
update_app_settings(cmd, resource_group_name, name, slot=slot, slot_settings=settings)
if scopes is not None:
existing_auth["identityProviders"]["apple"]["login"]["scopes"] = scopes.split(",")
if client_id is not None or client_secret is not None or client_secret_setting_name is not None:
existing_auth["identityProviders"]["apple"]["registration"] = registration
updated_auth_settings = update_auth_settings_v2_rest_call(cmd, resource_group_name, name, existing_auth, slot)
return updated_auth_settings["identityProviders"]["apple"]
# endregion
# region webapp auth openid-connect
def get_openid_connect_provider_settings(cmd, resource_group_name, name, provider_name, slot=None): # pylint: disable=unused-argument
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
raise CLIError('Usage Error: The following custom OpenID Connect provider '
'has not been configured: ' + provider_name)
if "customOpenIdConnectProviders" not in auth_settings["identityProviders"].keys():
raise CLIError('Usage Error: The following custom OpenID Connect provider '
'has not been configured: ' + provider_name)
if provider_name not in auth_settings["identityProviders"]["customOpenIdConnectProviders"].keys():
raise CLIError('Usage Error: The following custom OpenID Connect provider '
'has not been configured: ' + provider_name)
return auth_settings["identityProviders"]["customOpenIdConnectProviders"][provider_name]
def add_openid_connect_provider_settings(cmd, resource_group_name, name, provider_name, slot=None, # pylint: disable=unused-argument
client_id=None, client_secret_setting_name=None, # pylint: disable=unused-argument
openid_configuration=None, scopes=None, # pylint: disable=unused-argument
client_secret=None, yes=False): # pylint: disable=unused-argument
if client_secret is not None and not yes:
msg = 'Configuring --client-secret will add app settings to the web app. ' \
'Are you sure you want to continue?'
if not prompt_y_n(msg, default="n"):
raise CLIError('Usage Error: --client-secret cannot be used without agreeing '
'to add app settings to the web app.')
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
auth_settings["identityProviders"] = {}
if "customOpenIdConnectProviders" not in auth_settings["identityProviders"].keys():
auth_settings["identityProviders"]["customOpenIdConnectProviders"] = {}
if provider_name in auth_settings["identityProviders"]["customOpenIdConnectProviders"].keys():
raise CLIError('Usage Error: The following custom OpenID Connect provider has already been '
'configured: ' + provider_name + '. Please use az webapp auth oidc update to '
'update the provider.')
final_client_secret_setting_name = client_secret_setting_name
if client_secret is not None:
final_client_secret_setting_name = get_oidc_client_setting_app_setting_name(provider_name)
settings = []
settings.append(final_client_secret_setting_name + '=' + client_secret)
update_app_settings(cmd, resource_group_name, name, slot=slot, slot_settings=settings)
auth_settings["identityProviders"]["customOpenIdConnectProviders"][provider_name] = {
"registration": {
"clientId": client_id,
"clientCredential": {
"clientSecretSettingName": final_client_secret_setting_name
},
"openIdConnectConfiguration": {
"wellKnownOpenIdConfiguration": openid_configuration
}
}
}
login = {}
if scopes is not None:
login["scopes"] = scopes.split(',')
else:
login["scopes"] = ["openid"]
auth_settings["identityProviders"]["customOpenIdConnectProviders"][provider_name]["login"] = login
updated_auth_settings = update_auth_settings_v2_rest_call(cmd, resource_group_name, name, auth_settings, slot)
return updated_auth_settings["identityProviders"]["customOpenIdConnectProviders"][provider_name]
def update_openid_connect_provider_settings(cmd, resource_group_name, name, provider_name, slot=None, # pylint: disable=unused-argument
client_id=None, client_secret_setting_name=None, # pylint: disable=unused-argument
openid_configuration=None, scopes=None, # pylint: disable=unused-argument
client_secret=None, yes=False): # pylint: disable=unused-argument
if client_secret is not None and not yes:
msg = 'Configuring --client-secret will add app settings to the web app. ' \
'Are you sure you want to continue?'
if not prompt_y_n(msg, default="n"):
raise CLIError('Usage Error: --client-secret cannot be used without agreeing '
'to add app settings to the web app.')
auth_settings = get_auth_settings_v2(cmd, resource_group_name, name, slot)["properties"]
if "identityProviders" not in auth_settings.keys():
raise CLIError('Usage Error: The following custom OpenID Connect provider '
'has not been configured: ' + provider_name)
if "customOpenIdConnectProviders" not in auth_settings["identityProviders"].keys():
raise CLIError('Usage Error: The following custom OpenID Connect provider '
'has not been configured: ' + provider_name)
if provider_name not in auth_settings["identityProviders"]["customOpenIdConnectProviders"].keys():
raise CLIError('Usage Error: The following custom OpenID Connect provider '
'has not been configured: ' + provider_name)
custom_open_id_connect_providers = auth_settings["identityProviders"]["customOpenIdConnectProviders"]
registration = {}
if client_id is not None or client_secret_setting_name is not None or openid_configuration is not None:
if "registration" not in custom_open_id_connect_providers[provider_name].keys():
custom_open_id_connect_providers[provider_name]["registration"] = {}
registration = custom_open_id_connect_providers[provider_name]["registration"]
if client_secret_setting_name is not None or client_secret is not None:
if "clientCredential" not in custom_open_id_connect_providers[provider_name]["registration"].keys():
custom_open_id_connect_providers[provider_name]["registration"]["clientCredential"] = {}
if openid_configuration is not None:
if "openIdConnectConfiguration" not in custom_open_id_connect_providers[provider_name]["registration"].keys():
custom_open_id_connect_providers[provider_name]["registration"]["openIdConnectConfiguration"] = {}
if scopes is not None:
if "login" not in auth_settings["identityProviders"]["customOpenIdConnectProviders"][provider_name].keys():
custom_open_id_connect_providers[provider_name]["login"] = {}
if client_id is not None:
registration["clientId"] = client_id
if client_secret_setting_name is not None: