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

Added the possibility to configure the length of the nonce and the state #305

Merged
merged 2 commits into from
Jul 16, 2021
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
2 changes: 1 addition & 1 deletion src/OidcClient/AuthorizeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public AuthorizeState CreateAuthorizeState(Parameters frontChannelParameters)

var state = new AuthorizeState
{
State = _crypto.CreateState(),
State = _crypto.CreateState(_options.StateLength),
RedirectUri = _options.RedirectUri,
CodeVerifier = pkce.CodeVerifier,
};
Expand Down
11 changes: 2 additions & 9 deletions src/OidcClient/CryptoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,11 @@ public bool ValidateHash(string data, string hashedData, string signatureAlgorit
}
}

public string CreateState()
public string CreateState(int length)
{
_logger.LogTrace("CreateState");

return CryptoRandom.CreateUniqueId(16);
}

public string CreateNonce()
{
_logger.LogTrace("CreateNonce");

return CryptoRandom.CreateUniqueId(16);
return CryptoRandom.CreateUniqueId(length);
}

public Pkce CreatePkceData()
Expand Down
8 changes: 8 additions & 0 deletions src/OidcClient/OidcClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public class OidcClientOptions
/// </value>
public string PostLogoutRedirectUri { get; set; }

/// <summary>
/// Gets or sets the state length.
/// </summary>
/// <value>
/// The state length.
/// </value>
public int StateLength { get; set; } = 16;

/// <summary>
/// Gets or sets the browser implementation.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/OidcClient/ResponseProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -78,6 +79,10 @@ private async Task<ResponseValidationResult> ProcessCodeFlowResponseAsync(
{
return new ResponseValidationResult($"Error redeeming code: {tokenResponse.Error ?? "no error code"} / {tokenResponse.ErrorDescription ?? "no description"}");
}
if (tokenResponse.HttpStatusCode != HttpStatusCode.OK)
{
return new ResponseValidationResult($"Error redeeming code: {tokenResponse.Raw}");
}

// validate token response
var tokenResponseValidationResult = await ValidateTokenResponseAsync(tokenResponse, state, requireIdentityToken:false, cancellationToken: cancellationToken);
Expand Down