Permalink
Browse files

Experimental implementation of HTTP Basic auth helper in AFRestClient

  • Loading branch information...
1 parent 79e4d0e commit f8d3550aafc414ec199062d55cfaaca0d5660b2e @mattt mattt committed Aug 24, 2011
@@ -38,6 +38,7 @@
- (NSString *)defaultValueForHeader:(NSString *)header;
- (void)setDefaultHeader:(NSString *)header value:(NSString *)value;
+- (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password;
- (void)setAuthorizationHeaderWithToken:(NSString *)token;
- (void)clearAuthorizationHeader;
@@ -23,6 +23,8 @@
#import "AFRestClient.h"
#import "AFJSONRequestOperation.h"
+#import "NSData+AFNetworking.h"
+
static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding;
@interface AFRestClient ()
@@ -86,6 +88,12 @@ - (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
[self.defaultHeaders setObject:value forKey:header];
}
+- (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
+ NSString *authHeader = [NSString stringWithFormat:@"%@:%@", username, password];
+ NSString *encodedAuthHeader = [[NSData dataWithBytes:[authHeader UTF8String] length:[authHeader length]] base64EncodedString];
+ [self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@", encodedAuthHeader]];
+}
+
- (void)setAuthorizationHeaderWithToken:(NSString *)token {
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
}
@@ -26,6 +26,7 @@ extern NSString * const kAFZlibErrorDomain;
@interface NSData (AFNetworking)
+- (NSString *)base64EncodedString;
- (NSData *)dataByGZipCompressingWithError:(NSError **)error;
- (NSData *)dataByGZipDecompressingDataWithError:(NSError **)error;
@@ -25,6 +25,8 @@
NSString * const kAFZlibErrorDomain = @"com.alamofire.zlib.error";
+static char Base64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
static inline NSUInteger NSDataEstimatedCompressedLength(NSData *data) {
return [data length] / 2;
}
@@ -88,6 +90,33 @@ + (NSData *)dataByTransformingData:(NSData *)data
@implementation NSData (AFNetworking)
+- (NSString *)base64EncodedString {
+ NSUInteger length = [self length];
+ NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
+
+ uint8_t *input = (uint8_t *)[self 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++) {
+ value <<= 8;
+
+ if (j < length) {
+ value |= (0xFF & input[j]);
+ }
+ }
+
+ NSInteger idx = (i / 3) * 4;
+ output[idx + 0] = Base64EncodingTable[(value >> 18) & 0x3F];
+ output[idx + 1] = Base64EncodingTable[(value >> 12) & 0x3F];
+ output[idx + 2] = (i + 1) < length ? Base64EncodingTable[(value >> 6) & 0x3F] : '=';
+ output[idx + 3] = (i + 2) < length ? Base64EncodingTable[(value >> 0) & 0x3F] : '=';
+ }
+
+ return [[[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding] autorelease];
+}
+
- (NSData *)dataByGZipCompressingWithError:(NSError **)error {
return [NSData dataByTransformingData:self usingGZipOperation:GzipDeflate error:error];
}

0 comments on commit f8d3550

Please sign in to comment.