FIRSecureTokenService.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. @class FIRAuthRequestConfiguration;
  18. @protocol FIRCustomTokenProviderDelegate;
  19. NS_ASSUME_NONNULL_BEGIN
  20. /** @typedef FIRFetchAccessTokenCallback
  21. @brief The callback used to return the value of attempting to fetch an access token.
  22. In the event the operation was successful @c token will be set and @c error will be @c nil.
  23. In the event of failure @c token will be @c nil and @c error will be set.
  24. @c tokenUpdated indicates whether either the access or the refresh token has been updated.
  25. The token returned should be considered ephemeral and not cached. It should be used immediately
  26. and discarded. All operations that need this token should call fetchAccessToken and do their
  27. work from the callback.
  28. */
  29. typedef void (^FIRFetchAccessTokenCallback)(NSString *_Nullable token,
  30. NSError *_Nullable error,
  31. BOOL tokenUpdated);
  32. /** @class FIRSecureTokenService
  33. @brief Provides services for token exchanges and refreshes.
  34. */
  35. @interface FIRSecureTokenService : NSObject <NSSecureCoding>
  36. /** @property requestConfiguration
  37. @brief The configuration for making requests to server.
  38. */
  39. @property(nonatomic, strong) FIRAuthRequestConfiguration *requestConfiguration;
  40. /** @property rawAccessToken
  41. @brief The cached access token.
  42. @remarks This method is specifically for providing the access token to internal clients during
  43. deserialization and sign-in events, and should not be used to retrieve the access token by
  44. anyone else.
  45. */
  46. @property(nonatomic, copy, readonly) NSString *rawAccessToken;
  47. /** @property refreshToken
  48. @brief The refresh token for the user, or @c nil if the user has yet completed sign-in flow.
  49. @remarks This property needs to be set manually after the instance is decoded from archive.
  50. */
  51. @property(nonatomic, copy, readonly, nullable) NSString *refreshToken;
  52. /** @property customTokenProviderDelegate
  53. @brief The delegate to provide a new custom token for "refreshing" the ID token in passthrough
  54. mode, when no refresh token is available.
  55. */
  56. @property(nonatomic, weak, nullable) id<FIRCustomTokenProviderDelegate> customTokenProviderDelegate;
  57. /** @property accessTokenExpirationDate
  58. @brief The expiration date of the cached access token.
  59. */
  60. @property(nonatomic, copy, readonly, nullable) NSDate *accessTokenExpirationDate;
  61. /** @fn initWithRequestConfiguration:accessToken:accessTokenExpirationDate:refreshToken
  62. @brief Creates a @c FIRSecureTokenService with access and refresh tokens.
  63. @param requestConfiguration The configuration for making requests to server.
  64. @param accessToken The STS access token.
  65. @param accessTokenExpirationDate The approximate expiration date of the access token.
  66. @param refreshToken The STS refresh token.
  67. */
  68. - (instancetype)initWithRequestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  69. accessToken:(nullable NSString *)accessToken
  70. accessTokenExpirationDate:(nullable NSDate *)accessTokenExpirationDate
  71. refreshToken:(nullable NSString *)refreshToken
  72. customTokenProviderDelegate:
  73. (nullable id<FIRCustomTokenProviderDelegate>)customTokenProviderDelegate;
  74. /** @fn fetchAccessTokenForcingRefresh:callback:
  75. @brief Fetch a fresh ephemeral access token for the ID associated with this instance. The token
  76. received in the callback should be considered short lived and not cached.
  77. @param forceRefresh Forces the token to be refreshed.
  78. @param callback Callback block that will be called to return either the token or an error.
  79. Invoked asyncronously on the auth global work queue in the future.
  80. */
  81. - (void)fetchAccessTokenForcingRefresh:(BOOL)forceRefresh
  82. callback:(FIRFetchAccessTokenCallback)callback;
  83. @end
  84. NS_ASSUME_NONNULL_END