PageRenderTime 236ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/Util/OAuthGateway.m

https://github.com/gholland/yammer_iphone
Objective C | 208 lines | 149 code | 50 blank | 9 comment | 44 complexity | 0884bfd6a2f6ff12fac4adea98d0a0be MD5 | raw file
  1. //
  2. // OAuthGateway.m
  3. // Yammer
  4. //
  5. // Created by aa on 1/28/09.
  6. // Copyright 2009 Yammer, Inc. All rights reserved.
  7. //
  8. #import "OAuthCustom.h"
  9. #import "OAuthGateway.h"
  10. #import "LocalStorage.h"
  11. #import "OAConsumer.h"
  12. #import "OAMutableURLRequest.h"
  13. #import "YammerAppDelegate.h"
  14. static NSString *ERROR_OUT_OF_RANGE = @"Network out of range.";
  15. @implementation OAuthGateway
  16. + (NSString *)baseURL {
  17. NSString *url = [LocalStorage getBaseURL];
  18. if (url)
  19. return url;
  20. //return @"http://aa.com:3000";
  21. //return @"http://localhost:3000";
  22. return @"https://www.yammer.com";
  23. }
  24. + (void)logout {
  25. [LocalStorage deleteAccountInfo];
  26. exit(0);
  27. }
  28. + (void)getRequestToken:(BOOL)createNewAccount {
  29. OAConsumer *consumer = [[OAConsumer alloc] initWithKey:OAUTH_KEY
  30. secret:OAUTH_SECRET];
  31. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/oauth/request_token", [OAuthGateway baseURL]]];
  32. OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
  33. consumer:consumer
  34. token:nil // we don't have a Token yet
  35. realm:nil // our service provider doesn't specify a realm
  36. signatureProvider:nil]; // use the default method, HMAC-SHA1
  37. [request setHTTPMethod:@"POST"];
  38. [request prepare];
  39. NSURLResponse *response;
  40. NSError *error;
  41. NSData *responseData;
  42. NSString *login = @"true";
  43. if (createNewAccount)
  44. login = @"false";
  45. responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  46. if (response == nil || responseData == nil || error != nil || [(NSHTTPURLResponse *)response statusCode] >= 400) {
  47. [YammerAppDelegate showError:@"oauth getRequestToken"];
  48. } else {
  49. NSString *responseBody = [[NSString alloc] initWithData:responseData
  50. encoding:NSUTF8StringEncoding];
  51. OAToken *requestToken = [[OAToken alloc] initWithHTTPResponseBody:responseBody];
  52. [LocalStorage saveRequestToken:responseBody];
  53. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:
  54. [NSString stringWithFormat:@"%@/oauth/authorize?oauth_token=%@&login=%@",
  55. [OAuthGateway baseURL],
  56. requestToken.key,
  57. login
  58. ]]];
  59. }
  60. }
  61. + (BOOL)getAccessToken:(NSString *)launchURL {
  62. OAToken *requestToken = [[OAToken alloc] initWithHTTPResponseBody:[LocalStorage getRequestToken]];
  63. OAConsumer *consumer = [[OAConsumer alloc] initWithKey:OAUTH_KEY
  64. secret:OAUTH_SECRET];
  65. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/oauth/access_token", [OAuthGateway baseURL]]];
  66. OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
  67. consumer:consumer
  68. token:requestToken
  69. realm:nil
  70. signatureProvider:nil];
  71. [request setHTTPMethod:@"POST"];
  72. request.HTTPShouldHandleCookies = NO;
  73. NSArray *parts = [launchURL componentsSeparatedByString:@"="];
  74. NSMutableArray *oauthParams = [NSMutableArray array];
  75. [oauthParams addObject:[[OARequestParameter alloc] initWithName:@"callback_token" value:[parts objectAtIndex:2]]];
  76. [request setParameters:oauthParams];
  77. [request prepare];
  78. NSURLResponse *response;
  79. NSError *error;
  80. NSData *responseData;
  81. responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  82. if (response == nil || responseData == nil || error != nil || [(NSHTTPURLResponse *)response statusCode] >= 400) {
  83. return false;
  84. } else {
  85. NSString *responseBody = [[NSString alloc] initWithData:responseData
  86. encoding:NSUTF8StringEncoding];
  87. [LocalStorage saveAccessToken:responseBody];
  88. return true;
  89. }
  90. }
  91. + (NSURL *)fixRelativeURL:(NSString *)path {
  92. if (![path hasPrefix:@"http"])
  93. return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", [OAuthGateway baseURL], path]];
  94. return [NSURL URLWithString:[NSString stringWithFormat:@"%@", path]];
  95. }
  96. + (NSString *)handleConnection:(OAMutableURLRequest *)request {
  97. NSHTTPURLResponse *response;
  98. NSError *error;
  99. NSData *responseData;
  100. responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  101. if ((response == nil || responseData == nil) && error == nil) {
  102. [YammerAppDelegate showError:ERROR_OUT_OF_RANGE];
  103. return nil;
  104. } else if (error != nil) {
  105. if ([error code] == -1012)
  106. [YammerAppDelegate showError:@"Login not valid, please logout via settings and login again."];
  107. else
  108. [YammerAppDelegate showError:ERROR_OUT_OF_RANGE];
  109. return nil;
  110. } else if ([response statusCode] >= 400) {
  111. NSString *detail = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  112. if ([detail length] > 30)
  113. detail = [detail substringToIndex:30];
  114. [YammerAppDelegate showError:[NSString stringWithFormat:@"%d %@", [response statusCode], detail]];
  115. return nil;
  116. }
  117. return [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  118. }
  119. + (NSString *)httpGet:(NSString *)path {
  120. NSURL *url = [OAuthGateway fixRelativeURL:path];
  121. OAConsumer *consumer = [[OAConsumer alloc] initWithKey:OAUTH_KEY
  122. secret:OAUTH_SECRET];
  123. OAToken *accessToken = [[OAToken alloc] initWithHTTPResponseBody:[LocalStorage getAccessToken]];
  124. OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
  125. consumer:consumer
  126. token:accessToken
  127. realm:nil
  128. signatureProvider:nil];
  129. request.HTTPShouldHandleCookies = NO;
  130. [request prepare];
  131. return [OAuthGateway handleConnection:request];
  132. }
  133. + (BOOL)httpGet200vsError:(NSString *)path {
  134. NSURL *url = [OAuthGateway fixRelativeURL:path];
  135. OAConsumer *consumer = [[OAConsumer alloc] initWithKey:OAUTH_KEY
  136. secret:OAUTH_SECRET];
  137. OAToken *accessToken = [[OAToken alloc] initWithHTTPResponseBody:[LocalStorage getAccessToken]];
  138. OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
  139. consumer:consumer
  140. token:accessToken
  141. realm:nil
  142. signatureProvider:nil];
  143. request.HTTPShouldHandleCookies = NO;
  144. [request prepare];
  145. NSHTTPURLResponse *response;
  146. NSError *error;
  147. NSData *responseData;
  148. responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  149. if ((response == nil || responseData == nil) && error == nil) {
  150. return false;
  151. } else if (error != nil) {
  152. return false;
  153. } else if ([response statusCode] >= 400) {
  154. return false;
  155. }
  156. return true;
  157. }
  158. @end