Skip to content

Commit f87cfad

Browse files
author
renkelvin
authored
Implement token revocation public api (#11001)
1 parent c1ae7d1 commit f87cfad

File tree

12 files changed

+711
-1
lines changed

12 files changed

+711
-1
lines changed

FirebaseAuth/Sources/Auth/FIRAuth.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeResponse.h"
4848
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordRequest.h"
4949
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordResponse.h"
50+
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h"
51+
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h"
5052
#import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
5153
#import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeResponse.h"
5254
#import "FirebaseAuth/Sources/Backend/RPC/FIRSetAccountInfoRequest.h"
@@ -1544,6 +1546,34 @@ - (void)setAdditionalFrameworkMarker:(nullable NSString *)additionalFrameworkMar
15441546
});
15451547
}
15461548

1549+
- (void)revokeTokenWithAuthorizationCode:(NSString *)authorizationCode
1550+
completion:(nullable void (^)(NSError *_Nullable error))completion {
1551+
[self.currentUser
1552+
getIDTokenWithCompletion:^(NSString *_Nullable idToken, NSError *_Nullable error) {
1553+
if (completion) {
1554+
if (error) {
1555+
completion(error);
1556+
return;
1557+
}
1558+
}
1559+
FIRRevokeTokenRequest *request =
1560+
[[FIRRevokeTokenRequest alloc] initWithToken:authorizationCode
1561+
idToken:idToken
1562+
requestConfiguration:self->_requestConfiguration];
1563+
[FIRAuthBackend
1564+
revokeToken:request
1565+
callback:^(FIRRevokeTokenResponse *_Nullable response, NSError *_Nullable error) {
1566+
if (completion) {
1567+
if (error) {
1568+
completion(error);
1569+
} else {
1570+
completion(nil);
1571+
}
1572+
}
1573+
}];
1574+
}];
1575+
}
1576+
15471577
#if TARGET_OS_IOS
15481578
#pragma clang diagnostic push
15491579
#pragma clang diagnostic ignored "-Wunused-property-ivar"

FirebaseAuth/Sources/Backend/FIRAuthBackend.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
@class FIRSignInWithGameCenterResponse;
5555
@class FIRSignUpNewUserRequest;
5656
@class FIRSignUpNewUserResponse;
57+
@class FIRRevokeTokenRequest;
58+
@class FIRRevokeTokenResponse;
5759

