Skip to content

Commit

Permalink
Added eventlog implementation for refresh and token exchange operations
Browse files Browse the repository at this point in the history
  • Loading branch information
acn-dgopa committed Sep 8, 2023
1 parent efbdaa8 commit c0c6a05
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Authentication/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ public async Task<ActionResult> RefreshJwtCookie()

string serializedToken = await GenerateToken(principal);

UserAuthenticationModel userAuthentication = AuthenticationHelper.GetUserFromToken(serializedToken, null);
EventlogHelper.CreateAuthenticationEvent(_featureManager, _eventLog, userAuthentication, AuthenticationEventType.Refresh);
_logger.LogInformation("End of refreshing token");

return Ok(serializedToken);
Expand Down Expand Up @@ -365,6 +367,8 @@ private async Task<ActionResult> AuthenticateAltinnStudioToken(string originalTo
ClaimsPrincipal principal = new ClaimsPrincipal(identity);

string serializedToken = await GenerateToken(principal);
UserAuthenticationModel userAuthentication = AuthenticationHelper.GetUserFromToken(serializedToken, null);
EventlogHelper.CreateAuthenticationEvent(_featureManager, _eventLog, userAuthentication, AuthenticationEventType.TokenExchange);
return Ok(serializedToken);
}
catch (Exception ex)
Expand Down Expand Up @@ -484,6 +488,8 @@ private async Task<ActionResult> AuthenticateMaskinportenToken(string originalTo
ClaimsPrincipal principal = new ClaimsPrincipal(identity);

string serializedToken = await GenerateToken(principal);
UserAuthenticationModel userAuthentication = AuthenticationHelper.GetUserFromToken(serializedToken, null);
EventlogHelper.CreateAuthenticationEvent(_featureManager, _eventLog, userAuthentication, AuthenticationEventType.TokenExchange);
return Ok(serializedToken);
}
catch (Exception ex)
Expand Down Expand Up @@ -617,6 +623,8 @@ private async Task<ActionResult> AuthenticateIdPortenToken(string originalToken)
ClaimsPrincipal principal = new ClaimsPrincipal(identity);

string serializedToken = await GenerateToken(principal, token.ValidTo);
UserAuthenticationModel userAuthentication = AuthenticationHelper.GetUserFromToken(serializedToken, null);
EventlogHelper.CreateAuthenticationEvent(_featureManager, _eventLog, userAuthentication, AuthenticationEventType.TokenExchange);
return Ok(serializedToken);
}
catch (Exception ex)
Expand Down
4 changes: 3 additions & 1 deletion src/Authentication/Enum/AuthenticationEventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public enum AuthenticationEventType
{
AuthenticationFailed,
Authenticated,
Logout
Refresh,
TokenExchange,
Logout,
}
}
18 changes: 18 additions & 0 deletions src/Authentication/Helpers/AuthenticationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ public static UserAuthenticationModel GetUserFromToken(JwtSecurityToken jwtSecur
return userAuthenticationModel;
}

/// <summary>
/// Get user information from the serializwd token string
/// </summary>
/// <param name="jwtToken">serialized jwt token string</param>
/// <param name="provider">token provider</param>
/// <returns></returns>
public static UserAuthenticationModel GetUserFromToken(string jwtToken, OidcProvider provider)
{
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
if (!string.IsNullOrEmpty(jwtToken))
{
JwtSecurityToken token = tokenHandler.ReadJwtToken(jwtToken);
return GetUserFromToken(token, provider);
}

return null;
}

/// <summary>
/// Converts IDporten acr claim �Authentication Context Class Reference� - The security level of assurance for the
/// authentication. Possible values are Level3 (i.e. MinID was used) or Level4 (other eIDs).
Expand Down

0 comments on commit c0c6a05

Please sign in to comment.