diff --git a/server/StrDss.Api/Authentication/KcJwtBearerEvents.cs b/server/StrDss.Api/Authentication/KcJwtBearerEvents.cs index 52fd1666..dd8da52e 100644 --- a/server/StrDss.Api/Authentication/KcJwtBearerEvents.cs +++ b/server/StrDss.Api/Authentication/KcJwtBearerEvents.cs @@ -2,6 +2,7 @@ using StrDss.Common; using StrDss.Model; using StrDss.Service; +using StrDss.Service.Bceid; namespace StrDss.Api.Authentication { @@ -9,12 +10,14 @@ public class KcJwtBearerEvents : JwtBearerEvents { private ICurrentUser _currentUser; private IUserService _userService; + private IBceidApi _bceid; private ILogger _logger; - public KcJwtBearerEvents(ICurrentUser currentUser, IUserService userService, ILogger logger) : base() + public KcJwtBearerEvents(ICurrentUser currentUser, IUserService userService, IBceidApi bceid, ILogger logger) : base() { _currentUser = currentUser; _userService = userService; + _bceid = bceid; _logger = logger; } @@ -74,6 +77,31 @@ public override async Task TokenValidated(TokenValidatedContext context) _currentUser.AddClaim(context!.Principal!, StrDssClaimTypes.Permission, permission); } } + + if (user.IdentityProviderNm == StrDssIdProviders.BceidBusiness && (int)(DateTime.UtcNow - user!.UpdDtm).TotalDays > 1) + { + try + { + var (error, account) = await _bceid.GetBceidAccountCachedAsync(_currentUser.UserGuid, "", StrDssIdProviders.BceidBusiness, _currentUser.UserGuid, _currentUser.IdentityProviderNm); + + if (account == null) + { + _logger.LogError($"BCeID call error: {error}"); + } + + if (account != null) + { + _currentUser.FirstName = account.FirstName; + _currentUser.LastName = account.LastName; + + await _userService.UpdateBceidUserInfo(user.UserIdentityId, account.FirstName, account.LastName); + } + } + catch + { + _logger.LogInformation("BCeID Web call failed - Skipping UpdateBceidUserInfo "); + } + } } } } diff --git a/server/StrDss.Data/Repositories/UserRepository.cs b/server/StrDss.Data/Repositories/UserRepository.cs index 4225f4a2..d5433283 100644 --- a/server/StrDss.Data/Repositories/UserRepository.cs +++ b/server/StrDss.Data/Repositories/UserRepository.cs @@ -22,6 +22,7 @@ public interface IUserRepository Task UpdateIsEnabled(UpdateIsEnabledDto dto); Task> GetAccessRequestStatuses(); Task AcceptTermsConditions(); + Task UpdateUserNamesAsync(long userId, string firstName, string lastName); } public class UserRepository : RepositoryBase, IUserRepository { @@ -158,5 +159,11 @@ public async Task AcceptTermsConditions() if(entity != null) entity.TermsAcceptanceDtm = DateTime.UtcNow; } + public async Task UpdateUserNamesAsync(long userId, string firstName, string lastName) + { + var entity = await _dbSet.FirstAsync(x => x.UserIdentityId == userId); + entity.FamilyNm = lastName; + entity.GivenNm = firstName; + } } } diff --git a/server/StrDss.Service/Bceid/BceidApi.cs b/server/StrDss.Service/Bceid/BceidApi.cs index e355e6dd..9d5a81ca 100644 --- a/server/StrDss.Service/Bceid/BceidApi.cs +++ b/server/StrDss.Service/Bceid/BceidApi.cs @@ -43,7 +43,7 @@ private void RefreshCache(object source, ElapsedEventArgs e) try { - var key = username + "||" + userType; + var key = userGuid?.ToString("N") + "||" + userType; if (_accountCache.ContainsKey(key)) { Debug.WriteLine($"BCeID cache hit: {key}"); diff --git a/server/StrDss.Service/UserService.cs b/server/StrDss.Service/UserService.cs index b6922c67..9f8711cb 100644 --- a/server/StrDss.Service/UserService.cs +++ b/server/StrDss.Service/UserService.cs @@ -23,6 +23,7 @@ public interface IUserService Task>> UpdateIsEnabled(UpdateIsEnabledDto dto); Task> GetAccessRequestStatuses(); Task>> AcceptTermsConditions(); + Task UpdateBceidUserInfo(long userId, string firstName, string LastName); } public class UserService : ServiceBase, IUserService { @@ -434,5 +435,12 @@ public async Task>> AcceptTermsConditions() return errors; } + + public async Task UpdateBceidUserInfo(long userId, string firstName, string LastName) + { + await _userRepo.UpdateUserNamesAsync(userId, firstName, LastName); + + _unitOfWork.Commit(); + } } }