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.

149 lines
6.8 KiB

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