FBSDKAccessToken.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #import <Foundation/Foundation.h>
  19. #import <FBSDKCoreKit/FBSDKCopying.h>
  20. #import <FBSDKCoreKit/FBSDKGraphRequestConnection.h>
  21. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  22. /**
  23. Notification indicating that the `currentAccessToken` has changed.
  24. the userInfo dictionary of the notification will contain keys
  25. `FBSDKAccessTokenChangeOldKey` and
  26. `FBSDKAccessTokenChangeNewKey`.
  27. */
  28. FOUNDATION_EXPORT NSNotificationName const FBSDKAccessTokenDidChangeNotification;
  29. #else
  30. /**
  31. Notification indicating that the `currentAccessToken` has changed.
  32. the userInfo dictionary of the notification will contain keys
  33. `FBSDKAccessTokenChangeOldKey` and
  34. `FBSDKAccessTokenChangeNewKey`.
  35. */
  36. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenDidChangeNotification;
  37. #endif
  38. /**
  39. A key in the notification's userInfo that will be set
  40. if and only if the user ID changed between the old and new tokens.
  41. Token refreshes can occur automatically with the SDK
  42. which do not change the user. If you're only interested in user
  43. changes (such as logging out), you should check for the existence
  44. of this key. The value is a NSNumber with a boolValue.
  45. On a fresh start of the app where the SDK reads in the cached value
  46. of an access token, this key will also exist since the access token
  47. is moving from a null state (no user) to a non-null state (user).
  48. */
  49. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenDidChangeUserIDKey;
  50. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenDidChangeUserID
  51. DEPRECATED_MSG_ATTRIBUTE("Renamed `FBSDKAccessTokenDidChangeUserIDKey`");
  52. /*
  53. key in notification's userInfo object for getting the old token.
  54. If there was no old token, the key will not be present.
  55. */
  56. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenChangeOldKey;
  57. /*
  58. key in notification's userInfo object for getting the new token.
  59. If there is no new token, the key will not be present.
  60. */
  61. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenChangeNewKey;
  62. /*
  63. A key in the notification's userInfo that will be set
  64. if and only if the token has expired.
  65. */
  66. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenDidExpireKey;
  67. FOUNDATION_EXPORT NSString *const FBSDKAccessTokenDidExpire
  68. DEPRECATED_MSG_ATTRIBUTE("Renamed `FBSDKAccessTokenDidExpireKey`");
  69. /**
  70. Represents an immutable access token for using Facebook services.
  71. */
  72. @interface FBSDKAccessToken : NSObject<FBSDKCopying, NSSecureCoding>
  73. /**
  74. Returns the app ID.
  75. */
  76. @property (readonly, copy, nonatomic) NSString *appID;
  77. /**
  78. Returns the expiration date for data access
  79. */
  80. @property (readonly, copy, nonatomic) NSDate *dataAccessExpirationDate;
  81. /**
  82. Returns the known declined permissions.
  83. */
  84. @property (readonly, copy, nonatomic) NSSet *declinedPermissions;
  85. /**
  86. Returns the expiration date.
  87. */
  88. @property (readonly, copy, nonatomic) NSDate *expirationDate;
  89. /**
  90. Returns the known granted permissions.
  91. */
  92. @property (readonly, copy, nonatomic) NSSet *permissions;
  93. /**
  94. Returns the date the token was last refreshed.
  95. */
  96. @property (readonly, copy, nonatomic) NSDate *refreshDate;
  97. /**
  98. Returns the opaque token string.
  99. */
  100. @property (readonly, copy, nonatomic) NSString *tokenString;
  101. /**
  102. Returns the user ID.
  103. */
  104. @property (readonly, copy, nonatomic) NSString *userID;
  105. /**
  106. Returns whether the access token is expired by checking its expirationDate property
  107. */
  108. @property (readonly, assign, nonatomic, getter = isExpired) BOOL expired;
  109. /**
  110. Returns whether user data access is still active for the given access token
  111. */
  112. @property (readonly, assign, nonatomic, getter = isDataAccessExpired) BOOL dataAccessExpired;
  113. - (instancetype)init NS_UNAVAILABLE;
  114. + (instancetype)new NS_UNAVAILABLE;
  115. /**
  116. Initializes a new instance.
  117. @param tokenString the opaque token string.
  118. @param permissions the granted permissions. Note this is converted to NSSet and is only
  119. an NSArray for the convenience of literal syntax.
  120. @param declinedPermissions the declined permissions. Note this is converted to NSSet and is only
  121. an NSArray for the convenience of literal syntax.
  122. @param appID the app ID.
  123. @param userID the user ID.
  124. @param expirationDate the optional expiration date (defaults to distantFuture).
  125. @param refreshDate the optional date the token was last refreshed (defaults to today).
  126. This initializer should only be used for advanced apps that
  127. manage tokens explicitly. Typical login flows only need to use `FBSDKLoginManager`
  128. along with `+currentAccessToken`.
  129. */
  130. - (instancetype)initWithTokenString:(NSString *)tokenString
  131. permissions:(NSArray *)permissions
  132. declinedPermissions:(NSArray *)declinedPermissions
  133. appID:(NSString *)appID
  134. userID:(NSString *)userID
  135. expirationDate:(NSDate *)expirationDate
  136. refreshDate:(NSDate *)refreshDate;
  137. /**
  138. Initializes a new instance.
  139. @param tokenString the opaque token string.
  140. @param permissions the granted permissions. Note this is converted to NSSet and is only
  141. an NSArray for the convenience of literal syntax.
  142. @param declinedPermissions the declined permissions. Note this is converted to NSSet and is only
  143. an NSArray for the convenience of literal syntax.
  144. @param appID the app ID.
  145. @param userID the user ID.
  146. @param expirationDate the optional expiration date (defaults to distantFuture).
  147. @param refreshDate the optional date the token was last refreshed (defaults to today).
  148. @param dataAccessExpirationDate the date which data access will expire for the given user
  149. (defaults to distantFuture).
  150. This initializer should only be used for advanced apps that
  151. manage tokens explicitly. Typical login flows only need to use `FBSDKLoginManager`
  152. along with `+currentAccessToken`.
  153. */
  154. - (instancetype)initWithTokenString:(NSString *)tokenString
  155. permissions:(NSArray *)permissions
  156. declinedPermissions:(NSArray *)declinedPermissions
  157. appID:(NSString *)appID
  158. userID:(NSString *)userID
  159. expirationDate:(NSDate *)expirationDate
  160. refreshDate:(NSDate *)refreshDate
  161. dataAccessExpirationDate:(NSDate *)dataAccessExpirationDate
  162. NS_DESIGNATED_INITIALIZER;
  163. /**
  164. Convenience getter to determine if a permission has been granted
  165. @param permission The permission to check.
  166. */
  167. - (BOOL)hasGranted:(NSString *)permission;
  168. /**
  169. Compares the receiver to another FBSDKAccessToken
  170. @param token The other token
  171. @return YES if the receiver's values are equal to the other token's values; otherwise NO
  172. */
  173. - (BOOL)isEqualToAccessToken:(FBSDKAccessToken *)token;
  174. /**
  175. Returns the "global" access token that represents the currently logged in user.
  176. The `currentAccessToken` is a convenient representation of the token of the
  177. current user and is used by other SDK components (like `FBSDKLoginManager`).
  178. */
  179. + (FBSDKAccessToken *)currentAccessToken;
  180. /**
  181. Returns YES if currentAccessToken is not nil AND currentAccessToken is not expired
  182. */
  183. + (BOOL)currentAccessTokenIsActive;
  184. /**
  185. Sets the "global" access token that represents the currently logged in user.
  186. @param token The access token to set.
  187. This will broadcast a notification and save the token to the app keychain.
  188. */
  189. + (void)setCurrentAccessToken:(FBSDKAccessToken *)token;
  190. /**
  191. Refresh the current access token's permission state and extend the token's expiration date,
  192. if possible.
  193. @param completionHandler an optional callback handler that can surface any errors related to permission refreshing.
  194. On a successful refresh, the currentAccessToken will be updated so you typically only need to
  195. observe the `FBSDKAccessTokenDidChangeNotification` notification.
  196. If a token is already expired, it cannot be refreshed.
  197. */
  198. + (void)refreshCurrentAccessToken:(FBSDKGraphRequestHandler)completionHandler;
  199. @end