-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom allow list options for IP based 2fa selection
- Loading branch information
1 parent
599ee30
commit 58b8544
Showing
5 changed files
with
59 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Cfo.Cats.Infrastructure.Services.Identity; | ||
|
||
public class AllowlistOptions | ||
{ | ||
public List<string> AllowedIPs { get; set; } = new(); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Infrastructure/Services/Identity/CustomSigninManager.cs
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,33 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Cfo.Cats.Infrastructure.Services.Identity; | ||
|
||
public class CustomSigninManager<TUser>(UserManager<TUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<TUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<TUser>> logger, IAuthenticationSchemeProvider schemes, IUserConfirmation<TUser> confirmation, IHttpContextAccessor httpContextAccessor, IOptions<AllowlistOptions> allowlistOptions) | ||
: SignInManager<TUser>(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes, confirmation) | ||
where TUser : class | ||
{ | ||
public override async Task<SignInResult> PasswordSignInAsync(string userName, string password, bool isPersistent, bool lockoutOnFailure) | ||
{ | ||
var user = await UserManager.FindByNameAsync(userName); | ||
|
||
if (user == null) | ||
{ | ||
return SignInResult.Failed; | ||
} | ||
|
||
var ipAddress = httpContextAccessor.HttpContext!.Connection.RemoteIpAddress?.ToString(); | ||
if (string.IsNullOrWhiteSpace(ipAddress) == false && allowlistOptions.Value.AllowedIPs.Contains(ipAddress)) | ||
{ | ||
var result = await CheckPasswordSignInAsync(user, password, lockoutOnFailure); | ||
if (result.Succeeded) | ||
{ | ||
await SignInAsync(user, isPersistent); | ||
} | ||
return result; | ||
} | ||
return await base.PasswordSignInAsync(userName, password, isPersistent, lockoutOnFailure); | ||
} | ||
|
||
|
||
} |
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,7 +1,8 @@ | ||
{ | ||
/* "DatabaseSettings": { | ||
"DbProvider": "sqlite", | ||
"ConnectionString": "Data Source=./cats.db;" | ||
},*/ | ||
"DetailedErrors": true | ||
"DetailedErrors": true, | ||
"AllowlistOptions": { | ||
"AllowedIPs": [ | ||
"::1" | ||
] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -49,5 +49,10 @@ | |
"ApiKey": "", | ||
"SmsTemplate": "", | ||
"EmailTemplate": "" | ||
}, | ||
"AllowlistOptions": { | ||
"AllowedIPs": [ | ||
|
||
] | ||
} | ||
} |