diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.h b/Sources/OAuth2Client/NXOAuth2AccountStore.h index 9325e8bf..d0ed6cf2 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.h +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.h @@ -84,6 +84,7 @@ typedef void(^NXOAuth2PreparedAuthorizationURLHandler)(NSURL *preparedURL); @property(nonatomic, strong) NSString *keychainAccessGroup; @property(nonatomic, strong) NSString *keychainServiceName; @property(nonatomic, strong, readonly) NSArray *accounts; +@property(nonatomic, copy) NSString *activeAccountType; - (NSArray *)accountsWithAccountType:(NSString *)accountType; - (NXOAuth2Account *)accountWithIdentifier:(NSString *)identifier; diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.m b/Sources/OAuth2Client/NXOAuth2AccountStore.m index 7f8978b2..09cc9ee2 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.m +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.m @@ -403,7 +403,7 @@ - (BOOL)handleRedirectURL:(NSURL *)aURL error: (NSError**) error for (NSString *accountType in accountTypes) { NXOAuth2Client *client = [self pendingOAuthClientForAccountType:accountType]; - if ([client openRedirectURL:fixedRedirectURL error:error]) { + if ([self.activeAccountType isEqualToString:accountType] && [client openRedirectURL:fixedRedirectURL error:error]) { return YES; } } diff --git a/Sources/OAuth2Client/NXOAuth2Client.h b/Sources/OAuth2Client/NXOAuth2Client.h index 6ee73d29..1dec4b8a 100644 --- a/Sources/OAuth2Client/NXOAuth2Client.h +++ b/Sources/OAuth2Client/NXOAuth2Client.h @@ -85,7 +85,6 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh; @property (nonatomic, unsafe_unretained) NSObject* delegate; #endif -@property (nonatomic, readonly) NXOAuth2Connection *authConnection; /*! * If set to NO, the access token is not stored any keychain, will be removed if it was. @@ -154,7 +153,6 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh; #pragma mark Public - (void)requestAccess; -- (void)requestAccessAndRetryConnection:(NXOAuth2Connection *)retryConnection; - (void)refreshAccessToken; - (void)refreshAccessTokenAndRetryConnection:(NXOAuth2Connection *)retryConnection; diff --git a/Sources/OAuth2Client/NXOAuth2Client.m b/Sources/OAuth2Client/NXOAuth2Client.m index 7e017539..cc281d7c 100644 --- a/Sources/OAuth2Client/NXOAuth2Client.m +++ b/Sources/OAuth2Client/NXOAuth2Client.m @@ -122,7 +122,6 @@ - (void)dealloc; @synthesize desiredScope, userAgent; @synthesize delegate, persistent, accessToken, authenticating; @synthesize additionalAuthenticationParameters; -@synthesize authConnection = authConnection; - (void)setAdditionalAuthenticationParameters:(NSDictionary *)value; { @@ -233,19 +232,8 @@ - (void)setDesiredScope:(NSSet *)aDesiredScope; #pragma mark Flow - (void)requestAccess; -{ - [self requestAccessAndRetryConnection:nil]; -} - -- (void)requestAccessAndRetryConnection:(NXOAuth2Connection *)retryConnection { if (!self.accessToken) { - - if (retryConnection) { - if (!waitingConnections) waitingConnections = [[NSMutableArray alloc] init]; - [waitingConnections addObject:retryConnection]; - } - [delegate oauthClientNeedsAuthentication:self]; } } diff --git a/Sources/OAuth2Client/NXOAuth2Connection.h b/Sources/OAuth2Client/NXOAuth2Connection.h index 7b885586..8fd362c8 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.h +++ b/Sources/OAuth2Client/NXOAuth2Connection.h @@ -40,7 +40,7 @@ #define NXOAuth2ConnectionDebug 0 #endif - +FOUNDATION_EXPORT NSString * const jsonContentType; extern NSString * const NXOAuth2ConnectionDidStartNotification; extern NSString * const NXOAuth2ConnectionDidEndNotification; diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index 8cc44405..14ab3b79 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -25,6 +25,7 @@ @interface NXOAuth2Client (Private) - (void)removeConnectionFromWaitingQueue:(NXOAuth2Connection *)connection; @end +NSString * const jsonContentType = @"application/json" ; NSString * const NXOAuth2ConnectionDidStartNotification = @"NXOAuth2ConnectionDidStartNotification"; NSString * const NXOAuth2ConnectionDidEndNotification = @"NXOAuth2ConnectionDidEndNotification"; @@ -244,7 +245,16 @@ - (void)applyParameters:(NSDictionary *)parameters onRequest:(NSMutableURLReques [aRequest setHTTPBodyStream:postBodyStream]; - } else if ([contentType isEqualToString:@"application/x-www-form-urlencoded"]) { + } + else if([contentType isEqualToString:jsonContentType]){ + NSError *error; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]; + if(!error) + { + [aRequest setHTTPBody:jsonData]; + } + } + else if ([contentType isEqualToString:@"application/x-www-form-urlencoded"]) { // sends the POST/PUT/PATCH request as application/x-www-form-urlencoded @@ -423,11 +433,9 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon && ([authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound || [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound )) { + [self cancel]; [client refreshAccessTokenAndRetryConnection:self]; - } else if (client.authConnection != self && authenticateHeader && client) { - [self cancel]; - [client requestAccessAndRetryConnection:self]; } else { if ([delegate respondsToSelector:@selector(oauthConnection:didReceiveData:)]) { [delegate oauthConnection:self didReceiveData:data]; // inform the delegate that we start with empty data @@ -600,4 +608,8 @@ - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallen } } +- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { + return nil; +} + @end diff --git a/Sources/OAuth2Client/NXOAuth2Request.h b/Sources/OAuth2Client/NXOAuth2Request.h index 09c9799f..96d9b003 100644 --- a/Sources/OAuth2Client/NXOAuth2Request.h +++ b/Sources/OAuth2Client/NXOAuth2Request.h @@ -37,6 +37,7 @@ sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler; + + (void)performMethod:(NSString *)method onResource:(NSURL *)resource usingParameters:(NSDictionary *)parameters @@ -58,6 +59,7 @@ @property (nonatomic, strong, readwrite) NXOAuth2Account *account; @property (nonatomic, strong, readwrite) NSString *requestMethod; +@property (nonatomic, strong, readwrite) NSString *requestContentType; @property (nonatomic, strong, readwrite) NSURL *resource; @property (nonatomic, strong, readwrite) NSDictionary *parameters; diff --git a/Sources/OAuth2Client/NXOAuth2Request.m b/Sources/OAuth2Client/NXOAuth2Request.m index 7c3cc1d1..7c6c2d59 100644 --- a/Sources/OAuth2Client/NXOAuth2Request.m +++ b/Sources/OAuth2Client/NXOAuth2Request.m @@ -22,6 +22,7 @@ #import "NXOAuth2Request.h" + @interface NXOAuth2Request () @property (nonatomic, strong, readwrite) NXOAuth2Connection *connection; @property (nonatomic, strong, readwrite) NXOAuth2Request *me; @@ -82,6 +83,17 @@ - (instancetype)initWithResource:(NSURL *)aResource method:(NSString *)aMethod p } +- (instancetype)initWithResource:(NSURL *)aResource method:(NSString *)aMethod contentType:(NSString *) aContentType parameters:(NSDictionary *)someParameters +{ + self = [super init]; + if (self) { + resource = aResource; + parameters = someParameters; + requestMethod = aMethod; + } + return self; +} + #pragma mark Accessors @synthesize parameters; @@ -125,7 +137,15 @@ - (void)performRequestWithSendingProgressHandler:(NXOAuth2ConnectionSendingProgr NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.resource]; [request setHTTPMethod:self.requestMethod]; + + [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; + if(self.requestContentType) + { + [request setValue:self.requestContentType forHTTPHeaderField:@"Content-Type"]; + } + [self applyHeaders:self.headerFields onRequest:request]; + self.connection = [[NXOAuth2Connection alloc] initWithRequest:request requestParameters:self.parameters oauthClient:self.account.oauthClient @@ -180,16 +200,15 @@ - (void)applyParameters:(NSDictionary *)someParameters onRequest:(NSMutableURLRe NSString *httpMethod = [aRequest HTTPMethod]; if (![@[@"POST",@"PUT",@"PATCH"] containsObject: [httpMethod uppercaseString]]) { aRequest.URL = [aRequest.URL nxoauth2_URLByAddingParameters:someParameters]; - } else { + } else { NSInputStream *postBodyStream = [[NXOAuth2PostBodyStream alloc] initWithParameters:parameters]; - NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", [(NXOAuth2PostBodyStream *)postBodyStream boundary]]; - NSString *contentLength = [NSString stringWithFormat:@"%llu", [(NXOAuth2PostBodyStream *)postBodyStream length]]; [aRequest setValue:contentType forHTTPHeaderField:@"Content-Type"]; + NSString *contentLength = [NSString stringWithFormat:@"%llu", [(NXOAuth2PostBodyStream *)postBodyStream length]]; [aRequest setValue:contentLength forHTTPHeaderField:@"Content-Length"]; - [aRequest setHTTPBodyStream:postBodyStream]; } + } - (void)applyHeaders:(NSDictionary *)someHeaders onRequest:(NSMutableURLRequest *)aRequest;