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

signV4 #370

Open
wants to merge 2 commits into
base: dev-2312
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
184 changes: 184 additions & 0 deletions AliyunOSSSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions AliyunOSSSDK/NSData+OSS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// NSData+OSS.h
// AliyunOSSSDK
//
// Created by ws on 2023/12/28.
// Copyright © 2023 aliyun. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSData (OSS)

- (NSString *)hexString;
- (NSData *)calculateSha256;

@end

NS_ASSUME_NONNULL_END
40 changes: 40 additions & 0 deletions AliyunOSSSDK/NSData+OSS.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// NSData+OSS.m
// AliyunOSSSDK
//
// Created by ws on 2023/12/28.
// Copyright © 2023 aliyun. All rights reserved.
//

#import "NSData+OSS.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSData (OSS)

- (NSString *)hexString {
NSMutableString *HexString = [NSMutableString string];
Byte *byte = (Byte *)[self bytes];
for (int i = 0; i<[self length]; i++) {
[HexString appendFormat:@"%x", (*(byte + i) >> 4) & 0xf];
[HexString appendFormat:@"%x", *(byte + i) & 0xf];
}
return HexString;
}

- (NSData *)calculateSha256 {
unsigned char *digest = NULL;

digest = malloc(CC_SHA256_DIGEST_LENGTH * sizeof(unsigned char));
memset(digest, 0x0, CC_SHA256_DIGEST_LENGTH);
CC_SHA256(self.bytes, (CC_LONG)self.length, digest);

if (digest) {
NSData *data = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
free(digest);
return data;
}

return nil;
}

@end
3 changes: 3 additions & 0 deletions AliyunOSSSDK/NSDate+OSS.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
+ (NSDate *)oss_dateFromString:(NSString *)string;
+ (NSDate *)oss_clockSkewFixedDate;
- (NSString *)oss_asStringValue;
+ (NSDate *)oss_dateFromString:(NSString *)string
dateFormat:(NSString *)dateFormat;
- (NSString *)oss_asStringValueWithDateFormat:(NSString *)dateFormat;
@end
19 changes: 19 additions & 0 deletions AliyunOSSSDK/NSDate+OSS.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,23 @@ - (NSString *)oss_asStringValue {
return [dateFormatter stringFromDate:self];
}

+ (NSDate *)oss_dateFromString:(NSString *)string
dateFormat:(NSString *)dateFormat {
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
dateFormatter.dateFormat = dateFormat;

return [dateFormatter dateFromString:string];
}

- (NSString *)oss_asStringValueWithDateFormat:(NSString *)dateFormat {
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
dateFormatter.dateFormat = dateFormat;

return [dateFormatter stringFromDate:self];
}

@end
19 changes: 19 additions & 0 deletions AliyunOSSSDK/NSSet+OSS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NSSet+OSS.h
// AliyunOSSSDK
//
// Created by ws on 2023/12/28.
// Copyright © 2023 aliyun. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSSet (OSS)

- (NSString *)componentsJoinedByString:(NSString *)separator;

@end

NS_ASSUME_NONNULL_END
30 changes: 30 additions & 0 deletions AliyunOSSSDK/NSSet+OSS.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// NSSet+OSS.m
// AliyunOSSSDK
//
// Created by ws on 2023/12/28.
// Copyright © 2023 aliyun. All rights reserved.
//

#import "NSSet+OSS.h"

@implementation NSSet (OSS)

- (NSString *)componentsJoinedByString:(NSString *)separator {
NSMutableString *builder = [NSMutableString new];
int i = 0;

for (NSObject *part in self) {
if ([part isKindOfClass:[NSString class]]) {
[builder appendString:(NSString *)part];
if (i < [self count] - 1) {
[builder appendString:separator];
}
}
i++;
}

return builder;
}

@end
4 changes: 3 additions & 1 deletion AliyunOSSSDK/OSSAllRequestNeededMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
@property (nonatomic, copy) NSDictionary *params;
@property (nonatomic, copy) NSString *contentSHA1;
@property (nonatomic, assign) BOOL isHostInCnameExcludeList;

@property (nonatomic, assign) BOOL isUseUrlSignature;
@property (nonatomic, assign) BOOL isAuthenticationRequired;
@property (nonatomic, copy) NSSet<NSString *> *additionalHeaderNames;

- (OSSTask *)validateRequestParamsInOperationType:(OSSOperationType)operType;

Expand Down
6 changes: 6 additions & 0 deletions AliyunOSSSDK/OSSClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, strong) OSSClientConfiguration * clientConfiguration;

/// OSS services Region
@property (nonatomic, copy) NSString *region;

/// cloudBoxId OSS cloud box id
@property (nonatomic, copy) NSString *cloudBoxId;

/**
oss operation task queue
*/
Expand Down
17 changes: 12 additions & 5 deletions AliyunOSSSDK/OSSClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ - (instancetype)initWithEndpoint:(NSString *)endpoint
}

