Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -113,7 +114,7 @@ await persistedGrantStore.RemoveAllAsync(new PersistedGrantFilter
existingSession.Scheme = ticket.AuthenticationScheme;
existingSession.SubjectId = ticket.Principal.GetSubjectId();
existingSession.SessionId = ticket.Properties.GetSessionId();
existingSession.DisplayName = ticket.Principal.FindFirstValue(JwtClaimTypes.Name);
existingSession.DisplayName = GetDisplayNameValue(ticket.Principal);
existingSession.Renewed = issued;
existingSession.Expires = ticket.Properties.ExpiresUtc?.UtcDateTime;
existingSession.Data = ToProtectedDataString(ticket);
Expand Down Expand Up @@ -224,10 +225,8 @@ private async Task<ServerSideSession> StoreNewSession(string key, Authentication
{
ticket.Properties.Items[JwtClaimTypes.Issuer] = issuerUri;
}

string? displayName = string.IsNullOrWhiteSpace(options.ServerSideSessions.UserDisplayNameClaimType)
? null
: ticket.Principal.FindFirstValue(options.ServerSideSessions.UserDisplayNameClaimType);

string? displayName = GetDisplayNameValue(ticket.Principal);

var issued = ticket.Properties.IssuedUtc?.UtcDateTime ?? timeProvider.GetUtcNow().UtcDateTime;
ServerSideSession serverSideSession = new ServerSideSession
Expand All @@ -248,6 +247,13 @@ private async Task<ServerSideSession> StoreNewSession(string key, Authentication
return serverSideSession;
}

private string? GetDisplayNameValue(ClaimsPrincipal principal)
{
return string.IsNullOrWhiteSpace(options.ServerSideSessions.UserDisplayNameClaimType)
? null
: principal.FindFirstValue(options.ServerSideSessions.UserDisplayNameClaimType);
}

private string ToProtectedDataString(AuthenticationTicket ticket)
{
string serializedTicket = JsonSerializer.Serialize(ticket.ToSerializableObj());
Expand Down Expand Up @@ -277,12 +283,25 @@ private string ToProtectedDataString(AuthenticationTicket ticket)
logger.LogError("failed retrieving '{SessionKey}', deserialisation failed, incorrect version '{VersionOrNull}'", existingSession.Key, dataProtectedSessionData?.Version);
return null;
}

string unprotectedData = dataProtector.Unprotect(dataProtectedSessionData.Payload);

SerializedAuthenticationTicket? serializedAuthTicket =
JsonSerializer.Deserialize<SerializedAuthenticationTicket>(unprotectedData);
try
{
string unprotectedData = dataProtector.Unprotect(dataProtectedSessionData.Payload);

SerializedAuthenticationTicket? serializedAuthTicket =
JsonSerializer.Deserialize<SerializedAuthenticationTicket>(unprotectedData);

return serializedAuthTicket?.ToAuthTicket(existingSession);
return serializedAuthTicket?.ToAuthTicket(existingSession);
}
catch (CryptographicException cryptException)
{
logger.LogError(cryptException, "failed to unprotect auth ticket data '{SessionKey}'", existingSession.Key);
return null;
}
catch (JsonException jsonException)
{
logger.LogError(jsonException, "failed deserialising unprotected auth ticket data '{SessionKey}'", existingSession.Key);
return null;
}
}
}
Loading
Loading