colyseus / colyseus-unity-sdk

⚔ Colyseus Multiplayer SDK for Unity
https://docs.colyseus.io/getting-started/unity-sdk/
MIT License
378 stars 104 forks source link

Bug: replacing "ws" with "http" in uri modifies access token #81

Closed MateusMendesSantana closed 5 years ago

MateusMendesSantana commented 5 years ago

if (HasToken) query["token"] = Token; Auth.cs, line 249 // FIXME: replacing "ws" with "http" is too hacky! req.url = uriBuilder.Uri.ToString().Replace("ws", "http");

client token:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZDM5ZWZjODFlMzA5M2UxYzRlMDJjMjkiLCJpYXQiOjE1NjQyMzQ4MjF9.y5FVhZcIQiESp1rIHKCWx3WC10wxnWwGCGh8plX49ws

has been replaced by:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZDM5ZWZjODFlMzA5M2UxYzRlMDJjMjkiLCJpYXQiOjE1NjQyMzQ4MjF9.y5FVhZcIQiESp1rIHKCWx3WC10wxnWwGCGh8plX49http

endel commented 5 years ago

Really well-observed @MateusMendesSantana, I think we can use a ReplaceFirst instead to prevent this from happening, like:

    public static string ReplaceFirst(this string text, string search, string replace)
    {
      int pos = text.IndexOf(search);
      if (pos < 0)
      {
        return text;
      }
      return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
    }
MateusMendesSantana commented 5 years ago

Thank you @endel. Could be, I just did it: req.url = uriBuilder.Uri.ToString().Replace("ws://", "http://"); I think the right thing would be to fragment the URI, separate the protocol, address and port. Mount the URI on request only

endel commented 5 years ago

@MateusMendesSantana sounds better indeed! I'd gladly merge if you don't mind sending a PR <3