-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryStringAuthGenerator.cs
More file actions
executable file
·273 lines (234 loc) · 9.9 KB
/
QueryStringAuthGenerator.cs
File metadata and controls
executable file
·273 lines (234 loc) · 9.9 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// This software code is made available "AS IS" without warranties of any
// kind. You may copy, display, modify and redistribute the software
// code either by itself or as incorporated into your code; provided that
// you do not remove any proprietary notices. Your use of this software
// code is at your own risk and you waive any claim against Amazon
// Digital Services, Inc. or its affiliates with respect to your use of
// this software code. (c) 2006 Amazon Digital Services, Inc. or its
// affiliates.
using System;
using System.Collections;
using System.Text;
using System.Web;
namespace com.CastRoller.AmazonS3DA
{
/// This class mimics the behavior of AWSAuthConnection, except instead of actually performing
/// the operation, QueryStringAuthGenerator will return URLs with query string parameters that
/// can be used to do the same thing. These parameters include an expiration date, so that
/// if you hand them off to someone else, they will only work for a limited amount of time.
public class QueryStringAuthGenerator
{
private string awsAccessKeyId;
private string awsSecretAccessKey;
private bool isSecure;
private string server;
private int port;
private long expiresIn = NOT_SET;
private long expires = NOT_SET;
// by default, expire in 1 minute
public static readonly long DEFAULT_EXPIRES_IN = 60 * 1000;
// Sentinel to indicate when a date is not set
private static readonly long NOT_SET = -1;
public QueryStringAuthGenerator( string awsAccessKeyId, string awsSecretAccessKey ) :
this( awsAccessKeyId, awsSecretAccessKey, true )
{
}
public QueryStringAuthGenerator( string awsAccessKeyId, string awsSecretAccessKey,
bool isSecure ) :
this( awsAccessKeyId, awsSecretAccessKey, isSecure, Utils.Host )
{
}
public QueryStringAuthGenerator( string awsAccessKeyId, string awsSecretAccessKey,
bool isSecure, string server ) :
this( awsAccessKeyId, awsSecretAccessKey, isSecure, server,
isSecure ? Utils.SecurePort : Utils.InsecurePort )
{
}
public QueryStringAuthGenerator( string awsAccessKeyId, string awsSecretAccessKey,
bool isSecure, string server, int port )
{
this.awsAccessKeyId = awsAccessKeyId;
this.awsSecretAccessKey = awsSecretAccessKey;
this.isSecure = isSecure;
this.server = server;
this.port = port;
this.expiresIn = DEFAULT_EXPIRES_IN;
this.expires = NOT_SET;
}
/// <summary>
/// Sets/Gets the milliseconds since the Epoch that this
/// expires at
/// </summary>
public long Expires {
get {
return expires;
}
set {
expires = value;
expiresIn = NOT_SET;
}
}
public long ExpiresIn {
get {
return expiresIn;
}
set {
expiresIn = value;
expires = NOT_SET;
}
}
public string createBucket( string bucket, SortedList headers, SortedList metadata )
{
return generateURL( "PUT", bucket, mergeMeta( headers, metadata ) );
}
public string listBucket(string bucket, string prefix, string marker,
int maxKeys, SortedList headers)
{
return listBucket(bucket, prefix, marker, maxKeys, null, headers);
}
public string listBucket(string bucket, string prefix, string marker,
int maxKeys, string delimiter, SortedList headers)
{
StringBuilder path = new StringBuilder(bucket);
path.Append("?");
if (prefix != null) path.Append("prefix=" + HttpUtility.UrlEncode(prefix) + "&");
if (marker != null) path.Append("marker=" + HttpUtility.UrlEncode(marker) + "&");
if (maxKeys != 0) path.Append("max-keys=" + maxKeys + "&");
if (delimiter != null) path.Append("delimiter=" + HttpUtility.UrlEncode(delimiter) + "&");
path.Remove(path.Length - 1, 1);
return generateURL("GET", path.ToString(), headers);
}
public string deleteBucket( string bucket, SortedList headers )
{
return generateURL( "DELETE", bucket, headers );
}
public string put( string bucket, string key, S3Object obj, SortedList headers )
{
SortedList metadata = null;
if ( obj != null )
{
metadata = obj.Metadata;
}
return generateURL("PUT", bucket + "/" + HttpUtility.UrlEncode(key), mergeMeta(headers, metadata));
}
public string get( string bucket, string key, SortedList headers )
{
// return generateURL("GET", bucket + "/" + HttpUtility.UrlEncode(key), headers);
return generateURL("GET", bucket + "/" + key, headers);
}
public string delete( string bucket, string key, SortedList headers )
{
return generateURL("DELETE", bucket + "/" + HttpUtility.UrlEncode(key), headers);
}
public string getBucketLogging(string bucket, SortedList headers)
{
return generateURL("GET", bucket + "?logging", headers);
}
public string putBucketLogging(string bucket, SortedList headers)
{
return generateURL("PUT", bucket + "?logging", headers);
}
public string getBucketACL(string bucket, SortedList headers)
{
return generateURL("GET", bucket + "?acl", headers);
}
public string getACL(string bucket, string key, SortedList headers)
{
return generateURL("GET", bucket + "/" + HttpUtility.UrlEncode(key) + "?acl", headers);
}
public string putBucketACL(string bucket, SortedList headers)
{
return generateURL("PUT", bucket + "?acl", headers);
}
public string putACL(string bucket, string key, SortedList headers)
{
return generateURL("PUT", bucket + "/" + HttpUtility.UrlEncode(key) + "?acl", headers);
}
public string listAllMyBuckets( SortedList headers )
{
return generateURL( "GET", "", headers );
}
public string makeBaseURL( string bucket, string key ) {
StringBuilder buffer = new StringBuilder();
if ( this.isSecure ) {
buffer.Append( "https://" );
} else {
buffer.Append( "http://" );
}
buffer.Append(this.server).Append(":").Append(this.port).Append("/");
if ( bucket != null ) {
buffer.Append( bucket );
if ( key != null ) {
buffer.Append( "/" );
buffer.Append( HttpUtility.UrlEncode(key) );
}
}
return buffer.ToString();
}
private string generateURL( string method, string path, SortedList headers )
{
long expires = 0L;
if ( this.expiresIn != NOT_SET )
{
expires = Utils.currentTimeMillis() + this.expiresIn;
}
else if ( this.expires != NOT_SET )
{
expires = this.expires;
}
else
{
throw new Exception( "Illegal expire state!" );
}
// convert to seconds
expires /= 1000;
string canonicalString = Utils.makeCanonicalString( method, path, headers, "" + expires );
string encodedCanonical = Utils.encode( this.awsSecretAccessKey, canonicalString, true );
StringBuilder builder = new StringBuilder();
if ( this.isSecure ) {
builder.Append( "https://" );
} else {
builder.Append( "http://" );
}
builder.Append( this.server ).Append( ":" ).Append( this.port ).Append( "/" ).Append( path );
if ( path.IndexOf( "?" ) == -1 )
{
builder.Append( "?" );
} else {
builder.Append( "&" );
}
builder.Append( "Signature=" ).Append( encodedCanonical );
builder.Append( "&Expires=" ).Append( expires );
builder.Append( "&AWSAccessKeyId=" ).Append( this.awsAccessKeyId );
return builder.ToString();
}
private SortedList mergeMeta( SortedList headers, SortedList metadata )
{
SortedList merged = new SortedList();
if ( headers != null )
{
foreach ( string key in headers.Keys )
{
merged.Add( key, headers[ key ] );
}
}
if ( metadata != null )
{
foreach ( string key in metadata.Keys )
{
string existing = merged[ key ] as string;
if ( existing != null )
{
existing += "," + metadata[ key ];
}
else
{
existing = metadata[ key ] as string;
}
merged.Add( key, existing );
}
}
return merged;
}
}
}