Skip to content

Commit d55cd9f

Browse files
committed
add UpToken
1 parent 7d2507c commit d55cd9f

19 files changed

+165
-161
lines changed

Example/RSDemo.cs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ public class RSDemo
1515
public static Client conn;
1616
public static RSService rs;
1717

18+
public static void Main()
19+
{
20+
Config.ACCESS_KEY = "<Please apply your access key>";
21+
Config.SECRET_KEY = "<Dont send your secret key to anyone>";
22+
23+
conn = new DigestAuthClient();
24+
rs = new RSService(conn, tableName);
25+
localFile = Process.GetCurrentProcess().MainModule.FileName;
26+
key = System.IO.Path.GetFileName(localFile);
27+
28+
try
29+
{
30+
PutFile();
31+
Get();
32+
Stat();
33+
Delete();
34+
CliPutFile();
35+
Get();
36+
Stat();
37+
Publish();
38+
UnPublish();
39+
Drop();
40+
41+
UpToken();
42+
}
43+
catch (Exception e)
44+
{
45+
Console.WriteLine("RS Exception");
46+
Console.WriteLine(e.ToString());
47+
}
48+
49+
Console.ReadLine();
50+
}
51+
1852
public static void PutFile()
1953
{
2054
Console.WriteLine("\n--- PutFile ---");
@@ -125,7 +159,7 @@ public static void UnPublish()
125159
{
126160
Console.WriteLine("\n--- UnPublish ---");
127161
PrintInput(tableName, null, null, DEMO_DOMAIN);
128-
PublishRet publishRet = rs.UnPublish(DEMO_DOMAIN);
162+
PublishRet publishRet = rs.Unpublish(DEMO_DOMAIN);
129163
PrintRet(publishRet);
130164
if (!publishRet.OK)
131165
{
@@ -170,6 +204,14 @@ public static void CliPutFile()
170204

171205
}
172206

207+
public static void UpToken()
208+
{
209+
Console.WriteLine("\n--- Gen UpToken ---");
210+
var authPolicy = new AuthPolicy(tableName, 3600);
211+
Console.WriteLine("Json: " + authPolicy.Marshal());
212+
Console.WriteLine("Token: " + authPolicy.MakeAuthTokenString());
213+
}
214+
173215
public static void PrintInput(
174216
string tblName, string key, string localFile, string domain)
175217
{
@@ -196,29 +238,5 @@ public static void PrintRet(CallRet callRet)
196238
Console.WriteLine("Response:\n" + callRet.Response);
197239
Console.WriteLine();
198240
}
199-
200-
public static void Main()
201-
{
202-
Config.ACCESS_KEY = "RLT1NBD08g3kih5-0v8Yi6nX6cBhesa2Dju4P7mT";
203-
Config.SECRET_KEY = "k6uZoSDAdKBXQcNYG3UOm4bP3spDVkTg-9hWHIKm";
204-
205-
conn = new DigestAuthClient();
206-
rs = new RSService(conn, tableName);
207-
localFile = Process.GetCurrentProcess().MainModule.FileName;
208-
key = System.IO.Path.GetFileName(localFile);
209-
210-
PutFile();
211-
Get();
212-
Stat();
213-
Delete();
214-
CliPutFile();
215-
Get();
216-
Stat();
217-
Publish();
218-
UnPublish();
219-
Drop();
220-
221-
Console.ReadLine();
222-
}
223241
}
224242
}

Json/JsonWriter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ public void Write (string str)
365365
context.ExpectingValue = false;
366366
}
367367

368-
[CLSCompliant(false)]
369368
public void Write (ulong number)
370369
{
371370
DoValidation (Condition.Value);

QBox/AuthPolicy.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Text;
3+
using System.IO;
4+
using LitJson;
5+
using System.Security.Cryptography;
6+
7+
namespace QBox
8+
{
9+
public class AuthPolicy
10+
{
11+
public string Scope { get; set; }
12+
public long Deadline { get; set; }
13+
public string CallbackUrl { get; set; }
14+
public string ReturnUrl { get; set; }
15+
16+
public AuthPolicy(string scope, long expires)
17+
{
18+
Scope = scope;
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["scope"] = Scope;
29+
data["deadline"] = Deadline;
30+
if (CallbackUrl != null)
31+
data["callbackUrl"] = CallbackUrl;
32+
if (ReturnUrl != null)
33+
data["returnUrl"] = ReturnUrl;
34+
return data.ToJson();
35+
}
36+
37+
public byte[] MakeAuthToken()
38+
{
39+
Encoding encoding = Encoding.ASCII;
40+
byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY);
41+
byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY);
42+
byte[] upToken = null;
43+
try
44+
{
45+
byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(Marshal()));
46+
byte[] digestBase64 = null;
47+
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
48+
{
49+
byte[] digest = hmac.ComputeHash(policyBase64);
50+
digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest));
51+
}
52+
using (MemoryStream buffer = new MemoryStream())
53+
{
54+
buffer.Write(accessKey, 0, accessKey.Length);
55+
buffer.WriteByte((byte)':');
56+
buffer.Write(digestBase64, 0, digestBase64.Length);
57+
buffer.WriteByte((byte)':');
58+
buffer.Write(policyBase64, 0, policyBase64.Length);
59+
upToken = buffer.ToArray();
60+
}
61+
}
62+
catch (Exception e)
63+
{
64+
Console.WriteLine(e.ToString());
65+
}
66+
return upToken;
67+
}
68+
69+
public string MakeAuthTokenString()
70+
{
71+
return Encoding.ASCII.GetString(MakeAuthToken());
72+
}
73+
}
74+
}

