Skip to content

Commit

Permalink
address peter's pr comments (#108)
Browse files Browse the repository at this point in the history
- move v2 check to authority helper
- fix name
  • Loading branch information
jennyf19 authored Apr 16, 2020
1 parent 2408678 commit a7c397b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 34 deletions.
8 changes: 8 additions & 0 deletions src/Microsoft.Identity.Web/AuthorityHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,13 @@ internal static string BuildAuthority(MicrosoftIdentityOptions options)
return new Uri(baseUri, new PathString($"{pathBase}/{tenantId}/v2.0")).ToString();
}
}

internal static string EnsureAuthorityIsV2(string authority)
{
authority = authority.Trim().TrimEnd('/');
if (!authority.EndsWith("v2.0"))
authority += "/v2.0";
return authority;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static AuthenticationBuilder AddProtectedWebApi(
options.Authority = AuthorityHelpers.BuildAuthority(microsoftIdentityOptions);

// This is a Microsoft identity platform Web API
EnsureAuthorityIsV2_0(options);
options.Authority = AuthorityHelpers.EnsureAuthorityIsV2(options.Authority);

// The valid audience could be given as Client Id or as Uri.
// If it does not start with 'api://', this variant is added to the list of valid audiences.
Expand Down Expand Up @@ -138,20 +138,6 @@ public static AuthenticationBuilder AddProtectedWebApi(
return builder;
}

/// <summary>
/// Ensures that the authority is a v2.0 authority
/// </summary>
/// <param name="options">Jwt bearer options read from the config file
/// or set by the developer, for which we want to ensure the authority
/// is a v2.0 authority</param>
internal static void EnsureAuthorityIsV2_0(JwtBearerOptions options)
{
var authority = options.Authority.Trim().TrimEnd('/');
if (!authority.EndsWith("v2.0"))
authority += "/v2.0";
options.Authority = authority;
}

/// <summary>
/// Ensure that if the audience is a GUID, api://{audience} is also added
/// as a valid audience (this is the default App ID URL in the app registration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public static AuthenticationBuilder AddSignIn(
if (string.IsNullOrWhiteSpace(options.Authority))
options.Authority = AuthorityHelpers.BuildAuthority(microsoftIdentityOptions);

// This is a Microsoft identity platform Web APP
EnsureAuthorityIsV2_0(options);
// This is a Microsoft identity platform Web app
options.Authority = AuthorityHelpers.EnsureAuthorityIsV2(options.Authority);

// B2C doesn't have preferred_username claims
if (microsoftIdentityOptions.IsB2C)
Expand Down Expand Up @@ -161,19 +161,5 @@ public static AuthenticationBuilder AddSignIn(

return builder;
}

/// <summary>
/// Ensures that the authority is a v2.0 authority
/// </summary>
/// <param name="options">OpenIdConnect options read from the config file
/// or set by the developer, for which we want to ensure the authority
/// is a v2.0 authority</param>
internal static void EnsureAuthorityIsV2_0(OpenIdConnectOptions options)
{
var authority = options.Authority.Trim().TrimEnd('/');
if (!authority.EndsWith("v2.0"))
authority += "/v2.0";
options.Authority = authority;
}
}
}
2 changes: 1 addition & 1 deletion tests/Microsoft.Identity.Web.Test/WebApiExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public void EnsureAuthorityIsV2_0(string initialAuthority, string expectedAuthor
Authority = initialAuthority
};

WebApiAuthenticationBuilderExtensions.EnsureAuthorityIsV2_0(options);
options.Authority = AuthorityHelpers.EnsureAuthorityIsV2(options.Authority);
Assert.Equal(expectedAuthority, options.Authority);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.Identity.Web.Test
{
public class WebAppExtentionsTests
public class WebAppExtensionsTests
{
[Theory]
[InlineData(TestConstants.AuthorityCommonTenant, TestConstants.AuthorityCommonTenantWithV2)]
Expand All @@ -25,7 +25,7 @@ public void EnsureAuthorityIsV2_0(string initialAuthority, string expectedAuthor
Authority = initialAuthority
};

WebAppAuthenticationBuilderExtensions.EnsureAuthorityIsV2_0(options);
options.Authority = AuthorityHelpers.EnsureAuthorityIsV2(options.Authority);
Assert.Equal(expectedAuthority, options.Authority);
}
}
Expand Down

0 comments on commit a7c397b

Please sign in to comment.