Skip to content

fix(auth): Support for verifying tenant ID tokens in FirebaseAuth #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.Threading.Tasks;
using FirebaseAdmin.Auth;
using FirebaseAdmin.Auth.Multitenancy;
using Xunit;
Expand All @@ -32,6 +33,30 @@ public void TenantId()
Assert.NotEmpty(this.Auth.TenantId);
}

[Fact]
public async Task VerifyIdTokenWithTenant()
{
var customToken = await this.Auth.CreateCustomTokenAsync("testuser");
var idToken = await AuthIntegrationUtils.SignInWithCustomTokenAsync(
customToken, this.Auth.TenantId);

// Verifies in FirebaseAuth
var decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
Assert.Equal(this.Auth.TenantId, decoded.TenantId);

// Verifies in TenantAwareFirebaseAuth(matching-tenant)
decoded = await this.Auth.VerifyIdTokenAsync(idToken);
Assert.Equal(this.Auth.TenantId, decoded.TenantId);

// Does not verify in TenantAwareFirebaseAuth(other-tenant)
var otherTenantAuth = FirebaseAuth.DefaultInstance.TenantManager
.AuthForTenant("other-tenant");
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
() => otherTenantAuth.VerifyIdTokenAsync(idToken));

Assert.Equal(AuthErrorCode.TenantIdMismatch, exception.AuthErrorCode);
}

public class Fixture : AbstractAuthFixture<TenantAwareFirebaseAuth>, IDisposable
{
private readonly TenantFixture tenant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,23 @@ await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => auth.VerifyIdTokenAsync(idToken, canceller.Token));
}

[Theory]
[MemberData(nameof(TestConfigs))]
public async Task TenantIdMismatch(TestConfig config)
[Fact]
public async Task TenantId()
{
var tenantConfig = TestConfig.ForTenantAwareFirebaseAuth("test-tenant");
var idTokenWithTenant = await tenantConfig.CreateIdTokenAsync();
FirebaseAuth auth = (FirebaseAuth)TestConfig.ForFirebaseAuth().CreateAuth();

var decoded = await auth.VerifyIdTokenAsync(idTokenWithTenant);

tenantConfig.AssertFirebaseToken(decoded);
Assert.Equal("test-tenant", decoded.TenantId);
}

[Fact]
public async Task TenantIdMismatch()
{
var config = TestConfig.ForTenantAwareFirebaseAuth("test-tenant");
var payload = new Dictionary<string, object>()
{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,29 +383,6 @@ public async Task CheckRevokedError(TestConfig config)
JwtTestUtils.AssertRevocationCheckRequest(null, handler.Requests[0].Url);
}

[Theory]
[MemberData(nameof(TestConfigs))]
public async Task TenantIdMismatch(TestConfig config)
{
var payload = new Dictionary<string, object>()
{
{
"firebase", new Dictionary<string, object>
{
{ "tenant", "other-tenant" },
}
},
};
var idToken = await config.CreateSessionCookieAsync(payloadOverrides: payload);
var auth = config.CreateAuth();

var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
async () => await auth.VerifySessionCookieAsync(idToken));

var expectedMessage = "Firebase session cookie has incorrect tenant ID.";
this.CheckException(exception, expectedMessage, AuthErrorCode.TenantIdMismatch);
}

private void CheckException(
FirebaseAuthException exception,
string prefix,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ internal async Task<FirebaseToken> VerifyTokenAsync(
{
error = $"Firebase {this.shortName} has a subject claim longer than 128 characters.";
}
else if (this.TenantId != payload.Firebase?.Tenant)
else if (this.TenantId != null && this.TenantId != payload.Firebase?.Tenant)
{
error = $"Firebase {this.shortName} has incorrect tenant ID. Expected "
+ $"{this.TenantId} but got {payload.Firebase?.Tenant}";
Expand Down