Skip to content

Commit f947760

Browse files
authored
fix(auth): Fixed auth error code parsing (#479)
* fix(auth): Fixed auth error code parsing * fix(chore): Add integration test to verify auth error parsing
1 parent 4a6bb68 commit f947760

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

FirebaseAdmin/FirebaseAdmin.IntegrationTests/Auth/AbstractFirebaseAuthTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public abstract class AbstractFirebaseAuthTest<T>
3636
HandleCodeInApp = false,
3737
};
3838

39+
private static readonly ActionCodeSettings InvalidEmailLinkSettingsWithCustomDomain = new ActionCodeSettings()
40+
{
41+
Url = ContinueUrl,
42+
HandleCodeInApp = true,
43+
LinkDomain = "cool.link.domain",
44+
};
45+
3946
private readonly AbstractAuthFixture<T> fixture;
4047
private readonly TemporaryUserBuilder userBuilder;
4148

@@ -658,6 +665,18 @@ public async Task SignInWithEmailLink()
658665
Assert.True(user.EmailVerified);
659666
}
660667

668+
[Fact]
669+
public async Task AuthErrorCodeParse()
670+
{
671+
var user = await this.userBuilder.CreateRandomUserAsync();
672+
673+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
674+
() => this.Auth.GeneratePasswordResetLinkAsync(
675+
user.Email, InvalidEmailLinkSettingsWithCustomDomain));
676+
Assert.Equal(ErrorCode.InvalidArgument, exception.ErrorCode);
677+
Assert.Equal(AuthErrorCode.InvalidHostingLinkDomain, exception.AuthErrorCode);
678+
}
679+
661680
private async Task<FirebaseToken> AssertValidIdTokenAsync(
662681
string idToken, bool checkRevoked = false)
663682
{

FirebaseAdmin/FirebaseAdmin.Tests/Auth/AuthErrorHandlerTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ public void KnownErrorCodeWithDetails(
121121
Assert.EndsWith($" ({code}): Some details.", error.Message);
122122
}
123123

124+
[Theory]
125+
[MemberData(nameof(AuthErrorCodes))]
126+
public void KnownErrorCodeWithDetailsAndWhiteSpace(
127+
string code, ErrorCode expectedCode, AuthErrorCode expectedAuthCode)
128+
{
129+
var json = $@"{{
130+
""error"": {{
131+
""message"": ""{code} : Some details. "",
132+
}}
133+
}}";
134+
var resp = new HttpResponseMessage()
135+
{
136+
StatusCode = HttpStatusCode.ServiceUnavailable,
137+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
138+
};
139+
140+
var error = AuthErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
141+
142+
Assert.Equal(expectedCode, error.ErrorCode);
143+
Assert.Equal(expectedAuthCode, error.AuthErrorCode);
144+
Assert.Same(resp, error.HttpResponse);
145+
Assert.Null(error.InnerException);
146+
Assert.EndsWith($" ({code}): Some details.", error.Message);
147+
}
148+
124149
[Fact]
125150
public void UnknownErrorCode()
126151
{

FirebaseAdmin/FirebaseAdmin/Auth/AuthErrorHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ private sealed class AuthError
216216

217217
/// <summary>
218218
/// Gets the Firebase Auth error code extracted from the response. Returns empty string
219-
/// if the error code cannot be determined.
219+
/// if the error code cannot be determined. These error messages take the form
220+
/// <c>{"error": {"message": "CODE : OPTIONAL DETAILS"}}</c>.
220221
/// </summary>
221222
internal string Code
222223
{
@@ -225,7 +226,7 @@ internal string Code
225226
var separator = this.GetSeparator();
226227
if (separator != -1)
227228
{
228-
return this.Message.Substring(0, separator);
229+
return this.Message.Substring(0, separator).Trim();
229230
}
230231

231232
return this.Message ?? string.Empty;

0 commit comments

Comments
 (0)