FIRAppCheckTokenRefresher.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2021 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. #import <Foundation/Foundation.h>
  17. #import "FirebaseAppCheck/Sources/Core/TokenRefresh/FIRAppCheckTimer.h"
  18. @protocol FIRAppCheckSettingsProtocol;
  19. NS_ASSUME_NONNULL_BEGIN
  20. /** The block to be called on the token refresh completion.
  21. * @param success If refresh was successful.
  22. * @param tokenExpirationDate The date when the token will expire.
  23. */
  24. typedef void (^FIRAppCheckTokenRefreshCompletion)(BOOL success,
  25. NSDate *_Nullable tokenExpirationDate);
  26. /** The block that will be called by `FIRAppCheckTokenRefresher` to trigger the token refresh.
  27. * @param completion The block that the client must call when the token refresh was completed.
  28. */
  29. typedef void (^FIRAppCheckTokenRefreshBlock)(FIRAppCheckTokenRefreshCompletion completion);
  30. @protocol FIRAppCheckTokenRefresherProtocol <NSObject>
  31. /// The block to be called when refresh is needed. The client is responsible for actual token
  32. /// refresh in the block.
  33. @property(nonatomic, copy) FIRAppCheckTokenRefreshBlock tokenRefreshHandler;
  34. /// Updates the next refresh date based on the new token expiration date. This method should be
  35. /// called when the token update was initiated not by the refresher.
  36. /// @param tokenExpirationDate A new token expiration date.
  37. - (void)updateTokenExpirationDate:(NSDate *)tokenExpirationDate;
  38. @end
  39. /// The class calls `tokenRefreshHandler` periodically to keep FAC token fresh to reduce FAC token
  40. /// exchange overhead for product requests.
  41. @interface FIRAppCheckTokenRefresher : NSObject <FIRAppCheckTokenRefresherProtocol>
  42. - (instancetype)init NS_UNAVAILABLE;
  43. /// The designated initializer.
  44. /// @param tokenExpirationDate The initial token expiration date when known. Pass current date or
  45. /// date in the past to trigger refresh once `tokenRefreshHandler` is set.
  46. /// @param tokenExpirationThreshold The token refresh will be triggered `tokenExpirationThreshold`
  47. /// seconds before the actual token expiration time.
  48. /// @param settings An object that handles Firebase app check settings.
  49. - (instancetype)initWithTokenExpirationDate:(NSDate *)tokenExpirationDate
  50. tokenExpirationThreshold:(NSTimeInterval)tokenExpirationThreshold
  51. timerProvider:(FIRTimerProvider)timerProvider
  52. settings:(id<FIRAppCheckSettingsProtocol>)settings
  53. NS_DESIGNATED_INITIALIZER;
  54. /// A convenience initializer with a timer provider returning an instance of `FIRAppCheckTimer`.
  55. - (instancetype)initWithTokenExpirationDate:(NSDate *)tokenExpirationDate
  56. tokenExpirationThreshold:(NSTimeInterval)tokenExpirationThreshold
  57. settings:(id<FIRAppCheckSettingsProtocol>)settings;
  58. @end
  59. NS_ASSUME_NONNULL_END