Skip to content
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

Implement token revocation public api #11001

Merged
merged 66 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
a912400
Initial commit
Dec 15, 2022
f362fe6
format
Dec 15, 2022
35cfee9
format
Dec 15, 2022
08d398d
License
Dec 15, 2022
23a1c24
Revert "format"
Dec 15, 2022
b96443c
Update FIRAuthBackend.h
Dec 15, 2022
12cddca
init tests
Dec 15, 2022
390a43d
Revert "init tests"
Dec 15, 2022
190b5c3
init tests
Dec 15, 2022
01c2273
Merge branch 'master' into token-revoke
Jan 4, 2023
9ec1ec3
format
Jan 4, 2023
c689004
tmp disable checks
Jan 4, 2023
353dbef
format
Jan 4, 2023
69ce74e
Update FIRRevokeTokenRequestTests.m
Jan 4, 2023
e0aaddf
Update FIRAuthBackend.m
Jan 4, 2023
0e5d600
Update FIRAuthBackend.m
Jan 4, 2023
91fb845
Update FIRAuthBackend.h
Jan 4, 2023
82323a1
pr issues
Jan 5, 2023
3dc34ff
complete request
Jan 10, 2023
0b121f4
Fix pr issues
Jan 10, 2023
b6e7b01
Update FIRRevokeTokenRequestTests.m
Jan 10, 2023
4ca686b
Fix pr issues
Jan 11, 2023
7516a47
Fix pr issues
Jan 11, 2023
a2bf8a9
Fix API docs
peterfriese Feb 6, 2023
520cff3
Update FIRAuth.h
Feb 7, 2023
7c2e1af
tmp updates
Mar 10, 2023
7577d3f
v2 prod
Mar 23, 2023
de5a8e2
Update FIRRevokeTokenRequest.m
Mar 23, 2023
82728bd
Update MainViewController+OAuth.m
Mar 23, 2023
83fe1dd
Initial commit
Dec 15, 2022
6173f05
format
Dec 15, 2022
93c2a96
format
Dec 15, 2022
3a81cc6
License
Dec 15, 2022
db6b627
Revert "format"
Dec 15, 2022
26b2a9d
Update FIRAuthBackend.h
Dec 15, 2022
70b5287
init tests
Dec 15, 2022
4bf47de
Revert "init tests"
Dec 15, 2022
45c7958
init tests
Dec 15, 2022
8d7ae94
format
Jan 4, 2023
e7f47ec
tmp disable checks
Jan 4, 2023
f6b0745
format
Jan 4, 2023
9e2ec8b
Update FIRRevokeTokenRequestTests.m
Jan 4, 2023
00fd739
Update FIRAuthBackend.m
Jan 4, 2023
b7deb32
Update FIRAuthBackend.m
Jan 4, 2023
e3cd757
Update FIRAuthBackend.h
Jan 4, 2023
4ed4c14
pr issues
Jan 5, 2023
147b473
complete request
Jan 10, 2023
64b72bd
Fix pr issues
Jan 10, 2023
69649d1
Update FIRRevokeTokenRequestTests.m
Jan 10, 2023
36fce19
Fix pr issues
Jan 11, 2023
e4cc427
Fix pr issues
Jan 11, 2023
071b48d
Fix API docs
peterfriese Feb 6, 2023
4e8bf64
Update FIRAuth.h
Feb 7, 2023
78ab016
tmp updates
Mar 10, 2023
f6e0644
v2 prod
Mar 23, 2023
ec19660
Update FIRRevokeTokenRequest.m
Mar 23, 2023
9207351
Update MainViewController+OAuth.m
Mar 23, 2023
38526b4
Update FIRRevokeTokenRequestTests.m
Mar 23, 2023
4c890ed
Merge branch 'token-revoke' of https://github.com/firebase/firebase-i…
Mar 23, 2023
75951fa
format
Mar 23, 2023
49e67f2
ci fix
Mar 23, 2023
5fd0924
support auth code revocation
Mar 30, 2023
90ac545
rename
Apr 3, 2023
297a5a5
Update MainViewController+OAuth.m
Apr 3, 2023
fde1ba7
style
Apr 3, 2023
3a71bb4
update tests
Apr 3, 2023
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
30 changes: 30 additions & 0 deletions FirebaseAuth/Sources/Auth/FIRAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRSetAccountInfoRequest.h"
Expand Down Expand Up @@ -1540,6 +1542,34 @@ - (void)setAdditionalFrameworkMarker:(nullable NSString *)additionalFrameworkMar
});
}

- (void)revokeTokenWithAuthorizationCode:(NSString *)authorizationCode
completion:(nullable void (^)(NSError *_Nullable error))completion {
[self.currentUser
getIDTokenWithCompletion:^(NSString *_Nullable idToken, NSError *_Nullable error) {
if (completion) {
if (error) {
completion(error);
return;
}
}
FIRRevokeTokenRequest *request =
[[FIRRevokeTokenRequest alloc] initWithToken:authorizationCode
idToken:idToken
requestConfiguration:self->_requestConfiguration];
[FIRAuthBackend
revokeToken:request
callback:^(FIRRevokeTokenResponse *_Nullable response, NSError *_Nullable error) {
if (completion) {
if (error) {
completion(error);
} else {
completion(nil);
}
}
}];
}];
}

