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

Refactor null checks with 'is not null' in JwtBuilder #484

Merged
merged 2 commits into from
Jan 21, 2024
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
18 changes: 9 additions & 9 deletions src/JWT/Algorithms/ECDSAAlgorithmFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ protected override IJwtAlgorithm Create(JwtAlgorithmName algorithm)
#if NETSTANDARD2_0 || NET6_0_OR_GREATER
private IJwtAlgorithm CreateES256Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new ES256Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new ES256Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new ES256Algorithm(_publicKey);
}
Expand All @@ -91,15 +91,15 @@ private IJwtAlgorithm CreateES256Algorithm()

private IJwtAlgorithm CreateES384Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new ES384Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new ES384Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new ES384Algorithm(_publicKey);
}
Expand All @@ -109,15 +109,15 @@ private IJwtAlgorithm CreateES384Algorithm()

private IJwtAlgorithm CreateES512Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new ES512Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new ES512Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new ES512Algorithm(_publicKey);
}
Expand Down
36 changes: 18 additions & 18 deletions src/JWT/Algorithms/RSAlgorithmFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ protected override IJwtAlgorithm Create(JwtAlgorithmName algorithm)

private RS256Algorithm CreateRS256Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new RS256Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new RS256Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new RS256Algorithm(_publicKey);
}
Expand All @@ -83,15 +83,15 @@ private RS256Algorithm CreateRS256Algorithm()

private RS384Algorithm CreateRS384Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new RS384Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new RS384Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new RS384Algorithm(_publicKey);
}
Expand All @@ -101,15 +101,15 @@ private RS384Algorithm CreateRS384Algorithm()

private RS512Algorithm CreateRS512Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new RS512Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new RS512Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new RS512Algorithm(_publicKey);
}
Expand All @@ -119,15 +119,15 @@ private RS512Algorithm CreateRS512Algorithm()

private RS1024Algorithm CreateRS1024Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new RS1024Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new RS1024Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new RS1024Algorithm(_publicKey);
}
Expand All @@ -137,15 +137,15 @@ private RS1024Algorithm CreateRS1024Algorithm()

private RS2048Algorithm CreateRS2048Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new RS2048Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new RS2048Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new RS2048Algorithm(_publicKey);
}
Expand All @@ -155,15 +155,15 @@ private RS2048Algorithm CreateRS2048Algorithm()

private RS4096Algorithm CreateRS4096Algorithm()
{
if (_certFactory is object)
if (_certFactory is not null)
{
return new RS4096Algorithm(_certFactory());
}
if (_publicKey is object && _privateKey is object)
if (_publicKey is not null && _privateKey is not null)
{
return new RS4096Algorithm(_publicKey, _privateKey);
}
if (_publicKey is object)
if (_publicKey is not null)
{
return new RS4096Algorithm(_publicKey);
}
Expand Down
25 changes: 11 additions & 14 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ private void TryCreateEncoder()
if (_urlEncoder is null)
throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithUrlEncoder)}.");

if (_algorithm is object)
if (_algorithm is not null)
_encoder = new JwtEncoder(_algorithm, jsonSerializer, _urlEncoder);
else if (_algFactory is object)
else if (_algFactory is not null)
_encoder = new JwtEncoder(_algFactory, jsonSerializer, _urlEncoder);
}

Expand All @@ -364,9 +364,9 @@ private void TryCreateDecoder()
if (_urlEncoder is null)
throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithUrlEncoder)}.");

if (_algorithm is object)
if (_algorithm is not null)
_decoder = new JwtDecoder(jsonSerializer, _validator, _urlEncoder, _algorithm);
else if (_algFactory is object)
else if (_algFactory is not null)
_decoder = new JwtDecoder(jsonSerializer, _validator, _urlEncoder, _algFactory);
else if (!_valParams.ValidateSignature)
_decoder = new JwtDecoder(jsonSerializer, _urlEncoder);
Expand All @@ -385,7 +385,7 @@ private void TryCreateDecoderForHeader()

private void TryCreateValidator()
{
if (_validator is object)
if (_validator is not null)
return;

var jsonSerializer = _jsonSerializerFactory.Create();
Expand Down Expand Up @@ -445,10 +445,10 @@ private void EnsureCanDecodeHeader()
/// Checks whether enough dependencies were supplied to encode a new token.
/// </summary>
private bool CanEncode() =>
(_algorithm is object || _algFactory is object) &&
_jsonSerializerFactory is object &&
_urlEncoder is object &&
_jwt.Payload is object;
(_algorithm is not null || _algFactory is not null) &&
_jsonSerializerFactory is not null &&
_urlEncoder is not null &&
_jwt.Payload is not null;

/// <summary>
/// Checks whether enough dependencies were supplied to decode a token.
Expand All @@ -459,7 +459,7 @@ private bool CanDecode()
return false;

if (_valParams.ValidateSignature)
return _validator is object && (_algorithm is object || _algFactory is object);
return _validator is not null && (_algorithm is not null || _algFactory is not null);

return true;
}
Expand All @@ -469,10 +469,7 @@ private bool CanDecodeHeader()
if (_urlEncoder is null)
return false;

if (_jsonSerializerFactory is null)
return false;

return true;
return _jsonSerializerFactory is not null;
}

private string GetPropName(MemberInfo prop)
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/IJwtEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class JwtEncoderExtensions
/// <returns>The generated JWT</returns>
/// <exception cref="ArgumentNullException" />
public static string Encode(this IJwtEncoder encoder, object payload, string key) =>
encoder.Encode(null, payload, key is object ? GetBytes(key) : null);
encoder.Encode(null, payload, key is not null ? GetBytes(key) : null);

/// <summary>
/// Creates a JWT given a payload, the signing key, and the algorithm to use.
Expand Down
4 changes: 2 additions & 2 deletions src/JWT/JwtValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public JwtValidator(IJsonSerializer jsonSerializer, IDateTimeProvider dateTimePr
public void Validate(string decodedPayload, string signature, params string[] decodedSignatures)
{
var ex = GetValidationException(decodedPayload, signature, decodedSignatures);
if (ex is object)
if (ex is not null)
throw ex;
}

Expand All @@ -83,7 +83,7 @@ public void Validate(string decodedPayload, string signature, params string[] de
public void Validate(string decodedPayload, IAsymmetricAlgorithm alg, byte[] bytesToSign, byte[] decodedSignature)
{
var ex = GetValidationException(alg, decodedPayload, bytesToSign, decodedSignature);
if (ex is object)
if (ex is not null)
throw ex;
}

Expand Down