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

Fix JWT key validation #849

Merged
merged 3 commits into from
Jun 12, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public class JwtDecoder
public TokenResult? DecodeToken(
DecoderParameters decodeParameters,
TokenParameters tokenParameters,
Action<TokenResultErrorEventArgs> decodingErrorCallBack)
Action<TokenResultErrorEventArgs> decodingErrorCallBack,
out JwtAlgorithm? jwtAlgorithm)
{
Arguments.NotNull(decodeParameters, nameof(decodeParameters));
Arguments.NotNull(tokenParameters, nameof(tokenParameters));
_decodingErrorCallBack = Arguments.NotNull(decodingErrorCallBack, nameof(decodingErrorCallBack));
Arguments.NotNullOrWhiteSpace(tokenParameters.Token, nameof(tokenParameters.Token));
jwtAlgorithm = null;

var tokenResult = new TokenResult();

Expand All @@ -49,7 +51,10 @@ public class JwtDecoder
JwtSecurityToken jwtSecurityToken = handler.ReadJwtToken(tokenParameters.Token);
tokenResult.Header = JsonHelper.Format(jwtSecurityToken.Header.SerializeToJson(), Indentation.TwoSpaces, false);
tokenResult.Payload = JsonHelper.Format(jwtSecurityToken.Payload.SerializeToJson(), Indentation.TwoSpaces, false);
tokenResult.TokenAlgorithm = tokenParameters.TokenAlgorithm;
jwtAlgorithm = tokenResult.TokenAlgorithm = tokenParameters.TokenAlgorithm =
Enum.TryParse(jwtSecurityToken.SignatureAlgorithm, out JwtAlgorithm parsedAlgorithm)
? parsedAlgorithm
: tokenParameters.TokenAlgorithm;

if (decodeParameters.ValidateSignature)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,32 @@ public void Receive(JwtJobAddedMessage message)
tokenParameters.ValidAudiences = ValidAudiences!.Split(',').ToHashSet();
}

TokenResult? result = _decoder.DecodeToken(decoderParamters, tokenParameters, TokenErrorCallBack);
TokenResult? result = _decoder.DecodeToken(decoderParamters, tokenParameters, TokenErrorCallBack, out JwtAlgorithm? jwtAlgorithm);

ThreadHelper.RunOnUIThreadAsync(ThreadPriority.Low, () =>
{
if (result is null)
{
return;
}

Header = result.Header;
Payload = result.Payload;

if (ValidateSignature)
{
RequireSignature = true;
if (result.TokenAlgorithm is
if (jwtAlgorithm is
not null and
not JwtAlgorithm.HS256 and
not JwtAlgorithm.HS384 and
not JwtAlgorithm.HS512)
{
RequireSignature = false;
}
}

if (result is null)
{
return;
}

DisplayValidationInfoBar();
Header = result.Header;
Payload = result.Payload;

DisplayValidationInfoBar();

if (ToolSuccessfullyWorked)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ AlgorithmMode.Value is JwtAlgorithm.HS384 ||
{
Token = result.Token;
}
HasError = JwtValidation.IsValid!;
HasError = !JwtValidation.IsValid;
if (ToolSuccessfullyWorked)
{
ToolSuccessfullyWorked = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task JwtDecoderEncoder_Generate_And_Decode_Basic_HS_Token_Without_S
};

var jwtDecoder = new JwtDecoder();
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack);
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack, out _);

Assert.IsNotNull(result);
Assert.IsNull(_validationResult);
Expand Down Expand Up @@ -126,7 +126,7 @@ public async Task JwtDecoderEncoder_Generate_And_Decode_Complex_HS_Token_With_Si
ValidAudiences = new HashSet<string> { "devtoys" },
};
var jwtDecoder = new JwtDecoder();
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack);
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack, out _);

Assert.IsNotNull(result);
Assert.IsNull(_validationResult);
Expand Down Expand Up @@ -165,7 +165,7 @@ public async Task JwtDecoderEncoder_Generate_And_Decode_Basic_RS_Token_Without_S
};

var jwtDecoder = new JwtDecoder();
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack);
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack, out _);

Assert.IsNotNull(result);
Assert.IsNull(_validationResult);
Expand Down Expand Up @@ -222,7 +222,7 @@ public async Task JwtDecoderEncoder_Generate_And_Decode_Complex_RS_Token_With_Si
ValidAudiences = new HashSet<string> { "devtoys" },
};
var jwtDecoder = new JwtDecoder();
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack);
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack, out _);

Assert.IsNotNull(result);
Assert.IsNull(_validationResult);
Expand Down Expand Up @@ -261,7 +261,7 @@ public async Task JwtDecoderEncoder_Generate_And_Decode_Basic_PS_Token_Without_S
};

var jwtDecoder = new JwtDecoder();
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack);
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack, out _);

Assert.IsNotNull(result);
Assert.IsNull(_validationResult);
Expand Down Expand Up @@ -318,7 +318,7 @@ public async Task JwtDecoderEncoder_Generate_And_Decode_Complex_PS_Token_With_Si
ValidAudiences = new HashSet<string> { "devtoys" },
};
var jwtDecoder = new JwtDecoder();
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack);
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack, out _);

Assert.IsNotNull(result);
Assert.IsNull(_validationResult);
Expand Down Expand Up @@ -357,7 +357,7 @@ public async Task JwtDecoderEncoder_Generate_And_Decode_Basic_ES_Token_Without_S
};

var jwtDecoder = new JwtDecoder();
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack);
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack, out _);

Assert.IsNotNull(result);
Assert.IsNull(_validationResult);
Expand Down Expand Up @@ -414,7 +414,7 @@ public async Task JwtDecoderEncoder_Generate_And_Decode_Complex_ES_Token_With_Si
ValidAudiences = new HashSet<string> { "devtoys" },
};
var jwtDecoder = new JwtDecoder();
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack);
result = jwtDecoder.DecodeToken(decodeParameters, tokenParameters, DecodingErrorCallBack, out _);

Assert.IsNotNull(result);
Assert.IsNull(_validationResult);
Expand Down
Loading