Permalink
Browse files

cleanup, redirect auth support

  • Loading branch information...
1 parent e768da4 commit 4bbbbdb949edab8ba34cc7982781f5490de921fd @stevedekorte stevedekorte committed Oct 8, 2012
Showing with 50 additions and 292 deletions.
  1. +2 −2 S3Kit/S3Kit/BS3Request.h
  2. +48 −44 S3Kit/S3Kit/BS3Request.m
  3. +0 −50 S3Kit/S3Kit/BS3Requestor.h
  4. +0 −196 S3Kit/S3Kit/BS3Requestor.m
@@ -25,6 +25,7 @@
@property (nonatomic, retain) NSString *resourcePath;
@property (nonatomic, retain) NSString *accessKey;
@property (nonatomic, retain) NSString *secretKey;
+@property (nonatomic, retain) NSString *redirectUrlString;
@property (nonatomic, retain) NSMutableDictionary *parameters;
@property (assign) Class requestClass;
@@ -38,8 +39,7 @@
- (NSString *)stringToSign;
- (NSString *)authorizationHeader;
-- (NSMutableURLRequest *)composedRequest;
-- (NSMutableURLRequest *)signedRequest;
+- (void)prepareAndSign;
- (void)show;
@@ -10,43 +10,39 @@
#import <CommonCrypto/CommonHMAC.h>
#import "NSString+Crypto.h"
-@implementation NSString (URL)
+@interface NSString (URL)
- (NSString *)urlEncoded;
@end
@implementation NSString (URL)
- (NSString *)urlEncoded
{
- return [[self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
+ NSString *result = self;
+ result = [[result stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
+ result = [[result stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
+ stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
+
+ return result;
+
+ //return [self stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
}
@end
@implementation BS3Request
-@synthesize bucketName;
-@synthesize resourcePath;
-@synthesize accessKey;
-@synthesize secretKey;
-
-@synthesize HTTPMethod;
-@synthesize allHTTPHeaderFields;
-@synthesize HTTPBody;
-@synthesize parameters;
-
-@synthesize usesSSL;
-@synthesize date;
-
- (id)init
{
self = [super init];
- parameters = [NSMutableDictionary dictionary];
- HTTPMethod = @"GET";
- date = [NSDate date];
- [allHTTPHeaderFields setObject:[self dateHeader] forKey:@"Date"];
- usesSSL = YES;
+ self.parameters = [NSMutableDictionary dictionary];
+ self.HTTPMethod = @"GET";
+ self.date = [NSDate date];
+ [self addValue:[self dateHeader] forHTTPHeaderField:@"Date"];
+ self.usesSSL = YES;
+
+
return self;
}
@@ -77,7 +73,7 @@ + (NSString *)dateHeaderForDate:(NSDate *)date
- (NSString *)stringForParameters
{
- if (!parameters || [[parameters allKeys] count] < 1) return @"";
+ if (!self.parameters || [[self.parameters allKeys] count] < 1) return @"";
static NSSet *validParameters;
if (!validParameters)
{
@@ -89,10 +85,10 @@ - (NSString *)stringForParameters
}
NSString *paramStr = @"";
- for (NSString *key in [parameters allKeys])
+ for (NSString *key in [self.parameters allKeys])
{
paramStr = [validParameters containsObject:key] ?
- [paramStr stringByAppendingFormat:@"&%@=%@", key, [[parameters objectForKey:key] urlEncoded]] :
+ [paramStr stringByAppendingFormat:@"&%@=%@", key, [[self.parameters objectForKey:key] urlEncoded]] :
paramStr;
}
@@ -105,28 +101,36 @@ - (NSString *)stringForParameters
- (NSString *)composedURLString
{
- NSString *scheme = usesSSL ? @"https" : @"http";
+ NSString *scheme = self.usesSSL ? @"https" : @"http";
+ NSString *urlString;
- //NSString *urlString = [NSString stringWithFormat:@"%@://s3.amazonaws.com/%@%@",
- // scheme, bucketName, resourcePath];
+ if(self.redirectUrlString)
+ {
+ urlString = [self.redirectUrlString stringByAppendingPathComponent:self.resourcePath];
+ }
+ else
+ {
+ //urlString = [NSString stringWithFormat:@"%@://s3.amazonaws.com/%@%@",
+ // scheme, bucketName, resourcePath];
- NSString *urlString = [NSString stringWithFormat:@"%@://%@.s3.amazonaws.com%@",
- scheme, bucketName, resourcePath];
-
- NSString *parameterString = [self stringForParameters];
+ urlString = [NSString stringWithFormat:@"%@://%@.s3.amazonaws.com%@",
+ scheme, self.bucketName, self.resourcePath];
+ }
+
+ NSString *parameterString = [self stringForParameters];
+
+ if (parameterString.length > 0)
+ {
+ urlString = [urlString stringByAppendingFormat:@"?%@", parameterString];
+ }
- if (parameterString.length > 0)
- {
- urlString = [urlString stringByAppendingFormat:@"?%@", parameterString];
- }
-
return urlString;
}
- (NSString *)dateHeader
{
- return [BS3Request dateHeaderForDate:date];
+ return [BS3Request dateHeaderForDate:self.date];
}
- (NSString *)stringToSign
@@ -154,32 +158,32 @@ - (NSString *)stringToSign
- (NSString *)authorizationHeader
{
- NSData *encryptedStringData = [[self stringToSign] encryptWithKey:secretKey];
+ NSData *encryptedStringData = [[self stringToSign] encryptWithKey:self.secretKey];
NSString *authToken = [encryptedStringData base64EncodedString];
- return [NSString stringWithFormat:@"AWS %@:%@", accessKey, authToken];
+ return [NSString stringWithFormat:@"AWS %@:%@", self.accessKey, authToken];
}
- (void)setHTTPBody:(NSData *)body
{
[super setHTTPBody:body];
- [allHTTPHeaderFields setObject:[HTTPBody md5SignatureBase64] forKey:@"Content-Md5"];
+ [self addValue:[self.HTTPBody md5SignatureBase64] forHTTPHeaderField:@"Content-Md5"];
}
- (void)prepareAndSign
{
NSString *urlString = [self composedURLString];
[self setURL:[NSURL URLWithString:urlString]];
- [request setValue:[self authorizationHeader] forHTTPHeaderField:@"Authorization"];
+ [self setValue:[self authorizationHeader] forHTTPHeaderField:@"Authorization"];
}
- (void)show
{
- printf("bucketName = %s\n", [bucketName UTF8String]);
- printf("resourcePath = %s\n", [resourcePath UTF8String]);
+ printf("bucketName = %s\n", [self.bucketName UTF8String]);
+ printf("resourcePath = %s\n", [self.resourcePath UTF8String]);
printf("composedURLString = %s\n", [[self composedURLString] UTF8String]);
printf("HTTPMethod = %s\n", [self.HTTPMethod UTF8String]);
- printf("parameters = %s\n", [[parameters description] UTF8String]);
- printf("allHTTPHeaderFields = %s\n", [[allHTTPHeaderFields description] UTF8String]);
+ printf("parameters = %s\n", [[self.parameters description] UTF8String]);
+ printf("allHTTPHeaderFields = %s\n", [[self.allHTTPHeaderFields description] UTF8String]);
printf("stringToSign = [[%s]]\n", [[self stringToSign] UTF8String]);
}
@@ -1,50 +0,0 @@
-//
-// BS3Request.h
-// BS3Request
-//
-// Created by Brandon Smith on 7/15/12.
-// Copyright (c) 2012 TokenGnome. All rights reserved.
-//
-// @param bucketName The name of the bucket to perform the request on or nil
-// @param resourcePath The path to the resouce targeted by the request.
-// @param params The dictionary of special request parameters to use
-// @see http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html#RESTBucketGET-requests-request-parameters
-// @param accessKey The Amazon AWS access key to be used to sign the request
-// @param secretKey The Amazon AWS secret key to be used to sign the request
-// @return a newly initialized NSURLRequest subclass with a signed URL
-
-
-#import <Foundation/Foundation.h>
-
-@interface BS3Requestor : NSObject
-{
-}
-
-@property (nonatomic, retain) NSDate *date;
-@property (nonatomic, retain) NSString *bucketName;
-@property (nonatomic, retain) NSString *resourcePath;
-@property (nonatomic, retain) NSString *accessKey;
-@property (nonatomic, retain) NSString *secretKey;
-
-@property (nonatomic, retain) NSString *HTTPMethod;
-@property (nonatomic, retain) NSMutableDictionary *allHTTPHeaderFields;
-@property (nonatomic, retain) NSData *HTTPBody;
-
-@property (nonatomic, retain) NSMutableDictionary *parameters;
-@property (assign) Class requestClass;
-@property (assign) BOOL usesSSL;
-
-
-// Computes and sets the authorization header based on the the
-// HTTP method of, MD5 hash of the HTTP body, etc.
-// so don't change those on the returned object
-
-- (NSString *)stringToSign;
-- (NSString *)authorizationHeader;
-
-- (NSMutableURLRequest *)composedRequest;
-- (NSMutableURLRequest *)signedRequest;
-
-- (void)show;
-
-@end
Oops, something went wrong.

0 comments on commit 4bbbbdb

Please sign in to comment.