Sin descripción

FIRInstanceIDTokenManager.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2019 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 "FIRInstanceID.h"
  17. @class FIRInstanceIDAuthService;
  18. @class FIRInstanceIDCheckinPreferences;
  19. @class FIRInstanceIDKeyPair;
  20. @class FIRInstanceIDTokenInfo;
  21. @class FIRInstanceIDStore;
  22. typedef NS_OPTIONS(NSUInteger, FIRInstanceIDInvalidTokenReason) {
  23. FIRInstanceIDInvalidTokenReasonNone = 0, // 0
  24. FIRInstanceIDInvalidTokenReasonAppVersion = (1 << 0), // 0...00001
  25. FIRInstanceIDInvalidTokenReasonAPNSToken = (1 << 1), // 0...00010
  26. };
  27. /**
  28. * Manager for the InstanceID token requests i.e `newToken` and `deleteToken`. This
  29. * manages the overall interaction of the `InstanceIDStore`, the token register
  30. * service and the callbacks associated with `GCMInstanceID`.
  31. */
  32. @interface FIRInstanceIDTokenManager : NSObject
  33. /// Expose the auth service, so it can be used by others
  34. @property(nonatomic, readonly, strong) FIRInstanceIDAuthService *authService;
  35. /**
  36. * Fetch new token for the given authorizedEntity and scope. This makes an
  37. * asynchronous request to the InstanceID backend to create a new token for
  38. * the service and returns it. This will replace any old token for the given
  39. * authorizedEntity and scope that has been cached before.
  40. *
  41. * @param authorizedEntity The authorized entity for the token, should not be nil.
  42. * @param scope The scope for the token, should not be nil.
  43. * @param keyPair The keyPair that represents the app identity.
  44. * @param options The options to be added to the fetch request.
  45. * @param handler The handler to be invoked once we have the token or the
  46. * fetch request to InstanceID backend results in an error. Also
  47. * since it's a public handler it should always be called
  48. * asynchronously. This should be non-nil.
  49. */
  50. - (void)fetchNewTokenWithAuthorizedEntity:(NSString *)authorizedEntity
  51. scope:(NSString *)scope
  52. keyPair:(FIRInstanceIDKeyPair *)keyPair
  53. options:(NSDictionary *)options
  54. handler:(FIRInstanceIDTokenHandler)handler;
  55. /**
  56. * Return the cached token info, if one exists, for the given authorizedEntity and scope.
  57. *
  58. * @param authorizedEntity The authorized entity for the token.
  59. * @param scope The scope for the token.
  60. *
  61. * @return The cached token info, if available, matching the parameters.
  62. */
  63. - (FIRInstanceIDTokenInfo *)cachedTokenInfoWithAuthorizedEntity:(NSString *)authorizedEntity
  64. scope:(NSString *)scope;
  65. /**
  66. * Delete the token for the given authorizedEntity and scope. If the token has
  67. * been cached, it will be deleted from the store. It will also make an
  68. * asynchronous request to the InstanceID backend to invalidate the token.
  69. *
  70. * @param authorizedEntity The authorized entity for the token, should not be nil.
  71. * @param scope The scope for the token, should not be nil.
  72. * @param keyPair The keyPair that represents the app identity.
  73. * @param handler The handler to be invoked once the delete request to
  74. * InstanceID backend has returned. If the request was
  75. * successful we invoke the handler with a nil error;
  76. * otherwise we call it with an appropriate error. Also since
  77. * it's a public handler it should always be called
  78. * asynchronously. This should be non-nil.
  79. */
  80. - (void)deleteTokenWithAuthorizedEntity:(NSString *)authorizedEntity
  81. scope:(NSString *)scope
  82. keyPair:(FIRInstanceIDKeyPair *)keyPair
  83. handler:(FIRInstanceIDDeleteTokenHandler)handler;
  84. /**
  85. * Deletes all cached tokens from the persistent store. This method should only be triggered
  86. * when InstanceID is deleted
  87. *
  88. * @param keyPair The keyPair for the given app.
  89. * @param handler The handler to be invoked once the delete request to InstanceID backend
  90. * has returned. If the request was successful we invoke the handler with
  91. * a nil error; else we pass in an appropriate error. This should be non-nil
  92. * and be called asynchronously.
  93. */
  94. - (void)deleteAllTokensWithKeyPair:(FIRInstanceIDKeyPair *)keyPair
  95. handler:(FIRInstanceIDDeleteHandler)handler;
  96. /**
  97. * Deletes all cached tokens from the persistent store.
  98. * @param handler The callback handler which is invoked when tokens deletion is complete,
  99. * with an error if there is any.
  100. *
  101. */
  102. - (void)deleteAllTokensLocallyWithHandler:(void (^)(NSError *error))handler;
  103. /**
  104. * Stop any ongoing token operations.
  105. */
  106. - (void)stopAllTokenOperations;
  107. #pragma mark - Invalidating Cached Tokens
  108. /**
  109. * Invalidate any cached tokens, if the app version has changed since last launch or if the token
  110. * is cached for more than 7 days.
  111. * @param IID The cached instanceID, check if token is prefixed by such IID.
  112. *
  113. * @return Whether we should fetch default token from server.
  114. *
  115. * @discussion This should safely be called prior to any tokens being retrieved from
  116. * the cache or being fetched from the network.
  117. */
  118. - (BOOL)checkTokenRefreshPolicyWithIID:(NSString *)IID;
  119. /**
  120. * Upon being provided with different APNs or sandbox, any locally cached tokens
  121. * should be deleted, and the new APNs token should be cached.
  122. *
  123. * @discussion It is possible for this method to be called while token operations are
  124. * in-progress or queued. In this case, the in-flight token operations will have stale
  125. * APNs information. The default token is checked for being out-of-date by Instance ID,
  126. * and re-fetched. Custom tokens are not currently checked.
  127. *
  128. * @param deviceToken The APNS device token, provided by the operating system.
  129. * @param isSandbox YES if the device token is for the sandbox environment, NO otherwise.
  130. *
  131. * @return The array of FIRInstanceIDTokenInfo objects which were invalidated.
  132. */
  133. - (NSArray<FIRInstanceIDTokenInfo *> *)updateTokensToAPNSDeviceToken:(NSData *)deviceToken
  134. isSandbox:(BOOL)isSandbox;
  135. @end