Skip to content

Commit 51193ec

Browse files
committed
Changed auth flow
1 parent 81e672e commit 51193ec

File tree

8 files changed

+103
-135
lines changed

8 files changed

+103
-135
lines changed

Sources/NSURL+NXOAuth2.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
- (NSString *)valueForQueryParameterKey:(NSString *)key;
2020

21-
- (NSString *)URLStringWithoutQuery;
21+
- (NSURL *)URLWithoutQueryString;
22+
- (NSString *)URLStringWithoutQueryString;
2223

2324
@end

Sources/NSURL+NXOAuth2.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import "NSURL+NXOAuth2.h"
1212

1313

14-
@implementation NSURL (SoundCloudAPI)
14+
@implementation NSURL (NXOAuth2)
1515

1616
- (NSURL *)URLByAddingParameters:(NSDictionary *)parameterDictionary {
1717
if (!parameterDictionary || [parameterDictionary count] == 0) {
@@ -37,7 +37,12 @@ - (NSString *)valueForQueryParameterKey:(NSString *)key;
3737
return [parameters objectForKey:key];
3838
}
3939

40-
- (NSString *)URLStringWithoutQuery
40+
- (NSURL *)URLWithoutQueryString;
41+
{
42+
return [NSURL URLWithString:[self URLStringWithoutQueryString]];
43+
}
44+
45+
- (NSString *)URLStringWithoutQueryString;
4146
{
4247
NSArray *parts = [[self absoluteString] componentsSeparatedByString:@"?"];
4348
return [parts objectAtIndex:0];

Sources/OAuth2Client/NXOAuth2Client.h

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,8 @@
3030
NSURL *authorizeURL;
3131
NSURL *tokenURL;
3232

33-
// webserver flow
34-
NSURL *redirectURL;
35-
36-
// user credentials flow
37-
NSString *username;
38-
NSString *password;
39-
40-
// grand & token exchange
33+
// token exchange
4134
NXOAuth2Connection *authConnection;
42-
NSString *authGrand;
4335
NXOAuth2AccessToken *accessToken;
4436
NSMutableArray *retryConnectionsAfterTokenExchange;
4537

@@ -51,9 +43,7 @@
5143
@property (nonatomic, readonly) NSString *clientSecret;
5244

5345
@property (nonatomic, retain) NXOAuth2AccessToken *accessToken;
54-
55-
56-
#pragma mark WebServer Flow
46+
@property (nonatomic, assign) NSObject<NXOAuth2ClientAuthDelegate>* authDelegate;
5747

5848
/*!
5949
* Initializes the Client
@@ -62,24 +52,20 @@
6252
clientSecret:(NSString *)clientSecret
6353
authorizeURL:(NSURL *)authorizeURL
6454
tokenURL:(NSURL *)tokenURL
65-
redirectURL:(NSURL *)redirectURL
6655
authDelegate:(NSObject<NXOAuth2ClientAuthDelegate> *)authDelegate;
6756

68-
- (BOOL)openRedirectURL:(NSURL *)URL;
6957

58+
- (BOOL)openRedirectURL:(NSURL *)URL;
7059

71-
#pragma mark User credentials Flow
60+
/*!
61+
* returns the URL to be opened to get access grant
62+
*/
63+
- (NSURL *)authorizeWithRedirectURL:(NSURL *)redirectURL; // web server flow
7264

7365
/*!
74-
* Initializes the Client
66+
* authenticate with username & password
7567
*/
76-
- (id)initWithClientID:(NSString *)clientId
77-
clientSecret:(NSString *)clientSecret
78-
authorizeURL:(NSURL *)authorizeURL
79-
tokenURL:(NSURL *)tokenURL
80-
username:(NSString *)username
81-
password:(NSString *)password
82-
authDelegate:(NSObject<NXOAuth2ClientAuthDelegate> *)authDelegate;
68+
- (void)authorizeWithUsername:(NSString *)username password:(NSString *)password; // user credentials flow
8369

8470

8571
#pragma mark Public
@@ -94,7 +80,11 @@
9480

9581

9682
@protocol NXOAuth2ClientAuthDelegate
97-
- (void)oauthClient:(NXOAuth2Client *)client requestedAuthorizationWithURL:(NSURL *)authorizationURL;
98-
- (void)oauthClientDidAuthorize:(NXOAuth2Client *)client;
99-
- (void)oauthClient:(NXOAuth2Client *)client didFailToAuthorizeWithError:(NSError *)error;
83+
- (void)oauthClientDidGetAccessToken:(NXOAuth2Client *)client;
84+
- (void)oauthClient:(NXOAuth2Client *)client didFailToGetAccessTokenWithError:(NSError *)error;
85+
86+
/*!
87+
* use one of the -autherize* methods
88+
*/
89+
- (void)oauthClientRequestedAuthorization:(NXOAuth2Client *)client;
10090
@end

Sources/OAuth2Client/NXOAuth2Client.m

Lines changed: 34 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717

1818
@interface NXOAuth2Client ()
19-
- (void)requestAccessGrand;
20-
21-
- (void)requestTokenWithAuthGrand;
22-
- (void)requestTokenWithUsernameAndPassword;
19+
- (void)requestTokenWithAuthGrand:(NSString *)authGrand andRedirectURL:(NSURL *)redirectURL;
2320
@end
2421

2522

@@ -32,72 +29,41 @@ - (id)initWithClientID:(NSString *)aClientId
3229
clientSecret:(NSString *)aClientSecret
3330
authorizeURL:(NSURL *)anAuthorizeURL
3431
tokenURL:(NSURL *)aTokenURL
35-
redirectURL:(NSURL *)aRedirectURL
3632
authDelegate:(NSObject<NXOAuth2ClientAuthDelegate> *)anAuthDelegate;
3733
{
38-
NSAssert(aRedirectURL != nil, @"WebServer flow without redirectURL.");
3934
NSAssert(aTokenURL != nil && anAuthorizeURL != nil, @"No token or no authorize URL");
4035
if (self = [super init]) {
4136
clientId = [aClientId copy];
4237
clientSecret = [aClientSecret copy];
4338
authorizeURL = [anAuthorizeURL copy];
4439
tokenURL = [aTokenURL copy];
45-
redirectURL = [aRedirectURL copy];
4640

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;
4942
}
5043
return self;
5144
}
5245

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-
7746
- (void)dealloc;
7847
{
7948
[retryConnectionsAfterTokenExchange release];
8049
[authConnection cancel];
8150
[authConnection release];
8251
[clientId release];
8352
[clientSecret release];
84-
[redirectURL release];
85-
[username release];
86-
[password release];
8753
[super dealloc];
8854
}
8955

9056

9157
#pragma mark Accessors
9258

93-
@synthesize clientId, clientSecret;
59+
@synthesize clientId, clientSecret, authDelegate;
9460

9561
@dynamic accessToken;
9662

9763
- (NXOAuth2AccessToken *)accessToken;
9864
{
9965
if (accessToken) return accessToken;
100-
accessToken = [NXOAuth2AccessToken tokenFromDefaultKeychainWithServiceProviderName:[tokenURL host]];
66+
accessToken = [[NXOAuth2AccessToken tokenFromDefaultKeychainWithServiceProviderName:[tokenURL host]] retain];
10167
return accessToken;
10268
}
10369

@@ -119,34 +85,20 @@ - (void)setAccessToken:(NXOAuth2AccessToken *)value;
11985

12086
- (void)requestAccess;
12187
{
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];
13592
}
13693
}
13794

