Skip to content

Commit 942e91a

Browse files
committed
Remove Base64Url Duplicated Methods
1 parent a90c38c commit 942e91a

File tree

5 files changed

+25
-41
lines changed

5 files changed

+25
-41
lines changed

src/CodeBeam.UltimateAuth.Client/Services/DefaultFlowClient.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using CodeBeam.UltimateAuth.Client.Options;
77
using CodeBeam.UltimateAuth.Core.Contracts;
88
using CodeBeam.UltimateAuth.Core.Domain;
9+
using CodeBeam.UltimateAuth.Core.Infrastructure;
910
using CodeBeam.UltimateAuth.Core.Options;
1011
using Microsoft.AspNetCore.Components;
1112
using Microsoft.Extensions.Options;
@@ -200,22 +201,14 @@ private Task NavigateToHubLoginAsync(string authorizationCode, string codeVerifi
200201
private static string CreateVerifier()
201202
{
202203
var bytes = RandomNumberGenerator.GetBytes(32);
203-
return Base64UrlEncode(bytes);
204+
return Base64Url.Encode(bytes);
204205
}
205206

206207
private static string CreateChallenge(string verifier)
207208
{
208209
using var sha256 = SHA256.Create();
209210
var hash = sha256.ComputeHash(Encoding.ASCII.GetBytes(verifier));
210-
return Base64UrlEncode(hash);
211-
}
212-
213-
private static string Base64UrlEncode(byte[] input)
214-
{
215-
return Convert.ToBase64String(input)
216-
.TrimEnd('=')
217-
.Replace('+', '-')
218-
.Replace('/', '_');
211+
return Base64Url.Encode(hash);
219212
}
220213
}
221214
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
namespace CodeBeam.UltimateAuth.Core.Options
1+
namespace CodeBeam.UltimateAuth.Core.Options;
2+
3+
public enum HeaderTokenFormat
24
{
3-
public enum HeaderTokenFormat
4-
{
5-
Bearer,
6-
Raw
7-
}
5+
Bearer,
6+
Raw
87
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
namespace CodeBeam.UltimateAuth.Core.Options
1+
namespace CodeBeam.UltimateAuth.Core.Options;
2+
3+
public enum TokenResponseMode
24
{
3-
public enum TokenResponseMode
4-
{
5-
None,
6-
Cookie,
7-
Header,
8-
Body
9-
}
5+
None,
6+
Cookie,
7+
Header,
8+
Body
109
}

src/CodeBeam.UltimateAuth.Server/Infrastructure/Pkce/PkceAuthorizationValidator.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Security.Cryptography;
1+
using CodeBeam.UltimateAuth.Core.Infrastructure;
2+
using System.Security.Cryptography;
23
using System.Text;
34

45
namespace CodeBeam.UltimateAuth.Server.Infrastructure;
@@ -55,16 +56,8 @@ private static bool IsVerifierValid(string verifier, string expectedChallenge)
5556
using var sha256 = SHA256.Create();
5657
byte[] hash = sha256.ComputeHash(Encoding.ASCII.GetBytes(verifier));
5758

58-
string computedChallenge = Base64UrlEncode(hash);
59+
string computedChallenge = Base64Url.Encode(hash);
5960

6061
return CryptographicOperations.FixedTimeEquals(Encoding.ASCII.GetBytes(computedChallenge), Encoding.ASCII.GetBytes(expectedChallenge));
6162
}
62-
63-
private static string Base64UrlEncode(byte[] input)
64-
{
65-
return Convert.ToBase64String(input)
66-
.TrimEnd('=')
67-
.Replace('+', '-')
68-
.Replace('/', '_');
69-
}
7063
}

tests/CodeBeam.UltimateAuth.Tests.Unit/Core/UserIdConverterTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void UserKey_Roundtrip_Should_Preserve_Value()
1313
var key = UserKey.New();
1414
var converter = new UAuthUserIdConverter<UserKey>();
1515

16-
var str = converter.ToString(key);
16+
var str = converter.ToCanonicalString(key);
1717
var parsed = converter.FromString(str);
1818

1919
Assert.Equal(key, parsed);
@@ -25,7 +25,7 @@ public void Guid_Roundtrip_Should_Work()
2525
var id = Guid.NewGuid();
2626
var converter = new UAuthUserIdConverter<Guid>();
2727

28-
var str = converter.ToString(id);
28+
var str = converter.ToCanonicalString(id);
2929
var parsed = converter.FromString(str);
3030

3131
Assert.Equal(id, parsed);
@@ -37,7 +37,7 @@ public void String_Roundtrip_Should_Work()
3737
var id = "user_123";
3838
var converter = new UAuthUserIdConverter<string>();
3939

40-
var str = converter.ToString(id);
40+
var str = converter.ToCanonicalString(id);
4141
var parsed = converter.FromString(str);
4242

4343
Assert.Equal(id, parsed);
@@ -49,7 +49,7 @@ public void Int_Should_Use_Invariant_Culture()
4949
var id = 1234;
5050
var converter = new UAuthUserIdConverter<int>();
5151

52-
var str = converter.ToString(id);
52+
var str = converter.ToCanonicalString(id);
5353

5454
Assert.Equal(id.ToString(CultureInfo.InvariantCulture), str);
5555
}
@@ -60,7 +60,7 @@ public void Long_Roundtrip_Should_Work()
6060
var id = 9_223_372_036_854_775_000L;
6161
var converter = new UAuthUserIdConverter<long>();
6262

63-
var str = converter.ToString(id);
63+
var str = converter.ToCanonicalString(id);
6464
var parsed = converter.FromString(str);
6565

6666
Assert.Equal(id, parsed);
@@ -71,7 +71,7 @@ public void Double_UserId_Should_Throw()
7171
{
7272
var converter = new UAuthUserIdConverter<double>();
7373

74-
Assert.ThrowsAny<Exception>(() => converter.ToString(12.34));
74+
Assert.ThrowsAny<Exception>(() => converter.ToCanonicalString(12.34));
7575
}
7676

7777
private sealed class CustomUserId
@@ -84,7 +84,7 @@ public void Custom_UserId_Should_Fail()
8484
{
8585
var converter = new UAuthUserIdConverter<CustomUserId>();
8686

87-
Assert.ThrowsAny<Exception>(() => converter.ToString(new CustomUserId()));
87+
Assert.ThrowsAny<Exception>(() => converter.ToCanonicalString(new CustomUserId()));
8888
}
8989

9090
[Fact]

0 commit comments

Comments
 (0)