Skip to content

Commit de11547

Browse files
authored
fix(auth): Integration test for RevokeRefreshTokensAsync() (#173)
* fix(auth): Integration test for RevokeRefreshTokensAsync() * Adding snippets
1 parent d2ac9c6 commit de11547

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAuthTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ public async Task CreateCustomTokenWithoutServiceAccount()
109109
}
110110
}
111111

112+
[Fact]
113+
public async Task RevokeRefreshTokens()
114+
{
115+
var customToken = await FirebaseAuth.DefaultInstance
116+
.CreateCustomTokenAsync("testuser");
117+
var idToken = await SignInWithCustomTokenAsync(customToken);
118+
var decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken, true);
119+
Assert.Equal("testuser", decoded.Uid);
120+
121+
await Task.Delay(1000);
122+
await FirebaseAuth.DefaultInstance.RevokeRefreshTokensAsync("testuser");
123+
124+
decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken, false);
125+
Assert.Equal("testuser", decoded.Uid);
126+
127+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
128+
async () => await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken, true));
129+
Assert.Equal(ErrorCode.InvalidArgument, exception.ErrorCode);
130+
Assert.Equal(AuthErrorCode.RevokedIdToken, exception.AuthErrorCode);
131+
132+
idToken = await SignInWithCustomTokenAsync(customToken);
133+
decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken, true);
134+
Assert.Equal("testuser", decoded.Uid);
135+
}
136+
112137
[Fact]
113138
public async Task SetCustomUserClaims()
114139
{

FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAuthSnippets.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,44 @@ internal static async Task SetCustomUserClaimsIncrementalAsync()
246246
// [END set_custom_user_claims_incremental]
247247
}
248248

249+
internal static async Task RevokeIdTokens(string idToken)
250+
{
251+
string uid = "someUid";
252+
// [START revoke_tokens]
253+
await FirebaseAuth.DefaultInstance.RevokeRefreshTokensAsync(uid);
254+
var user = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
255+
Console.WriteLine("Tokens revoked at: " + user.TokensValidAfterTimestamp);
256+
// [END revoke_tokens]
257+
}
258+
259+
internal static async Task VerifyIdTokenCheckRevoked(string idToken)
260+
{
261+
// [START verify_id_token_check_revoked]
262+
try
263+
{
264+
// Verify the ID token while checking if the token is revoked by passing checkRevoked
265+
// as true.
266+
bool checkRevoked = true;
267+
var decodedToken = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(
268+
idToken, checkRevoked);
269+
// Token is valid and not revoked.
270+
string uid = decodedToken.Uid;
271+
}
272+
catch (FirebaseAuthException ex)
273+
{
274+
if (ex.AuthErrorCode == AuthErrorCode.RevokedIdToken)
275+
{
276+
// Token has been revoked. Inform the user to re-authenticate or signOut() the user.
277+
}
278+
else
279+
{
280+
// Token is invalid.
281+
}
282+
}
283+
284+
// [END verify_id_token_check_revoked]
285+
}
286+
249287
internal static ActionCodeSettings InitActionCodeSettings()
250288
{
251289
// [START init_action_code_settings]

0 commit comments

Comments
 (0)