-
Notifications
You must be signed in to change notification settings - Fork 5
/
QiniuPutPolicy.m
executable file
·86 lines (65 loc) · 2.31 KB
/
QiniuPutPolicy.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// QiniuAuthPolicy.m
// QiniuSDK
//
// Created by Qiniu Developers 2013
//
#import "QiniuPutPolicy.h"
#import <CommonCrypto/CommonHMAC.h>
#import "GTMBase64.h"
#import "JSONKit.h"
@implementation QiniuPutPolicy
@synthesize scope;
@synthesize callbackUrl;
@synthesize callbackBody;
@synthesize returnUrl;
@synthesize returnBody;
@synthesize asyncOps;
@synthesize endUser;
@synthesize expires;
// Make a token string conform to the UpToken spec.
- (NSString *)makeToken:(NSString *)accessKey secretKey:(NSString *)secretKey
{
const char *secretKeyStr = [secretKey UTF8String];
NSString *policy = [self marshal];
NSData *policyData = [policy dataUsingEncoding:NSUTF8StringEncoding];
NSString *encodedPolicy = [GTMBase64 stringByWebSafeEncodingData:policyData padded:TRUE];
const char *encodedPolicyStr = [encodedPolicy cStringUsingEncoding:NSUTF8StringEncoding];
char digestStr[CC_SHA1_DIGEST_LENGTH];
bzero(digestStr, 0);
CCHmac(kCCHmacAlgSHA1, secretKeyStr, strlen(secretKeyStr), encodedPolicyStr, strlen(encodedPolicyStr), digestStr);
NSString *encodedDigest = [GTMBase64 stringByWebSafeEncodingBytes:digestStr length:CC_SHA1_DIGEST_LENGTH padded:TRUE];
NSString *token = [NSString stringWithFormat:@"%@:%@:%@", accessKey, encodedDigest, encodedPolicy];
return token;
}
// Marshal as JSON format string.
- (NSString *)marshal
{
time_t deadline;
time(&deadline);
deadline += (self.expires > 0) ? self.expires : 3600; // 1 hour by default.
NSNumber *deadlineNumber = [NSNumber numberWithLongLong:deadline];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if (self.scope) {
[dic setObject:self.scope forKey:@"scope"];
}
if (self.callbackUrl) {
[dic setObject:self.callbackUrl forKey:@"callbackUrl"];
}
if (self.callbackBody) {
[dic setObject:self.callbackBody forKey:@"callbackBody"];
}
if (self.returnUrl) {
[dic setObject:self.returnUrl forKey:@"returnUrl"];
}
if (self.returnBody) {
[dic setObject:self.returnBody forKey:@"returnBody"];
}
if (self.endUser) {
[dic setObject:self.endUser forKey:@"endUser"];
}
[dic setObject:deadlineNumber forKey:@"deadline"];
NSString *json = [dic JSONString];
return json;
}
@end