FBSessionTokenCachingStrategy.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2010-present Facebook.
  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. #import "FBAccessTokenData.h"
  18. #import "FBSDKMacros.h"
  19. /*!
  20. @class
  21. @abstract
  22. The `FBSessionTokenCachingStrategy` class is responsible for persisting and retrieving cached data related to
  23. an <FBSession> object, including the user's Facebook access token.
  24. @discussion
  25. `FBSessionTokenCachingStrategy` is designed to be instantiated directly or used as a base class. Usually default
  26. token caching behavior is sufficient, and you do not need to interface directly with `FBSessionTokenCachingStrategy` objects.
  27. However, if you need to control where or how `FBSession` information is cached, then you may take one of two approaches.
  28. The first and simplest approach is to instantiate an instance of `FBSessionTokenCachingStrategy`, and then pass
  29. the instance to `FBSession` class' `init` method. This enables your application to control the key name used in
  30. the iOS Keychain to store session information. You may consider this approach if you plan to cache session information
  31. for multiple users.
  32. The second and more advanced approached is to derive a custom class from `FBSessionTokenCachingStrategy`, which will
  33. be responsible for caching behavior of your application. This approach is useful if you need to change where the
  34. information is cached, for example if you prefer to use the filesystem or make a network connection to fetch and
  35. persist cached tokens. Inheritors should override the cacheTokenInformation, fetchTokenInformation, and clearToken methods.
  36. Doing this enables your application to implement any token caching scheme, including no caching at all (see
  37. `[FBSessionTokenCachingStrategy nullCacheInstance]`.
  38. Direct use of `FBSessionTokenCachingStrategy`is an advanced technique. Most applications use <FBSession> objects without
  39. passing an `FBSessionTokenCachingStrategy`, which yields default caching to the iOS Keychain.
  40. */
  41. @interface FBSessionTokenCachingStrategy : NSObject
  42. /*!
  43. @abstract Initializes and returns an instance
  44. */
  45. - (instancetype)init;
  46. /*!
  47. @abstract
  48. Initializes and returns an instance
  49. @param tokenInformationKeyName Specifies a key name to use for cached token information in the iOS Keychain, nil
  50. indicates a default value of @"FBAccessTokenInformationKey"
  51. */
  52. - (instancetype)initWithUserDefaultTokenInformationKeyName:(NSString *)tokenInformationKeyName;
  53. /*!
  54. @abstract
  55. Called by <FBSession> (and overridden by inheritors), in order to cache token information.
  56. @param tokenInformation Dictionary containing token information to be cached by the method
  57. @discussion You should favor overriding this instead of `cacheFBAccessTokenData` only if you intend
  58. to cache additional data not captured by the FBAccessTokenData type.
  59. */
  60. - (void)cacheTokenInformation:(NSDictionary *)tokenInformation;
  61. /*!
  62. @abstract Cache the supplied token.
  63. @param accessToken The token instance.
  64. @discussion This essentially wraps a call to `cacheTokenInformation` so you should
  65. override this when providing a custom token caching strategy.
  66. */
  67. - (void)cacheFBAccessTokenData:(FBAccessTokenData *)accessToken;
  68. /*!
  69. @abstract
  70. Called by <FBSession> (and overridden by inheritors), in order to fetch cached token information
  71. @discussion
  72. An overriding implementation should only return a token if it
  73. can also return an expiration date, otherwise return nil.
  74. You should favor overriding this instead of `fetchFBAccessTokenData` only if you intend
  75. to cache additional data not captured by the FBAccessTokenData type.
  76. */
  77. - (NSDictionary *)fetchTokenInformation;
  78. /*!
  79. @abstract
  80. Fetches the cached token instance.
  81. @discussion
  82. This essentially wraps a call to `fetchTokenInformation` so you should
  83. override this when providing a custom token caching strategy.
  84. In order for an `FBSession` instance to be able to use a cached token,
  85. the token must be not be expired (see `+isValidTokenInformation:`) and
  86. must also contain all permissions in the initialized session instance.
  87. */
  88. - (FBAccessTokenData *)fetchFBAccessTokenData;
  89. /*!
  90. @abstract
  91. Called by <FBSession> (and overridden by inheritors), in order delete any cached information for the current token
  92. */
  93. - (void)clearToken;
  94. /*!
  95. @abstract
  96. Helper function called by the SDK as well as apps, in order to fetch the default strategy instance.
  97. */
  98. + (FBSessionTokenCachingStrategy *)defaultInstance;
  99. /*!
  100. @abstract
  101. Helper function to return a FBSessionTokenCachingStrategy instance that does not perform any caching.
  102. */
  103. + (FBSessionTokenCachingStrategy *)nullCacheInstance;
  104. /*!
  105. @abstract
  106. Helper function called by the SDK as well as application code, used to determine whether a given dictionary
  107. contains the minimum token information usable by the <FBSession>.
  108. @param tokenInformation Dictionary containing token information to be validated
  109. */
  110. + (BOOL)isValidTokenInformation:(NSDictionary *)tokenInformation;
  111. @end
  112. // The key to use with token information dictionaries to get and set the token value
  113. FBSDK_EXTERN NSString *const FBTokenInformationTokenKey;
  114. // The to use with token information dictionaries to get and set the expiration date
  115. FBSDK_EXTERN NSString *const FBTokenInformationExpirationDateKey;
  116. // The to use with token information dictionaries to get and set the refresh date
  117. FBSDK_EXTERN NSString *const FBTokenInformationRefreshDateKey;
  118. // The key to use with token information dictionaries to get the related user's fbid
  119. FBSDK_EXTERN NSString *const FBTokenInformationUserFBIDKey;
  120. // The key to use with token information dictionaries to determine whether the token was fetched via Facebook Login
  121. FBSDK_EXTERN NSString *const FBTokenInformationIsFacebookLoginKey;
  122. // The key to use with token information dictionaries to determine whether the token was fetched via the OS
  123. FBSDK_EXTERN NSString *const FBTokenInformationLoginTypeLoginKey;
  124. // The key to use with token information dictionaries to get the latest known permissions
  125. FBSDK_EXTERN NSString *const FBTokenInformationPermissionsKey;
  126. // The key to use with token information dictionaries to get the latest known declined permissions
  127. FBSDK_EXTERN NSString *const FBTokenInformationDeclinedPermissionsKey;
  128. // The key to use with token information dictionaries to get the date the permissions were last refreshed.
  129. FBSDK_EXTERN NSString *const FBTokenInformationPermissionsRefreshDateKey;
  130. // The key to use with token information dictionaries to get the id of the creator app
  131. FBSDK_EXTERN NSString *const FBTokenInformationAppIDKey;