Skip to content

Commit 8b9eb07

Browse files
authored
CIAM updates (#2422) (#2432)
* CIAM updates (#2422) * Updates for CIAM * Updates for code modifier configs * Update version to 2.0.4
1 parent 623e5d8 commit 8b9eb07

File tree

10 files changed

+230
-171
lines changed

10 files changed

+230
-171
lines changed

eng/Versions.MSIdentity.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<UsingToolNetFrameworkReferenceAssemblies>true</UsingToolNetFrameworkReferenceAssemblies>
77
</PropertyGroup>
88
<PropertyGroup>
9-
<VersionPrefix>2.0.3</VersionPrefix>
9+
<VersionPrefix>2.0.4</VersionPrefix>
1010
<PreReleaseVersionLabel>rtm</PreReleaseVersionLabel>
1111
<IsServicingBuild Condition="'$(PreReleaseVersionLabel)' == 'servicing'">true</IsServicingBuild>
1212
<!--

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/AuthenticationParameters/ApplicationParameters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public string? Domain1
8787
/// </summary>
8888
public bool IsB2C { get; set; }
8989

90+
/// <summary>
91+
/// Is authenticated with CIAM.
92+
/// </summary>
93+
public bool IsCiam { get; set; }
94+
9095
/// <summary>
9196
/// Sign-up/sign-in policy in the case of B2C.
9297
/// </summary>

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/AuthenticationParameters/AzureAdProperties.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static class PropertyNames
2929
// https://github.com/dotnet/aspnetcore/blob/6bc4b79f4ee7af00edcbb435e5ee4c1de349a110/src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/appsettings.json
3030
public static class DefaultProperties
3131
{
32+
public const string Authority = "https://qualified.domain.name/";
3233
public const string Domain = "qualified.domain.name";
3334
public const string TenantId = "22222222-2222-2222-2222-222222222222";
3435
public const string ClientId = "11111111-1111-1111-11111111111111111";
@@ -53,6 +54,7 @@ public class AzureAdBlock
5354
public bool IsBlazorWasm;
5455
public bool IsWebApi;
5556
public bool IsB2C;
57+
public bool IsCIAM;
5658
public bool HasClientSecret;
5759

5860
public string? ClientId;
@@ -76,18 +78,23 @@ public AzureAdBlock(ApplicationParameters applicationParameters, JObject? existi
7678
IsBlazorWasm = applicationParameters.IsBlazorWasm;
7779
IsWebApi = applicationParameters.IsWebApi.GetValueOrDefault();
7880
IsB2C = applicationParameters.IsB2C;
81+
IsCIAM = applicationParameters.IsCiam;
7982
HasClientSecret = applicationParameters.CallsDownstreamApi || applicationParameters.CallsMicrosoftGraph;
8083

8184
Domain = !string.IsNullOrEmpty(applicationParameters.Domain) ? applicationParameters.Domain : existingBlock?.GetValue(PropertyNames.Domain)?.ToString() ?? DefaultProperties.Domain;
85+
if (IsCIAM)
86+
{
87+
Domain = Domain.Replace("onmicrosoft.com", "ciamlogin.com");
88+
}
89+
8290
TenantId = !string.IsNullOrEmpty(applicationParameters.TenantId) ? applicationParameters.TenantId : existingBlock?.GetValue(PropertyNames.TenantId)?.ToString() ?? DefaultProperties.TenantId;
8391
ClientId = !string.IsNullOrEmpty(applicationParameters.ClientId) ? applicationParameters.ClientId : existingBlock?.GetValue(PropertyNames.ClientId)?.ToString() ?? DefaultProperties.ClientId;
8492
Instance = !string.IsNullOrEmpty(applicationParameters.Instance) ? applicationParameters.Instance : existingBlock?.GetValue(PropertyNames.Instance)?.ToString() ?? DefaultProperties.Instance;
8593
CallbackPath = !string.IsNullOrEmpty(applicationParameters.CallbackPath) ? applicationParameters.CallbackPath : existingBlock?.GetValue(PropertyNames.CallbackPath)?.ToString() ?? DefaultProperties.CallbackPath;
8694
Scopes = !string.IsNullOrEmpty(applicationParameters.CalledApiScopes) ? applicationParameters.CalledApiScopes : existingBlock?.GetValue(PropertyNames.Scopes)?.ToString()
8795
?? (applicationParameters.CallsDownstreamApi ? DefaultProperties.ApiScopes : applicationParameters.CallsMicrosoftGraph ? DefaultProperties.MicrosoftGraphScopes : null);
8896
SignUpSignInPolicyId = !string.IsNullOrEmpty(applicationParameters.SusiPolicy) ? applicationParameters.SusiPolicy : existingBlock?.GetValue(PropertyNames.SignUpSignInPolicyId)?.ToString() ?? DefaultProperties.SignUpSignInPolicyId;
89-
// TODO determine the SusiPolicy from the graph beta
90-
Authority = IsB2C ? $"{Instance}{Domain}/{SignUpSignInPolicyId}" : $"{Instance}{Domain}";
97+
Authority = IsCIAM ? $"https://{Domain}/" : IsB2C ? $"{Instance}{Domain}/{SignUpSignInPolicyId}" : $"{Instance}{Domain}";
9198
ClientSecret = existingBlock?.GetValue(PropertyNames.ClientSecret)?.ToString() ?? DefaultProperties.ClientSecret;
9299
ClientCertificates = existingBlock?.GetValue(PropertyNames.ClientCertificates)?.ToObject<string[]>();
93100
}
@@ -99,6 +106,15 @@ public AzureAdBlock(ApplicationParameters applicationParameters, JObject? existi
99106
ValidateAuthority = !IsB2C
100107
};
101108

109+
public dynamic CIAMSettings => new
110+
{
111+
Authority = Authority ?? DefaultProperties.Authority,
112+
ClientId = ClientId ?? DefaultProperties.ClientId,
113+
ClientSecret = ClientSecret ?? DefaultProperties.ClientSecret,
114+
ClientCertificates = ClientCertificates ?? Array.Empty<string>(),
115+
CallbackPath = CallbackPath ?? DefaultProperties.CallbackPath
116+
};
117+
102118
public dynamic WebAppSettings => new
103119
{
104120
Instance = Instance ?? DefaultProperties.Instance,
@@ -140,6 +156,11 @@ public JObject ToJObject()
140156
return JObject.FromObject(BlazorSettings);
141157
}
142158

159+
if (IsCIAM)
160+
{
161+
return JObject.FromObject(CIAMSettings);
162+
}
163+
143164
var jObject = IsWebApi ? JObject.FromObject(WebApiSettings) : JObject.FromObject(WebAppSettings);
144165

145166
if (IsB2C)

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/CodeModifierConfigs/cm_dotnet_blazorserver.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
]
251251
},
252252
{
253-
"FileName": "LoginDisplay.razor",
253+
"FileName": "blazorserver_LoginDisplay.razor",
254254
"AddFilePath": "Shared/LoginDisplay.razor"
255255
},
256256
{
@@ -268,4 +268,4 @@
268268
]
269269
}
270270
]
271-
}
271+
}

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/CodeModifierConfigs/cm_dotnet_blazorwasm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@
193193
"AddFilePath": "Pages/Authentication.razor"
194194
},
195195
{
196-
"FileName": "LoginDisplay.razor",
196+
"FileName": "blazorwasm_LoginDisplay.razor",
197197
"AddFilePath": "Shared/LoginDisplay.razor"
198198
},
199199
{

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/CodeModifierConfigs/cm_dotnet_webapp.json

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,23 @@
140140
"LeadingTrivia": {
141141
"Newline": true
142142
}
143-
},
143+
},
144144
{
145145
"CodeChangeType": "Lambda",
146146
"Parent": "WebApplication.CreateBuilder.Services.AddAuthorization",
147147
"Block": "options.FallbackPolicy = options.DefaultPolicy",
148148
"Parameter": "options",
149-
"LeadingTrivia":
150-
{
151-
"Newline":true,
149+
"LeadingTrivia": {
150+
"Newline": true,
152151
"NumberOfSpaces": 4
153152
}
154153
},
155154
{
156155
"Parent": "WebApplication.CreateBuilder.Services.AddRazorPages",
157156
"CodeChangeType": "MemberAccess",
158157
"Block": "AddMicrosoftIdentityUI()",
159-
"LeadingTrivia":
160-
{
161-
"Newline":true,
158+
"LeadingTrivia": {
159+
"Newline": true,
162160
"NumberOfSpaces": 4
163161
}
164162
},
@@ -187,10 +185,10 @@
187185
},
188186
{
189187
"FileName": "Index.cshtml.cs",
190-
"Options" : [ "MicrosoftGraph", "DownstreamApi" ],
188+
"Options": [ "MicrosoftGraph", "DownstreamApi" ],
191189
"ClassProperties": [
192190
{
193-
"Block" : "private readonly GraphServiceClient _graphServiceClient",
191+
"Block": "private readonly GraphServiceClient _graphServiceClient",
194192
"Options": [ "MicrosoftGraph" ]
195193
},
196194
{
@@ -209,50 +207,49 @@
209207
}
210208
],
211209
"Methods": {
212-
"OnGet":
213-
{
214-
"EditType" : {
215-
"Block": "async Task",
216-
"Options": ["MicrosoftGraph", "DownstreamApi"]
210+
"OnGet": {
211+
"EditType": {
212+
"Block": "async Task",
213+
"Options": [ "MicrosoftGraph", "DownstreamApi" ]
217214
},
218215
"CodeChanges": [
219216
{
220217
"Block": "var user = await _graphServiceClient.Me.Request().GetAsync();",
221218
"LeadingTrivia": {
222219
"NumberOfSpaces": 12
223220
},
224-
"Options" : [ "MicrosoftGraph"]
221+
"Options": [ "MicrosoftGraph" ]
225222
},
226223
{
227224
"Block": "ViewData[\"GraphApiResult\"] = user.DisplayName;",
228225
"LeadingTrivia": {
229226
"NumberOfSpaces": 12
230227
},
231-
"Options" : [ "MicrosoftGraph"]
228+
"Options": [ "MicrosoftGraph" ]
232229
},
233230
{
234-
"Block" : "using var response = await _downstreamWebApi.CallWebApiForUserAsync(\"DownstreamApi\").ConfigureAwait(false);",
231+
"Block": "using var response = await _downstreamWebApi.CallWebApiForUserAsync(\"DownstreamApi\").ConfigureAwait(false);",
235232
"LeadingTrivia": {
236233
"NumberOfSpaces": 12
237234
},
238-
"Options" : [ "DownstreamApi"]
235+
"Options": [ "DownstreamApi" ]
239236
},
240237
{
241238
"Block": "\n\n if (response.StatusCode == System.Net.HttpStatusCode.OK)\n {\n var apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);\n ViewData[\"ApiResult\"] = apiResult;\n }\n else\n {\n var error = await response.Content.ReadAsStringAsync().ConfigureAwait(false);\n throw new HttpRequestException($\"Invalid status code in the HttpResponseMessage: {response.StatusCode}: {error}\");\n }",
242239
"Options": [ "DownstreamApi" ]
243240
}
244241
]
245242
},
246-
"IndexModel" : {
247-
"Parameters" : [ "ILogger<IndexModel>" ],
248-
"AddParameters" : [
243+
"IndexModel": {
244+
"Parameters": [ "ILogger<IndexModel>" ],
245+
"AddParameters": [
249246
{
250-
"Block":"GraphServiceClient graphServiceClient",
251-
"Options": [ "MicrosoftGraph"]
247+
"Block": "GraphServiceClient graphServiceClient",
248+
"Options": [ "MicrosoftGraph" ]
252249
},
253250
{
254-
"Block":"IDownstreamWebApi downstreamWebApi",
255-
"Options": [ "DownstreamApi"]
251+
"Block": "IDownstreamWebApi downstreamWebApi",
252+
"Options": [ "DownstreamApi" ]
256253
}
257254
],
258255
"CodeChanges": [
@@ -273,7 +270,7 @@
273270
]
274271
}
275272
},
276-
"Usings" : [
273+
"Usings": [
277274
"Microsoft.Identity.Web",
278275
"System.Net"
279276
],
@@ -301,6 +298,30 @@
301298
]
302299
}
303300
}
301+
},
302+
{
303+
"FileName": "_Layout.cshtml",
304+
"Methods": {
305+
"Global": {
306+
"CodeChanges": [
307+
{
308+
"MultiLineBlock": [
309+
"</ul>",
310+
" <partial name=\"_LoginPartial\" />",
311+
" </div>"
312+
],
313+
"ReplaceSnippet": [
314+
"</ul>",
315+
" </div>"
316+
]
317+
}
318+
]
319+
}
320+
}
321+
},
322+
{
323+
"FileName": "LoginPartial.cshtml",
324+
"AddFilePath": "Pages/Shared/_LoginPartial.cshtml"
304325
}
305326
]
306327
}

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/ProjectModifier.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,21 @@ public async Task AddAuthCodeAsync()
8888
var filteredFiles = codeModifierConfig.Files.Where(f => ProjectModifierHelper.FilterOptions(f.Options, options));
8989
foreach (var file in filteredFiles)
9090
{
91-
await HandleCodeFileAsync(file, project, options, codeModifierConfig.Identifier);
91+
await HandleCodeFileAsync(file, project, options);
9292
}
9393

9494
_consoleLogger.LogJsonMessage(State.Success, output: _output.ToString().TrimEnd());
9595
}
9696

