Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VCST-1431: Ignore expired password when using external sign in #85

Merged
merged 5 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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);
Expand All @@ -42,8 +44,11 @@ public UserType(IContactAggregateRepository contactAggregateRepository, IUserMan
Field(x => x.TwoFactorEnabled);
Field(x => x.UserName);
Field(x => x.UserType, true);
Field<BooleanGraphType>("forcePasswordChange", resolve: x => x.Source.PasswordExpired, description: "Make this user change their password when they sign in next time");
Field<IntGraphType>("passwordExpiryInDays", resolve: x => GetPasswordExpiryInDays(userOptionsExtended.Value, x.Source), description: "Password expiry in days");

Field<NonNullGraphType<BooleanGraphType>>("passwordExpired", resolve: x => GetPasswordExpired(x));
Field<BooleanGraphType>("forcePasswordChange", resolve: x => GetPasswordExpired(x), description: "Make this user change their password when they sign in next time");
Field<IntGraphType>("passwordExpiryInDays", resolve: x => GetPasswordExpiryInDays(x, userOptionsExtended.Value), description: "Password expiry in days");


AddField(new FieldType
{
Expand Down Expand Up @@ -91,11 +96,19 @@ public UserType(IContactAggregateRepository contactAggregateRepository, IUserMan
});
}

private static int? GetPasswordExpiryInDays(UserOptionsExtended userOptionsExtended, ApplicationUser user)
private static bool GetPasswordExpired(IResolveFieldContext<ApplicationUser> context)
{
return context.Source.PasswordExpired && !IsExternalSignIn(context);
}

private static int? GetPasswordExpiryInDays(IResolveFieldContext<ApplicationUser> 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)
Expand All @@ -112,5 +125,10 @@ public UserType(IContactAggregateRepository contactAggregateRepository, IUserMan

return result;
}

private static bool IsExternalSignIn(IResolveFieldContext<ApplicationUser> context)
{
return context.Source.Id == context.GetCurrentUserId() && context.GetCurrentPrincipal().IsExternalSignIn();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="VirtoCommerce.Xapi.Core" Version="3.800.0" />
<PackageReference Include="VirtoCommerce.Xapi.Core" Version="3.802.0" />
<PackageReference Include="VirtoCommerce.XOrder.Core" Version="3.800.0" />
<PackageReference Include="VirtoCommerce.Platform.Security" Version="3.841.0" />
<PackageReference Include="VirtoCommerce.Platform.Security" Version="3.848.0" />
<PackageReference Include="VirtoCommerce.CustomerModule.Core" Version="3.811.0" />
<PackageReference Include="VirtoCommerce.MarketingModule.Core" Version="3.800.0" />
<PackageReference Include="VirtoCommerce.NotificationsModule.Core" Version="3.800.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<version>3.813.0</version>
<version-tag />

<platformVersion>3.841.0</platformVersion>
<platformVersion>3.848.0</platformVersion>
<dependencies>
<dependency id="VirtoCommerce.Xapi" version="3.800.0" />
<dependency id="VirtoCommerce.Xapi" version="3.802.0" />
<dependency id="VirtoCommerce.XOrder" version="3.800.0" optional="true" />
<dependency id="VirtoCommerce.Core" version="3.800.0" />
<dependency id="VirtoCommerce.Customer" version="3.811.0" />
Expand Down
Loading