File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed
FirebaseAdmin.IntegrationTests Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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]
You can’t perform that action at this time.
0 commit comments