Skip to content

Commit 9c2b566

Browse files
authored
fix(auth): Support for verifying tenant ID tokens in FirebaseAuth (#244)
* fix: Enabling AsyncUsageAnalyzers and fixing violations * fix: Removed experimental changes * fix(auth): Support for verifying tenant ID tokens in FirebaseAuth
1 parent b6c3359 commit 9c2b566

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

FirebaseAdmin/FirebaseAdmin.IntegrationTests/Auth/TenantAwareFirebaseAuthTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Threading.Tasks;
1617
using FirebaseAdmin.Auth;
1718
using FirebaseAdmin.Auth.Multitenancy;
1819
using Xunit;
@@ -32,6 +33,30 @@ public void TenantId()
3233
Assert.NotEmpty(this.Auth.TenantId);
3334
}
3435

36+
[Fact]
37+
public async Task VerifyIdTokenWithTenant()
38+
{
39+
var customToken = await this.Auth.CreateCustomTokenAsync("testuser");
40+
var idToken = await AuthIntegrationUtils.SignInWithCustomTokenAsync(
41+
customToken, this.Auth.TenantId);
42+
43+
// Verifies in FirebaseAuth
44+
var decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
45+
Assert.Equal(this.Auth.TenantId, decoded.TenantId);
46+
47+
// Verifies in TenantAwareFirebaseAuth(matching-tenant)
48+
decoded = await this.Auth.VerifyIdTokenAsync(idToken);
49+
Assert.Equal(this.Auth.TenantId, decoded.TenantId);
50+
51+
// Does not verify in TenantAwareFirebaseAuth(other-tenant)
52+
var otherTenantAuth = FirebaseAuth.DefaultInstance.TenantManager
53+
.AuthForTenant("other-tenant");
54+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
55+
() => otherTenantAuth.VerifyIdTokenAsync(idToken));
56+
57+
Assert.Equal(AuthErrorCode.TenantIdMismatch, exception.AuthErrorCode);
58+
}
59+
3560
public class Fixture : AbstractAuthFixture<TenantAwareFirebaseAuth>, IDisposable
3661
{
3762
private readonly TenantFixture tenant;

FirebaseAdmin/FirebaseAdmin.Tests/Auth/Jwt/IdTokenVerificationTest.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,23 @@ await Assert.ThrowsAnyAsync<OperationCanceledException>(
419419
() => auth.VerifyIdTokenAsync(idToken, canceller.Token));
420420
}
421421

422-
[Theory]
423-
[MemberData(nameof(TestConfigs))]
424-
public async Task TenantIdMismatch(TestConfig config)
422+
[Fact]
423+
public async Task TenantId()
424+
{
425+
var tenantConfig = TestConfig.ForTenantAwareFirebaseAuth("test-tenant");
426+
var idTokenWithTenant = await tenantConfig.CreateIdTokenAsync();
427+
FirebaseAuth auth = (FirebaseAuth)TestConfig.ForFirebaseAuth().CreateAuth();
428+
429+
var decoded = await auth.VerifyIdTokenAsync(idTokenWithTenant);
430+
431+
tenantConfig.AssertFirebaseToken(decoded);
432+
Assert.Equal("test-tenant", decoded.TenantId);
433+
}
434+
435+
[Fact]
436+
public async Task TenantIdMismatch()
425437
{
438+
var config = TestConfig.ForTenantAwareFirebaseAuth("test-tenant");
426439
var payload = new Dictionary<string, object>()
427440
{
428441
{

FirebaseAdmin/FirebaseAdmin.Tests/Auth/Jwt/SessionCookieVerificationTest.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -383,29 +383,6 @@ public async Task CheckRevokedError(TestConfig config)
383383
JwtTestUtils.AssertRevocationCheckRequest(null, handler.Requests[0].Url);
384384
}
385385

386-
[Theory]
387-
[MemberData(nameof(TestConfigs))]
388-
public async Task TenantIdMismatch(TestConfig config)
389-
{
390-
var payload = new Dictionary<string, object>()
391-
{
392-
{
393-
"firebase", new Dictionary<string, object>
394-
{
395-
{ "tenant", "other-tenant" },
396-
}
397-
},
398-
};
399-
var idToken = await config.CreateSessionCookieAsync(payloadOverrides: payload);
400-
var auth = config.CreateAuth();
401-
402-
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
403-
async () => await auth.VerifySessionCookieAsync(idToken));
404-
405-
var expectedMessage = "Firebase session cookie has incorrect tenant ID.";
406-
this.CheckException(exception, expectedMessage, AuthErrorCode.TenantIdMismatch);
407-
}
408-
409386
private void CheckException(
410387
FirebaseAuthException exception,
411388
string prefix,

FirebaseAdmin/FirebaseAdmin/Auth/Jwt/FirebaseTokenVerifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ internal async Task<FirebaseToken> VerifyTokenAsync(
238238
{
239239
error = $"Firebase {this.shortName} has a subject claim longer than 128 characters.";
240240
}
241-
else if (this.TenantId != payload.Firebase?.Tenant)
241+
else if (this.TenantId != null && this.TenantId != payload.Firebase?.Tenant)
242242
{
243243
error = $"Firebase {this.shortName} has incorrect tenant ID. Expected "
244244
+ $"{this.TenantId} but got {payload.Firebase?.Tenant}";

0 commit comments

Comments
 (0)