Skip to content

Commit

Permalink
update for header xauth
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanlakhani committed Oct 24, 2013
1 parent 0550af3 commit 9147d44
Showing 1 changed file with 47 additions and 42 deletions.
89 changes: 47 additions & 42 deletions AFXAuthClient/AFXAuthClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Updates made by @arc90/Zeeshan Lakhani
//

#import "AFXAuthClient.h"
#import "AFHTTPRequestOperation.h"
Expand All @@ -40,10 +42,10 @@
{
NSUInteger length = [data length];
NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];

uint8_t *input = (uint8_t *)[data bytes];
uint8_t *output = (uint8_t *)[mutableData mutableBytes];

for (NSUInteger i = 0; i < length; i += 3) {
NSUInteger value = 0;
for (NSUInteger j = i; j < (i + 3); j++) {
Expand All @@ -52,16 +54,16 @@
value |= (0xFF & input[j]);
}
}

static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

NSUInteger idx = (i / 3) * 4;
output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
}

return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
}

Expand All @@ -75,7 +77,7 @@
// Instapaper authentication
static NSString * const kAFCharactersToBeEscaped = @":/?#[]@!$&'()*+,;=";
static NSString * const kAFCharactersToLeaveUnescaped = @"-._~";

return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescaped, (__bridge CFStringRef)kAFCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding));
}

Expand All @@ -86,22 +88,22 @@
NSScanner *parameterScanner = [[NSScanner alloc] initWithString:queryString];
NSString *name = nil;
NSString *value = nil;

while (![parameterScanner isAtEnd]) {
name = nil;
[parameterScanner scanUpToString:@"=" intoString:&name];
[parameterScanner scanString:@"=" intoString:NULL];

value = nil;
[parameterScanner scanUpToString:@"&" intoString:&value];
[parameterScanner scanString:@"&" intoString:NULL];

if (name && value) {
[parameters setValue:[value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:[name stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
}
}

return parameters;
}

Expand Down Expand Up @@ -149,44 +151,44 @@ - (NSString *)baseStringWithRequest:(NSURLRequest *)request parameters:(NSDictio
NSString *oauth_signature_method = RFC3986EscapedStringWithEncoding(@"HMAC-SHA1", NSUTF8StringEncoding);
NSString *oauth_timestamp = RFC3986EscapedStringWithEncoding(_timestamp, NSUTF8StringEncoding);
NSString *oauth_version = RFC3986EscapedStringWithEncoding(@"1.0", NSUTF8StringEncoding);

NSArray *params = @[[NSString stringWithFormat:@"%@%%3D%@", @"oauth_consumer_key", oauth_consumer_key],
[NSString stringWithFormat:@"%@%%3D%@", @"oauth_nonce", oauth_nonce],
[NSString stringWithFormat:@"%@%%3D%@", @"oauth_signature_method", oauth_signature_method],
[NSString stringWithFormat:@"%@%%3D%@", @"oauth_timestamp", oauth_timestamp],
[NSString stringWithFormat:@"%@%%3D%@", @"oauth_version", oauth_version]];

for (NSString *key in parameters) {
NSString *param = RFC3986EscapedStringWithEncoding([parameters objectForKey:key], NSUTF8StringEncoding);
param = RFC3986EscapedStringWithEncoding(param, NSUTF8StringEncoding);
params = [params arrayByAddingObjectsFromArray:@[[NSString stringWithFormat:@"%@%%3D%@", key, param]]];
}
if (self.token)
params = [params arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@%%3D%@", @"oauth_token", RFC3986EscapedStringWithEncoding(self.token.key, NSUTF8StringEncoding)], nil]];


params = [params sortedArrayUsingSelector:@selector(compare:)];
NSString *baseString = [@[request.HTTPMethod,
RFC3986EscapedStringWithEncoding([[request.URL.absoluteString componentsSeparatedByString:@"?"] objectAtIndex:0], NSUTF8StringEncoding),
[params componentsJoinedByString:@"%26"]] componentsJoinedByString:@"&"];
RFC3986EscapedStringWithEncoding([[request.URL.absoluteString componentsSeparatedByString:@"?"] objectAtIndex:0], NSUTF8StringEncoding),
[params componentsJoinedByString:@"%26"]] componentsJoinedByString:@"&"];
return baseString;
}

- (NSString *)authorizationHeaderForParameters:(NSDictionary *)parameters
{
static NSString * const kAFOAuth1AuthorizationFormatString = @"OAuth %@";

if (!parameters) {
return nil;
}

NSArray *sortedComponents = [[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) componentsSeparatedByString:@"&"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSMutableArray *mutableComponents = [NSMutableArray array];
for (NSString *component in sortedComponents) {
NSArray *subcomponents = [component componentsSeparatedByString:@"="];
[mutableComponents addObject:[NSString stringWithFormat:@"%@=\"%@\"", [subcomponents objectAtIndex:0], [subcomponents objectAtIndex:1]]];
}

return [NSString stringWithFormat:kAFOAuth1AuthorizationFormatString, [mutableComponents componentsJoinedByString:@", "]];
}

Expand All @@ -210,13 +212,13 @@ -(void)authorizeUsingXAuthWithAccessTokenPath:(NSString *)accessTokenPath
{
_username = username;
_password = password;

NSDictionary *parameters = @{@"x_auth_mode": mode,
NSDictionary *xAuthParametersForHeader = @{@"x_auth_mode": mode,
@"x_auth_password": self.password,
@"x_auth_username": self.username};

NSMutableURLRequest *request = [self requestWithMethod:accessMethod path:accessTokenPath parameters:parameters];

NSMutableURLRequest *request = [self requestWithMethod:accessMethod path:accessTokenPath parameters:xAuthParametersForHeader];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *queryString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
_token = [[AFXAuthToken alloc] initWithQueryString:queryString];
Expand All @@ -226,21 +228,24 @@ -(void)authorizeUsingXAuthWithAccessTokenPath:(NSString *)accessTokenPath
if (failure)
failure(error);
}];

[self enqueueHTTPRequestOperation:operation];
}

- (NSMutableDictionary *)authorizationHeaderWithRequest:(NSURLRequest *)request parameters:(NSDictionary *)parameters
{
NSMutableDictionary *authorizationHeader = [[NSMutableDictionary alloc] initWithDictionary:@{@"oauth_nonce": _nonce,
@"oauth_signature_method": @"HMAC-SHA1",
@"oauth_timestamp": _timestamp,
@"oauth_consumer_key": self.consumerKey,
@"oauth_signature": AFHMACSHA1Signature([self baseStringWithRequest:request parameters:parameters], _consumerSecret, _token.secret),
@"oauth_version": @"1.0"}];
@"oauth_signature_method": @"HMAC-SHA1",
@"oauth_timestamp": _timestamp,
@"oauth_consumer_key": self.consumerKey,
@"oauth_signature": AFHMACSHA1Signature([self baseStringWithRequest:request parameters:parameters], _consumerSecret, _token.secret),
@"oauth_version": @"1.0",
@"x_auth_mode": parameters[@"mode"],
@"x_auth_password": parameters[@"x_auth_password"],
@"x_auth_username": parameters[@"x_auth_username"]}];
if (self.token)
[authorizationHeader setObject:RFC3986EscapedStringWithEncoding(self.token.key, NSUTF8StringEncoding) forKey:@"oauth_token"];