138-
- (void)requestAccessGrand;
95+
- (NSURL *)authorizeWithRedirectURL:(NSURL *)redirectURL;
13996
{
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]];
150102
}
151103

152104

@@ -155,9 +107,7 @@ - (BOOL)openRedirectURL:(NSURL *)URL;
155107
{
156108
NSString *accessGrand = [URL valueForQueryParameterKey:@"code"];
157109
if (accessGrand) {
158-
[authGrand release];
159-
authGrand = [accessGrand copy];
160-
[self requestTokenWithAuthGrand];
110+
[self requestTokenWithAuthGrand:accessGrand andRedirectURL:[URL URLWithoutQueryString]];
161111
return YES;
162112
}
163113
return NO;
@@ -166,7 +116,7 @@ - (BOOL)openRedirectURL:(NSURL *)URL;
166116
#pragma mark accessGrand -> accessToken
167117

168118
// Web Server Flow only
169-
- (void)requestTokenWithAuthGrand;
119+
- (void)requestTokenWithAuthGrand:(NSString *)authGrand andRedirectURL:(NSURL *)redirectURL;
170120
{
171121
NSAssert(!authConnection, @"invalid state");
172122

@@ -187,7 +137,7 @@ - (void)requestTokenWithAuthGrand;
187137

188138

189139
// User Password Flow Only
190-
- (void)requestTokenWithUsernameAndPassword;
140+
- (void)authorizeWithUsername:(NSString *)username password:(NSString *)password;
191141
{
192142
NSAssert(!authConnection, @"invalid state");
193143
NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL];
@@ -196,7 +146,6 @@ - (void)requestTokenWithUsernameAndPassword;
196146
@"password", @"grant_type",
197147
clientId, @"client_id",
198148
clientSecret, @"client_secret",
199-
[redirectURL absoluteString], @"redirect_uri",
200149
username, @"username",
201150
password, @"password",
202151
nil]];
@@ -216,23 +165,25 @@ - (void)refreshAccessToken;
216165

