-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIATwitterEngine.m
198 lines (155 loc) · 8.18 KB
/
IATwitterEngine.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// IATwitterEngine.m
//
// Created by Ivan Ablamskyi on 1/23/12.
// Copyright (c) 2012 Coppertino Inc. All rights reserved.
//
// Licensed under the BSD License <http://www.opensource.org/licenses/bsd-license>
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import "IATwitterEngine.h"
#define TW_HOSTNAME @"https://api.twitter.com"
#define TW_REQUEST_TOKEN @"oauth/request_token"
#define TW_ACCESS_TOKEN @"oauth/access_token"
#define TW_STATUS_UPDATE @"1/statuses/update.json"
// Never share this information
#if !defined(TW_CONSUMER_KEY) || !defined(TW_CONSUMER_SECRET)
#error Define your Consumer Key and Secret
#endif
// This will be called after the user authorizes your app
#ifndef TW_CALLBACK_URL
#define TW_CALLBACK_URL @"iatwitterengine://auth_token"
#endif
#define TW_AUTHORIZE(__TOKEN__) [NSString stringWithFormat:@"https://api.twitter.com/oauth/authorize?oauth_token=%@", __TOKEN__]
@interface IATwitterEngine ( /* Private */ )
@property (nonatomic, copy, readwrite) NSString *screenName;
@property (copy) IATwitterEngineCompletionBlock oAuthCompletionBlock;
- (void)removeOAuthTokenFromKeychain;
- (void)storeOAuthTokenInKeychain;
- (void)retrieveOAuthTokenFromKeychain;
@end
@implementation IATwitterEngine
@synthesize delegate = _delegate;
@synthesize screenName = _screenName;
@synthesize oAuthCompletionBlock = _oAuthCompletionBlock;
- (id)initWithDelegate:(id <IATwitterEngineDelegate>)delegate {
self = [super initWithHostName:TW_HOSTNAME customHeaderFields:nil signatureMethod:IAOAuthHMAC_SHA1 consumerKey:TW_CONSUMER_KEY consumerSecret:TW_CONSUMER_SECRET callbackURL:TW_CALLBACK_URL];
if (self) {
self.oAuthCompletionBlock = nil;
self.screenName = nil;
self.delegate = delegate;
// Retrieve OAuth access token (if previously stored)
[self retrieveOAuthTokenFromKeychain];
}
return self;
}
- (void)dealloc {
self.oAuthCompletionBlock = nil;
self.screenName = nil;
self.delegate = nil;
[super dealloc];
}
#pragma mark - OAuth Access Token store/retrieve
- (void)removeOAuthTokenFromKeychain {
[self removeValueFromKeychainUsingName:@"oauth_token"];
[self removeValueFromKeychainUsingName:@"oauth_token_secret"];
}
- (void)storeOAuthTokenInKeychain {
[self addToKeychainUsingName:@"oauth_token" andValue:self.token];
[self addToKeychainUsingName:@"oauth_token_secret" andValue:self.tokenSecret];
}
- (void)retrieveOAuthTokenFromKeychain {
NSString *token = [self findValueFromKeychainUsingName:@"oauth_token"],
*tokenSecret = [self findValueFromKeychainUsingName:@"oauth_token_secret"];
if (token && tokenSecret) {
[self setAccessToken:token secret:tokenSecret];
}
}
#pragma mark - OAuth Authentication Flow
- (void)authenticateWithCompletionBlock:(IATwitterEngineCompletionBlock)completionBlock {
// Store the Completion Block to call after Authenticated
self.oAuthCompletionBlock = completionBlock;
// First we reset the OAuth token, so we won't send previous tokens in the request
[self resetOAuthToken];
// OAuth Step 1 - Obtain a request token
[self postPath:TW_REQUEST_TOKEN parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Fill the request token with the returned data
[self fillTokenWithResponseBody:[operation responseString] type:IAOAuthRequestToken];
// OAuth Step 2 - Redirect user to authorization page
NSAssert(self.delegate && [self.delegate respondsToSelector:@selector(twitterEngine:statusUpdate:)], @"Delegate %@ should implement statusUpdate:", self.delegate);
[self.delegate twitterEngine:self statusUpdate:@"Waiting for user authorization..."];
NSAssert(self.delegate && [self.delegate respondsToSelector:@selector(twitterEngine:needsToOpenURL:)], @"Delegate %@ should implement needsToOpenURL:", self.delegate);
NSURL *url = [NSURL URLWithString:TW_AUTHORIZE(self.token)];
[self.delegate twitterEngine:self needsToOpenURL:url];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completionBlock(error, nil);
self.oAuthCompletionBlock = nil;
}];
NSAssert(self.delegate && [self.delegate respondsToSelector:@selector(twitterEngine:statusUpdate:)], @"Delegate %@ should implement statusUpdate:", self.delegate);
[self.delegate twitterEngine:self statusUpdate:@"Requesting Tokens..."];
}
- (void)resumeAuthenticationFlowWithURL:(NSURL *)url {
// Fill the request token with data returned in the callback URL
[self fillTokenWithResponseBody:url.query type:IAOAuthRequestToken];
// OAuth Step 3 - Exchange the request token with an access token
[self postPath:TW_ACCESS_TOKEN
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self fillTokenWithResponseBody:[operation responseString] type:IAOAuthAccessToken];
// Retrieve the user's screen name
self.screenName = [self.customValues valueForKey:@"screen_name"];
// Store the OAuth access token
[self storeOAuthTokenInKeychain];
// Finished, return to previous method
if (self.oAuthCompletionBlock) self.oAuthCompletionBlock(nil, nil);
self.oAuthCompletionBlock = nil;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (self.oAuthCompletionBlock) self.oAuthCompletionBlock(error, nil);
self.oAuthCompletionBlock = nil;
}
];
[self.delegate twitterEngine:self statusUpdate:@"Authenticating..."];
}
- (void)cancelAuthentication {
NSDictionary *ui = [NSDictionary dictionaryWithObjectsAndKeys:@"Authentication cancelled.", NSLocalizedDescriptionKey, nil];
NSError *error = [NSError errorWithDomain:[[[NSBundle mainBundle] bundleIdentifier] stringByAppendingString:@".ErrorDomain"] code:401 userInfo:ui];
if (self.oAuthCompletionBlock) self.oAuthCompletionBlock(error, nil);
self.oAuthCompletionBlock = nil;
}
- (void)forgetStoredToken {
[self removeOAuthTokenFromKeychain];
[self resetOAuthToken];
self.screenName = nil;
}
#pragma mark - Public Methods
- (void)sendTweet:(NSDictionary *)tweetParams withCompletionBlock:(IATwitterEngineCompletionBlock)completionBlock {
if (!self.isAuthenticated) {
[self authenticateWithCompletionBlock:^(NSError *error, id json) {
if (error) {
// Authentication failed, return the error
completionBlock(error, json);
} else {
// Authentication succeeded, call this method again
[self sendTweet:tweetParams withCompletionBlock:completionBlock];
}
}];
// This method will be called again once the authentication completes
return;
}
// Fill the post body with the tweet
[[AFJSONRequestOperation JSONRequestOperationWithRequest:[self requestWithMethod:@"POST" path:TW_STATUS_UPDATE parameters:tweetParams]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
completionBlock(nil, JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
if (response.statusCode == 401 || response.statusCode == 403) {
// Auth was revoken - foreget
[self forgetStoredToken];
NSLog(@"frs:twauth revoken external");
[self sendTweet:tweetParams withCompletionBlock:completionBlock];
return;
}
completionBlock(error, JSON);
}
] start];
[self.delegate twitterEngine:self statusUpdate:@"Sending tweet..."];
}
@end