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

Adds lock on calls to ComputeHash to handle concurrency #6061

Merged
merged 1 commit into from
May 31, 2024
Merged
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 @@ -43,6 +43,8 @@ internal class JwtController : ServiceLocator<IJwtController, JwtController>, IJ
private static readonly HashAlgorithm Hasher = SHA384.Create();
private static readonly Encoding TextEncoder = Encoding.UTF8;

private static object hasherLock = new object();

/// <inheritdoc/>
public string SchemeType => "JWT";

Expand Down Expand Up @@ -151,7 +153,12 @@ public LoginResultData LoginUser(HttpRequestMessage request, LoginData loginData
// save hash values in DB so no one with access can create JWT header from existing data
var sessionId = NewSessionId;
var now = DateTime.UtcNow;
var renewalToken = EncodeBase64(Hasher.ComputeHash(Guid.NewGuid().ToByteArray()));
string renewalToken = string.Empty;
lock (hasherLock)
{
renewalToken = EncodeBase64(Hasher.ComputeHash(Guid.NewGuid().ToByteArray()));
}

var ptoken = new PersistedToken
{
TokenId = sessionId,
Expand Down Expand Up @@ -381,7 +388,13 @@ private static string EncodeBase64(byte[] data)

private static string GetHashedStr(string data)
{
return EncodeBase64(Hasher.ComputeHash(TextEncoder.GetBytes(data)));
string hash = string.Empty;
lock (hasherLock)
{
hash = EncodeBase64(Hasher.ComputeHash(TextEncoder.GetBytes(data)));
}

return hash;
}

private LoginResultData UpdateToken(string renewalToken, PersistedToken ptoken, UserInfo userInfo)
Expand Down