Skip to content

B sjy v5 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2013
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
298 changes: 112 additions & 186 deletions Demo/Demo.cs

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<RootNamespace>Demo</RootNamespace>
<AssemblyName>Demo</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
Expand Down Expand Up @@ -41,14 +42,17 @@
<ItemGroup>
<Compile Include="Demo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\QBox\QBox.csproj">
<Project>{1C8C9909-57ED-44E4-81A5-0904E96FA00E}</Project>
<Name>QBox</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
3 changes: 3 additions & 0 deletions Demo/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
26 changes: 12 additions & 14 deletions QBox/Auth/AuthToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text;
using System.IO;
using System.Security.Cryptography;
using QBox.RS;
using QBox.Conf;
using QBox.Util;

namespace QBox.Auth
Expand All @@ -11,35 +11,33 @@ public static class AuthToken
{
public static byte[] Make(string scope)
{
Encoding encoding = Encoding.ASCII;
byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY);
byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY);
byte[] upToken = null;
byte[] accessKey = Config.Encoding.GetBytes(Config.ACCESS_KEY);
byte[] secretKey = Config.Encoding.GetBytes(Config.SECRET_KEY);
byte[] token = null;
try
{
byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(scope));
byte[] digestBase64 = null;
byte[] encodedScope = Config.Encoding.GetBytes(Base64URLSafe.Encode(scope));
byte[] digestScope = null;
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
{
byte[] digest = hmac.ComputeHash(policyBase64);
digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest));
byte[] digest = hmac.ComputeHash(encodedScope);
digestScope = Config.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.Write(digestScope, 0, digestScope.Length);
buffer.WriteByte((byte)':');
buffer.Write(policyBase64, 0, policyBase64.Length);
upToken = buffer.ToArray();
buffer.Write(encodedScope, 0, encodedScope.Length);
token = buffer.ToArray();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return upToken;
return token;
}

}
}
83 changes: 40 additions & 43 deletions QBox/Auth/DownloadPolicy.cs → QBox/Auth/GetPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using LitJson;
using QBox.RS;
using QBox.Util;

namespace QBox.Auth
{
public class DownloadPolicy
{
public string Pattern { get; set; }
public long Deadline { get; set; }

public DownloadPolicy(string pattern, long expires)
{
Pattern = pattern;
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["S"] = Pattern;
data["E"] = Deadline;
return data.ToJson();
}

public byte[] MakeAuthToken()
{
return AuthToken.Make(Marshal());
}

public string MakeAuthTokenString()
{
return Encoding.ASCII.GetString(MakeAuthToken());
}
}
}
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Security.Cryptography;
using QBox.Util;
using QBox.Conf;

