|
18 | 18 | #import "OAuthIOModal.h"
|
19 | 19 |
|
20 | 20 | @implementation OAuthIOModal
|
21 |
| - |
22 | 21 | NSString *_host;
|
23 | 22 |
|
24 | 23 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
|
@@ -146,10 +145,63 @@ - (void)getTokens:(NSString *)url
|
146 | 145 | [data_to_file writeToFile:file_url atomically:YES];
|
147 | 146 | }
|
148 | 147 |
|
| 148 | + if (_saved_cookies != nil) { |
| 149 | + NSHTTPCookie *cookie; |
| 150 | + NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; |
| 151 | + for (cookie in _saved_cookies) { |
| 152 | + [storage setCookie:cookie]; |
| 153 | + } |
| 154 | + [[NSUserDefaults standardUserDefaults] synchronize]; |
| 155 | + } |
| 156 | + |
| 157 | + |
149 | 158 | @try {
|
150 | 159 | OAuthIORequest *request = [self buildRequestObject:json];
|
151 |
| - if ([self.delegate respondsToSelector:@selector(didReceiveOAuthIOResponse:)]) |
152 |
| - [self.delegate didReceiveOAuthIOResponse:request]; |
| 160 | + if ([[request getCredentials] objectForKey:@"code"] != nil && _authUrl != nil) { |
| 161 | + //Signing in to the selected URL, giving back the response |
| 162 | + |
| 163 | + NSString *code = [[request getCredentials] objectForKey:@"code"]; |
| 164 | + NSString *post = [NSString stringWithFormat:@"code=%@", code]; |
| 165 | + NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; |
| 166 | + NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; |
| 167 | + NSMutableURLRequest *state_request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_authUrl] |
| 168 | + cachePolicy:NSURLRequestUseProtocolCachePolicy |
| 169 | + timeoutInterval:60.0]; |
| 170 | + [state_request setHTTPMethod:@"POST"]; |
| 171 | + [state_request setValue:postLength forHTTPHeaderField:@"Content-Length"]; |
| 172 | + [state_request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; |
| 173 | + [state_request setHTTPBody:postData]; |
| 174 | + |
| 175 | + [[_session dataTaskWithRequest:state_request |
| 176 | + completionHandler:^(NSData *data, |
| 177 | + NSURLResponse *response, |
| 178 | + NSError *error) { |
| 179 | + NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; |
| 180 | + NSString *body = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; |
| 181 | + if ([httpResponse statusCode] == 200) { |
| 182 | + if ([self.delegate respondsToSelector:@selector(didAuthenticateServerSide:andResponse:)]) |
| 183 | + [self.delegate didAuthenticateServerSide:body andResponse:response]; |
| 184 | + } else { |
| 185 | + NSDictionary *userInfo = @{ |
| 186 | + NSLocalizedDescriptionKey: NSLocalizedString(@"Operation was unsuccessful.", nil), |
| 187 | + NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The authentication endpoint did not return 200 OK.", nil), |
| 188 | + NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString(@"Are you using the right authentication endpoint?", nil) |
| 189 | + }; |
| 190 | + NSError *error = [NSError errorWithDomain:@"com.oauthio.error" |
| 191 | + code:-1 |
| 192 | + userInfo:userInfo]; |
| 193 | + if ([self.delegate respondsToSelector:@selector(didFailAuthenticationServerSide:andResponse:andError:)]) |
| 194 | + [self.delegate didFailAuthenticationServerSide:body andResponse:response andError:error]; |
| 195 | + } |
| 196 | + }] resume]; |
| 197 | + |
| 198 | + } else if ([[request getCredentials] objectForKey:@"code"] != nil) { |
| 199 | + if ([self.delegate respondsToSelector:@selector(didReceiveOAuthIOCode:)]) |
| 200 | + [self.delegate didReceiveOAuthIOCode:[[request getCredentials] objectForKey:@"code"]]; |
| 201 | + }else { |
| 202 | + if ([self.delegate respondsToSelector:@selector(didReceiveOAuthIOResponse:)]) |
| 203 | + [self.delegate didReceiveOAuthIOResponse:request]; |
| 204 | + } |
153 | 205 | }
|
154 | 206 | @catch (NSException *e) {
|
155 | 207 | NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
|
@@ -251,6 +303,38 @@ - (void)showWithProvider:(NSString *)provider {
|
251 | 303 | [self showWithProvider:provider options:nil];
|
252 | 304 | }
|
253 | 305 |
|
| 306 | +- (void)showWithProvider:(NSString *)provider options:(NSDictionary*)options stateTokenUrl:(NSString*) stateUrl authUrl:(NSString*) authUrl; |
| 307 | +{ |
| 308 | + _session = [NSURLSession sharedSession]; |
| 309 | + _authUrl = authUrl; |
| 310 | + [[_session dataTaskWithURL:[NSURL URLWithString:stateUrl] |
| 311 | + completionHandler:^(NSData *data, |
| 312 | + NSURLResponse *response, |
| 313 | + NSError *error) { |
| 314 | + NSString *body = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; |
| 315 | + NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; |
| 316 | + if (dictionary != nil && [dictionary objectForKey:@"token"] != nil) { |
| 317 | + |
| 318 | + NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; |
| 319 | + NSMutableDictionary *options_mut = [options mutableCopy]; |
| 320 | + [options_mut setObject:[dictionary objectForKey:@"token"] forKey:@"state"]; |
| 321 | + [self showWithProvider:provider options:options_mut]; |
| 322 | + } else { |
| 323 | + NSDictionary *userInfo = @{ |
| 324 | + NSLocalizedDescriptionKey: NSLocalizedString(@"Operation was unsuccessful.", nil), |
| 325 | + NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The state token could not be retrieved.", nil), |
| 326 | + NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString(@"Are you using the right state token retrieval endpoint?", nil) |
| 327 | + }; |
| 328 | + NSError *error = [NSError errorWithDomain:@"com.oauthio.error" |
| 329 | + code:-1 |
| 330 | + userInfo:userInfo]; |
| 331 | + if ([self.delegate respondsToSelector:@selector(didFailAuthenticationServerSide:andResponse:andError:)]) |
| 332 | + [self.delegate didFailAuthenticationServerSide:body andResponse:response andError:error]; |
| 333 | + } |
| 334 | + |
| 335 | + }] resume]; |
| 336 | +} |
| 337 | + |
254 | 338 | - (void)showWithProvider:(NSString *)provider options:(NSDictionary*)options
|
255 | 339 | {
|
256 | 340 | _options = options;
|
@@ -293,11 +377,12 @@ - (void)showWithProvider:(NSString *)provider options:(NSDictionary*)options
|
293 | 377 |
|
294 | 378 | NSURLRequest *url = [_oauth getOAuthRequest:provider andUrl:_callback_url andOptions:options];
|
295 | 379 | if ([[_options objectForKey:@"clear-popup-cache"] isEqual: @"true"]) {
|
296 |
| -// [[NSURLCache sharedURLCache] removeAllCachedResponses]; |
297 | 380 | NSHTTPCookie *cookie;
|
298 | 381 | NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
|
| 382 | + _saved_cookies = [[NSMutableArray alloc] init]; |
299 | 383 | for (cookie in [storage cookies]) {
|
300 | 384 | [storage deleteCookie:cookie];
|
| 385 | + [_saved_cookies addObject:cookie]; |
301 | 386 | }
|
302 | 387 | [[NSUserDefaults standardUserDefaults] synchronize];
|
303 | 388 | }
|
|
0 commit comments