Skip to content
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
17 changes: 12 additions & 5 deletions src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ internal static int Decode(ReadOnlySpan<char> strSpan, Span<byte> output)

return Decode(strSpan, output, decodedLength);
}
#elif NET6_0_OR_GREATER
internal static int Decode(ReadOnlySpan<char> strSpan, Span<byte> output)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think we need to adjust the API files for this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be a follow up

{
int mod = strSpan.Length % 4;
if (mod == 1)
throw LogHelper.LogExceptionMessage(new FormatException(LogHelper.FormatInvariant(LogMessages.IDX10400, strSpan.ToString())));
bool needReplace = strSpan.IndexOfAny(Base64UrlCharacter62, Base64UrlCharacter63) >= 0;
int decodedLength = strSpan.Length + (4 - mod) % 4;

return Decode(strSpan, output, needReplace, decodedLength);
}
#else
internal static void Decode(ReadOnlySpan<char> strSpan, Span<byte> output)
{
Expand All @@ -267,13 +278,9 @@ internal static void Decode(ReadOnlySpan<char> strSpan, Span<byte> output)
throw LogHelper.LogExceptionMessage(new FormatException(LogHelper.FormatInvariant(LogMessages.IDX10400, strSpan.ToString())));
bool needReplace = strSpan.IndexOfAny(Base64UrlCharacter62, Base64UrlCharacter63) >= 0;
int decodedLength = strSpan.Length + (4 - mod) % 4;
#if NET6_0_OR_GREATER
Decode(strSpan, output, needReplace, decodedLength);
#else

Decode(strSpan, output, needReplace, decodedLength);
#endif
}

#endif

#if NET9_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Reflection;
using System.Text;
using Microsoft.IdentityModel.TestUtils;
using Xunit;
Expand Down Expand Up @@ -343,5 +344,61 @@ public void EncodeDecode_InvalidParameters_ThrowsExceptionTests()
Assert.Throws<ArgumentNullException>(static () => Base64UrlEncoding.Decode<object, object>("abc", 0, 0, null, null));
Assert.Throws<ArgumentNullException>(static () => Base64UrlEncoding.Decode<object, object, object, object>(null, 0, 0, null, null, null, null));
}

[Fact]
public void Base64UrlEncoder_PublicApiIsNotChanged()
{
// Public APIs
string encoded1 = Base64UrlEncoder.Encode("test");
string encoded2 = Base64UrlEncoder.Encode(new byte[] { 1, 2, 3 });
string encoded3 = Base64UrlEncoder.Encode(new byte[] { 1, 2, 3 }, 0, 3);
char[] charBuffer = new char[10];
int charsWritten = Base64UrlEncoder.Encode(new ReadOnlySpan<byte>(new byte[] { 1, 2, 3 }), new Span<char>(charBuffer));
byte[] decodedBytes = Base64UrlEncoder.DecodeBytes("dGVzdA");
string decodedString = Base64UrlEncoder.Decode("dGVzdA");

// Internal APIs
byte[] decodedBytesFromSpan = Base64UrlEncoder.Decode("test".AsSpan());
Span<byte> output = stackalloc byte[10];
#if NET6_0_OR_GREATER
int bytesWritten = Base64UrlEncoder.Decode("test".AsSpan(), output);
#else
Base64UrlEncoder.Decode("test".AsSpan(), output);
var method = typeof(Base64UrlEncoder).GetMethod(
"Decode",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public,
null,
new[] { typeof(ReadOnlySpan<char>), typeof(Span<byte>), },
null
);
Assert.Equal(typeof(void), method.ReturnType);
#endif
}

[Fact]
public void Base64UrlEncoding_PublicApiIsNotChanged()
{
// Public APIs
byte[] decoded1 = Base64UrlEncoding.Decode("test");
byte[] decoded2 = Base64UrlEncoding.Decode("test", 0, 4);
string genericDecoded1 = Base64UrlEncoding.Decode<string>("test", 0, 4, (bytes, len) => "test");
string genericDecoded2 = Base64UrlEncoding.Decode<string, int>("test", 0, 4, 1, (bytes, len, x) => "test");
string genericDecoded3 = Base64UrlEncoding.Decode<string, int, int, int>("test", 0, 4, 1, 2, 3, (bytes, len, x, y, z) => "test");
string encoded1 = Base64UrlEncoding.Encode(new byte[] { 1, 2, 3 });
string encoded2 = Base64UrlEncoding.Encode(new byte[] { 1, 2, 3 }, 0, 3);

// Internal APIs
int outputSize = Base64UrlEncoding.ValidateAndGetOutputSize("test".AsSpan(), 0, 4);
byte[] output = new byte[10];
Base64UrlEncoding.Decode("test".AsSpan(), 0, 4, output);
var method = typeof(Base64UrlEncoding).GetMethod(
"Decode",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public,
null,
new[] { typeof(ReadOnlySpan<char>), typeof(int), typeof(int), typeof(byte[]) },
null
);
Assert.Equal(typeof(void), method.ReturnType);
}
}
}
Loading