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

Custom header fields in NXOAuth2Request #132

Open
wants to merge 5 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
2 changes: 1 addition & 1 deletion Sources/NSString+NXOAuth2.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (NSString *)nxoauth2_URLEncodedString;
return (__bridge_transfer NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, //Allocator
(__bridge CFStringRef)self, //Original String
NULL, //Characters to leave unescaped
CFSTR("!*'();:@&=+$,/?%#[]"), //Legal Characters to be escaped
NULL, //CFSTR("!*'();:@&=+$,/?%#[]"), //Legal Characters to be escaped
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not leave in commented out code. If you think this should be removed do so, otherwise remove the comment.

kCFStringEncodingUTF8); //Encoding
}

Expand Down
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
14 changes: 7 additions & 7 deletions Sources/OAuth2Client/NXOAuth2Connection.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ - (NSURLConnection *)createConnection;
![[requestParameters objectForKey:@"grant_type"] isEqualToString:@"refresh_token"]) {

// if token is expired don't bother starting this connection.
NSDate *tenSecondsAgo = [NSDate dateWithTimeIntervalSinceNow:(-10)];
NSDate *tokenExpiresAt = client.accessToken.expiresAt;
if (client.accessToken.refreshToken && [tenSecondsAgo earlierDate:tokenExpiresAt] == tokenExpiresAt) {
if (client.accessToken.refreshToken && client.accessToken.hasExpired) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merge request is for adding header fields (a useful thing) but please only do that and do not change unrelated code.

[self cancel];
[client refreshAccessTokenAndRetryConnection:self];
return nil;
Expand All @@ -187,6 +185,8 @@ - (NSURLConnection *)createConnection;

if (oauthAuthorizationHeader) {
[startRequest setValue:oauthAuthorizationHeader forHTTPHeaderField:@"Authorization"];
// some services require the access token in the "t_auth_token" header field
[startRequest setValue:client.accessToken.accessToken forHTTPHeaderField:@"t_auth_token"];
}

if (client.userAgent && ![startRequest valueForHTTPHeaderField:@"User-Agent"]) {
Expand Down Expand Up @@ -415,10 +415,10 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
}
}
}
if (/*self.statusCode == 401 // TODO: check for status code once the bug returning 500 is fixed
&&*/ client.accessToken.refreshToken != nil
&& authenticateHeader
&& [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound) {

BOOL shouldRefresh = (self.statusCode == 401) && (client.accessToken.hasExpired) && (client.accessToken.refreshToken != nil);

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 *headerFields;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think customHeaderFields would be a better name, making the intention clear?

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 @@ -50,6 +59,7 @@
@property (nonatomic, strong, readwrite) NSString *requestMethod;
@property (nonatomic, strong, readwrite) NSURL *resource;
@property (nonatomic, strong, readwrite) NSDictionary *parameters;
@property (nonatomic, strong, readwrite) NSDictionary *headerFields;


#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.headerFields = 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 @@ - (id)initWithResource:(NSURL *)aResource method:(NSString *)aMethod parameters:
#pragma mark Accessors

@synthesize parameters;
@synthesize headerFields;
@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.headerFields) {
[self.headerFields enumerateKeysAndObjectsUsingBlock:^(NSString *field, NSString *value, BOOL *stop) {
if ([field isKindOfClass:NSString.class] && [value isKindOfClass:NSString.class]) {
[request setValue:value forHTTPHeaderField:field];
}
}];
}

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