Skip to content

Commit 891410f

Browse files
committed
add downloadToken
1 parent 6199353 commit 891410f

File tree

5 files changed

+102
-28
lines changed

5 files changed

+102
-28
lines changed

Demo/Demo.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public static void Main()
4545
RSPutFile();
4646
ImageOps();
4747

48+
MakeDownloadToken();
49+
4850
Console.ReadLine();
4951
}
5052

@@ -181,6 +183,15 @@ public static void Drop()
181183
}
182184
}
183185

186+
public static void MakeDownloadToken()
187+
{
188+
Console.WriteLine("\n===> Auth.MakeDownloadToken");
189+
string pattern = "*/*";
190+
var downloadPolicy = new DownloadPolicy(pattern, 3600);
191+
string dnToken = downloadPolicy.MakeAuthTokenString();
192+
Console.WriteLine("dnToken: " + dnToken);
193+
}
194+
184195
public static void ImageOps()
185196
{
186197
Console.WriteLine("\n===> FileOp.ImageInfo");

QBox/Auth/AuthPolicy.cs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,7 @@ public string Marshal()
4747

4848
public byte[] MakeAuthToken()
4949
{
50-
Encoding encoding = Encoding.ASCII;
51-
byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY);
52-
byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY);
53-
byte[] upToken = null;
54-
try
55-
{
56-
byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(Marshal()));
57-
byte[] digestBase64 = null;
58-
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
59-
{
60-
byte[] digest = hmac.ComputeHash(policyBase64);
61-
digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest));
62-
}
63-
using (MemoryStream buffer = new MemoryStream())
64-
{
65-
buffer.Write(accessKey, 0, accessKey.Length);
66-
buffer.WriteByte((byte)':');
67-
buffer.Write(digestBase64, 0, digestBase64.Length);
68-
buffer.WriteByte((byte)':');
69-
buffer.Write(policyBase64, 0, policyBase64.Length);
70-
upToken = buffer.ToArray();
71-
}
72-
}
73-
catch (Exception e)
74-
{
75-
Console.WriteLine(e.ToString());
76-
}
77-
return upToken;
50+
return AuthToken.Make(Marshal());
7851
}
7952

8053
public string MakeAuthTokenString()

QBox/Auth/AuthToken.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Text;
3+
using System.IO;
4+
using System.Security.Cryptography;
5+
using QBox.RS;
6+
using QBox.Util;
7+
8+
namespace QBox.Auth
9+
{
10+
public static class AuthToken
11+
{
12+
public static byte[] Make(string scope)
13+
{
14+
Encoding encoding = Encoding.ASCII;
15+
byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY);
16+
byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY);
17+
byte[] upToken = null;
18+
try
19+
{
20+
byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(scope));
21+
byte[] digestBase64 = null;
22+
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
23+
{
24+
byte[] digest = hmac.ComputeHash(policyBase64);
25+
digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest));
26+
}
27+
using (MemoryStream buffer = new MemoryStream())
28+
{
29+
buffer.Write(accessKey, 0, accessKey.Length);
30+
buffer.WriteByte((byte)':');
31+
buffer.Write(digestBase64, 0, digestBase64.Length);
32+
buffer.WriteByte((byte)':');
33+
buffer.Write(policyBase64, 0, policyBase64.Length);
34+
upToken = buffer.ToArray();
35+
}
36+
}
37+
catch (Exception e)
38+
{
39+
Console.WriteLine(e.ToString());
40+
}
41+
return upToken;
42+
}
43+
44+
}
45+
}

QBox/Auth/DownloadPolicy.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Text;
3+
using System.IO;
4+
using System.Security.Cryptography;
5+
using LitJson;
6+
using QBox.RS;
7+
using QBox.Util;
8+
9+
namespace QBox.Auth
10+
{
11+
public class DownloadPolicy
12+
{
13+
public string Pattern { get; set; }
14+
public long Deadline { get; set; }
15+
16+
public DownloadPolicy(string pattern, long expires)
17+
{
18+
Pattern = pattern;
19+
DateTime begin = new DateTime(1970, 1, 1);
20+
DateTime now = DateTime.Now;
21+
TimeSpan interval = new TimeSpan(now.Ticks - begin.Ticks);
22+
Deadline = (long)interval.TotalSeconds + expires;
23+
}
24+
25+
public string Marshal()
26+
{
27+
JsonData data = new JsonData();
28+
data["S"] = Pattern;
29+
data["E"] = Deadline;
30+
return data.ToJson();
31+
}
32+
33+
public byte[] MakeAuthToken()
34+
{
35+
return AuthToken.Make(Marshal());
36+
}
37+
38+
public string MakeAuthTokenString()
39+
{
40+
return Encoding.ASCII.GetString(MakeAuthToken());
41+
}
42+
}
43+
}

QBox/QBox.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
<StartupObject />
3939
</PropertyGroup>
4040
<ItemGroup>
41+
<Compile Include="Auth\AuthToken.cs" />
42+
<Compile Include="Auth\DownloadPolicy.cs" />
4143
<Compile Include="Auth\PutAuthClient.cs" />
4244
<Compile Include="FileOp\ImageMogrifySpec.cs" />
4345
<Compile Include="FileOp\ImageViewSpec.cs" />

0 commit comments

Comments
 (0)