namespace QBox.Auth
{
public class GetPolicy
{
public string Pattern { get; set; }
public long Deadline { get; set; }

public GetPolicy(string pattern, long expires)
{
Pattern = pattern;
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()
{
var data = new Dictionary<string, dynamic>();
data["S"] = Pattern;
data["E"] = Deadline;
var jss = new JavaScriptSerializer();
return jss.Serialize(data);
}

public string Token()
{
return Config.Encoding.GetString(AuthToken.Make(Marshal()));
}
}
}
119 changes: 61 additions & 58 deletions QBox/Auth/AuthPolicy.cs → QBox/Auth/PutPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
using System;
using System.Text;
using System.IO;
using LitJson;
using System.Security.Cryptography;
using QBox.RS;
using QBox.Util;

namespace QBox.Auth
{
public class AuthPolicy
{
public string Scope { get; set; }
public long Deadline { get; set; }
public string CallbackUrl { get; set; }
public string CallbackBodyType { get; set; }
public bool Escape { get; set; }
public string AsyncOps { get; set; }
public string ReturnBody { 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 (!String.IsNullOrEmpty(CallbackUrl))
data["callbackUrl"] = CallbackUrl;
if (!String.IsNullOrEmpty(CallbackBodyType))
data["callbackBodyType"] = CallbackBodyType;
if (Escape)
data["escape"] = 1;
if (!String.IsNullOrEmpty(AsyncOps))
data["asyncOps"] = AsyncOps;
if (!String.IsNullOrEmpty(ReturnBody))
data["returnBody"] = ReturnBody;
return data.ToJson();
}

public byte[] MakeAuthToken()
{
return AuthToken.Make(Marshal());
}

public string MakeAuthTokenString()
{
return Encoding.ASCII.GetString(MakeAuthToken());
}
}
}
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Security.Cryptography;
using QBox.Util;
using QBox.Conf;

namespace QBox.Auth
{
public class PutPolicy
{
public string Scope { get; set; }
public string CallbackUrl { get; set; }
public string CallbackBodyType { get; set; }
public string Customer { get; set; }
public string AsyncOps { get; set; }
public string ReturnBody { get; set; }
public UInt32 Expires { get; set; }
public UInt16 Escape { get; set; }
public UInt16 DetectMime { get; set; }

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

public string Marshal()
{
var data = new Dictionary<string,dynamic>();
data["scope"] = Scope;
data["deadline"] = Expires;
if (!String.IsNullOrEmpty(CallbackUrl))
data["callbackUrl"] = CallbackUrl;
if (!String.IsNullOrEmpty(CallbackBodyType))
data["callbackBodyType"] = CallbackBodyType;
if (!String.IsNullOrEmpty(AsyncOps))
data["asyncOps"] = AsyncOps;
if (!String.IsNullOrEmpty(ReturnBody))
data["returnBody"] = ReturnBody;
if (!String.IsNullOrEmpty(Customer))
data["customer"] = Customer;
if (Escape != 0)
data["escape"] = Escape;
if (DetectMime != 0)
data["detectMime"] = DetectMime;
var jss = new JavaScriptSerializer();
return jss.Serialize(data);
}

public string Token()
{
return Config.Encoding.GetString(AuthToken.Make(Marshal()));
}
}
}
86 changes: 43 additions & 43 deletions QBox/Auth/DigestAuthClient.cs → QBox/Auth/QBoxAuthClient.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using QBox.Util;
using QBox.RPC;
using QBox.RS;
namespace QBox.Auth
{
public class DigestAuthClient : Client
{
public override void SetAuth(HttpWebRequest request, Stream body)
{
byte[] secretKey = Encoding.ASCII.GetBytes(Config.SECRET_KEY);
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
{
string pathAndQuery = request.Address.PathAndQuery;
byte[] pathAndQueryBytes = Encoding.ASCII.GetBytes(pathAndQuery);
using (MemoryStream buffer = new MemoryStream())
{
buffer.Write(pathAndQueryBytes, 0, pathAndQueryBytes.Length);
buffer.WriteByte((byte)'\n');
if (request.ContentType == "application/x-www-form-urlencoded" && body != null)
{
if (!body.CanSeek)
{
throw new Exception("stream can not seek");
}
StreamUtil.Copy(body, buffer);
body.Seek(0, SeekOrigin.Begin);
}
byte[] digest = hmac.ComputeHash(buffer.ToArray());
string digestBase64 = Base64UrlSafe.Encode(digest);
string authHead = "QBox " + Config.ACCESS_KEY + ":" + digestBase64;
request.Headers.Add("Authorization", authHead);
}
}
}
}
}
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using QBox.Util;
using QBox.RPC;
using QBox.Conf;

namespace QBox.Auth
{
public class QBoxAuthClient : Client
{
public override void SetAuth(HttpWebRequest request, Stream body)
{
byte[] secretKey = Config.Encoding.GetBytes(Config.SECRET_KEY);
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
{
string pathAndQuery = request.Address.PathAndQuery;
byte[] pathAndQueryBytes = Config.Encoding.GetBytes(pathAndQuery);
using (MemoryStream buffer = new MemoryStream())
{
buffer.Write(pathAndQueryBytes, 0, pathAndQueryBytes.Length);
buffer.WriteByte((byte)'\n');
if (request.ContentType == "application/x-www-form-urlencoded" && body != null)
{
if (!body.CanSeek)
{
throw new Exception("stream can not seek");
}
Util.IO.Copy(body, buffer);
body.Seek(0, SeekOrigin.Begin);
}
byte[] digest = hmac.ComputeHash(buffer.ToArray());
string digestBase64 = Base64URLSafe.Encode(digest);

string authHead = "QBox " + Config.ACCESS_KEY + ":" + digestBase64;
request.Headers.Add("Authorization", authHead);
}
}
}
}
}
Loading