Skip to content

feat(auth): Adding tenant-aware token verification support #230

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 13, 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
1 change: 1 addition & 0 deletions FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseAuthTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void NoTenantId()
FirebaseAuth auth = FirebaseAuth.DefaultInstance;

Assert.Null(auth.TokenFactory.TenantId);
Assert.Null(auth.IdTokenVerifier.TenantId);
Assert.Null(auth.UserManager.TenantId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,26 @@
// limitations under the License.

using System;
using System.Collections.Generic;
using Google.Apis.Auth.OAuth2;
using Xunit;

namespace FirebaseAdmin.Auth.Jwt.Tests
{
public class FirebaseTokenVerifierTest : IDisposable
{
public static readonly IEnumerable<object[]> InvalidStrings = new List<object[]>
{
new object[] { null },
new object[] { string.Empty },
};

private static readonly GoogleCredential MockCredential =
GoogleCredential.FromAccessToken("test-token");

private static readonly IPublicKeySource KeySource = new FileSystemPublicKeySource(
"./resources/public_cert.pem");

[Fact]
public void NoProjectId()
{
Expand Down Expand Up @@ -85,6 +95,85 @@ public void ProjectIdFromEnvironment()
}
}

[Theory]
[MemberData(nameof(InvalidStrings))]
public void InvalidProjectId(string projectId)
{
var args = FirebaseTokenVerifierArgs.ForIdTokens(projectId, KeySource);

Assert.Throws<ArgumentException>(() => new FirebaseTokenVerifier(args));
}

[Fact]
public void NullKeySource()
{
var args = FirebaseTokenVerifierArgs.ForIdTokens("test-project", null);

Assert.Throws<ArgumentNullException>(() => new FirebaseTokenVerifier(args));
}

[Theory]
[MemberData(nameof(InvalidStrings))]
public void InvalidShortName(string shortName)
{
var args = FirebaseTokenVerifierArgs.ForIdTokens("test-project", KeySource);
args.ShortName = shortName;

Assert.Throws<ArgumentException>(() => new FirebaseTokenVerifier(args));
}

[Theory]
[MemberData(nameof(InvalidStrings))]
public void InvalidIssuer(string issuer)
{
var args = FirebaseTokenVerifierArgs.ForIdTokens("test-project", KeySource);
args.Issuer = issuer;

Assert.Throws<ArgumentException>(() => new FirebaseTokenVerifier(args));
}

[Theory]
[MemberData(nameof(InvalidStrings))]
public void InvalidOperation(string operation)
{
var args = FirebaseTokenVerifierArgs.ForIdTokens("test-project", KeySource);
args.Operation = operation;

Assert.Throws<ArgumentException>(() => new FirebaseTokenVerifier(args));
}

[Theory]
[MemberData(nameof(InvalidStrings))]
public void InvalidUrl(string url)
{
var args = FirebaseTokenVerifierArgs.ForIdTokens("test-project", KeySource);
args.Url = url;

Assert.Throws<ArgumentException>(() => new FirebaseTokenVerifier(args));
}

[Fact]
public void TenantId()
{
var args = FirebaseTokenVerifierArgs.ForIdTokens(
"test-project", KeySource, tenantId: "test-tenant");

var verifier = new FirebaseTokenVerifier(args);

Assert.Equal("test-tenant", verifier.TenantId);
}

[Fact]
public void EmptyTenantId()
{
var args = FirebaseTokenVerifierArgs.ForIdTokens(
"test-project", KeySource, tenantId: string.Empty);

var ex = Assert.Throws<ArgumentException>(() => new FirebaseTokenVerifier(args));

Assert.Equal("Tenant ID must not be empty.", ex.Message);
}

public void Dispose()
{
FirebaseApp.DeleteAll();
Expand Down
Loading