diff options
Diffstat (limited to 'Firestore/Source/Auth')
-rw-r--r-- | Firestore/Source/Auth/FSTCredentialsProvider.h | 88 | ||||
-rw-r--r-- | Firestore/Source/Auth/FSTCredentialsProvider.mm | 157 | ||||
-rw-r--r-- | Firestore/Source/Auth/FSTEmptyCredentialsProvider.h | 28 | ||||
-rw-r--r-- | Firestore/Source/Auth/FSTEmptyCredentialsProvider.mm | 53 |
4 files changed, 0 insertions, 326 deletions
diff --git a/Firestore/Source/Auth/FSTCredentialsProvider.h b/Firestore/Source/Auth/FSTCredentialsProvider.h deleted file mode 100644 index d2f04e0..0000000 --- a/Firestore/Source/Auth/FSTCredentialsProvider.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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> - -#include "Firestore/core/src/firebase/firestore/auth/token.h" -#include "Firestore/core/src/firebase/firestore/auth/user.h" - -NS_ASSUME_NONNULL_BEGIN - -@class FIRApp; -@class FSTDispatchQueue; - -#pragma mark - Typedefs - -/** - * `FSTVoidTokenErrorBlock` is a block that gets a token or an error. - * - * @param token An auth token, either valid or invalid when error occurred. - * @param error The error if one occurred, or else `nil`. - */ -typedef void (^FSTVoidGetTokenResultBlock)(firebase::firestore::auth::Token token, - NSError *_Nullable error); - -/** Listener block notified with a User. */ -typedef void (^FSTVoidUserBlock)(firebase::firestore::auth::User user); - -#pragma mark - FSTCredentialsProvider - -/** Provides methods for getting the uid and token for the current user and listen for changes. */ -@protocol FSTCredentialsProvider <NSObject> - -/** Requests token for the current user, optionally forcing a refreshed token to be fetched. */ -- (void)getTokenForcingRefresh:(BOOL)forceRefresh completion:(FSTVoidGetTokenResultBlock)completion; - -/** - * A listener to be notified of user changes (sign-in / sign-out). It is immediately called once - * with the initial user. - * - * Note that this block will be called back on an arbitrary thread that is not the normal Firestore - * worker thread. - */ -@property(nonatomic, copy, nullable, readwrite) FSTVoidUserBlock userChangeListener; - -@end - -#pragma mark - FSTFirebaseCredentialsProvider - -/** - * `FSTFirebaseCredentialsProvider` uses Firebase Auth via `FIRApp` to get an auth token. - * - * NOTE: To simplify the implementation, it requires that you set `userChangeListener` with a - * non-`nil` value no more than once and don't call `getTokenForcingRefresh:` after setting - * it to `nil`. - * - * This class must be implemented in a thread-safe manner since it is accessed from the thread - * backing our internal worker queue and the callbacks from FIRAuth will be executed on an - * arbitrary different thread. - */ -@interface FSTFirebaseCredentialsProvider : NSObject <FSTCredentialsProvider> - -/** - * Initializes a new FSTFirebaseCredentialsProvider. - * - * @param app The Firebase app from which to get credentials. - * - * @return A new instance of FSTFirebaseCredentialsProvider. - */ -- (instancetype)initWithApp:(FIRApp *)app NS_DESIGNATED_INITIALIZER; - -- (id)init NS_UNAVAILABLE; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Auth/FSTCredentialsProvider.mm b/Firestore/Source/Auth/FSTCredentialsProvider.mm deleted file mode 100644 index e0dc8aa..0000000 --- a/Firestore/Source/Auth/FSTCredentialsProvider.mm +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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 "Firestore/Source/Auth/FSTCredentialsProvider.h" - -#import <FirebaseCore/FIRApp.h> -#import <FirebaseCore/FIRAppInternal.h> -#import <GRPCClient/GRPCCall.h> - -#import "FIRFirestoreErrors.h" -#import "Firestore/Source/Util/FSTAssert.h" -#import "Firestore/Source/Util/FSTClasses.h" -#import "Firestore/Source/Util/FSTDispatchQueue.h" - -#include "Firestore/core/src/firebase/firestore/auth/token.h" -#include "Firestore/core/src/firebase/firestore/auth/user.h" -#include "Firestore/core/src/firebase/firestore/util/string_apple.h" - -namespace util = firebase::firestore::util; -using firebase::firestore::auth::Token; -using firebase::firestore::auth::User; - -NS_ASSUME_NONNULL_BEGIN - -#pragma mark - FSTFirebaseCredentialsProvider -@interface FSTFirebaseCredentialsProvider () { - /** The current user as reported to us via our AuthStateDidChangeListener. */ - User _currentUser; -} - -@property(nonatomic, strong, readonly) FIRApp *app; - -/** Handle used to stop receiving auth changes once userChangeListener is removed. */ -@property(nonatomic, strong, nullable, readwrite) id<NSObject> authListenerHandle; - -/** - * Counter used to detect if the user changed while a -getTokenForcingRefresh: request was - * outstanding. - */ -@property(nonatomic, assign, readwrite) int userCounter; - -@end - -@implementation FSTFirebaseCredentialsProvider { - FSTVoidUserBlock _userChangeListener; -} - -- (instancetype)initWithApp:(FIRApp *)app { - self = [super init]; - if (self) { - _app = app; - _currentUser = User::FromUid([self.app getUID]); - _userCounter = 0; - - // Register for user changes so that we can internally track the current user. - FSTWeakify(self); - _authListenerHandle = [[NSNotificationCenter defaultCenter] - addObserverForName:FIRAuthStateDidChangeInternalNotification - object:nil - queue:nil - usingBlock:^(NSNotification *notification) { - FSTStrongify(self); - if (self) { - @synchronized(self) { - NSDictionary *userInfo = notification.userInfo; - - // ensure we're only notifiying for the current app. - FIRApp *notifiedApp = - userInfo[FIRAuthStateDidChangeInternalNotificationAppKey]; - if (![self.app isEqual:notifiedApp]) { - return; - } - - NSString *uid = userInfo[FIRAuthStateDidChangeInternalNotificationUIDKey]; - User newUser = User::FromUid(uid); - if (newUser != self->_currentUser) { - self->_currentUser = newUser; - self.userCounter++; - FSTVoidUserBlock listenerBlock = self.userChangeListener; - if (listenerBlock) { - listenerBlock(self->_currentUser); - } - } - } - } - }]; - } - return self; -} - -- (void)getTokenForcingRefresh:(BOOL)forceRefresh - completion:(FSTVoidGetTokenResultBlock)completion { - FSTAssert(self.authListenerHandle, @"getToken cannot be called after listener removed."); - - // Take note of the current value of the userCounter so that this method can fail (with a - // FIRFirestoreErrorCodeAborted error) if there is a user change while the request is outstanding. - int initialUserCounter = self.userCounter; - - void (^getTokenCallback)(NSString *, NSError *) = - ^(NSString *_Nullable token, NSError *_Nullable error) { - @synchronized(self) { - if (initialUserCounter != self.userCounter) { - // Cancel the request since the user changed while the request was outstanding so the - // response is likely for a previous user (which user, we can't be sure). - NSDictionary *errorInfo = @{@"details" : @"getToken aborted due to user change."}; - NSError *cancelError = [NSError errorWithDomain:FIRFirestoreErrorDomain - code:FIRFirestoreErrorCodeAborted - userInfo:errorInfo]; - completion(Token::Invalid(), cancelError); - } else { - completion(Token(util::MakeStringView(token), self->_currentUser), error); - } - }; - }; - - [self.app getTokenForcingRefresh:forceRefresh withCallback:getTokenCallback]; -} - -- (void)setUserChangeListener:(nullable FSTVoidUserBlock)block { - @synchronized(self) { - if (block) { - FSTAssert(!_userChangeListener, @"UserChangeListener set twice!"); - - // Fire initial event. - block(_currentUser); - } else { - FSTAssert(self.authListenerHandle, @"UserChangeListener removed twice!"); - FSTAssert(_userChangeListener, @"UserChangeListener removed without being set!"); - [[NSNotificationCenter defaultCenter] removeObserver:self.authListenerHandle]; - self.authListenerHandle = nil; - } - _userChangeListener = block; - } -} - -- (nullable FSTVoidUserBlock)userChangeListener { - @synchronized(self) { - return _userChangeListener; - } -} - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Auth/FSTEmptyCredentialsProvider.h b/Firestore/Source/Auth/FSTEmptyCredentialsProvider.h deleted file mode 100644 index f805363..0000000 --- a/Firestore/Source/Auth/FSTEmptyCredentialsProvider.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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 "Firestore/Source/Auth/FSTCredentialsProvider.h" - -NS_ASSUME_NONNULL_BEGIN - -/** `FSTEmptyCredentialsProvider` always yields an empty token. */ -@interface FSTEmptyCredentialsProvider : NSObject <FSTCredentialsProvider> - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Auth/FSTEmptyCredentialsProvider.mm b/Firestore/Source/Auth/FSTEmptyCredentialsProvider.mm deleted file mode 100644 index 77c08d1..0000000 --- a/Firestore/Source/Auth/FSTEmptyCredentialsProvider.mm +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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 "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h" - -#import "Firestore/Source/Util/FSTAssert.h" -#import "Firestore/Source/Util/FSTDispatchQueue.h" - -#include "Firestore/core/src/firebase/firestore/auth/token.h" -#include "Firestore/core/src/firebase/firestore/auth/user.h" - -using firebase::firestore::auth::Token; -using firebase::firestore::auth::User; - -NS_ASSUME_NONNULL_BEGIN - -@implementation FSTEmptyCredentialsProvider - -- (void)getTokenForcingRefresh:(BOOL)forceRefresh - completion:(FSTVoidGetTokenResultBlock)completion { - // Invalid token will force the GRPC fallback to use default settings. - completion(Token::Invalid(), nil); -} - -- (void)setUserChangeListener:(nullable FSTVoidUserBlock)block { - // Since the user never changes, we just need to fire the initial event and don't need to hang - // onto the block. - if (block) { - block(User::Unauthenticated()); - } -} - -- (nullable FSTVoidUserBlock)userChangeListener { - // TODO(mikelehen): Implementation omitted for convenience since it's not actually required. - FSTFail(@"Not implemented."); -} - -@end - -NS_ASSUME_NONNULL_END |