Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for JSON payloads. Added support for HTTP PATCH. Oaut… #185

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/OAuth2Client/NXOAuth2AccountStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Sources/OAuth2Client/NXOAuth2AccountStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 0 additions & 2 deletions Sources/OAuth2Client/NXOAuth2Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
@property (nonatomic, unsafe_unretained) NSObject<NXOAuth2ClientDelegate>* 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.
Expand Down Expand Up @@ -154,7 +153,6 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
#pragma mark Public

- (void)requestAccess;
- (void)requestAccessAndRetryConnection:(NXOAuth2Connection *)retryConnection;

- (void)refreshAccessToken;
- (void)refreshAccessTokenAndRetryConnection:(NXOAuth2Connection *)retryConnection;
Expand Down
12 changes: 0 additions & 12 deletions Sources/OAuth2Client/NXOAuth2Client.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ - (void)dealloc;
@synthesize desiredScope, userAgent;
@synthesize delegate, persistent, accessToken, authenticating;
@synthesize additionalAuthenticationParameters;
@synthesize authConnection = authConnection;

- (void)setAdditionalAuthenticationParameters:(NSDictionary *)value;
{
Expand Down Expand Up @@ -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];
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OAuth2Client/NXOAuth2Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#define NXOAuth2ConnectionDebug 0
#endif


FOUNDATION_EXPORT NSString * const jsonContentType;

extern NSString * const NXOAuth2ConnectionDidStartNotification;
extern NSString * const NXOAuth2ConnectionDidEndNotification;
Expand Down
20 changes: 16 additions & 4 deletions Sources/OAuth2Client/NXOAuth2Connection.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -600,4 +608,8 @@ - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallen
}
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}

@end
2 changes: 2 additions & 0 deletions Sources/OAuth2Client/NXOAuth2Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;


+ (void)performMethod:(NSString *)method
onResource:(NSURL *)resource
usingParameters:(NSDictionary *)parameters
Expand All @@ -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;

Expand Down
27 changes: 23 additions & 4 deletions Sources/OAuth2Client/NXOAuth2Request.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#import "NXOAuth2Request.h"


@interface NXOAuth2Request () <NXOAuth2ConnectionDelegate>
@property (nonatomic, strong, readwrite) NXOAuth2Connection *connection;
@property (nonatomic, strong, readwrite) NXOAuth2Request *me;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down