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

Feature/header fields #168

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
11 changes: 11 additions & 0 deletions Sources/OAuth2Client/NXOAuth2Client.m
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,17 @@ - (void)refreshAccessTokenAndRetryConnection:(NXOAuth2Connection *)retryConnecti
clientSecret, @"client_secret",
accessToken.refreshToken, @"refresh_token",
nil];

if (self.additionalAuthenticationParameters) {
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
}

if (self.customHeaderFields) {
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[tokenRequest addValue:obj forHTTPHeaderField:key];
}];
}

if (self.desiredScope) {
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
}
Expand Down
12 changes: 8 additions & 4 deletions Sources/OAuth2Client/NXOAuth2Connection.m
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,14 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
}
}
}
if (self.statusCode == 401
&& client.accessToken.refreshToken != nil
&& authenticateHeader
&& [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound) {

BOOL shouldRefresh = (self.statusCode == 401)
&& (client.accessToken.hasExpired)
&& (client.accessToken.refreshToken != nil)
&& authenticateHeader
&& [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound;

if (shouldRefresh) {
[self cancel];
[client refreshAccessTokenAndRetryConnection:self];
} else {
Expand Down
10 changes: 10 additions & 0 deletions Sources/OAuth2Client/NXOAuth2Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@interface NXOAuth2Request : NSObject {
@private
NSDictionary *parameters;
NSDictionary *customHeaderFields;
NSURL *resource;
NSString *requestMethod;
NXOAuth2Account *account;
Expand All @@ -37,6 +38,14 @@
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;

+ (void)performMethod:(NSString *)method
onResource:(NSURL *)resource
usingParameters:(NSDictionary *)parameters
headerFields:(NSDictionary *)headerFields
withAccount:(NXOAuth2Account *)account
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;


#pragma mark Lifecycle

Expand All @@ -52,6 +61,7 @@
@property (nonatomic, strong, readwrite) NSString *requestMethod;
@property (nonatomic, strong, readwrite) NSURL *resource;
@property (nonatomic, strong, readwrite) NSDictionary *parameters;
@property (nonatomic, strong, readwrite) NSDictionary *customHeaderFields;


#pragma mark Signed NSURLRequest
Expand Down
26 changes: 26 additions & 0 deletions Sources/OAuth2Client/NXOAuth2Request.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ @implementation NXOAuth2Request
+ (void)performMethod:(NSString *)aMethod
onResource:(NSURL *)aResource
usingParameters:(NSDictionary *)someParameters
headerFields:(NSDictionary *)headerFields
withAccount:(NXOAuth2Account *)anAccount
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;
Expand All @@ -45,9 +46,24 @@ + (void)performMethod:(NSString *)aMethod
method:aMethod
parameters:someParameters];
request.account = anAccount;
request.customHeaderFields = headerFields;
[request performRequestWithSendingProgressHandler:progressHandler responseHandler:responseHandler];
}

+ (void)performMethod:(NSString *)aMethod
onResource:(NSURL *)aResource
usingParameters:(NSDictionary *)someParameters
withAccount:(NXOAuth2Account *)anAccount
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;
{
[self performMethod:aMethod
onResource:aResource
usingParameters:someParameters
withAccount:anAccount
sendProgressHandler:progressHandler
responseHandler:responseHandler];
}

#pragma mark Lifecycle

Expand All @@ -66,6 +82,7 @@ - (instancetype)initWithResource:(NSURL *)aResource method:(NSString *)aMethod p
#pragma mark Accessors

@synthesize parameters;
@synthesize customHeaderFields;
@synthesize resource;
@synthesize requestMethod;
@synthesize account;
Expand Down Expand Up @@ -105,6 +122,15 @@ - (void)performRequestWithSendingProgressHandler:(NXOAuth2ConnectionSendingProgr

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.resource];
[request setHTTPMethod:self.requestMethod];

if (self.customHeaderFields) {
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *field, NSString *value, BOOL *stop) {
NSAssert([field isKindOfClass:NSString.class], @"Invalid Type For Field - String Expected");
NSAssert([value isKindOfClass:NSString.class], @"Invalid Type For Value - String Expected");
[request setValue:value forHTTPHeaderField:field];
}];
}

self.connection = [[NXOAuth2Connection alloc] initWithRequest:request
requestParameters:self.parameters
oauthClient:self.account.oauthClient
Expand Down