Skip to content

Commit

Permalink
Add logging to serversidetokenstore
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdecock committed Aug 16, 2024
1 parent f5d8786 commit 560550b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Duende.Bff.Blazor/ServerSideTokenStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
using Duende.AccessTokenManagement.OpenIdConnect;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Logging; // TODO - Add useful logging to this class

using Microsoft.Extensions.Logging;
namespace Duende.Bff.Blazor;

/// <summary>
Expand All @@ -21,6 +20,7 @@ public class ServerSideTokenStore(
private readonly IDataProtector protector = dataProtectionProvider.CreateProtector(ServerSideTicketStore.DataProtectorPurpose);
public async Task<UserToken> GetTokenAsync(ClaimsPrincipal user, UserTokenRequestParameters? parameters = null)
{
logger.LogDebug("Retrieving token for user {user}", user.Identity?.Name);
var session = await GetSession(user);
var ticket = session.Deserialize(protector, logger) ?? throw new InvalidOperationException("Failed to deserialize authentication ticket from session");

Expand All @@ -32,6 +32,8 @@ private async Task<UserSession> GetSession(ClaimsPrincipal user)
var sub = user.FindFirst("sub")?.Value ?? throw new InvalidOperationException("no sub claim");
var sid = user.FindFirst("sid")?.Value ?? throw new InvalidOperationException("no sid claim");

logger.LogDebug("Retrieving session {sid} for sub {sub}", sid, sub);

var sessions = await sessionStore.GetUserSessionsAsync(new UserSessionsFilter
{
SubjectId = sub,
Expand All @@ -46,6 +48,7 @@ private async Task<UserSession> GetSession(ClaimsPrincipal user)

public async Task StoreTokenAsync(ClaimsPrincipal user, UserToken token, UserTokenRequestParameters? parameters = null)
{
logger.LogDebug("Storing token for user {user}", user.Identity?.Name);
await UpdateTicket(user, ticket =>
{
tokensInAuthProperties.SetUserToken(token, ticket.Properties, parameters);
Expand All @@ -54,6 +57,7 @@ await UpdateTicket(user, ticket =>

public async Task ClearTokenAsync(ClaimsPrincipal user, UserTokenRequestParameters? parameters = null)
{
logger.LogDebug("Removing token for user {user}", user.Identity?.Name);
await UpdateTicket(user, ticket =>
{
tokensInAuthProperties.RemoveUserToken(ticket.Properties, parameters);
Expand Down

0 comments on commit 560550b

Please sign in to comment.