-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add API and make ROPC call * Add silent call before attempting ROPC * Minor updates to constants and comments * Undo changes to txt files * Address comments * Exclude file ClaimsConstant as it contains the constant Password * Address comments - Add logging and update constants * Resolve warnings * Alternative to GetAccounts * Update to only add the claim if not already present * Update to use existing constants * Add check before setting * Add comment to the method * fixing warnings - public API - no ConfigureAwait(false) in tests (instable) * Address comments --------- Co-authored-by: Jean-Marc Prieur <jmprieur@microsoft.com>
- Loading branch information
1 parent
3dc8286
commit 011bd15
Showing
18 changed files
with
246 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net462/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net472/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net6.0/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net7.0/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net8.0/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,128 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Security.Claims; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Microsoft.Identity.Web | ||
{ | ||
/// <summary> | ||
/// Factory class to create <see cref="ClaimsPrincipal"/> objects. | ||
/// </summary> | ||
public static class ClaimsPrincipalFactory | ||
{ | ||
/// <summary> | ||
/// Instantiate a <see cref="ClaimsPrincipal"/> from a home account object ID and home tenant ID. This can | ||
/// be useful when the web app subscribes to another service on behalf of the user | ||
/// and then is called back by a notification where the user is identified by their home tenant | ||
/// ID and home object ID (like in Microsoft Graph Web Hooks). | ||
/// </summary> | ||
/// <param name="homeTenantId">Home tenant ID of the account.</param> | ||
/// <param name="homeObjectId">Home object ID of the account in this tenant ID.</param> | ||
/// <returns>A <see cref="ClaimsPrincipal"/> containing these two claims.</returns> | ||
/// | ||
/// <example> | ||
/// <code> | ||
/// private async Task GetChangedMessagesAsync(IEnumerable<Notification> notifications) | ||
/// { | ||
/// HttpContext.User = ClaimsPrincipalExtension.FromHomeTenantIdAndHomeObjectId(subscription.HomeTenantId, | ||
/// subscription.HomeUserId); | ||
/// foreach (var notification in notifications) | ||
/// { | ||
/// SubscriptionStore subscription = | ||
/// subscriptionStore.GetSubscriptionInfo(notification.SubscriptionId); | ||
/// string accessToken = await tokenAcquisition.GetAccessTokenForUserAsync(scopes); | ||
/// ...} | ||
/// } | ||
/// </code> | ||
/// </example> | ||
public static ClaimsPrincipal FromHomeTenantIdAndHomeObjectId(string homeTenantId, string homeObjectId) | ||
{ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Security.Claims; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Microsoft.Identity.Web | ||
{ | ||
/// <summary> | ||
/// Factory class to create <see cref="ClaimsPrincipal"/> objects. | ||
/// </summary> | ||
public static class ClaimsPrincipalFactory | ||
{ | ||
/// <summary> | ||
/// Instantiate a <see cref="ClaimsPrincipal"/> from a home account object ID and home tenant ID. This can | ||
/// be useful when the web app subscribes to another service on behalf of the user | ||
/// and then is called back by a notification where the user is identified by their home tenant | ||
/// ID and home object ID (like in Microsoft Graph Web Hooks). | ||
/// </summary> | ||
/// <param name="homeTenantId">Home tenant ID of the account.</param> | ||
/// <param name="homeObjectId">Home object ID of the account in this tenant ID.</param> | ||
/// <returns>A <see cref="ClaimsPrincipal"/> containing these two claims.</returns> | ||
/// | ||
/// <example> | ||
/// <code> | ||
/// private async Task GetChangedMessagesAsync(IEnumerable<Notification> notifications) | ||
/// { | ||
/// HttpContext.User = ClaimsPrincipalExtension.FromHomeTenantIdAndHomeObjectId(subscription.HomeTenantId, | ||
/// subscription.HomeUserId); | ||
/// foreach (var notification in notifications) | ||
/// { | ||
/// SubscriptionStore subscription = | ||
/// subscriptionStore.GetSubscriptionInfo(notification.SubscriptionId); | ||
/// string accessToken = await tokenAcquisition.GetAccessTokenForUserAsync(scopes); | ||
/// ...} | ||
/// } | ||
/// </code> | ||
/// </example> | ||
public static ClaimsPrincipal FromHomeTenantIdAndHomeObjectId(string homeTenantId, string homeObjectId) | ||
{ | ||
if (AppContextSwitches.UseClaimsIdentityType) | ||
{ | ||
#pragma warning disable RS0030 // Do not use banned APIs | ||
return new ClaimsPrincipal( | ||
new ClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimConstants.UniqueTenantIdentifier, homeTenantId), | ||
{ | ||
new Claim(ClaimConstants.UniqueTenantIdentifier, homeTenantId), | ||
new Claim(ClaimConstants.UniqueObjectIdentifier, homeObjectId), | ||
})); | ||
#pragma warning restore RS0030 // Do not use banned APIs | ||
} | ||
else | ||
{ | ||
return new ClaimsPrincipal( | ||
new CaseSensitiveClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimConstants.UniqueTenantIdentifier, homeTenantId), | ||
new Claim(ClaimConstants.UniqueObjectIdentifier, homeObjectId), | ||
})); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Instantiate a <see cref="ClaimsPrincipal"/> from an account object ID and tenant ID. This can | ||
/// be useful when the web app subscribes to another service on behalf of the user | ||
/// and then is called back by a notification where the user is identified by their tenant | ||
/// ID and object ID (like in Microsoft Graph Web Hooks). | ||
/// </summary> | ||
/// <param name="tenantId">Tenant ID of the account.</param> | ||
/// <param name="objectId">Object ID of the account in this tenant ID.</param> | ||
/// <returns>A <see cref="ClaimsPrincipal"/> containing these two claims.</returns> | ||
/// | ||
/// <example> | ||
/// <code> | ||
/// private async Task GetChangedMessagesAsync(IEnumerable<Notification> notifications) | ||
/// { | ||
/// HttpContext.User = ClaimsPrincipalExtension.FromTenantIdAndObjectId(subscription.TenantId, | ||
/// subscription.UserId); | ||
/// foreach (var notification in notifications) | ||
/// { | ||
/// SubscriptionStore subscription = | ||
/// subscriptionStore.GetSubscriptionInfo(notification.SubscriptionId); | ||
/// string accessToken = await tokenAcquisition.GetAccessTokenForUserAsync(scopes); | ||
/// ...} | ||
/// } | ||
/// </code> | ||
/// </example> | ||
public static ClaimsPrincipal FromTenantIdAndObjectId(string tenantId, string objectId) | ||
{ | ||
{ | ||
return new ClaimsPrincipal( | ||
new CaseSensitiveClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimConstants.UniqueTenantIdentifier, homeTenantId), | ||
new Claim(ClaimConstants.UniqueObjectIdentifier, homeObjectId), | ||
})); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Instantiate a <see cref="ClaimsPrincipal"/> from an account object ID and tenant ID. This can | ||
/// be useful when the web app subscribes to another service on behalf of the user | ||
/// and then is called back by a notification where the user is identified by their tenant | ||
/// ID and object ID (like in Microsoft Graph Web Hooks). | ||
/// </summary> | ||
/// <param name="tenantId">Tenant ID of the account.</param> | ||
/// <param name="objectId">Object ID of the account in this tenant ID.</param> | ||
/// <returns>A <see cref="ClaimsPrincipal"/> containing these two claims.</returns> | ||
/// | ||
/// <example> | ||
/// <code> | ||
/// private async Task GetChangedMessagesAsync(IEnumerable<Notification> notifications) | ||
/// { | ||
/// HttpContext.User = ClaimsPrincipalExtension.FromTenantIdAndObjectId(subscription.TenantId, | ||
/// subscription.UserId); | ||
/// foreach (var notification in notifications) | ||
/// { | ||
/// SubscriptionStore subscription = | ||
/// subscriptionStore.GetSubscriptionInfo(notification.SubscriptionId); | ||
/// string accessToken = await tokenAcquisition.GetAccessTokenForUserAsync(scopes); | ||
/// ...} | ||
/// } | ||
/// </code> | ||
/// </example> | ||
public static ClaimsPrincipal FromTenantIdAndObjectId(string tenantId, string objectId) | ||
{ | ||
if (AppContextSwitches.UseClaimsIdentityType) | ||
{ | ||
#pragma warning disable RS0030 // Do not use banned APIs | ||
return new ClaimsPrincipal( | ||
new ClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimConstants.Tid, tenantId), | ||
{ | ||
new Claim(ClaimConstants.Tid, tenantId), | ||
new Claim(ClaimConstants.Oid, objectId), | ||
})); | ||
#pragma warning restore RS0030 // Do not use banned APIs | ||
} else | ||
{ | ||
return new ClaimsPrincipal( | ||
new CaseSensitiveClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimConstants.Tid, tenantId), | ||
{ | ||
new Claim(ClaimConstants.Tid, tenantId), | ||
new Claim(ClaimConstants.Oid, objectId), | ||
})); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Instantiate a <see cref="ClaimsPrincipal"/> from a username and password. | ||
/// This can be used for ROPC flow for testing purposes. | ||
/// </summary> | ||
/// <param name="username">UPN of the user for example username@domain.</param> | ||
/// <param name="password">Password for the user.</param> | ||
/// <returns>A <see cref="ClaimsPrincipal"/> containing these two claims.</returns> | ||
public static ClaimsPrincipal FromUsernamePassword(string username, string password) | ||
{ | ||
return new ClaimsPrincipal( | ||
new CaseSensitiveClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimConstants.Username, username), | ||
new Claim(ClaimConstants.Password, password), | ||
})); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#nullable enable | ||
static Microsoft.Identity.Web.ClaimsPrincipalFactory.FromUsernamePassword(string! username, string! password) -> System.Security.Claims.ClaimsPrincipal! | ||
Check warning on line 2 in src/Microsoft.Identity.Web/PublicAPI/net462/PublicAPI.Unshipped.txt GitHub Actions / Build and run unit tests
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#nullable enable | ||
static Microsoft.Identity.Web.ClaimsPrincipalFactory.FromUsernamePassword(string! username, string! password) -> System.Security.Claims.ClaimsPrincipal! | ||
Check warning on line 2 in src/Microsoft.Identity.Web/PublicAPI/net472/PublicAPI.Unshipped.txt GitHub Actions / Build and run unit tests
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#nullable enable | ||
static Microsoft.Identity.Web.ClaimsPrincipalFactory.FromUsernamePassword(string! username, string! password) -> System.Security.Claims.ClaimsPrincipal! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#nullable enable | ||
static Microsoft.Identity.Web.ClaimsPrincipalFactory.FromUsernamePassword(string! username, string! password) -> System.Security.Claims.ClaimsPrincipal! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#nullable enable | ||
static Microsoft.Identity.Web.ClaimsPrincipalFactory.FromUsernamePassword(string! username, string! password) -> System.Security.Claims.ClaimsPrincipal! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#nullable enable | ||
static Microsoft.Identity.Web.ClaimsPrincipalFactory.FromUsernamePassword(string! username, string! password) -> System.Security.Claims.ClaimsPrincipal! |
Oops, something went wrong.