Skip to content

Commit

Permalink
Fixed the issue that accessing api failed
Browse files Browse the repository at this point in the history
  • Loading branch information
bcgov-brwang committed Feb 13, 2023
1 parent 4b39b06 commit c12f2f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
22 changes: 18 additions & 4 deletions api/Crt.Api/Authentication/CrtJwtBearerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,25 @@ private async Task<bool> PopulateCurrentUserFromDb(ClaimsPrincipal principal)
_ = bool.TryParse(principal.FindFirstValue(CrtClaimTypes.KcIsApiClient), out bool isApiClient);
isApiClient = true;
var preferredUsername = principal.FindFirstValue(CrtClaimTypes.PreferredUsername);
var usernames = preferredUsername.Split("@");
var username = usernames[0].ToUpperInvariant();
var userGuid = new Guid(Guid.Parse(username).ToString());
string[] usernames = null;
var username = "";
var userGuid = new Guid("00000000-0000-0000-0000-000000000000");
var email = "";
if (preferredUsername.Contains("@"))
{
usernames = preferredUsername.Split("@");
username = usernames[0].ToUpperInvariant();
userGuid = new Guid(Guid.Parse(username).ToString());
email = principal.FindFirstValue(ClaimTypes.Email).ToUpperInvariant();
}
else
{
username = principal.FindFirstValue(CrtClaimTypes.KcClientId).ToUpperInvariant();
userGuid = new Guid(principal.FindFirstValue("idir_userid")?.ToUpperInvariant());
email = principal.FindFirstValue(ClaimTypes.Email)?.ToUpperInvariant();
}

var email = principal.FindFirstValue(ClaimTypes.Email).ToUpperInvariant();


var user = await _userService.GetActiveUserEntityAsync(userGuid);
if (user == null)
Expand Down
1 change: 1 addition & 0 deletions api/Crt.Model/CrtClaimTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public static class CrtClaimTypes
public const string KcUsername = "idir_username";
public const string KcIdirGuid = "idir_user_guid";
public const string KcIsApiClient = "api_client";
public const string KcClientId = "clientId";
public const string KcApiUsername = "username";
public const string PreferredUsername = "preferred_username";
public const string Permission = "CRT_PERMISSION";
Expand Down

0 comments on commit c12f2f1

Please sign in to comment.