#if TARGET_OS_IOS
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-property-ivar"
Expand Down
31 changes: 31 additions & 0 deletions FirebaseAuth/Sources/Backend/FIRAuthBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
@class FIRSignInWithGameCenterResponse;
@class FIRSignUpNewUserRequest;
@class FIRSignUpNewUserResponse;
@class FIRRevokeTokenRequest;
@class FIRRevokeTokenResponse;

@protocol FIRAuthBackendImplementation;
@protocol FIRAuthBackendRPCIssuer;
Expand Down Expand Up @@ -220,6 +222,15 @@ typedef void (^FIRVerifyPhoneNumberResponseCallback)(
typedef void (^FIRVerifyClientResponseCallback)(FIRVerifyClientResponse *_Nullable response,
NSError *_Nullable error);

/** @typedef FIRRevokeTokenResponseCallback
@brief The type of block used to return the result of a call to the revokeToken endpoint.
@param response The received response, if any.
@param error The error which occurred, if any.
@remarks One of response or error will be non-nil.
*/
typedef void (^FIRRevokeTokenResponseCallback)(FIRRevokeTokenResponse *_Nullable response,
NSError *_Nullable error);

/** @typedef FIRSignInWithGameCenterResponseCallback
@brief The type of block used to return the result of a call to the SignInWithGameCenter
endpoint.
Expand Down Expand Up @@ -414,8 +425,18 @@ typedef void (^FIRSignInWithGameCenterResponseCallback)(
*/
+ (void)verifyClient:(FIRVerifyClientRequest *)request
callback:(FIRVerifyClientResponseCallback)callback;

#endif

/** @fn revokeToken:callback:
@brief Calls the revokeToken endpoint, which is responsible for revoking the given token
provided in the request parameters.
@param request The request parameters.
@param callback The callback.
*/
+ (void)revokeToken:(FIRRevokeTokenRequest *)request
callback:(FIRRevokeTokenResponseCallback)callback;

@end

/** @protocol FIRAuthBackendRPCIssuer
Expand Down Expand Up @@ -578,8 +599,18 @@ typedef void (^FIRSignInWithGameCenterResponseCallback)(
*/
- (void)verifyClient:(FIRVerifyClientRequest *)request
callback:(FIRVerifyClientResponseCallback)callback;

#endif

/** @fn revokeToken:callback:
@brief Calls the revokeToken endpoint, which is responsible for revoking the given token
provided in the request parameters.
@param request The request parameters.
@param callback The callback.
*/
- (void)revokeToken:(FIRRevokeTokenRequest *)request
callback:(FIRRevokeTokenResponseCallback)callback;

/** @fn SignInWithGameCenter:callback:
@brief Calls the SignInWithGameCenter endpoint, which is responsible for authenticating a user
who has Game Center credentials.
Expand Down
25 changes: 25 additions & 0 deletions FirebaseAuth/Sources/Backend/FIRAuthBackend.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRSecureTokenRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRSecureTokenResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
Expand Down Expand Up @@ -603,8 +605,14 @@ + (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
+ (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callback {
[[self implementation] verifyClient:request callback:callback];
}

#endif

+ (void)revokeToken:(FIRRevokeTokenRequest *)request
callback:(FIRRevokeTokenResponseCallback)callback {
[[self implementation] revokeToken:request callback:callback];
}

+ (void)resetPassword:(FIRResetPasswordRequest *)request
callback:(FIRResetPasswordCallback)callback {
[[self implementation] resetPassword:request callback:callback];
Expand Down Expand Up @@ -967,8 +975,25 @@ - (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callb
callback(response, nil);
}];
}

#endif

- (void)revokeToken:(FIRRevokeTokenRequest *)request
callback:(FIRRevokeTokenResponseCallback)callback {
FIRRevokeTokenResponse *response = [[FIRRevokeTokenResponse alloc] init];
[self
postWithRequest:request
response:response
callback:^(NSError *error) {
if (error) {
callback(nil, [FIRAuthErrorUtils
invalidCredentialErrorWithMessage:[error localizedDescription]]);
return;
}
callback(response, nil);
}];
}

- (void)resetPassword:(FIRResetPasswordRequest *)request
callback:(FIRResetPasswordCallback)callback {
FIRResetPasswordResponse *response = [[FIRResetPasswordResponse alloc] init];
Expand Down
63 changes: 63 additions & 0 deletions FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "FirebaseAuth/Sources/Backend/FIRAuthRPCRequest.h"
#import "FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRRevokeTokenRequest : FIRIdentityToolkitRequest <FIRAuthRPCRequest>

/** @property providerID
@brief The provider that issued the token to revoke.
*/
@property(nonatomic, copy, nullable) NSString *providerID;

/** @property tokenType
@brief The type of the token to revoke.
*/
@property(nonatomic) NSInteger tokenType;

/** @property token
@brief The token to be revoked.
*/
@property(nonatomic, copy, nullable) NSString *token;

/** @property idToken
@brief The ID Token associated with this credential.
*/
@property(nonatomic, copy, nullable) NSString *idToken;

/** @fn initWithEndpoint:requestConfiguration:
@brief Please use initWithToken:requestConfiguration: instead.
*/
- (nullable instancetype)initWithEndpoint:(NSString *)endpoint
requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
NS_UNAVAILABLE;

/** @fn initWithAppToken:isSandbox:requestConfiguration:
@brief Designated initializer.
@param token The token to be revoked.
@param idToken The id token associated with the current user.
@param requestConfiguration An object containing configurations to be added to the request.
*/
- (nullable instancetype)initWithToken:(NSString *)token
idToken:(NSString *)idToken
requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration;

@end

NS_ASSUME_NONNULL_END
103 changes: 103 additions & 0 deletions FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h"

NS_ASSUME_NONNULL_BEGIN

/** @var kRevokeTokenEndpoint
@brief The endpoint for the revokeToken request.
*/
static NSString *const kRevokeTokenEndpoint = @"accounts:revokeToken";

/** @var kProviderIDKey
@brief The key for the provider that issued the token to revoke.
*/
static NSString *const kProviderIDKey = @"providerId";

/** @var kTokenTypeKey
@brief The key for the type of the token to revoke.
*/
static NSString *const kTokenTypeKey = @"tokenType";

/** @var kTokenKey
@brief The key for the token to be revoked.
*/
static NSString *const kTokenKey = @"token";

/** @var kIDTokenKey
@brief The key for the ID Token associated with this credential.
*/
static NSString *const kIDTokenKey = @"idToken";

typedef NS_ENUM(NSInteger, FIRTokenType) {
/** Indicates that the token type is unspecified.
*/
FIRTokenTypeUnspecified = 0,

/** Indicates that the token type is refresh token.
*/
FIRTokenTypeRefreshToken = 1,

/** Indicates that the token type is access token.
*/
FIRTokenTypeAccessToken = 2,

/** Indicates that the token type is authorization code.
*/
FIRTokenTypeAuthorizationCode = 3,
};

@implementation FIRRevokeTokenRequest

- (nullable instancetype)initWithToken:(NSString *)token
idToken:(NSString *)idToken
requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
self = [super initWithEndpoint:kRevokeTokenEndpoint
requestConfiguration:requestConfiguration
useIdentityPlatform:YES
useStaging:NO];
if (self) {
// Apple and authorization code are the only provider and token type we support for now.
// Generalize this initializer to accept other providers and token types once supported.
_providerID = @"apple.com";
_tokenType = FIRTokenTypeAuthorizationCode;
_token = token;
_idToken = idToken;
}
return self;
}

