-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMPOAuthAPI.m
More file actions
224 lines (172 loc) · 8.49 KB
/
MPOAuthAPI.m
File metadata and controls
224 lines (172 loc) · 8.49 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// MPOAuthAPI.m
// MPOAuthConnection
//
// Created by Karl Adam on 08.12.05.
// Copyright 2008 matrixPointer. All rights reserved.
//
#import "MPOAuthAPIRequestLoader.h"
#import "MPOAuthAPI.h"
#import "MPOAuthCredentialConcreteStore.h"
#import "MPOAuthURLRequest.h"
#import "MPOAuthURLResponse.h"
#import "MPURLRequestParameter.h"
#import "MPOAuthAuthenticationMethod.h"
#import "NSURL+MPURLParameterAdditions.h"
NSString *kMPOAuthCredentialConsumerKey = @"kMPOAuthCredentialConsumerKey";
NSString *kMPOAuthCredentialConsumerSecret = @"kMPOAuthCredentialConsumerSecret";
NSString *kMPOAuthCredentialUsername = @"kMPOAuthCredentialUsername";
NSString *kMPOAuthCredentialPassword = @"kMPOAuthCredentialPassword";
NSString *kMPOAuthCredentialRequestToken = @"kMPOAuthCredentialRequestToken";
NSString *kMPOAuthCredentialRequestTokenSecret = @"kMPOAuthCredentialRequestTokenSecret";
NSString *kMPOAuthCredentialAccessToken = @"kMPOAuthCredentialAccessToken";
NSString *kMPOAuthCredentialAccessTokenSecret = @"kMPOAuthCredentialAccessTokenSecret";
NSString *kMPOAuthCredentialSessionHandle = @"kMPOAuthCredentialSessionHandle";
NSString *kMPOAuthSignatureMethod = @"kMPOAuthSignatureMethod";
NSString * const MPOAuthTokenRefreshDateDefaultsKey = @"MPOAuthAutomaticTokenRefreshLastExpiryDate";
@interface MPOAuthAPI ()
@property (nonatomic, readwrite, retain) id <MPOAuthCredentialStore, MPOAuthParameterFactory> credentials;
@property (nonatomic, readwrite, retain) NSURL *authenticationURL;
@property (nonatomic, readwrite, retain) NSURL *baseURL;
@property (nonatomic, readwrite, retain) NSMutableArray *activeLoaders;
@property (nonatomic, readwrite, assign) MPOAuthAuthenticationState authenticationState;
- (void)performMethod:(NSString *)inMethod atURL:(NSURL *)inURL withParameters:(NSArray *)inParameters withTarget:(id)inTarget andAction:(SEL)inAction usingHTTPMethod:(NSString *)inHTTPMethod;
@end
@implementation MPOAuthAPI
- (id)initWithCredentials:(NSDictionary *)inCredentials andBaseURL:(NSURL *)inBaseURL {
return [self initWithCredentials:inCredentials authenticationURL:inBaseURL andBaseURL:inBaseURL];
}
- (id)initWithCredentials:(NSDictionary *)inCredentials authenticationURL:(NSURL *)inAuthURL andBaseURL:(NSURL *)inBaseURL {
return [self initWithCredentials:inCredentials authenticationURL:inBaseURL andBaseURL:inBaseURL autoStart:YES];
}
- (id)initWithCredentials:(NSDictionary *)inCredentials authenticationURL:(NSURL *)inAuthURL andBaseURL:(NSURL *)inBaseURL autoStart:(BOOL)aFlag {
if (self = [super init]) {
self.authenticationURL = inAuthURL;
self.baseURL = inBaseURL;
self.authenticationState = MPOAuthAuthenticationStateUnauthenticated;
credentials_ = [[MPOAuthCredentialConcreteStore alloc] initWithCredentials:inCredentials forBaseURL:inBaseURL withAuthenticationURL:inAuthURL];
self.authenticationMethod = [[MPOAuthAuthenticationMethod alloc] initWithAPI:self forURL:inAuthURL];
self.signatureScheme = MPOAuthSignatureSchemeHMACSHA1;
activeLoaders_ = [[NSMutableArray alloc] initWithCapacity:10];
if (aFlag) {
[self authenticate];
}
}
return self;
}
- (oneway void)dealloc {
self.credentials = nil;
self.baseURL = nil;
self.authenticationURL = nil;
self.authenticationMethod = nil;
self.activeLoaders = nil;
[super dealloc];
}
@synthesize credentials = credentials_;
@synthesize baseURL = baseURL_;
@synthesize authenticationURL = authenticationURL_;
@synthesize authenticationMethod = authenticationMethod_;
@synthesize signatureScheme = signatureScheme_;
@synthesize activeLoaders = activeLoaders_;
@synthesize authenticationState = oauthAuthenticationState_;
#pragma mark -
- (void)setSignatureScheme:(MPOAuthSignatureScheme)inScheme {
signatureScheme_ = inScheme;
NSString *methodString = @"HMAC-SHA1";
switch (signatureScheme_) {
case MPOAuthSignatureSchemePlainText:
methodString = @"PLAINTEXT";
break;
case MPOAuthSignatureSchemeRSASHA1:
methodString = @"RSA-SHA1";
case MPOAuthSignatureSchemeHMACSHA1:
default:
// already initted to the default
break;
}
[(MPOAuthCredentialConcreteStore *)credentials_ setSignatureMethod:methodString];
}
#pragma mark -
- (void)authenticate {
NSAssert(credentials_.consumerKey, @"A Consumer Key is required for use of OAuth.");
[self.authenticationMethod authenticate];
}
- (BOOL)isAuthenticated {
return (self.authenticationState == MPOAuthAuthenticationStateAuthenticated);
}
#pragma mark -
- (void)performMethod:(NSString *)inMethod withTarget:(id)inTarget andAction:(SEL)inAction {
[self performMethod:inMethod atURL:self.baseURL withParameters:nil withTarget:inTarget andAction:inAction usingHTTPMethod:@"GET"];
}
- (void)performMethod:(NSString *)inMethod atURL:(NSURL *)inURL withParameters:(NSArray *)inParameters withTarget:(id)inTarget andAction:(SEL)inAction {
[self performMethod:inMethod atURL:inURL withParameters:inParameters withTarget:inTarget andAction:inAction usingHTTPMethod:@"GET"];
}
- (void)performPOSTMethod:(NSString *)inMethod atURL:(NSURL *)inURL withParameters:(NSArray *)inParameters withTarget:(id)inTarget andAction:(SEL)inAction {
[self performMethod:inMethod atURL:inURL withParameters:inParameters withTarget:inTarget andAction:inAction usingHTTPMethod:@"POST"];
}
- (void)performMethod:(NSString *)inMethod atURL:(NSURL *)inURL withParameters:(NSArray *)inParameters withTarget:(id)inTarget andAction:(SEL)inAction usingHTTPMethod:(NSString *)inHTTPMethod {
if (!inMethod && ![inURL path] && ![inURL query]) {
[NSException raise:@"MPOAuthNilMethodRequestException" format:@"Nil was passed as the method to be performed on %@", inURL];
}
NSURL *requestURL = inMethod ? [NSURL URLWithString:inMethod relativeToURL:inURL] : inURL;
MPOAuthURLRequest *aRequest = [[MPOAuthURLRequest alloc] initWithURL:requestURL andParameters:inParameters];
MPOAuthAPIRequestLoader *loader = [[MPOAuthAPIRequestLoader alloc] initWithRequest:aRequest];
aRequest.HTTPMethod = inHTTPMethod;
loader.credentials = self.credentials;
loader.target = inTarget;
loader.action = inAction ? inAction : @selector(_performedLoad:receivingData:);
[loader loadSynchronously:NO];
// [self.activeLoaders addObject:loader];
[loader release];
[aRequest release];
}
- (void)performURLRequest:(NSURLRequest *)inRequest withTarget:(id)inTarget andAction:(SEL)inAction {
if (!inRequest && ![[inRequest URL] path] && ![[inRequest URL] query]) {
[NSException raise:@"MPOAuthNilMethodRequestException" format:@"Nil was passed as the method to be performed on %@", inRequest];
}
MPOAuthURLRequest *aRequest = [[MPOAuthURLRequest alloc] initWithURLRequest:inRequest];
MPOAuthAPIRequestLoader *loader = [[MPOAuthAPIRequestLoader alloc] initWithRequest:aRequest];
loader.credentials = self.credentials;
loader.target = inTarget;
loader.action = inAction ? inAction : @selector(_performedLoad:receivingData:);
[loader loadSynchronously:NO];
// [self.activeLoaders addObject:loader];
[loader release];
[aRequest release];
}
- (NSData *)dataForMethod:(NSString *)inMethod {
return [self dataForURL:self.baseURL andMethod:inMethod withParameters:nil];
}
- (NSData *)dataForMethod:(NSString *)inMethod withParameters:(NSArray *)inParameters {
return [self dataForURL:self.baseURL andMethod:inMethod withParameters:inParameters];
}
- (NSData *)dataForURL:(NSURL *)inURL andMethod:(NSString *)inMethod withParameters:(NSArray *)inParameters {
NSURL *requestURL = [NSURL URLWithString:inMethod relativeToURL:inURL];
MPOAuthURLRequest *aRequest = [[MPOAuthURLRequest alloc] initWithURL:requestURL andParameters:inParameters];
MPOAuthAPIRequestLoader *loader = [[MPOAuthAPIRequestLoader alloc] initWithRequest:aRequest];
loader.credentials = self.credentials;
[loader loadSynchronously:YES];
[loader autorelease];
[aRequest release];
return loader.data;
}
#pragma mark -
- (id)credentialNamed:(NSString *)inCredentialName {
return [self.credentials credentialNamed:inCredentialName];
}
- (void)setCredential:(id)inCredential withName:(NSString *)inName {
[(MPOAuthCredentialConcreteStore *)self.credentials setCredential:inCredential withName:inName];
}
- (void)removeCredentialNamed:(NSString *)inName {
[(MPOAuthCredentialConcreteStore *)self.credentials removeCredentialNamed:inName];
}
- (void)discardCredentials {
[self.credentials discardOAuthCredentials];
self.authenticationState = MPOAuthAuthenticationStateUnauthenticated;
}
#pragma mark -
#pragma mark - Private APIs -
- (void)_performedLoad:(MPOAuthAPIRequestLoader *)inLoader receivingData:(NSData *)inData {
// NSLog(@"loaded %@, and got %@", inLoader, inData);
}
@end