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
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,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain this change? These characters need to be escaped here for URL encoding to be RFC compliant.

Copy link
Author

Choose a reason for hiding this comment

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

I was making this request based on the previous one, re-added Code!

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
10 changes: 6 additions & 4 deletions Sources/OAuth2Client/NXOAuth2Connection.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,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"];
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be that some services require this, but this is not part of the IETF standard in any RFC that I'm aware of, or am I missing something?

Copy link
Author

Choose a reason for hiding this comment

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

I was making this request based on the previous one, removed Code!

}

if (client.userAgent && ![startRequest valueForHTTPHeaderField:@"User-Agent"]) {
Expand Down Expand Up @@ -415,10 +417,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);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm for using a BOOL it makes things more clear. As for the commented out code, that was an oversight in another merge and it's removed in 5fe6147


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) {
if ([field isKindOfClass:NSString.class] && [value isKindOfClass:NSString.class]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather NSAssert() the class type here instead of silently failing if it's wrong. Since I would consider not passing NSString here for both a programmer error, which warrants an exception in objc.

[request setValue:value forHTTPHeaderField:field];
}
}];
}

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