Skip to content

Commit

Permalink
Merge pull request #250 from bcgov/yj
Browse files Browse the repository at this point in the history
fix: bceid cache not working
  • Loading branch information
ychung-mot authored May 1, 2024
2 parents 795d545 + 1147a71 commit cca64ac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
30 changes: 29 additions & 1 deletion server/StrDss.Api/Authentication/KcJwtBearerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
using StrDss.Common;
using StrDss.Model;
using StrDss.Service;
using StrDss.Service.Bceid;

namespace StrDss.Api.Authentication
{
public class KcJwtBearerEvents : JwtBearerEvents
{
private ICurrentUser _currentUser;
private IUserService _userService;
private IBceidApi _bceid;
private ILogger<StrDssLogger> _logger;

public KcJwtBearerEvents(ICurrentUser currentUser, IUserService userService, ILogger<StrDssLogger> logger) : base()
public KcJwtBearerEvents(ICurrentUser currentUser, IUserService userService, IBceidApi bceid, ILogger<StrDssLogger> logger) : base()
{
_currentUser = currentUser;
_userService = userService;
_bceid = bceid;
_logger = logger;
}

Expand Down Expand Up @@ -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 ");
}
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions server/StrDss.Data/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public interface IUserRepository
Task UpdateIsEnabled(UpdateIsEnabledDto dto);
Task<List<DropdownStrDto>> GetAccessRequestStatuses();
Task AcceptTermsConditions();
Task UpdateUserNamesAsync(long userId, string firstName, string lastName);
}
public class UserRepository : RepositoryBase<DssUserIdentity>, IUserRepository
{
Expand Down Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion server/StrDss.Service/Bceid/BceidApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
8 changes: 8 additions & 0 deletions server/StrDss.Service/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface IUserService
Task<Dictionary<string, List<string>>> UpdateIsEnabled(UpdateIsEnabledDto dto);
Task<List<DropdownStrDto>> GetAccessRequestStatuses();
Task<Dictionary<string, List<string>>> AcceptTermsConditions();
Task UpdateBceidUserInfo(long userId, string firstName, string LastName);
}
public class UserService : ServiceBase, IUserService
{
Expand Down Expand Up @@ -434,5 +435,12 @@ public async Task<Dictionary<string, List<string>>> AcceptTermsConditions()

return errors;
}

public async Task UpdateBceidUserInfo(long userId, string firstName, string LastName)
{
await _userRepo.UpdateUserNamesAsync(userId, firstName, LastName);

_unitOfWork.Commit();
}
}
}

0 comments on commit cca64ac

Please sign in to comment.