FBSDKAccessToken.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. #import <Foundation/Foundation.h>
  9. #import <FBSDKCoreKit/FBSDKAccessTokenProviding.h>
  10. #import <FBSDKCoreKit/FBSDKTokenStringProviding.h>
  11. #import <FBSDKCoreKit/FBSDKGraphRequestConnection.h>
  12. #import <FBSDKCoreKit/FBSDKTokenCaching.h>
  13. @protocol FBSDKGraphRequestConnectionFactory;
  14. @protocol FBSDKGraphRequestPiggybackManaging;
  15. @protocol FBSDKErrorCreating;
  16. NS_ASSUME_NONNULL_BEGIN
  17. /**
  18. Notification indicating that the `currentAccessToken` has changed.
  19. the userInfo dictionary of the notification will contain keys
  20. `FBSDKAccessTokenChangeOldKey` and
  21. `FBSDKAccessTokenChangeNewKey`.
  22. */
  23. FOUNDATION_EXPORT NSNotificationName const FBSDKAccessTokenDidChangeNotification
  24. NS_SWIFT_NAME(AccessTokenDidChange);
  25. /**
  26. A key in the notification's userInfo that will be set
  27. if and only if the user ID changed between the old and new tokens.
  28. Token refreshes can occur automatically with the SDK
  29. which do not change the user. If you're only interested in user
  30. changes (such as logging out), you should check for the existence
  31. of this key. The value is a NSNumber with a boolValue.
  32. On a fresh start of the app where the SDK reads in the cached value
  33. of an access token, this key will also exist since the access token
  34. is moving from a null state (no user) to a non-null state (user).
  35. */
  36. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenDidChangeUserIDKey
  37. NS_SWIFT_NAME(AccessTokenDidChangeUserIDKey);
  38. /*
  39. key in notification's userInfo object for getting the old token.
  40. If there was no old token, the key will not be present.
  41. */
  42. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenChangeOldKey
  43. NS_SWIFT_NAME(AccessTokenChangeOldKey);
  44. /*
  45. key in notification's userInfo object for getting the new token.
  46. If there is no new token, the key will not be present.
  47. */
  48. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenChangeNewKey
  49. NS_SWIFT_NAME(AccessTokenChangeNewKey);
  50. /*
  51. A key in the notification's userInfo that will be set
  52. if and only if the token has expired.
  53. */
  54. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenDidExpireKey
  55. NS_SWIFT_NAME(AccessTokenDidExpireKey);
  56. /// Represents an immutable access token for using Facebook services.
  57. NS_SWIFT_NAME(AccessToken)
  58. @interface FBSDKAccessToken : NSObject <NSCopying, NSObject, NSSecureCoding, FBSDKAccessTokenProviding, FBSDKTokenStringProviding>
  59. /**
  60. The "global" access token that represents the currently logged in user.
  61. The `currentAccessToken` is a convenient representation of the token of the
  62. current user and is used by other SDK components (like `FBSDKLoginManager`).
  63. */
  64. @property (class, nullable, nonatomic, copy) FBSDKAccessToken *currentAccessToken NS_SWIFT_NAME(current);
  65. /// Returns YES if currentAccessToken is not nil AND currentAccessToken is not expired
  66. @property (class, nonatomic, readonly, getter = isCurrentAccessTokenActive, assign) BOOL currentAccessTokenIsActive;
  67. /**
  68. Internal Type exposed to facilitate transition to Swift.
  69. API Subject to change or removal without warning. Do not use.
  70. @warning INTERNAL - DO NOT USE
  71. */
  72. @property (class, nullable, nonatomic, copy) id<FBSDKTokenCaching> tokenCache;
  73. /// Returns the app ID.
  74. @property (nonatomic, readonly, copy) NSString *appID;
  75. /// Returns the expiration date for data access
  76. @property (nonatomic, readonly, copy) NSDate *dataAccessExpirationDate;
  77. /// Returns the known declined permissions.
  78. @property (nonatomic, readonly, copy) NSSet<NSString *> *declinedPermissions
  79. NS_REFINED_FOR_SWIFT;
  80. /// Returns the known declined permissions.
  81. @property (nonatomic, readonly, copy) NSSet<NSString *> *expiredPermissions
  82. NS_REFINED_FOR_SWIFT;
  83. /// Returns the expiration date.
  84. @property (nonatomic, readonly, copy) NSDate *expirationDate;
  85. /// Returns the known granted permissions.
  86. @property (nonatomic, readonly, copy) NSSet<NSString *> *permissions
  87. NS_REFINED_FOR_SWIFT;
  88. /// Returns the date the token was last refreshed.
  89. @property (nonatomic, readonly, copy) NSDate *refreshDate;
  90. /// Returns the opaque token string.
  91. @property (nonatomic, readonly, copy) NSString *tokenString;
  92. /// Returns the user ID.
  93. @property (nonatomic, readonly, copy) NSString *userID;
  94. /// Returns whether the access token is expired by checking its expirationDate property
  95. @property (nonatomic, readonly, getter = isExpired, assign) BOOL expired;
  96. /// Returns whether user data access is still active for the given access token
  97. @property (nonatomic, readonly, getter = isDataAccessExpired, assign) BOOL dataAccessExpired;
  98. - (instancetype)init NS_UNAVAILABLE;
  99. + (instancetype)new NS_UNAVAILABLE;
  100. /**
  101. Initializes a new instance.
  102. @param tokenString the opaque token string.
  103. @param permissions the granted permissions. Note this is converted to NSSet and is only
  104. an NSArray for the convenience of literal syntax.
  105. @param declinedPermissions the declined permissions. Note this is converted to NSSet and is only
  106. an NSArray for the convenience of literal syntax.
  107. @param expiredPermissions the expired permissions. Note this is converted to NSSet and is only
  108. an NSArray for the convenience of literal syntax.
  109. @param appID the app ID.
  110. @param userID the user ID.
  111. @param expirationDate the optional expiration date (defaults to distantFuture).
  112. @param refreshDate the optional date the token was last refreshed (defaults to today).
  113. @param dataAccessExpirationDate the date which data access will expire for the given user
  114. (defaults to distantFuture).
  115. This initializer should only be used for advanced apps that
  116. manage tokens explicitly. Typical login flows only need to use `FBSDKLoginManager`
  117. along with `+currentAccessToken`.
  118. */
  119. - (instancetype)initWithTokenString:(NSString *)tokenString
  120. permissions:(NSArray<NSString *> *)permissions
  121. declinedPermissions:(NSArray<NSString *> *)declinedPermissions
  122. expiredPermissions:(NSArray<NSString *> *)expiredPermissions
  123. appID:(NSString *)appID
  124. userID:(NSString *)userID
  125. expirationDate:(nullable NSDate *)expirationDate
  126. refreshDate:(nullable NSDate *)refreshDate
  127. dataAccessExpirationDate:(nullable NSDate *)dataAccessExpirationDate
  128. NS_DESIGNATED_INITIALIZER;
  129. /**
  130. Convenience getter to determine if a permission has been granted
  131. @param permission The permission to check.
  132. */
  133. // UNCRUSTIFY_FORMAT_OFF
  134. - (BOOL)hasGranted:(NSString *)permission
  135. NS_SWIFT_NAME(hasGranted(permission:));
  136. // UNCRUSTIFY_FORMAT_ON
  137. /**
  138. Compares the receiver to another FBSDKAccessToken
  139. @param token The other token
  140. @return YES if the receiver's values are equal to the other token's values; otherwise NO
  141. */
  142. - (BOOL)isEqualToAccessToken:(FBSDKAccessToken *)token;
  143. /**
  144. Refresh the current access token's permission state and extend the token's expiration date,
  145. if possible.
  146. @param completion an optional callback handler that can surface any errors related to permission refreshing.
  147. On a successful refresh, the currentAccessToken will be updated so you typically only need to
  148. observe the `FBSDKAccessTokenDidChangeNotification` notification.
  149. If a token is already expired, it cannot be refreshed.
  150. */
  151. + (void)refreshCurrentAccessTokenWithCompletion:(nullable FBSDKGraphRequestCompletion)completion;
  152. /**
  153. Internal method exposed to facilitate transition to Swift.
  154. API Subject to change or removal without warning. Do not use.
  155. @warning INTERNAL - DO NOT USE
  156. */
  157. + (void)configureWithTokenCache:(id<FBSDKTokenCaching>)tokenCache
  158. graphRequestConnectionFactory:(id<FBSDKGraphRequestConnectionFactory>)graphRequestConnectionFactory
  159. graphRequestPiggybackManager:(id<FBSDKGraphRequestPiggybackManaging>)graphRequestPiggybackManager
  160. errorFactory:(id<FBSDKErrorCreating>)errorFactory
  161. NS_SWIFT_NAME(configure(tokenCache:graphRequestConnectionFactory:graphRequestPiggybackManager:errorFactory:));
  162. @end
  163. NS_ASSUME_NONNULL_END