diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Schemas/ProfileSchema.cs b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Schemas/ProfileSchema.cs index 044758bc..eeefe175 100644 --- a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Schemas/ProfileSchema.cs +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Schemas/ProfileSchema.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Logging; using VirtoCommerce.Platform.Core.Security; using VirtoCommerce.Platform.Security.Authorization; +using VirtoCommerce.Platform.Security.Extensions; using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates; using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Contact; using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Organization; @@ -756,7 +757,9 @@ userName isAdministrator roles { name } userType memberId storeId // PT-1654: Fix Authentication public async Task CheckAuthAsync(IResolveFieldContext context, object resource, string permission = null, bool checkPasswordExpired = true) { - var userId = context.GetCurrentUserId(); + var principal = context.GetCurrentPrincipal(); + var userId = principal.GetCurrentUserId(); + var isExternalSignIn = principal.IsExternalSignIn(); var signInManager = _signInManagerFactory(); try @@ -767,11 +770,12 @@ public async Task CheckAuthAsync(IResolveFieldContext context, object resource, UserName = Xapi.Core.ModuleConstants.AnonymousUser.UserName, }; - if (checkPasswordExpired && user.PasswordExpired) + if (checkPasswordExpired && user.PasswordExpired && !isExternalSignIn) { throw AuthorizationError.PasswordExpired(); } + // Why do we create a new principal??? var userPrincipal = await signInManager.CreateUserPrincipalAsync(user); if (!string.IsNullOrEmpty(permission) && PermissionRequired(user, resource)) diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Schemas/UserType.cs b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Schemas/UserType.cs index 9ec29e88..8cc36d6f 100644 --- a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Schemas/UserType.cs +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Schemas/UserType.cs @@ -1,15 +1,18 @@ using System; using System.Linq; using System.Threading.Tasks; +using GraphQL; using GraphQL.Resolvers; using GraphQL.Types; using MediatR; using Microsoft.Extensions.Options; -using VirtoCommerce.Xapi.Core.Helpers; -using VirtoCommerce.Xapi.Core.Services; using VirtoCommerce.Platform.Core.Security; +using VirtoCommerce.Platform.Security.Extensions; using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Contact; using VirtoCommerce.ProfileExperienceApiModule.Data.Queries; +using VirtoCommerce.Xapi.Core.Extensions; +using VirtoCommerce.Xapi.Core.Helpers; +using VirtoCommerce.Xapi.Core.Services; namespace VirtoCommerce.ProfileExperienceApiModule.Data.Schemas { @@ -31,7 +34,6 @@ public UserType(IContactAggregateRepository contactAggregateRepository, IUserMan Field(x => x.ModifiedDate, true); Field(x => x.NormalizedEmail, true); Field(x => x.NormalizedUserName, true); - Field(x => x.PasswordExpired); Field(x => x.PhoneNumber, true); Field(x => x.PhoneNumberConfirmed); Field(x => x.PhotoUrl, true); @@ -42,8 +44,11 @@ public UserType(IContactAggregateRepository contactAggregateRepository, IUserMan Field(x => x.TwoFactorEnabled); Field(x => x.UserName); Field(x => x.UserType, true); - Field("forcePasswordChange", resolve: x => x.Source.PasswordExpired, description: "Make this user change their password when they sign in next time"); - Field("passwordExpiryInDays", resolve: x => GetPasswordExpiryInDays(userOptionsExtended.Value, x.Source), description: "Password expiry in days"); + + Field>("passwordExpired", resolve: x => GetPasswordExpired(x)); + Field("forcePasswordChange", resolve: x => GetPasswordExpired(x), description: "Make this user change their password when they sign in next time"); + Field("passwordExpiryInDays", resolve: x => GetPasswordExpiryInDays(x, userOptionsExtended.Value), description: "Password expiry in days"); + AddField(new FieldType { @@ -91,11 +96,19 @@ public UserType(IContactAggregateRepository contactAggregateRepository, IUserMan }); } - private static int? GetPasswordExpiryInDays(UserOptionsExtended userOptionsExtended, ApplicationUser user) + private static bool GetPasswordExpired(IResolveFieldContext context) + { + return context.Source.PasswordExpired && !IsExternalSignIn(context); + } + + private static int? GetPasswordExpiryInDays(IResolveFieldContext context, UserOptionsExtended userOptionsExtended) { var result = (int?)null; + var user = context.Source; + if (!user.PasswordExpired && + !IsExternalSignIn(context) && userOptionsExtended.RemindPasswordExpiryInDays > 0 && userOptionsExtended.MaxPasswordAge != null && userOptionsExtended.MaxPasswordAge.Value > TimeSpan.Zero) @@ -112,5 +125,10 @@ public UserType(IContactAggregateRepository contactAggregateRepository, IUserMan return result; } + + private static bool IsExternalSignIn(IResolveFieldContext context) + { + return context.Source.Id == context.GetCurrentUserId() && context.GetCurrentPrincipal().IsExternalSignIn(); + } } } diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Data/VirtoCommerce.ProfileExperienceApiModule.Data.csproj b/src/VirtoCommerce.ProfileExperienceApiModule.Data/VirtoCommerce.ProfileExperienceApiModule.Data.csproj index 6ae397a1..388e5700 100644 --- a/src/VirtoCommerce.ProfileExperienceApiModule.Data/VirtoCommerce.ProfileExperienceApiModule.Data.csproj +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Data/VirtoCommerce.ProfileExperienceApiModule.Data.csproj @@ -19,9 +19,9 @@ - + - + diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Web/module.manifest b/src/VirtoCommerce.ProfileExperienceApiModule.Web/module.manifest index c3c946a3..6d0d0596 100644 --- a/src/VirtoCommerce.ProfileExperienceApiModule.Web/module.manifest +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Web/module.manifest @@ -4,9 +4,9 @@ 3.813.0 - 3.841.0 + 3.848.0 - +