5860
@protocol FIRAuthBackendImplementation;
5961
@protocol FIRAuthBackendRPCIssuer;
@@ -220,6 +222,15 @@ typedef void (^FIRVerifyPhoneNumberResponseCallback)(
220222
typedef void (^FIRVerifyClientResponseCallback)(FIRVerifyClientResponse *_Nullable response,
221223
NSError *_Nullable error);
222224

225+
/** @typedef FIRRevokeTokenResponseCallback
226+
@brief The type of block used to return the result of a call to the revokeToken endpoint.
227+
@param response The received response, if any.
228+
@param error The error which occurred, if any.
229+
@remarks One of response or error will be non-nil.
230+
*/
231+
typedef void (^FIRRevokeTokenResponseCallback)(FIRRevokeTokenResponse *_Nullable response,
232+
NSError *_Nullable error);
233+
223234
/** @typedef FIRSignInWithGameCenterResponseCallback
224235
@brief The type of block used to return the result of a call to the SignInWithGameCenter
225236
endpoint.
@@ -414,8 +425,18 @@ typedef void (^FIRSignInWithGameCenterResponseCallback)(
414425
*/
415426
+ (void)verifyClient:(FIRVerifyClientRequest *)request
416427
callback:(FIRVerifyClientResponseCallback)callback;
428+
417429
#endif
418430

431+
/** @fn revokeToken:callback:
432+
@brief Calls the revokeToken endpoint, which is responsible for revoking the given token
433+
provided in the request parameters.
434+
@param request The request parameters.
435+
@param callback The callback.
436+
*/
437+
+ (void)revokeToken:(FIRRevokeTokenRequest *)request
438+
callback:(FIRRevokeTokenResponseCallback)callback;
439+
419440
@end
420441

421442
/** @protocol FIRAuthBackendRPCIssuer
@@ -578,8 +599,18 @@ typedef void (^FIRSignInWithGameCenterResponseCallback)(
578599
*/
579600
- (void)verifyClient:(FIRVerifyClientRequest *)request
580601
callback:(FIRVerifyClientResponseCallback)callback;
602+
581603
#endif
582604

605+
/** @fn revokeToken:callback:
606+
@brief Calls the revokeToken endpoint, which is responsible for revoking the given token
607+
provided in the request parameters.
608+
@param request The request parameters.
609+
@param callback The callback.
610+
*/
611+
- (void)revokeToken:(FIRRevokeTokenRequest *)request
612+
callback:(FIRRevokeTokenResponseCallback)callback;
613+
583614
/** @fn SignInWithGameCenter:callback:
584615
@brief Calls the SignInWithGameCenter endpoint, which is responsible for authenticating a user
585616
who has Game Center credentials.

FirebaseAuth/Sources/Backend/FIRAuthBackend.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
#import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigResponse.h"
4545
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordRequest.h"
4646
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordResponse.h"
47+
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h"
48+
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h"
4749
#import "FirebaseAuth/Sources/Backend/RPC/FIRSecureTokenRequest.h"
4850
#import "FirebaseAuth/Sources/Backend/RPC/FIRSecureTokenResponse.h"
4951
#import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
@@ -606,8 +608,14 @@ + (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
606608
+ (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callback {
607609
[[self implementation] verifyClient:request callback:callback];
608610
}
611+
609612
#endif
610613

614+
+ (void)revokeToken:(FIRRevokeTokenRequest *)request
615+
callback:(FIRRevokeTokenResponseCallback)callback {
616+
[[self implementation] revokeToken:request callback:callback];
617+
}
618+
611619
+ (void)resetPassword:(FIRResetPasswordRequest *)request
612620
callback:(FIRResetPasswordCallback)callback {
613621
[[self implementation] resetPassword:request callback:callback];
@@ -989,8 +997,25 @@ - (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callb
989997
callback(response, nil);
990998
}];
991999
}
1000+
9921001
#endif
9931002

1003+
- (void)revokeToken:(FIRRevokeTokenRequest *)request
1004+
callback:(FIRRevokeTokenResponseCallback)callback {
1005+
FIRRevokeTokenResponse *response = [[FIRRevokeTokenResponse alloc] init];
1006+
[self
1007+
postWithRequest:request
1008+
response:response
1009+
callback:^(NSError *error) {
1010+
if (error) {
1011+
callback(nil, [FIRAuthErrorUtils
1012+
invalidCredentialErrorWithMessage:[error localizedDescription]]);
1013+
return;
1014+
}
1015+
callback(response, nil);
1016+
}];
1017+
}
1018+
9941019
- (void)resetPassword:(FIRResetPasswordRequest *)request
9951020
callback:(FIRResetPasswordCallback)callback {
9961021
FIRResetPasswordResponse *response = [[FIRResetPasswordResponse alloc] init];
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "FirebaseAuth/Sources/Backend/FIRAuthRPCRequest.h"
18+
#import "FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.h"
19+
20+
NS_ASSUME_NONNULL_BEGIN
21+
22+
@interface FIRRevokeTokenRequest : FIRIdentityToolkitRequest <FIRAuthRPCRequest>
23+
24+
/** @property providerID
25+
@brief The provider that issued the token to revoke.
26+
*/
27+
@property(nonatomic, copy, nullable) NSString *providerID;
28+
29+
/** @property tokenType
30+
@brief The type of the token to revoke.
31+
*/
32+
@property(nonatomic) NSInteger tokenType;
33+
34+
/** @property token
35+
@brief The token to be revoked.
36+
*/
37+
@property(nonatomic, copy, nullable) NSString *token;
38+
39+
/** @property idToken
40+
@brief The ID Token associated with this credential.
41+
*/
42+
@property(nonatomic, copy, nullable) NSString *idToken;
43+
44+
/** @fn initWithEndpoint:requestConfiguration:
45+
@brief Please use initWithToken:requestConfiguration: instead.
46+
*/
47+
- (nullable instancetype)initWithEndpoint:(NSString *)endpoint
48+
requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
49+
NS_UNAVAILABLE;
50+
51+
/** @fn initWithAppToken:isSandbox:requestConfiguration:
52+
@brief Designated initializer.
53+
@param token The token to be revoked.
54+
@param idToken The id token associated with the current user.
55+
@param requestConfiguration An object containing configurations to be added to the request.
56+
*/
57+
- (nullable instancetype)initWithToken:(NSString *)token
58+
idToken:(NSString *)idToken
59+
requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration;
60+
61+
@end
62+
63+
NS_ASSUME_NONNULL_END
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h"
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
/** @var kRevokeTokenEndpoint
22+
@brief The endpoint for the revokeToken request.
23+
*/
24+
static NSString *const kRevokeTokenEndpoint = @"accounts:revokeToken";
25+
26+
/** @var kProviderIDKey
27+
@brief The key for the provider that issued the token to revoke.
28+
*/
29+
static NSString *const kProviderIDKey = @"providerId";
30+
31+
/** @var kTokenTypeKey
32+
@brief The key for the type of the token to revoke.
33+
*/
34+
static NSString *const kTokenTypeKey = @"tokenType";
35+
36+
/** @var kTokenKey
37+
@brief The key for the token to be revoked.
38+
*/
39+
static NSString *const kTokenKey = @"token";
40+
41+
/** @var kIDTokenKey
42+
@brief The key for the ID Token associated with this credential.
43+
*/
44+
static NSString *const kIDTokenKey = @"idToken";
45+
46+
typedef NS_ENUM(NSInteger, FIRTokenType) {
47+
/** Indicates that the token type is unspecified.
48+
*/
49+
FIRTokenTypeUnspecified = 0,
50+
51+
/** Indicates that the token type is refresh token.
52+
*/
53+
FIRTokenTypeRefreshToken = 1,
54+
55+
/** Indicates that the token type is access token.
56+
*/
57+
FIRTokenTypeAccessToken = 2,
58+
59+
/** Indicates that the token type is authorization code.
60+
*/
61+
FIRTokenTypeAuthorizationCode = 3,
62+
};
63+
64+
@implementation FIRRevokeTokenRequest
65+
66+
- (nullable instancetype)initWithToken:(NSString *)token
67+
idToken:(NSString *)idToken
68+
requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
69+
self = [super initWithEndpoint:kRevokeTokenEndpoint
70+
requestConfiguration:requestConfiguration
71+
useIdentityPlatform:YES
72+
useStaging:NO];
73+
if (self) {
74+
// Apple and authorization code are the only provider and token type we support for now.
75+
// Generalize this initializer to accept other providers and token types once supported.
76+
_providerID = @"apple.com";
77+
_tokenType = FIRTokenTypeAuthorizationCode;
78+
_token = token;
79+
_idToken = idToken;
80+
}
81+
return self;
82+
}
83+
84+
- (nullable id)unencodedHTTPRequestBodyWithError:(NSError *__autoreleasing _Nullable *)error {
85+
NSMutableDictionary *postBody = [NSMutableDictionary dictionary];
86+
if (_providerID) {
87+
postBody[kProviderIDKey] = _providerID;
88+
}
89+
if (_tokenType) {
90+
postBody[kTokenTypeKey] = [NSNumber numberWithInteger:_tokenType].stringValue;
91+
}
92+
if (_token) {
93+
postBody[kTokenKey] = _token;
94+
}
95+
if (_idToken) {
96+
postBody[kIDTokenKey] = _idToken;
97+
}
98+
return [postBody copy];
99+
}
100+
101+
@end
102+
103+
NS_ASSUME_NONNULL_END
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import "FirebaseAuth/Sources/Backend/FIRAuthRPCResponse.h"
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@interface FIRRevokeTokenResponse : NSObject <FIRAuthRPCResponse>
24+
25+
@end
26+
27+
NS_ASSUME_NONNULL_END
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h"
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
@implementation FIRRevokeTokenResponse
22+
23+
- (BOOL)setWithDictionary:(NSDictionary *)dictionary error:(NSError *_Nullable *_Nullable)error {
24+
return YES;
25+
}
26+
27+
@end
28+
29+
NS_ASSUME_NONNULL_END

FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,14 @@ NS_SWIFT_NAME(Auth)
850850
*/
851851
- (BOOL)canHandleNotification:(NSDictionary *)userInfo API_UNAVAILABLE(macos, tvos, watchos);
852852

853+
/** @fn revokeTokenWithAuthorizationCode:Completion
854+
@brief Revoke the users token with authorization code.
855+
@param completion (Optional) the block invoked when the request to revoke the token is
856+
complete, or fails. Invoked asynchronously on the main thread in the future.
857+
*/
858+
- (void)revokeTokenWithAuthorizationCode:(NSString *)authorizationCode
859+
completion:(nullable void (^)(NSError *_Nullable error))completion;
860+
853861
#pragma mark - User sharing
854862

855863
/** @fn useUserAccessGroup:error:

0 commit comments

Comments
 (0)