- (OSSTask *)invokeRequest:(OSSNetworkingRequestDelegate *)request requireAuthentication:(BOOL)requireAuthentication {
request.allNeededMessage.isAuthenticationRequired = requireAuthentication;
/* if content-type haven't been set, we set one */
if (!request.allNeededMessage.contentType.oss_isNotEmpty
&& ([request.allNeededMessage.httpMethod isEqualToString:@"POST"] || [request.allNeededMessage.httpMethod isEqualToString:@"PUT"])) {
Expand All @@ -142,13 +143,19 @@ - (OSSTask *)invokeRequest:(OSSNetworkingRequestDelegate *)request requireAuthen

id<OSSRequestInterceptor> uaSetting = [[OSSUASettingInterceptor alloc] initWithClientConfiguration:self.clientConfiguration];
[request.interceptors addObject:uaSetting];

/* check if the authentication is required */
if (requireAuthentication) {
id<OSSRequestInterceptor> signer = [[OSSSignerInterceptor alloc] initWithCredentialProvider:self.credentialProvider];
[request.interceptors addObject:signer];
if (self.clientConfiguration.signVersion == OSSSignVersionV4 && self.region == nil) {
return [OSSTask taskWithError:[NSError errorWithDomain:OSSClientErrorDomain
code:OSSClientErrorCodeInvalidArgument
userInfo:@{OSSErrorMessageTOKEN: @"Region haven't been set!"}]];
}

OSSSignerInterceptor *signer = [[OSSSignerInterceptor alloc] initWithCredentialProvider:self.credentialProvider];
signer.version = self.clientConfiguration.signVersion;
signer.region = self.region;
signer.cloudBoxId = self.cloudBoxId;
[request.interceptors addObject:signer];

request.isHttpdnsEnable = self.clientConfiguration.isHttpdnsEnable;
request.isPathStyleAccessEnable = self.clientConfiguration.isPathStyleAccessEnable;
request.isCustomPathPrefixEnable = self.clientConfiguration.isCustomPathPrefixEnable;
Expand Down
6 changes: 6 additions & 0 deletions AliyunOSSSDK/OSSConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,10 @@ OBJC_EXTERN NSString * const OSSHTTPMethodPOST;
OBJC_EXTERN NSString * const OSSHTTPMethodDELETE;


typedef NS_ENUM(NSInteger, OSSSignVersion)
{
OSSSignVersionV1,
OSSSignVersionV4
};

NS_ASSUME_NONNULL_END
23 changes: 22 additions & 1 deletion AliyunOSSSDK/OSSDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#elif TARGET_OS_OSX
#define OSSUAPrefix @"aliyun-sdk-mac"
#endif
#define OSSSDKVersion @"2.10.18"
#define OSSSDKVersion @"2.10.19"

#define OSSListBucketResultXMLTOKEN @"ListBucketResult"
#define OSSNameXMLTOKEN @"Name"
Expand Down Expand Up @@ -73,12 +73,33 @@
#define OSSHttpHeaderObjectACL @"x-oss-object-acl"
#define OSSHttpHeaderCopySource @"x-oss-copy-source"
#define OSSHttpHeaderSymlinkTarget @"x-oss-symlink-target"
#define OSSHttpHeaderDate @"Date"
#define OSSHttpHeaderSecurityToken @"x-oss-security-token"
#define OSSHttpHeaderAuthorization @"Authorization"
#define OSSHttpHeaderHost @"Host"
#define OSSHttpHeaderContentSha256 @"x-oss-content-sha256"

#define OSSRequestParameterExpires @"Expires"
#define OSSRequestParameterAccessKeyId @"OSSAccessKeyId"
#define OSSRequestParameterSignature @"Signature"

#define OSSHttpQueryProcess @"x-oss-process"
#define OSSPrefix @"x-oss-"

#define OSSDefaultRetryCount 3
#define OSSDefaultMaxConcurrentNum 5
#define OSSDefaultTimeoutForRequestInSecond 15
#define OSSDefaultTimeoutForResourceInSecond 7 * 24 * 60 * 60

#define OSSAuthorizationPrefix @"OSS "

#define OSSProductDefault @"oss"
#define OSSProductCloudBox @"oss-cloudbox"

#define OSSContentStringToSign @"stringToSign"
#define OSSContentDate @"date"
#define OSSContentAlgorithm @"algorithm"
#define OSSContentRegion @"region"
#define OSSContentProduct @"product"

#endif /* OSSDefine_h */
9 changes: 9 additions & 0 deletions AliyunOSSSDK/OSSModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@class OSSFederationToken;
@class OSSTask;
@class OSSClientConfiguration;
@class OSSSignerParams;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -56,6 +57,9 @@ typedef OSSFederationToken * _Nullable (^OSSGetFederationTokenBlock) (void);
Token's expiration time in GMT format string.
*/
@property (atomic, strong, nullable) NSString *expirationTimeInGMTFormat;

- (BOOL)useSecurityToken;

@end

/**
Expand Down Expand Up @@ -228,6 +232,8 @@ Sets the session Id for background file transmission
/// Set whether to allow the redirection with a modified request
@property (nonatomic, assign) BOOL isFollowRedirectsEnable;

@property (nonatomic, assign) OSSSignVersion signVersion;

@end

@protocol OSSRequestInterceptor <NSObject>
Expand All @@ -239,6 +245,9 @@ Sets the session Id for background file transmission
*/
@interface OSSSignerInterceptor : NSObject <OSSRequestInterceptor>
@property (nonatomic, strong) id<OSSCredentialProvider> credentialProvider;
@property (nonatomic, assign) OSSSignVersion version;
@property (nonatomic, copy) NSString *region;
@property (nonatomic, copy) NSString *cloudBoxId;

- (instancetype)initWithCredentialProvider:(id<OSSCredentialProvider>)credentialProvider;
@end
Expand Down
Loading