Align .editorconfig files
When debugging only app.Services.GetRequiredService<IPCRBService>(); Injected AppSettings instead of using GetEnvironmentVariable at Services level Get ready to use VSCode IDE
This commit is contained in:
@ -7,6 +7,7 @@ using System.Security.Principal;
|
||||
using System.Text;
|
||||
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Models;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
@ -30,17 +31,13 @@ public class AuthenticationService : IAuthenticationService {
|
||||
private readonly string _jwtAudience;
|
||||
private readonly string _jwtKey;
|
||||
|
||||
public AuthenticationService(ILogger<AuthenticationService> logger, IMemoryCache cache, IUserService userService) {
|
||||
public AuthenticationService(ILogger<AuthenticationService> logger, IMemoryCache cache, IUserService userService, AppSettings appSettings) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_cache = cache ?? throw new ArgumentNullException("IMemoryCache not injected");
|
||||
_userService = userService ?? throw new ArgumentNullException("IUserService not injected");
|
||||
|
||||
_jwtIssuer = Environment.GetEnvironmentVariable("FabApprovalJwtIssuer") ??
|
||||
throw new ArgumentNullException("FabApprovalJwtIssuer environment variable not found");
|
||||
_jwtAudience = Environment.GetEnvironmentVariable("FabApprovalJwtAudience") ??
|
||||
throw new ArgumentNullException("FabApprovalJwtAudience environment variable not found");
|
||||
_jwtKey = Environment.GetEnvironmentVariable("FabApprovalJwtKey") ??
|
||||
throw new ArgumentNullException("FabApprovalJwtKey environment variable not found");
|
||||
_jwtKey = appSettings.JwtKey;
|
||||
_jwtIssuer = appSettings.JwtIssuer;
|
||||
_jwtAudience = appSettings.JwtAudience;
|
||||
}
|
||||
|
||||
public async Task<LoginResult> AuthenticateUser(AuthAttempt login) {
|
||||
@ -59,7 +56,7 @@ public class AuthenticationService : IAuthenticationService {
|
||||
|
||||
if (user is null) {
|
||||
user = await _userService.GetUserByLoginId(login.LoginID);
|
||||
|
||||
|
||||
_cache.Set<User>($"user{login.LoginID}", user, DateTimeOffset.Now.AddDays(1));
|
||||
}
|
||||
|
||||
@ -77,8 +74,8 @@ public class AuthenticationService : IAuthenticationService {
|
||||
};
|
||||
} else {
|
||||
return new LoginResult() {
|
||||
IsAuthenticated= false,
|
||||
AuthTokens = new() {
|
||||
IsAuthenticated = false,
|
||||
AuthTokens = new() {
|
||||
JwtToken = "",
|
||||
RefreshToken = ""
|
||||
},
|
||||
@ -105,7 +102,7 @@ public class AuthenticationService : IAuthenticationService {
|
||||
if (user.IsManager) roles.Add("manager");
|
||||
if (user.IsAdmin) roles.Add("admin");
|
||||
|
||||
AuthAttempt authAttempt = new() {
|
||||
AuthAttempt authAttempt = new() {
|
||||
LoginID = user.LoginID,
|
||||
};
|
||||
|
||||
@ -132,7 +129,7 @@ public class AuthenticationService : IAuthenticationService {
|
||||
|
||||
byte[] key = Encoding.ASCII.GetBytes(_jwtKey);
|
||||
|
||||
List<Claim> claims = new() {
|
||||
List<Claim> claims = new() {
|
||||
new Claim(nameof(authAttempt.LoginID), authAttempt.LoginID)
|
||||
};
|
||||
|
||||
@ -171,7 +168,7 @@ public class AuthenticationService : IAuthenticationService {
|
||||
|
||||
_cache.Set<List<string>>(authAttempt.LoginID, refreshTokensForUser, DateTimeOffset.Now.AddHours(4));
|
||||
|
||||
return new AuthTokens {
|
||||
return new AuthTokens {
|
||||
JwtToken = jwt,
|
||||
RefreshToken = refreshToken
|
||||
};
|
||||
@ -251,4 +248,4 @@ public class AuthenticationService : IAuthenticationService {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user