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

[Testing] Demo Test #48248

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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 global.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Microsoft.Build.Traversal": "3.2.0"
},
"sdk": {
"version": "8.0.100",
"version": "9.0.102",
"rollForward": "feature"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ public static AccessToken CreateAccessToken(string token)

internal static JwtPayload DecodeJwtPayload(string token)
{
const string TokenNotFormattedCorrectly = "Token is not formatted correctly.";
const string TokenPartsIncorrect = "Token does not have the correct number of parts.";
const string TokenPayloadIncorrect = "Token payload is not formatted correctly.";

var tokenParts = token.Split('.');
if (tokenParts.Length < 2)
throw new FormatException(TokenNotFormattedCorrectly);
if (tokenParts.Length != 3)
throw new FormatException(TokenPartsIncorrect);

try
{
return JsonSerializer.Deserialize<JwtPayload>(Base64Url.DecodeString(tokenParts[1]));
var payloadJson = Base64Url.DecodeString(tokenParts[1]);
return JsonSerializer.Deserialize<JwtPayload>(payloadJson) ?? throw new FormatException(TokenPayloadIncorrect);
}
catch (JsonException ex)
{
throw new FormatException(TokenNotFormattedCorrectly, ex);
throw new FormatException(TokenPayloadIncorrect, ex);
}
catch (ArgumentException ex)
{
throw new FormatException(TokenPayloadIncorrect, ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Communication;
using NUnit.Framework;

namespace Azure.Communication
{
public class JwtTokenParserTests
{
[Test]
public void DecodeJwtPayload_WithInvalidBase64String_ShouldThrowFormatException()
{
string invalidBase64UrlString = "Invalid_Base64_String";
Assert.Throws<FormatException>(() => JwtTokenParser.DecodeJwtPayload(invalidBase64UrlString), "Token payload is not formatted correctly.");
mjafferi-msft marked this conversation as resolved.
Show resolved Hide resolved
}

[Test]
public void DecodeJwtPayload_WithInvalidTokenParts_ShouldThrowFormatException()
{
string invalidToken = "Invalid.Token";
Assert.Throws<FormatException>(() => JwtTokenParser.DecodeJwtPayload(invalidToken), "Token does not have the correct number of parts.");
}
}
}
Loading