FSTCredentialsProvider.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2017 Google
  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. #import <Foundation/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. @class FIRApp;
  19. @class FSTDispatchQueue;
  20. @class FSTUser;
  21. #pragma mark - FSTGetTokenResult
  22. /**
  23. * The current FSTUser and the authentication token provided by the underlying authentication
  24. * mechanism. This is the result of calling -[FSTCredentialsProvider getTokenForcingRefresh].
  25. *
  26. * ## Portability notes: no TokenType on iOS
  27. *
  28. * The TypeScript client supports 1st party Oauth tokens (for the Firebase Console to auth as the
  29. * developer) and OAuth2 tokens for the node.js sdk to auth with a service account. We don't have
  30. * plans to support either case on mobile so there's no TokenType here.
  31. */
  32. // TODO(mcg): Rename FSTToken, change parameter order to line up with the other platforms.
  33. @interface FSTGetTokenResult : NSObject
  34. - (instancetype)init NS_UNAVAILABLE;
  35. - (instancetype)initWithUser:(FSTUser *)user
  36. token:(NSString *_Nullable)token NS_DESIGNATED_INITIALIZER;
  37. /** The user with which the token is associated (used for persisting user state on disk, etc.). */
  38. @property(nonatomic, nonnull, readonly) FSTUser *user;
  39. /** The actual raw token. */
  40. @property(nonatomic, copy, nullable, readonly) NSString *token;
  41. @end
  42. #pragma mark - Typedefs
  43. /**
  44. * `FSTVoidTokenErrorBlock` is a block that gets a token or an error.
  45. *
  46. * @param token An auth token as a string.
  47. * @param error The error if one occurred, or else `nil`.
  48. */
  49. typedef void (^FSTVoidGetTokenResultBlock)(FSTGetTokenResult *_Nullable token,
  50. NSError *_Nullable error);
  51. /** Listener block notified with an FSTUser. */
  52. typedef void (^FSTVoidUserBlock)(FSTUser *user);
  53. #pragma mark - FSTCredentialsProvider
  54. /** Provides methods for getting the uid and token for the current user and listen for changes. */
  55. @protocol FSTCredentialsProvider <NSObject>
  56. /** Requests token for the current user, optionally forcing a refreshed token to be fetched. */
  57. - (void)getTokenForcingRefresh:(BOOL)forceRefresh completion:(FSTVoidGetTokenResultBlock)completion;
  58. /**
  59. * A listener to be notified of user changes (sign-in / sign-out). It is immediately called once
  60. * with the initial user.
  61. *
  62. * Note that this block will be called back on an arbitrary thread that is not the normal Firestore
  63. * worker thread.
  64. */
  65. @property(nonatomic, copy, nullable, readwrite) FSTVoidUserBlock userChangeListener;
  66. @end
  67. #pragma mark - FSTFirebaseCredentialsProvider
  68. /**
  69. * `FSTFirebaseCredentialsProvider` uses Firebase Auth via `FIRApp` to get an auth token.
  70. *
  71. * NOTE: To simplify the implementation, it requires that you set `userChangeListener` with a
  72. * non-`nil` value no more than once and don't call `getTokenForcingRefresh:` after setting
  73. * it to `nil`.
  74. *
  75. * This class must be implemented in a thread-safe manner since it is accessed from the thread
  76. * backing our internal worker queue and the callbacks from FIRAuth will be executed on an
  77. * arbitrary different thread.
  78. */
  79. @interface FSTFirebaseCredentialsProvider : NSObject <FSTCredentialsProvider>
  80. /**
  81. * Initializes a new FSTFirebaseCredentialsProvider.
  82. *
  83. * @param app The Firebase app from which to get credentials.
  84. *
  85. * @return A new instance of FSTFirebaseCredentialsProvider.
  86. */
  87. - (instancetype)initWithApp:(FIRApp *)app NS_DESIGNATED_INITIALIZER;
  88. - (id)init NS_UNAVAILABLE;
  89. @end
  90. NS_ASSUME_NONNULL_END