16
16
17
17
18
18
@interface NXOAuth2Client ()
19
- - (void )requestAccessGrand ;
20
-
21
- - (void )requestTokenWithAuthGrand ;
22
- - (void )requestTokenWithUsernameAndPassword ;
19
+ - (void )requestTokenWithAuthGrand : (NSString *)authGrand andRedirectURL : (NSURL *)redirectURL ;
23
20
@end
24
21
25
22
@@ -32,72 +29,41 @@ - (id)initWithClientID:(NSString *)aClientId
32
29
clientSecret : (NSString *)aClientSecret
33
30
authorizeURL : (NSURL *)anAuthorizeURL
34
31
tokenURL : (NSURL *)aTokenURL
35
- redirectURL : (NSURL *)aRedirectURL
36
32
authDelegate : (NSObject <NXOAuth2ClientAuthDelegate> *)anAuthDelegate ;
37
33
{
38
- NSAssert (aRedirectURL != nil , @" WebServer flow without redirectURL." );
39
34
NSAssert (aTokenURL != nil && anAuthorizeURL != nil , @" No token or no authorize URL" );
40
35
if (self = [super init ]) {
41
36
clientId = [aClientId copy ];
42
37
clientSecret = [aClientSecret copy ];
43
38
authorizeURL = [anAuthorizeURL copy ];
44
39
tokenURL = [aTokenURL copy ];
45
- redirectURL = [aRedirectURL copy ];
46
40
47
- authDelegate = anAuthDelegate;
48
- if (self.accessToken && !self.accessToken .hasExpired ) [authDelegate oauthClientDidAuthorize: self ]; // if we have a valid access token in the keychain
41
+ self.authDelegate = anAuthDelegate;
49
42
}
50
43
return self;
51
44
}
52
45
53
- - (id )initWithClientID : (NSString *)aClientId
54
- clientSecret : (NSString *)aClientSecret
55
- authorizeURL : (NSURL *)anAuthorizeURL
56
- tokenURL : (NSURL *)aTokenURL
57
- username : (NSString *)aUsername
58
- password : (NSString *)aPassword
59
- authDelegate : (NSObject <NXOAuth2ClientAuthDelegate> *)anAuthDelegate ;
60
- {
61
- NSAssert (aUsername != nil && aPassword != nil , @" Username & password flow without username & password." );
62
- NSAssert (aTokenURL != nil && anAuthorizeURL != nil , @" No token or no authorize URL" );
63
- if (self = [super init ]) {
64
- clientId = [aClientId copy ];
65
- clientSecret = [aClientSecret copy ];
66
- authorizeURL = [anAuthorizeURL copy ];
67
- tokenURL = [aTokenURL copy ];
68
- username = [aUsername copy ];
69
- password = [aPassword copy ];
70
-
71
- authDelegate = anAuthDelegate;
72
- if (self.accessToken && !self.accessToken .hasExpired ) [authDelegate oauthClientDidAuthorize: self ]; // if we have a valid access token in the keychain
73
- }
74
- return self;
75
- }
76
-
77
46
- (void )dealloc ;
78
47
{
79
48
[retryConnectionsAfterTokenExchange release ];
80
49
[authConnection cancel ];
81
50
[authConnection release ];
82
51
[clientId release ];
83
52
[clientSecret release ];
84
- [redirectURL release ];
85
- [username release ];
86
- [password release ];
87
53
[super dealloc ];
88
54
}
89
55
90
56
91
57
#pragma mark Accessors
92
58
93
- @synthesize clientId, clientSecret;
59
+ @synthesize clientId, clientSecret, authDelegate ;
94
60
95
61
@dynamic accessToken;
96
62
97
63
- (NXOAuth2AccessToken *)accessToken ;
98
64
{
99
65
if (accessToken) return accessToken;
100
- accessToken = [NXOAuth2AccessToken tokenFromDefaultKeychainWithServiceProviderName: [tokenURL host ]];
66
+ accessToken = [[ NXOAuth2AccessToken tokenFromDefaultKeychainWithServiceProviderName: [tokenURL host ]] retain ];
101
67
return accessToken;
102
68
}
103
69
@@ -119,34 +85,20 @@ - (void)setAccessToken:(NXOAuth2AccessToken *)value;
119
85
120
86
- (void )requestAccess ;
121
87
{
122
- if (self.accessToken ) {
123
- if (self.accessToken .hasExpired ){
124
- [self refreshAccessToken ];
125
- }
126
- } else if (username != nil && password != nil ) { // username password flow
127
- [self requestTokenWithUsernameAndPassword ];
128
- } else { // web server flow
129
- NSAssert (redirectURL, @" Web server flow without redirectURL" );
130
- if (authGrand) { // we have grand already
131
- [self requestTokenWithAuthGrand ];
132
- } else {
133
- [self requestAccessGrand ];
134
- }
88
+ if (!self.accessToken ) {
89
+ [authDelegate oauthClientRequestedAuthorization: self ];
90
+ } else {
91
+ [authDelegate oauthClientDidGetAccessToken: self ];
135
92
}
136
93
}
137
94
138
- - (void ) requestAccessGrand ;
95
+ - (NSURL *) authorizeWithRedirectURL : ( NSURL *) redirectURL ;
139
96
{
140
- if (authConnection) { // authentication is already running
141
- return ;
142
- }
143
-
144
- NSURL *URL = [authorizeURL URLByAddingParameters: [NSDictionary dictionaryWithObjectsAndKeys:
145
- @" code" , @" response_type" ,
146
- clientId, @" client_id" ,
147
- [redirectURL absoluteString ], @" redirect_uri" ,
148
- nil ]];
149
- [authDelegate oauthClient: self requestedAuthorizationWithURL: URL];
97
+ return [authorizeURL URLByAddingParameters: [NSDictionary dictionaryWithObjectsAndKeys:
98
+ @" code" , @" response_type" ,
99
+ clientId, @" client_id" ,
100
+ [redirectURL absoluteString ], @" redirect_uri" ,
101
+ nil ]];
150
102
}
151
103
152
104
@@ -155,9 +107,7 @@ - (BOOL)openRedirectURL:(NSURL *)URL;
155
107
{
156
108
NSString *accessGrand = [URL valueForQueryParameterKey: @" code" ];
157
109
if (accessGrand) {
158
- [authGrand release ];
159
- authGrand = [accessGrand copy ];
160
- [self requestTokenWithAuthGrand ];
110
+ [self requestTokenWithAuthGrand: accessGrand andRedirectURL: [URL URLWithoutQueryString ]];
161
111
return YES ;
162
112
}
163
113
return NO ;
@@ -166,7 +116,7 @@ - (BOOL)openRedirectURL:(NSURL *)URL;
166
116
#pragma mark accessGrand -> accessToken
167
117
168
118
// Web Server Flow only
169
- - (void )requestTokenWithAuthGrand ;
119
+ - (void )requestTokenWithAuthGrand : ( NSString *) authGrand andRedirectURL : ( NSURL *) redirectURL ;
170
120
{
171
121
NSAssert (!authConnection, @" invalid state" );
172
122
@@ -187,7 +137,7 @@ - (void)requestTokenWithAuthGrand;
187
137
188
138
189
139
// User Password Flow Only
190
- - (void )requestTokenWithUsernameAndPassword ;
140
+ - (void )authorizeWithUsername : ( NSString *) username password : ( NSString *) password ;
191
141
{
192
142
NSAssert (!authConnection, @" invalid state" );
193
143
NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL: tokenURL];
@@ -196,7 +146,6 @@ - (void)requestTokenWithUsernameAndPassword;
196
146
@" password" , @" grant_type" ,
197
147
clientId, @" client_id" ,
198
148
clientSecret, @" client_secret" ,
199
- [redirectURL absoluteString ], @" redirect_uri" ,
200
149
username, @" username" ,
201
150
password, @" password" ,
202
151
nil ]];
@@ -216,23 +165,25 @@ - (void)refreshAccessToken;
216
165
217
166
- (void )refreshAccessTokenAndRetryConnection : (NXOAuth2Connection *)retryConnection ;
218
167
{
219
- NSAssert ((accessToken.refreshToken != nil ), @"invalid state");
220
- NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL: tokenURL];
221
- [tokenRequest setHTTPMethod: @" POST" ];
222
- [tokenRequest setParameters: [NSDictionary dictionaryWithObjectsAndKeys:
223
- @" refresh_token" , @" grant_type" ,
224
- clientId, @" client_id" ,
225
- clientSecret, @" client_secret" ,
226
- accessToken.refreshToken, @" refresh_token" ,
227
- nil ]];
228
- [authConnection release ]; // just to be sure
229
- authConnection = [[NXOAuth2Connection alloc ] initWithRequest: tokenRequest
230
- oauthClient: self
231
- delegate: self ];
232
168
if (retryConnection) {
233
169
if (!retryConnectionsAfterTokenExchange) retryConnectionsAfterTokenExchange = [[NSMutableArray alloc ] init ];
234
170
[retryConnectionsAfterTokenExchange addObject: retryConnection];
235
171
}
172
+ if (!authConnection) {
173
+ NSAssert ((accessToken.refreshToken != nil ), @"invalid state");
174
+ NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL: tokenURL];
175
+ [tokenRequest setHTTPMethod: @" POST" ];
176
+ [tokenRequest setParameters: [NSDictionary dictionaryWithObjectsAndKeys:
177
+ @" refresh_token" , @" grant_type" ,
178
+ clientId, @" client_id" ,
179
+ clientSecret, @" client_secret" ,
180
+ accessToken.refreshToken, @" refresh_token" ,
181
+ nil ]];
182
+ [authConnection release ]; // not needed, but looks more clean to me :)
183
+ authConnection = [[NXOAuth2Connection alloc ] initWithRequest: tokenRequest
184
+ oauthClient: nil
185
+ delegate: self ];
186
+ }
236
187
}
237
188
238
189
- (void )abortRetryOfConnection : (NXOAuth2Connection *)retryConnection ;
@@ -252,7 +203,7 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSDa
252
203
NXOAuth2AccessToken *newToken = [NXOAuth2AccessToken tokenWithResponseBody: result];
253
204
NSAssert (newToken != nil , @" invalid response?" );
254
205
self.accessToken = newToken;
255
- [authDelegate oauthClientDidAuthorize :self ];
206
+ [authDelegate oauthClientDidGetAccessToken :self ];
256
207
257
208
for (NXOAuth2Connection *retryConnection in retryConnectionsAfterTokenExchange) {
258
209
[retryConnection retry ];
@@ -264,7 +215,7 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSDa
264
215
- (void )oauthConnection : (NXOAuth2Connection *)connection didFailWithError : (NSError *)error ;
265
216
{
266
217
if (connection == authConnection) {
267
- [authDelegate oauthClient: self didFailToAuthorizeWithError : error]; // TODO: create own error domain?
218
+ [authDelegate oauthClient: self didFailToGetAccessTokenWithError : error]; // TODO: create own error domain?
268
219
}
269
220
}
270
221
0 commit comments