Skip to content

Commit 1644937

Browse files
authored
Merge pull request #723 from bcgov/yj
chore: bcsc
2 parents ddb0db6 + 63f691d commit 1644937

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

server/StrDss.Api/Authorization/ApiAuthorizeAttribute.cs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.AspNetCore.Mvc.Filters;
4+
using Microsoft.IdentityModel.Protocols.WsTrust;
45
using StrDss.Common;
6+
using System.Security.Claims;
57

68
namespace StrDss.Api.Authorization
79
{
@@ -21,7 +23,8 @@ public void OnAuthorization(AuthorizationFilterContext context)
2123
_logger = loggerFactory.CreateLogger<StrDssLogger>();
2224

2325
var user = context.HttpContext.User;
24-
var username = user?.Identity?.Name ?? "Unknown";
26+
var username = user?.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? user?.FindFirst(ClaimTypes.Name)?.Value ?? "Unknown";
27+
2528
var ipAddress = context.HttpContext.Connection.RemoteIpAddress;
2629
var ip = ipAddress == null ? "Unknown" : ipAddress.ToString();
2730

@@ -39,33 +42,14 @@ public void OnAuthorization(AuthorizationFilterContext context)
3942
if (identityProviderNm == "" && clientId != "")
4043
{
4144
identityProviderNm = StrDssIdProviders.Aps;
45+
username = clientId;
4246
}
4347

4448
var displayName = user.GetCustomClaim(StrDssClaimTypes.DisplayName);
4549

46-
string userId;
47-
switch (identityProviderNm)
48-
{
49-
case StrDssIdProviders.Idir:
50-
userId = user.GetCustomClaim(StrDssClaimTypes.IdirUserGuid);
51-
break;
52-
case StrDssIdProviders.BceidBusiness:
53-
userId = user.GetCustomClaim(StrDssClaimTypes.BceidUserGuid);
54-
break;
55-
case StrDssIdProviders.StrDss:
56-
userId = user.GetCustomClaim(StrDssClaimTypes.StrDssUserGuid);
57-
break;
58-
case StrDssIdProviders.Aps:
59-
userId = clientId;
60-
break;
61-
default:
62-
userId = "Unknown";
63-
break;
64-
}
65-
6650
if (_permissions.Length == 0)
6751
{
68-
_logger.LogInformation($"[AUTH] User '{userId}' is authorized to access {context.ActionDescriptor.DisplayName} from IP address {ip}.");
52+
_logger.LogInformation($"[AUTH] User '{username}' is authorized to access {context.ActionDescriptor.DisplayName} from IP address {ip}.");
6953
return;
7054
}
7155

@@ -82,12 +66,12 @@ public void OnAuthorization(AuthorizationFilterContext context)
8266

8367
if (!hasPermission)
8468
{
85-
_logger.LogInformation($"[AUTH] User '{userId}' does not have permission to access {context.ActionDescriptor.DisplayName} from IP address {ip}.");
69+
_logger.LogInformation($"[AUTH] User '{username}' does not have permission to access {context.ActionDescriptor.DisplayName} from IP address {ip}.");
8670
context.Result = new UnauthorizedResult(); //401
8771
return;
8872
}
8973

90-
_logger.LogInformation($"[AUTH] User '{userId}' is authorized to access {context.ActionDescriptor.DisplayName} from IP address {ip}.");
74+
_logger.LogInformation($"[AUTH] User '{username}' is authorized to access {context.ActionDescriptor.DisplayName} from IP address {ip}.");
9175
}
9276
}
9377
}

server/StrDss.Data/Repositories/UserRepository.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface IUserRepository
1818
Task<(UserDto? user, List<string> permissions)> GetUserAndPermissionsByDisplayNameAsync(string displayName);
1919
Task<UserDto?> GetUserById(long id);
2020
Task<UserDto?> GetUserByGuid(Guid guid);
21+
Task<UserDto?> GetUserByCurrentUser();
2122
Task UpdateUserAsync(UserDto dto);
2223
Task UpdateUserAsync(UserUpdateDto dto);
2324
Task DenyAccessRequest(AccessRequestDenyDto dto);
@@ -186,6 +187,22 @@ public async Task CreateUserAsync(UserCreateDto dto)
186187
return _mapper.Map<UserDto>(entity);
187188
}
188189

190+
public async Task<UserDto?> GetUserByCurrentUser()
191+
{
192+
DssUserIdentity? entity;
193+
194+
if (_currentUser.IsBcServicesCard)
195+
{
196+
entity = await _dbSet.AsNoTracking().FirstOrDefaultAsync(x => x.ExternalIdentityCd == _currentUser.ExternalIdentityCd);
197+
}
198+
else
199+
{
200+
entity = await _dbSet.AsNoTracking().FirstOrDefaultAsync(x => x.UserGuid == _currentUser.UserGuid);
201+
}
202+
203+
return _mapper.Map<UserDto>(entity);
204+
}
205+
189206
public async Task UpdateUserAsync(UserDto dto)
190207
{
191208
var entity = await _dbSet.FirstAsync(x => x.UserIdentityId == dto.UserIdentityId);

server/StrDss.Model/CurrentUser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public void LoadUserSession(ClaimsPrincipal user)
6969
FirstName = textInfo.ToTitleCase(user.GetCustomClaim(ClaimTypes.GivenName));
7070
LastName = textInfo.ToTitleCase(user.GetCustomClaim(ClaimTypes.Surname));
7171
DisplayName = user.GetCustomClaim(StrDssClaimTypes.DisplayName);
72-
ExternalIdentityCd = user.GetCustomClaim(StrDssClaimTypes.Sub);
73-
IsBcServicesCard = ExternalIdentityCd == Environment.GetEnvironmentVariable("SSO_CLIENT");
72+
ExternalIdentityCd = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
73+
IsBcServicesCard = IdentityProviderNm == Environment.GetEnvironmentVariable("SSO_CLIENT");
7474

7575
switch (IdentityProviderNm)
7676
{

server/StrDss.Service/UserService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public async Task<Dictionary<string, List<string>>> CreateAccessRequestAsync(Acc
147147

148148
_unitOfWork.Commit();
149149

150-
var user = await _userRepo.GetUserByGuid(_currentUser.UserGuid);
150+
var user = await _userRepo.GetUserByCurrentUser();
151151

152152
var adminUsers = await _userRepo.GetAdminUsers();
153153

@@ -197,7 +197,7 @@ public async Task<Dictionary<string, List<string>>> CreateAccessRequestAsync(Acc
197197
{
198198
var errors = new Dictionary<string, List<string>>();
199199

200-
var userDto = await _userRepo.GetUserByGuid(_currentUser.UserGuid);
200+
var userDto = await _userRepo.GetUserByCurrentUser();
201201
if (userDto != null)
202202
{
203203
if (userDto.AccessRequestStatusCd == AccessRequestStatuses.Requested)

0 commit comments

Comments
 (0)