Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex encode consent keys #1489

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion src/IdentityServer/Stores/Default/DefaultGrantStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ protected DefaultGrantStore(string grantType,
}

private const string KeySeparator = ":";
const string HexEncodingFormatSuffix = "-1";

/// <summary>
/// The suffix added to keys to indicate that hex encoding should be used.
/// </summary>
protected const string HexEncodingFormatSuffix = "-1";

/// <summary>
/// Creates a handle.
Expand Down
26 changes: 22 additions & 4 deletions src/IdentityServer/Stores/Default/DefaultUserConsentStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ public DefaultUserConsentStore(
{
}

private string GetConsentKey(string subjectId, string clientId)
private string GetConsentKey(string subjectId, string clientId, bool useHexEncoding = true)
{
return clientId + "|" + subjectId;
if(useHexEncoding)
{
return $"{clientId}|{subjectId}{HexEncodingFormatSuffix}";
} else
{
return $"{clientId}|{subjectId}";
}
}

/// <summary>
Expand All @@ -55,12 +61,24 @@ public Task StoreUserConsentAsync(Consent consent)
/// <param name="subjectId">The subject identifier.</param>
/// <param name="clientId">The client identifier.</param>
/// <returns></returns>
public Task<Consent> GetUserConsentAsync(string subjectId, string clientId)
public async Task<Consent> GetUserConsentAsync(string subjectId, string clientId)
{
using var activity = Tracing.StoreActivitySource.StartActivity("DefaultUserConsentStore.GetUserConsent");

var key = GetConsentKey(subjectId, clientId);
return GetItemAsync(key);
var consent = await GetItemAsync(key);
if(consent == null)
{
var legacyKey = GetConsentKey(subjectId, clientId, useHexEncoding: false);
consent = await GetItemAsync(legacyKey);
if(consent != null)
{
await StoreUserConsentAsync(consent); // Write back the consent record to update its key
await RemoveItemAsync(legacyKey);
}
}

return consent;
}

/// <summary>
Expand Down