Browse Source

fix userid conversion

pull/1206/head
Chris Johnston 6 years ago
parent
commit
d0a0fe43e8
2 changed files with 6 additions and 4 deletions
  1. +5
    -4
      src/Discord.Net.Core/Utils/TokenUtils.cs
  2. +1
    -0
      test/Discord.Net.Tests/Tests.TokenUtils.cs

+ 5
- 4
src/Discord.Net.Core/Utils/TokenUtils.cs View File

@@ -1,4 +1,5 @@
using System;
using System.Text;

namespace Discord
{
@@ -41,10 +42,10 @@ namespace Discord
try
{
// decode the first segment as base64
var v = Convert.FromBase64String(segments[0]);
BitConverter.ToUInt64(v, 0);
// if no exception thrown, token is valid
return true;
var bytes = Convert.FromBase64String(segments[0]);
var idStr = Encoding.UTF8.GetString(bytes);
// discard id
return ulong.TryParse(idStr, out var id);
}
catch (FormatException)
{


+ 1
- 0
test/Discord.Net.Tests/Tests.TokenUtils.cs View File

@@ -138,6 +138,7 @@ namespace Discord
[InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4. this part is invalid. this part is also invalid", true)]
[InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.", false)]
[InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4", false)]
[InlineData("NDI4NDc3OTQ0MDA5MTk1NTIw.xxxx.xxxxx", true)]
// should not throw an unexpected exception
[InlineData("", false)]
[InlineData(null, false)]


Loading…
Cancel
Save