OAToken.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // OAToken.m
  3. // OAuthConsumer
  4. //
  5. // Created by Jon Crosby on 10/19/07.
  6. // Copyright 2007 Kaboomerang LLC. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "OAToken.h"
  26. @implementation OAToken
  27. + (OAToken *)token {
  28. return [[[[self class]alloc]init]autorelease];
  29. }
  30. + (OAToken *)tokenWithKey:(NSString *)aKey secret:(NSString *)aSecret {
  31. return [[[[self class]alloc]initWithKey:aKey secret:aSecret]autorelease];
  32. }
  33. + (OAToken *)tokenWithHTTPResponseBody:(NSString *)body {
  34. return [[[[self class]alloc]initWithHTTPResponseBody:body]autorelease];
  35. }
  36. + (OAToken *)tokenWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:(NSString *)prefix {
  37. return [[[[self class]alloc]initWithUserDefaultsUsingServiceProviderName:provider prefix:prefix]autorelease];
  38. }
  39. - (NSString *)pin {
  40. return self.verifier;
  41. }
  42. - (void)setPin:(NSString *)aPin {
  43. [self setVerifier:aPin];
  44. }
  45. - (id)init {
  46. if (self = [super init]) {
  47. self.key = @"";
  48. self.secret = @"";
  49. self.verifier = @"";
  50. }
  51. return self;
  52. }
  53. - (id)initWithKey:(NSString *)aKey secret:(NSString *)aSecret {
  54. if (self = [super init]) {
  55. self.key = aKey;
  56. self.secret = aSecret;
  57. self.verifier = @"";
  58. }
  59. return self;
  60. }
  61. - (id)initWithHTTPResponseBody:(NSString *)body {
  62. if (self = [super init]) {
  63. if (body == nil) {
  64. body = @"";
  65. }
  66. NSArray *pairs = [body componentsSeparatedByString:@"&"];
  67. for (NSString *pair in pairs) {
  68. NSArray *elements = [pair componentsSeparatedByString:@"="];
  69. if ([[elements objectAtIndex:0] isEqualToString:@"oauth_token"]) {
  70. self.key = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  71. } else if ([[elements objectAtIndex:0] isEqualToString:@"oauth_token_secret"]) {
  72. self.secret = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  73. }
  74. }
  75. self.verifier = @"";
  76. }
  77. return self;
  78. }
  79. - (id)initWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:(NSString *)prefix {
  80. self = [super init];
  81. if (self) {
  82. NSString *theKey = [[NSUserDefaults standardUserDefaults]stringForKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]];
  83. NSString *theSecret = [[NSUserDefaults standardUserDefaults]stringForKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]];
  84. BOOL nokey = (theKey.length == 0);
  85. BOOL nosecret = (theSecret.length == 0);
  86. if ((nokey && nosecret) || (nokey || nosecret)) {
  87. return nil;
  88. }
  89. self.key = theKey;
  90. self.secret = theSecret;
  91. self.verifier = @"";
  92. }
  93. return self;
  94. }
  95. - (void)dealloc {
  96. [self setVerifier:nil];
  97. [self setKey:nil];
  98. [self setSecret:nil];
  99. [super dealloc];
  100. }
  101. - (void)storeInUserDefaultsWithServiceProviderName:(NSString *)provider prefix:(NSString *)prefix {
  102. [[NSUserDefaults standardUserDefaults]setObject:self.key forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]];
  103. [[NSUserDefaults standardUserDefaults]setObject:self.secret forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]];
  104. [[NSUserDefaults standardUserDefaults]synchronize];
  105. }
  106. @end