97-
internal static string GetCodeFileString(CodeFile file, string identifier) // todo make all code files strings
97+
internal static string GetCodeFileString(CodeFile file)
9898
{
9999
// Resource files cannot contain '-' (dash) or '.' (period)
100-
var codeFilePropertyName = $"add_{identifier.Replace('-', '_')}_{file.FileName.Replace('.', '_')}";
100+
var codeFilePropertyName = $"add_{file.FileName.Replace('.', '_')}";
101101
var property = AppProvisioningTool.Properties.FirstOrDefault(
102-
p => p.Name.Equals(codeFilePropertyName));
103-
104-
if (property is null)
105-
{
106-
throw new FormatException($"Resource property for {file.FileName} could not be found. ");
107-
}
102+
p => p.Name.Equals(codeFilePropertyName))
103+
?? throw new FormatException($"Resource property for {file.FileName} could not be found. ");
108104

109105
var codeFileString = property.GetValue(typeof(Resources))?.ToString();
110-
111106
if (string.IsNullOrEmpty(codeFileString))
112107
{
113108
throw new FormatException($"CodeFile string for {file.FileName} was empty.");
@@ -116,7 +111,7 @@ internal static string GetCodeFileString(CodeFile file, string identifier) // to
116111
return codeFileString;
117112
}
118113