- (nullable id)unencodedHTTPRequestBodyWithError:(NSError *__autoreleasing _Nullable *)error {
NSMutableDictionary *postBody = [NSMutableDictionary dictionary];
if (_providerID) {
postBody[kProviderIDKey] = _providerID;
}
if (_tokenType) {
postBody[kTokenTypeKey] = [NSNumber numberWithInteger:_tokenType].stringValue;
}
if (_token) {
postBody[kTokenKey] = _token;
}
if (_idToken) {
postBody[kIDTokenKey] = _idToken;
}
return [postBody copy];
}

@end

NS_ASSUME_NONNULL_END
27 changes: 27 additions & 0 deletions FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "FirebaseAuth/Sources/Backend/FIRAuthRPCResponse.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRRevokeTokenResponse : NSObject <FIRAuthRPCResponse>

@end

NS_ASSUME_NONNULL_END
29 changes: 29 additions & 0 deletions FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h"

NS_ASSUME_NONNULL_BEGIN

@implementation FIRRevokeTokenResponse

- (BOOL)setWithDictionary:(NSDictionary *)dictionary error:(NSError *_Nullable *_Nullable)error {
return YES;
}

@end

NS_ASSUME_NONNULL_END
8 changes: 8 additions & 0 deletions FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,14 @@ NS_SWIFT_NAME(Auth)
*/
- (BOOL)canHandleNotification:(NSDictionary *)userInfo API_UNAVAILABLE(macos, tvos, watchos);

/** @fn revokeTokenWithAuthorizationCode:Completion
@brief Revoke the users token with authorization code.
@param completion (Optional) the block invoked when the request to revoke the token is
complete, or fails. Invoked asynchronously on the main thread in the future.
*/
- (void)revokeTokenWithAuthorizationCode:(NSString *)authorizationCode
completion:(nullable void (^)(NSError *_Nullable error))completion;

#pragma mark - User sharing

/** @fn useUserAccessGroup:error:
Expand Down
Loading