Skip to content

Commit

Permalink
use some pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Nov 27, 2024
1 parent d1e1717 commit c55be01
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
7 changes: 4 additions & 3 deletions src/IdentityServer/Configuration/CryptoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ internal static bool IsValidCurveForAlgorithm(ECDsaSecurityKey key, string algor
}
internal static bool IsValidCrvValueForAlgorithm(string crv)
{
return crv == JsonWebKeyECTypes.P256 ||
crv == JsonWebKeyECTypes.P384 ||
crv == JsonWebKeyECTypes.P521;
return crv is
JsonWebKeyECTypes.P256 or
JsonWebKeyECTypes.P384 or
JsonWebKeyECTypes.P521;
}

internal static string GetRsaSigningAlgorithmValue(IdentityServerConstants.RsaSigningAlgorithm value)
Expand Down
20 changes: 11 additions & 9 deletions src/IdentityServer/Endpoints/Results/AuthorizeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ private async Task ProcessErrorAsync(AuthorizeResponse response, HttpContext con
// these are the conditions where we can send a response
// back directly to the client, otherwise we're only showing the error UI
var isSafeError =
response.Error == OidcConstants.AuthorizeErrors.AccessDenied ||
response.Error == OidcConstants.AuthorizeErrors.AccountSelectionRequired ||
response.Error == OidcConstants.AuthorizeErrors.LoginRequired ||
response.Error == OidcConstants.AuthorizeErrors.ConsentRequired ||
response.Error == OidcConstants.AuthorizeErrors.InteractionRequired ||
response.Error == OidcConstants.AuthorizeErrors.TemporarilyUnavailable ||
response.Error == OidcConstants.AuthorizeErrors.UnmetAuthenticationRequirements;
response.Error is
OidcConstants.AuthorizeErrors.AccessDenied or
OidcConstants.AuthorizeErrors.AccountSelectionRequired or
OidcConstants.AuthorizeErrors.LoginRequired or
OidcConstants.AuthorizeErrors.ConsentRequired or
OidcConstants.AuthorizeErrors.InteractionRequired or
OidcConstants.AuthorizeErrors.TemporarilyUnavailable or
OidcConstants.AuthorizeErrors.UnmetAuthenticationRequirements;
if (isSafeError)
{
// this scenario we can return back to the client
Expand All @@ -131,8 +132,9 @@ private async Task ProcessResponseAsync(AuthorizeResponse response, HttpContext

private async Task RenderAuthorizeResponseAsync(AuthorizeResponse response, HttpContext context)
{
if (response.Request.ResponseMode == OidcConstants.ResponseModes.Query ||
response.Request.ResponseMode == OidcConstants.ResponseModes.Fragment)
if (response.Request.ResponseMode is
OidcConstants.ResponseModes.Query or
OidcConstants.ResponseModes.Fragment)
{
context.Response.SetNoCache();
context.Response.Redirect(BuildRedirectUri(response));
Expand Down
3 changes: 1 addition & 2 deletions src/IdentityServer/Extensions/ClaimsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public static Dictionary<string, object> ToClaimsDictionary(this IEnumerable<Cla

private static object GetValue(Claim claim)
{
if (claim.ValueType == ClaimValueTypes.Integer ||
claim.ValueType == ClaimValueTypes.Integer32)
if (claim.ValueType is ClaimValueTypes.Integer or ClaimValueTypes.Integer32)
{
if (Int32.TryParse(claim.Value, out var value))
{
Expand Down
2 changes: 1 addition & 1 deletion src/IdentityServer/Extensions/TokenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static object AddObject(Claim claim)
return bool.Parse(claim.Value);
}

if (claim.ValueType == ClaimValueTypes.Integer || claim.ValueType == ClaimValueTypes.Integer32)
if (claim.ValueType is ClaimValueTypes.Integer or ClaimValueTypes.Integer32)
{
return int.Parse(claim.Value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected override Task HandleChallengeAsync(AuthenticationProperties properties

var sb = new StringBuilder();

if (Options.TokenMode == LocalApiTokenMode.BearerOnly || Options.TokenMode == LocalApiTokenMode.DPoPAndBearer)
if (Options.TokenMode is LocalApiTokenMode.BearerOnly or LocalApiTokenMode.DPoPAndBearer)
{
sb.Append("Bearer");

Expand All @@ -220,7 +220,7 @@ protected override Task HandleChallengeAsync(AuthenticationProperties properties
}
}

if (Options.TokenMode == LocalApiTokenMode.DPoPOnly || Options.TokenMode == LocalApiTokenMode.DPoPAndBearer)
if (Options.TokenMode is LocalApiTokenMode.DPoPOnly or LocalApiTokenMode.DPoPAndBearer)
{
if (sb.Length > 0) sb.Append(", ");
sb.Append("DPoP");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private AuthorizeRequestValidationResult ValidateCoreParameters(ValidatedAuthori
//////////////////////////////////////////////////////////
// check if PKCE is required and validate parameters
//////////////////////////////////////////////////////////
if (request.GrantType == GrantType.AuthorizationCode || request.GrantType == GrantType.Hybrid)
if (request.GrantType is GrantType.AuthorizationCode or GrantType.Hybrid)
{
_logger.LogDebug("Checking for PKCE parameters");

Expand Down Expand Up @@ -449,8 +449,7 @@ private async Task<AuthorizeRequestValidationResult> ValidateScopeAndResourceAsy
// check scope vs response_type plausability
//////////////////////////////////////////////////////////
var requirement = Constants.ResponseTypeToScopeRequirement[request.ResponseType];
if (requirement == Constants.ScopeRequirement.Identity ||
requirement == Constants.ScopeRequirement.IdentityOnly)
if (requirement is Constants.ScopeRequirement.Identity or Constants.ScopeRequirement.IdentityOnly)
{
if (request.IsOpenIdRequest == false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public virtual Task<bool> IsRedirectUriValidAsync(RedirectUriValidationContext c
// was pushed)
if (_options?.PushedAuthorization?.AllowUnregisteredPushedRedirectUris == true &&
context.Client.RequireClientSecret &&
(context.AuthorizeRequestType == AuthorizeRequestType.PushedAuthorization ||
context.AuthorizeRequestType == AuthorizeRequestType.AuthorizeWithPushedParameters))
context.AuthorizeRequestType is
AuthorizeRequestType.PushedAuthorization or
AuthorizeRequestType.AuthorizeWithPushedParameters)
{
return Task.FromResult(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,12 @@ public class ValidatedAuthorizeRequest : ValidatedRequest
/// <value>
/// <c>true</c> if an access token was requested; otherwise, <c>false</c>.
/// </value>
public bool AccessTokenRequested => ResponseType == OidcConstants.ResponseTypes.IdTokenToken ||
ResponseType == OidcConstants.ResponseTypes.Code ||
ResponseType == OidcConstants.ResponseTypes.CodeIdToken ||
ResponseType == OidcConstants.ResponseTypes.CodeToken ||
ResponseType == OidcConstants.ResponseTypes.CodeIdTokenToken;


public bool AccessTokenRequested => ResponseType is
OidcConstants.ResponseTypes.IdTokenToken or
OidcConstants.ResponseTypes.Code or
OidcConstants.ResponseTypes.CodeIdToken or
OidcConstants.ResponseTypes.CodeToken or
OidcConstants.ResponseTypes.CodeIdTokenToken;

/// <summary>
/// Initializes a new instance of the <see cref="ValidatedAuthorizeRequest"/> class.
Expand Down

0 comments on commit c55be01

Please sign in to comment.