119-
internal static ClassDeclarationSyntax ModifyMethods(string fileName, ClassDeclarationSyntax classNode, DocumentBuilder documentBuilder, Dictionary<string, Method> methods, CodeChangeOptions options, StringBuilder output)
114+
internal static ClassDeclarationSyntax ModifyMethods(string fileName, ClassDeclarationSyntax classNode, Dictionary<string, Method> methods, CodeChangeOptions options, StringBuilder output)
120115
{
121116
foreach ((string methodName, Method methodChanges) in methods)
122117
{
@@ -283,13 +278,13 @@ private PropertyInfo? CodeModifierConfigPropertyInfo
283278
}
284279
}
285280

286-
private async Task HandleCodeFileAsync(CodeFile file, CodeAnalysis.Project project, CodeChangeOptions options, string identifier)
281+
private async Task HandleCodeFileAsync(CodeFile file, CodeAnalysis.Project project, CodeChangeOptions options)
287282
{
288283
try
289284
{
290285
if (!string.IsNullOrEmpty(file.AddFilePath))
291286
{
292-
AddFile(file, identifier);
287+
AddFile(file);
293288
_output.AppendLine(string.Format(Resources.AddedCodeFile, file.AddFilePath));
294289
}
295290
else
@@ -325,15 +320,15 @@ private async Task HandleCodeFileAsync(CodeFile file, CodeAnalysis.Project proje
325320
/// <param name="file"></param>
326321
/// <param name="identifier"></param>
327322
/// <exception cref="FormatException"></exception>
328-
private void AddFile(CodeFile file, string identifier)
323+
private void AddFile(CodeFile file)
329324
{
330325
var filePath = Path.Combine(_toolOptions.ProjectPath, file.AddFilePath);
331326
if (File.Exists(filePath))
332327
{
333328
return; // File exists, don't need to create
334329
}
335330

336-
var codeFileString = GetCodeFileString(file, identifier);
331+
var codeFileString = GetCodeFileString(file);
337332

338333
var fileDir = Path.GetDirectoryName(filePath);
339334
if (!string.IsNullOrEmpty(fileDir))
@@ -396,7 +391,7 @@ node is ClassDeclarationSyntax cds &&
396391
//add class attributes
397392
modifiedClassDeclarationSyntax = documentBuilder.AddClassAttributes(modifiedClassDeclarationSyntax, options);
398393
//add code snippets/changes.
399-
modifiedClassDeclarationSyntax = ModifyMethods(file.FileName, modifiedClassDeclarationSyntax, documentBuilder, file.Methods, options, _output);
394+
modifiedClassDeclarationSyntax = ModifyMethods(file.FileName, modifiedClassDeclarationSyntax, file.Methods, options, _output);
400395

401396
//replace class node with all the updates.
402397
#pragma warning disable CS8631 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match constraint type.

0 commit comments

Comments
 (0)