Skip to content
Merged
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
68 changes: 43 additions & 25 deletions Example/RSDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ public class RSDemo
public static Client conn;
public static RSService rs;

public static void Main()
{
Config.ACCESS_KEY = "<Please apply your access key>";
Config.SECRET_KEY = "<Dont send your secret key to anyone>";

conn = new DigestAuthClient();
rs = new RSService(conn, tableName);
localFile = Process.GetCurrentProcess().MainModule.FileName;
key = System.IO.Path.GetFileName(localFile);

try
{
PutFile();
Get();
Stat();
Delete();
CliPutFile();
Get();
Stat();
Publish();
UnPublish();
Drop();

UpToken();
}
catch (Exception e)
{
Console.WriteLine("RS Exception");
Console.WriteLine(e.ToString());
}

Console.ReadLine();
}

public static void PutFile()
{
Console.WriteLine("\n--- PutFile ---");
Expand Down Expand Up @@ -125,7 +159,7 @@ public static void UnPublish()
{
Console.WriteLine("\n--- UnPublish ---");
PrintInput(tableName, null, null, DEMO_DOMAIN);
PublishRet publishRet = rs.UnPublish(DEMO_DOMAIN);
PublishRet publishRet = rs.Unpublish(DEMO_DOMAIN);
PrintRet(publishRet);
if (!publishRet.OK)
{
Expand Down Expand Up @@ -170,6 +204,14 @@ public static void CliPutFile()

}

public static void UpToken()
{
Console.WriteLine("\n--- Gen UpToken ---");
var authPolicy = new AuthPolicy(tableName, 3600);
Console.WriteLine("Json: " + authPolicy.Marshal());
Console.WriteLine("Token: " + authPolicy.MakeAuthTokenString());
}

public static void PrintInput(
string tblName, string key, string localFile, string domain)
{
Expand All @@ -196,29 +238,5 @@ public static void PrintRet(CallRet callRet)
Console.WriteLine("Response:\n" + callRet.Response);
Console.WriteLine();
}

public static void Main()
{
Config.ACCESS_KEY = "RLT1NBD08g3kih5-0v8Yi6nX6cBhesa2Dju4P7mT";
Config.SECRET_KEY = "k6uZoSDAdKBXQcNYG3UOm4bP3spDVkTg-9hWHIKm";

conn = new DigestAuthClient();
rs = new RSService(conn, tableName);
localFile = Process.GetCurrentProcess().MainModule.FileName;
key = System.IO.Path.GetFileName(localFile);

PutFile();
Get();
Stat();
Delete();
CliPutFile();
Get();
Stat();
Publish();
UnPublish();
Drop();

Console.ReadLine();
}
}
}
1 change: 0 additions & 1 deletion Json/JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ public void Write (string str)
context.ExpectingValue = false;
}

[CLSCompliant(false)]
public void Write (ulong number)
{
DoValidation (Condition.Value);
Expand Down
74 changes: 74 additions & 0 deletions QBox/AuthPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Text;
using System.IO;
using LitJson;
using System.Security.Cryptography;

namespace QBox
{
public class AuthPolicy
{
public string Scope { get; set; }
public long Deadline { get; set; }
public string CallbackUrl { get; set; }
public string ReturnUrl { get; set; }

public AuthPolicy(string scope, long expires)
{
Scope = scope;
DateTime begin = new DateTime(1970, 1, 1);
DateTime now = DateTime.Now;
TimeSpan interval = new TimeSpan(now.Ticks - begin.Ticks);
Deadline = (long)interval.TotalSeconds + expires;
}

public string Marshal()
{
JsonData data = new JsonData();
data["scope"] = Scope;
data["deadline"] = Deadline;
if (CallbackUrl != null)
data["callbackUrl"] = CallbackUrl;
if (ReturnUrl != null)
data["returnUrl"] = ReturnUrl;
return data.ToJson();
}

public byte[] MakeAuthToken()
{
Encoding encoding = Encoding.ASCII;
byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY);
byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY);
byte[] upToken = null;
try
{
byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(Marshal()));
byte[] digestBase64 = null;
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
{
byte[] digest = hmac.ComputeHash(policyBase64);
digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest));
}
using (MemoryStream buffer = new MemoryStream())
{
buffer.Write(accessKey, 0, accessKey.Length);
buffer.WriteByte((byte)':');
buffer.Write(digestBase64, 0, digestBase64.Length);
buffer.WriteByte((byte)':');
buffer.Write(policyBase64, 0, policyBase64.Length);
upToken = buffer.ToArray();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return upToken;
}

public string MakeAuthTokenString()
{
return Encoding.ASCII.GetString(MakeAuthToken());
}
}
}
2 changes: 0 additions & 2 deletions QBox/Base64UrlSafe.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QBox
Expand Down
3 changes: 0 additions & 3 deletions QBox/CallRet.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace QBox
Expand Down
55 changes: 13 additions & 42 deletions QBox/Client.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

Expand All @@ -14,18 +11,15 @@ public abstract class Client
public CallRet Call(string url)
{
Console.WriteLine("URL: " + url);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
if (request == null)
throw new NullReferenceException("request is not a http request");

try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
SetAuth(request);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
CallRet callRet = HandleResult(response);
response.Close();
return callRet;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
return HandleResult(response);
}
}
catch (Exception e)
{
Expand All @@ -37,12 +31,9 @@ public CallRet Call(string url)
public CallRet CallWithBinary(string url, string contentType, byte[] body)
{
Console.WriteLine("URL: " + url);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
if (request == null)
throw new NullReferenceException("request is not a http request");

try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = contentType;
request.ContentLength = body.Length;
Expand All @@ -51,19 +42,10 @@ public CallRet CallWithBinary(string url, string contentType, byte[] body)
{
requestStream.Write(body, 0, body.Length);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return new CallRet(HttpStatusCode.BadRequest, e);
}

try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
CallRet callRet = HandleResult(response);
response.Close();
return callRet;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
return HandleResult(response);
}
}
catch (Exception e)
{
Expand All @@ -74,23 +56,12 @@ public CallRet CallWithBinary(string url, string contentType, byte[] body)

public static CallRet HandleResult(HttpWebResponse response)
{
if (response == null)
return new CallRet(HttpStatusCode.BadRequest, "No response");

HttpStatusCode statusCode = response.StatusCode;
string responseStr;
try
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
StreamReader reader = new StreamReader(response.GetResponseStream());
responseStr = reader.ReadToEnd();
string responseStr = reader.ReadToEnd();
return new CallRet(statusCode, responseStr);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return new CallRet(HttpStatusCode.BadRequest, e);
}

return new CallRet(statusCode, responseStr);
}
}
}
5 changes: 1 addition & 4 deletions QBox/Config.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QBox
{
public class Config
{
public static string ACCESS_KEY = "<Please apply your access key>";
public static string SECRET_KEY = "<Dont change here>";
public static string SECRET_KEY = "<Dont send your secret key to anyone>";

public static string REDIRECT_URI = "<RedirectURL>";
public static string AUTHORIZATION_ENDPOINT = "<AuthURL>";
Expand Down
3 changes: 0 additions & 3 deletions QBox/DeleteRet.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QBox
{
Expand Down
2 changes: 0 additions & 2 deletions QBox/DigestAuthClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
Expand Down
3 changes: 0 additions & 3 deletions QBox/DropRet.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QBox
{
Expand Down
4 changes: 0 additions & 4 deletions QBox/FileParameter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace QBox
{
Expand Down
3 changes: 0 additions & 3 deletions QBox/GetRet.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using LitJson;

Expand Down
Loading