217166
- (void)refreshAccessTokenAndRetryConnection:(NXOAuth2Connection *)retryConnection;
218167
{
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];
232168
if (retryConnection) {
233169
if (!retryConnectionsAfterTokenExchange) retryConnectionsAfterTokenExchange = [[NSMutableArray alloc] init];
234170
[retryConnectionsAfterTokenExchange addObject:retryConnection];
235171
}
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+
}
236187
}
237188

238189
- (void)abortRetryOfConnection:(NXOAuth2Connection *)retryConnection;
@@ -252,7 +203,7 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSDa
252203
NXOAuth2AccessToken *newToken = [NXOAuth2AccessToken tokenWithResponseBody:result];
253204
NSAssert(newToken != nil, @"invalid response?");
254205
self.accessToken = newToken;
255-
[authDelegate oauthClientDidAuthorize:self];
206+
[authDelegate oauthClientDidGetAccessToken:self];
256207

257208
for (NXOAuth2Connection *retryConnection in retryConnectionsAfterTokenExchange) {
258209
[retryConnection retry];
@@ -264,7 +215,7 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSDa
264215
- (void)oauthConnection:(NXOAuth2Connection *)connection didFailWithError:(NSError *)error;
265216
{
266217
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?
268219
}
269220
}
270221

Sources/OAuth2Client/NXOAuth2Connection.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@
3434
@private
3535
NSURLConnection *connection;
3636
NSURLRequest *request;
37+
NSURLResponse *response;
3738

3839
NSMutableData *data;
39-
NSUInteger expectedContentLength;
40-
NSInteger statusCode;
4140

42-
id context;
43-
NSDictionary *userInfo;
41+
id context;
42+
NSDictionary *userInfo;
4443

4544
NXOAuth2Client *client;
4645

4746
NSObject<NXOAuth2ConnectionDelegate> *delegate; // assigned
4847
}
4948

5049
@property (readonly) NSData *data;
51-
@property (readonly) NSUInteger expectedContentLength;
50+
@property (readonly) long long expectedContentLength;
5251
@property (readonly) NSInteger statusCode;
5352
@property (retain) id context;
5453
@property (retain) NSDictionary *userInfo;

0 commit comments

Comments
 (0)