QBox/Base64UrlSafe.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Text;
53

64
namespace QBox

QBox/CallRet.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using System.Net;
63

74
namespace QBox

QBox/Client.cs

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using System.Net;
63
using System.IO;
74

@@ -14,18 +11,15 @@ public abstract class Client
1411
public CallRet Call(string url)
1512
{
1613
Console.WriteLine("URL: " + url);
17-
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
18-
if (request == null)
19-
throw new NullReferenceException("request is not a http request");
20-
2114
try
2215
{
16+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
2317
request.Method = "POST";
2418
SetAuth(request);
25-
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
26-
CallRet callRet = HandleResult(response);
27-
response.Close();
28-
return callRet;
19+
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
20+
{
21+
return HandleResult(response);
22+
}
2923
}
3024
catch (Exception e)
3125
{
@@ -37,12 +31,9 @@ public CallRet Call(string url)
3731
public CallRet CallWithBinary(string url, string contentType, byte[] body)
3832
{
3933
Console.WriteLine("URL: " + url);
40-
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
41-
if (request == null)
42-
throw new NullReferenceException("request is not a http request");
43-
4434
try
4535
{
36+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
4637
request.Method = "POST";
4738
request.ContentType = contentType;
4839
request.ContentLength = body.Length;
@@ -51,19 +42,10 @@ public CallRet CallWithBinary(string url, string contentType, byte[] body)
5142
{
5243
requestStream.Write(body, 0, body.Length);
5344
}
54-
}
55-
catch (Exception e)
56-
{
57-
Console.WriteLine(e.ToString());
58-
return new CallRet(HttpStatusCode.BadRequest, e);
59-
}
60-
61-
try
62-
{
63-
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
64-
CallRet callRet = HandleResult(response);
65-
response.Close();
66-
return callRet;
45+
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
46+
{
47+
return HandleResult(response);
48+
}
6749
}
6850
catch (Exception e)
6951
{
@@ -74,23 +56,12 @@ public CallRet CallWithBinary(string url, string contentType, byte[] body)
7456

7557
public static CallRet HandleResult(HttpWebResponse response)
7658
{
77-
if (response == null)
78-
return new CallRet(HttpStatusCode.BadRequest, "No response");
79-
8059
HttpStatusCode statusCode = response.StatusCode;
81-
string responseStr;
82-
try
60+
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
8361
{
84-
StreamReader reader = new StreamReader(response.GetResponseStream());
85-
responseStr = reader.ReadToEnd();
62+
string responseStr = reader.ReadToEnd();
63+
return new CallRet(statusCode, responseStr);
8664
}
87-
catch (Exception e)
88-
{
89-
Console.WriteLine(e.ToString());
90-
return new CallRet(HttpStatusCode.BadRequest, e);
91-
}
92-
93-
return new CallRet(statusCode, responseStr);
9465
}
9566
}
9667
}

QBox/Config.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52

63
namespace QBox
74
{
85
public class Config
96
{
107
public static string ACCESS_KEY = "<Please apply your access key>";
11-
public static string SECRET_KEY = "<Dont change here>";
8+
public static string SECRET_KEY = "<Dont send your secret key to anyone>";
129

1310
public static string REDIRECT_URI = "<RedirectURL>";
1411
public static string AUTHORIZATION_ENDPOINT = "<AuthURL>";

QBox/DeleteRet.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52

63
namespace QBox
74
{

QBox/DigestAuthClient.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Text;
53
using System.Net;
64
using System.IO;

QBox/DropRet.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52

63
namespace QBox
74
{

0 commit comments

Comments
 (0)