You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.5 KiB

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