return authorizationHeader;
}

Expand All @@ -252,10 +257,10 @@ - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
{
_nonce = [NSString stringWithFormat:@"%d", arc4random()];
_timestamp = [NSString stringWithFormat:@"%d", (int)ceil((float)[[NSDate date] timeIntervalSince1970])];

NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:nil];
NSMutableDictionary *authorizationHeader = [self authorizationHeaderWithRequest:request parameters:parameters];

[request setValue:[self authorizationHeaderForParameters:authorizationHeader] forHTTPHeaderField:@"Authorization"];
[request setHTTPShouldHandleCookies:NO];
return request;
Expand All @@ -265,10 +270,10 @@ - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method path:
{
_nonce = [NSString stringWithFormat:@"%d", arc4random()];
_timestamp = [NSString stringWithFormat:@"%d", (int)ceil((float)[[NSDate date] timeIntervalSince1970])];

NSMutableURLRequest *request = [super multipartFormRequestWithMethod:method path:path parameters:parameters constructingBodyWithBlock:block];
NSMutableDictionary *authorizationHeader = [self authorizationHeaderWithRequest:request parameters:parameters];

[request setValue:[self authorizationHeaderForParameters:authorizationHeader] forHTTPHeaderField:@"Authorization"];
[request setHTTPShouldHandleCookies:NO];
return request;
Expand All @@ -292,7 +297,7 @@ - (id)initWithQueryString:(NSString *)queryString
if (!queryString || [queryString length] == 0) {
return nil;
}

NSDictionary *attributes = AFParametersFromQueryString(queryString);
return [self initWithKey:[attributes objectForKey:@"oauth_token"] secret:[attributes objectForKey:@"oauth_token_secret"]];
}
Expand All @@ -302,15 +307,15 @@ - (id)initWithKey:(NSString *)key
{
NSParameterAssert(key);
NSParameterAssert(secret);

self = [super init];
if (!self) {
return nil;
}

self.key = key;
self.secret = secret;

return self;
}

Expand All @@ -325,12 +330,12 @@ - (void)encodeWithCoder:(NSCoder *)coder
- (id)initWithCoder:(NSCoder *)coder
{
self = [super init];

if (self) {
self.key = [coder decodeObjectForKey:@"AFXAuthClientKey"];
self.secret = [coder decodeObjectForKey:@"AFXAuthClientSecret"];
}

return self;
}

Expand Down

0 comments on commit 9147d44

Please sign in to comment.