step-up-labs / firebase-authentication-dotnet

C# library for Firebase Authentication
MIT License
376 stars 129 forks source link

Observed in version 3.7.2 : Parsing error when the password ends (or maybe contains also) a backslash \ #213

Open DamienChailley opened 6 months ago

DamienChailley commented 6 months ago

This happens when calling: public async Task<FirebaseAuthLink> SignInWithEmailAndPasswordAsync(string email, string password, string tenantId = null); with a valid account and a valid password but that contains a backslash,

Result : The method : private async Task<FirebaseAuthLink> ExecuteWithPostContentAsync(string googleUrl, string postContent) and in particular the response (var response) inside the method returns a json parse error.

To solve it I had to download the code (and create a project dependency of the code) and modify the

public async Task<FirebaseAuthLink> SignInWithEmailAndPasswordAsync(string email, string password,
            string tenantId = null)

method to modify the postContent.

This is how I modified the method, this is a solution but I guess there are better once so I wont publish any pullrequest :)

        /// <summary>
        /// Using the provided email and password, get the firebase auth with token and basic user credentials.
        /// </summary>
        /// <param name="email"> The email. </param>
        /// <param name="password"> The password. </param>
        /// <param name="tenantId"></param>
        /// <returns> The <see cref="FirebaseAuth"/>. </returns>
        public async Task<FirebaseAuthLink> SignInWithEmailAndPasswordAsync(string email, string password,
            string tenantId = null)
        {
            bool returnSecureToken = true;

            var data = new
            {
                email,
                password,
                returnSecureToken
            };

            // Serialize the object to JSON with proper encoding
            string postContent = JsonConvert.SerializeObject(data, new JsonSerializerSettings
            {
                StringEscapeHandling = StringEscapeHandling.EscapeHtml
            });

            if (tenantId != null)
            {
                postContent = postContent.Remove(postContent.Length - 1, 1);
                postContent = string.Concat(postContent, $",\"tenantId\":\"{tenantId}\"",@"}");
            }

            FirebaseAuthLink firebaseAuthLink = await this.ExecuteWithPostContentAsync(GooglePasswordUrl, postContent).ConfigureAwait(false);
            firebaseAuthLink.User = await this.GetUserAsync(firebaseAuthLink.FirebaseToken).ConfigureAwait(false);
            return firebaseAuthLink;
        }
DamienChailley commented 6 months ago

btw, thank you for this project ! :) it helped me a lot with firebase system :)

DamienChailley commented 5 months ago

For info, I have migrated to your version 4.1.0 and the problem seems to have disappeared.