commit | author | age
|
655e66
|
1 |
/* |
H |
2 |
* Copyright (C) 2017 Twitter, Inc. |
|
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 |
*/ |
a0a843
|
17 |
|
H |
18 |
@class TWTRAuthConfig; |
|
19 |
@class TWTRGuestSession; |
|
20 |
@class TWTRSession; |
|
21 |
@protocol TWTRAuthSession; |
|
22 |
@protocol TWTRAPIServiceConfig; |
|
23 |
@protocol TWTRErrorLogger; |
|
24 |
|
|
25 |
NS_ASSUME_NONNULL_BEGIN |
|
26 |
|
|
27 |
#pragma mark - TWTRSessionRefreshingStore Protocol |
|
28 |
|
|
29 |
/** |
|
30 |
* Completion block called when a session refresh succeeds or fails. |
|
31 |
* |
|
32 |
* @param refreshedSession The refreshed session |
|
33 |
* @param error Error that will be non nil if the refresh request failed |
|
34 |
*/ |
|
35 |
typedef void (^TWTRSessionStoreRefreshCompletion)(id _Nullable refreshedSession, NSError *_Nullable error); |
|
36 |
|
|
37 |
/** |
|
38 |
* Protocol for session stores that can refresh expired sessions. |
|
39 |
*/ |
|
40 |
@protocol TWTRSessionRefreshingStore <NSObject> |
|
41 |
|
|
42 |
/** |
|
43 |
* Refresh an expired session. |
|
44 |
* |
|
45 |
* @param sessionClass The class of the session |
|
46 |
* @param sessionID ID of the session wherever applicable e.g. `userID` if it's a user session. |
|
47 |
* @param completion The completion block to call when the refresh request succeeds or fails. |
|
48 |
*/ |
|
49 |
- (void)refreshSessionClass:(Class)sessionClass sessionID:(nullable NSString *)sessionID completion:(TWTRSessionStoreRefreshCompletion)completion; |
|
50 |
|
|
51 |
/** |
|
52 |
* Determines whether the given session has expired. |
|
53 |
* |
|
54 |
* @param session The session to check for expiration |
|
55 |
* @param response API request response to check for expiration |
|
56 |
* |
|
57 |
* @return Whether the session has expired. |
|
58 |
*/ |
|
59 |
- (BOOL)isExpiredSession:(id)session response:(NSHTTPURLResponse *)response; |
|
60 |
|
|
61 |
/** |
|
62 |
* Determines whether the given session has expired based on a given error. |
|
63 |
* |
|
64 |
* @param session The session to check for expiration |
|
65 |
* @param error API request error to check for expiration |
|
66 |
* |
|
67 |
* @return Whether the session has expired. |
|
68 |
*/ |
|
69 |
- (BOOL)isExpiredSession:(id)session error:(NSError *)error; |
|
70 |
|
|
71 |
@end |
|
72 |
|
|
73 |
#pragma mark - TWTRUserSessionStore Protocol |
|
74 |
|
|
75 |
/** |
|
76 |
* Completion block called when a user session saved to the session store or fails. |
|
77 |
* |
|
78 |
* @param session The saved session |
|
79 |
* @param error Error that will be non nil if the save request fails. |
|
80 |
*/ |
|
81 |
typedef void (^TWTRSessionStoreSaveCompletion)(id<TWTRAuthSession> _Nullable session, NSError *_Nullable error); |
|
82 |
|
|
83 |
/** |
|
84 |
* Completion block called when fetching all stored user sessions completes or fails. |
|
85 |
* |
|
86 |
* @param sessions All stored user sessions or empty array if there are no user sessions found. |
|
87 |
*/ |
|
88 |
typedef void (^TWTRSessionStoreBatchFetchCompletion)(NSArray *sessions); |
|
89 |
|
|
90 |
/** |
|
91 |
* Completion block to call when the session is deleted or fails. |
|
92 |
* |
|
93 |
* @param session The deleted session or nil if none was found for the user. |
|
94 |
*/ |
|
95 |
typedef void (^TWTRSessionStoreDeleteCompletion)(id<TWTRAuthSession> _Nullable session); |
|
96 |
|
|
97 |
/** |
|
98 |
* Protocol for session store that manages user sessions. |
|
99 |
*/ |
|
100 |
@protocol TWTRUserSessionStore <NSObject> |
|
101 |
|
|
102 |
/** |
|
103 |
* Saves the existing session to the store after validations. |
|
104 |
* |
|
105 |
* @param session The user session to save |
|
106 |
* @param completion Completion block to call when the save request succeeds or fails |
|
107 |
*/ |
|
108 |
- (void)saveSession:(id<TWTRAuthSession>)session completion:(TWTRSessionStoreSaveCompletion)completion; |
|
109 |
|
|
110 |
/** |
|
111 |
* Fetches the user session for for the given auth tokens and saves it to the store after validations. |
|
112 |
* |
|
113 |
* @param authToken The existing authToken to use for authentication. |
|
114 |
* @param authTokenSecret The existing authTokenSecret to use for authentication. |
|
115 |
* @param completion Completion block to call when the save request succeeds or fails |
|
116 |
*/ |
|
117 |
- (void)saveSessionWithAuthToken:(NSString *)authToken authTokenSecret:(NSString *)authTokenSecret completion:(TWTRSessionStoreSaveCompletion)completion; |
|
118 |
|
|
119 |
/** |
|
120 |
* Checks to see if the user is logged in and has a saved session. |
|
121 |
* |
|
122 |
* @param userID The user ID to fetch session for. |
|
123 |
*/ |
|
124 |
- (nullable id<TWTRAuthSession>)sessionForUserID:(NSString *)userID; |
|
125 |
|
|
126 |
/** |
|
127 |
* Retrieve all logged in user sessions in ascending order of last saved date |
|
128 |
* |
|
129 |
* @note This is a blocking call. |
|
130 |
*/ |
|
131 |
- (NSArray *)existingUserSessions; |
|
132 |
|
655e66
|
133 |
/** |
H |
134 |
* Returns YES if there are existing user sessions. |
|
135 |
* |
|
136 |
* @note This is a blocking call. |
|
137 |
*/ |
a0a843
|
138 |
- (BOOL)hasLoggedInUsers; |
H |
139 |
|
|
140 |
/** |
|
141 |
* Retrieves the last logged in user session. |
|
142 |
* |
|
143 |
* @return The last logged in user session. |
|
144 |
*/ |
|
145 |
- (nullable id<TWTRAuthSession>)session; |
|
146 |
|
|
147 |
/** |
|
148 |
* Deletes the local Twitter user session from this app. This will not remove the system Twitter account nor make a network request to invalidate the session. |
|
149 |
* |
|
150 |
* @param userID ID of the user to log out |
|
151 |
*/ |
|
152 |
- (void)logOutUserID:(NSString *)userID; |
|
153 |
|
|
154 |
@end |
|
155 |
|
|
156 |
#pragma mark - TWTRGuestSessionStore Protocol |
|
157 |
|
|
158 |
/** |
|
159 |
* Completion block called when retrieving a guest session succeeds or fails. |
|
160 |
* |
|
161 |
* @param guestSession The retrieved guest session |
|
162 |
* @param error Error that will be non nil if the save request fails. |
|
163 |
*/ |
|
164 |
typedef void (^TWTRSessionGuestLogInCompletion)(TWTRGuestSession *_Nullable guestSession, NSError *_Nullable error); |
|
165 |
|
|
166 |
/** |
|
167 |
* Protocol for session stores that can manage guest sessions. |
|
168 |
*/ |
|
169 |
@protocol TWTRGuestSessionStore <NSObject> |
|
170 |
|
|
171 |
/** |
|
172 |
* Log in as a guest user and return the guest session. This can be used when the user is not a Twitter user. |
|
173 |
* |
|
174 |
* @param completion Completion block to call when the authentication succeeds or fails. |
|
175 |
* |
|
176 |
* @warning This method assumes your application, as indicated by the `consumerKey` and `consumerSecret` in the `authConfig`, has been whitelisted for guest authentication. |
|
177 |
*/ |
|
178 |
- (void)fetchGuestSessionWithCompletion:(TWTRSessionGuestLogInCompletion)completion; |
|
179 |
|
|
180 |
@end |
|
181 |
|
|
182 |
#pragma mark - Composite TWTRSessionStore Protocol |
|
183 |
|
|
184 |
/** |
|
185 |
* Convenience composite protocol of a store that handles user, guest, and refreshable sessions. |
|
186 |
*/ |
|
187 |
@protocol TWTRSessionStore <TWTRUserSessionStore, TWTRGuestSessionStore, TWTRSessionRefreshingStore> |
|
188 |
|
|
189 |
/** |
|
190 |
* Returns the store's auth config. |
|
191 |
*/ |
|
192 |
@property (nonatomic, readonly) TWTRAuthConfig *authConfig; |
|
193 |
|
|
194 |
@end |
|
195 |
|
|
196 |
#pragma mark - Concrete Session Store Class |
|
197 |
|
|
198 |
/** |
|
199 |
* Concrete implementation of <TWTRSessionStore>. This session store supports fetching and storage of |
|
200 |
* user and guest sessions. In addition, the session store also supports refreshing of such sessions when they expire. |
|
201 |
* |
|
202 |
* @warning Instances of the session manager at the same path are not synchronized. The session store |
|
203 |
* will simply choose the latest version in the case of conflicts. |
|
204 |
*/ |
|
205 |
@interface TWTRSessionStore : NSObject <TWTRSessionStore> |
|
206 |
|
|
207 |
- (instancetype)init NS_UNAVAILABLE; |
|
208 |
|
|
209 |
/** |
|
210 |
* Provides a mechanism for reloading the session store. This method will force the session store |
|
211 |
* to find any sessions that may have been saved by another session store or application that is |
|
212 |
* using the same keychain access groups. |
|
213 |
* |
|
214 |
* Most applications will not need to call this method. You may need to call this method if you are |
|
215 |
* using multiple stores within your application and you need to synchronize when one writes to the |
|
216 |
* store. The more likely case for needing to call this method is if you are sharing credentials |
|
217 |
* between applications. In this situation you will want to call this method when the application |
|
218 |
* comes back to the foreground. |
|
219 |
* |
|
220 |
* This method does not need to be called when the store is created because this process happens |
|
221 |
* by default at time of instantiation. |
|
222 |
* |
|
223 |
* You should avoid calling this method if you do not have a specific reason to do so, like the reasons |
|
224 |
* mentioned above as this method does cause disk I/O and multiple calls can cause performance problems. |
|
225 |
*/ |
|
226 |
- (void)reloadSessionStore; |
|
227 |
|
|
228 |
@end |
|
229 |
|
|
230 |
NS_ASSUME_NONNULL_END |