Skip to content

Commit 53184e8

Browse files
authored
ID Token Verification API (#3)
* ID token verification and unit tests * Added documentation * More documentation * More documentation and cleanup * Documentation * Formatting changes * Added a test case * Code formatting changes * Implementing signature verification as a LINQ query * Separated FirebaseToken interface from impl; Added documentation * Renamed IFirebaseToken back to FirebaseToken to stick with the approved API. Using FirebaseTokenArgs class to separate interface from impl * Removed Uid from FirebaseTokenArgs
1 parent 47753b9 commit 53184e8

File tree

15 files changed

+1342
-2
lines changed

15 files changed

+1342
-2
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2018, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.IO;
18+
using System.Security.Cryptography;
19+
using System.Security.Cryptography.X509Certificates;
20+
using System.Text;
21+
using System.Threading;
22+
using System.Threading.Tasks;
23+
using Xunit;
24+
using FirebaseAdmin.Auth;
25+
using Google.Apis.Auth;
26+
using Google.Apis.Auth.OAuth2;
27+
28+
[assembly: CollectionBehavior(DisableTestParallelization = true)]
29+
namespace FirebaseAdmin.Auth.Tests
30+
{
31+
public class FirebaseAuthTest: IDisposable
32+
{
33+
private static readonly GoogleCredential mockCredential =
34+
GoogleCredential.FromAccessToken("test-token");
35+
36+
[Fact]
37+
public void GetAuthWithoutApp()
38+
{
39+
Assert.Null(FirebaseAuth.DefaultInstance);
40+
}
41+
42+
[Fact]
43+
public void GetDefaultAuth()
44+
{
45+
var app = FirebaseApp.Create(new AppOptions(){Credential = mockCredential});
46+
FirebaseAuth auth = FirebaseAuth.DefaultInstance;
47+
Assert.Same(auth, FirebaseAuth.DefaultInstance);
48+
app.Delete();
49+
Assert.Null(FirebaseAuth.DefaultInstance);
50+
}
51+
52+
[Fact]
53+
public void GetAuth()
54+
{
55+
var app = FirebaseApp.Create(new AppOptions(){Credential = mockCredential}, "MyApp");
56+
FirebaseAuth auth = FirebaseAuth.GetAuth(app);
57+
Assert.Same(auth, FirebaseAuth.GetAuth(app));
58+
app.Delete();
59+
Assert.Throws<InvalidOperationException>(() => FirebaseAuth.GetAuth(app));
60+
}
61+
62+
[Fact]
63+
public async Task VerifyIdTokenNoProjectId()
64+
{
65+
FirebaseApp.Create(new AppOptions(){Credential = mockCredential});
66+
var idToken = await FirebaseTokenVerifierTest.CreateTestTokenAsync();
67+
await Assert.ThrowsAsync<ArgumentException>(
68+
async () => await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken));
69+
}
70+
71+
[Fact]
72+
public async Task VerifyIdTokenCancel()
73+
{
74+
FirebaseApp.Create(new AppOptions()
75+
{
76+
Credential = mockCredential,
77+
ProjectId = "test-project",
78+
});
79+
var canceller = new CancellationTokenSource();
80+
canceller.Cancel();
81+
var idToken = await FirebaseTokenVerifierTest.CreateTestTokenAsync();
82+
await Assert.ThrowsAsync<OperationCanceledException>(
83+
async () => await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(
84+
idToken, canceller.Token));
85+
}
86+
87+
public void Dispose()
88+
{
89+
FirebaseApp.DeleteAll();
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)