Skip to content

Commit f049706

Browse files
committed
add x-www-form-urlencoded body for digest
1 parent 5165b06 commit f049706

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

QBox/Client.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ namespace QBox
66
{
77
public abstract class Client
88
{
9-
public abstract void SetAuth(HttpWebRequest request);
10-
9+
public abstract void SetAuth(HttpWebRequest request, byte[] body);
10+
1111
public CallRet Call(string url)
1212
{
1313
Console.WriteLine("URL: " + url);
1414
try
1515
{
1616
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
1717
request.Method = "POST";
18-
SetAuth(request);
18+
SetAuth(request, null);
1919
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
2020
{
2121
return HandleResult(response);
@@ -37,7 +37,7 @@ public CallRet CallWithBinary(string url, string contentType, byte[] body)
3737
request.Method = "POST";
3838
request.ContentType = contentType;
3939
request.ContentLength = body.Length;
40-
SetAuth(request);
40+
SetAuth(request, body);
4141
using (Stream requestStream = request.GetRequestStream())
4242
{
4343
requestStream.Write(body, 0, body.Length);

QBox/DigestAuthClient.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,27 @@ namespace QBox
88
{
99
public class DigestAuthClient : Client
1010
{
11-
public override void SetAuth(HttpWebRequest request)
11+
public override void SetAuth(HttpWebRequest request, byte[] body)
1212
{
1313
byte[] secretKey = Encoding.ASCII.GetBytes(Config.SECRET_KEY);
1414
using (HMACSHA1 hmac = new HMACSHA1(secretKey))
1515
{
1616
string pathAndQuery = request.Address.PathAndQuery;
17-
byte[] bytesIn = Encoding.ASCII.GetBytes(pathAndQuery + "\n");
18-
byte[] digest = hmac.ComputeHash(bytesIn);
19-
string digestBase64 = Base64UrlSafe.Encode(digest);
17+
byte[] pathAndQueryBytes = Encoding.ASCII.GetBytes(pathAndQuery);
18+
using (MemoryStream buffer = new MemoryStream())
19+
{
20+
buffer.Write(pathAndQueryBytes, 0, pathAndQueryBytes.Length);
21+
buffer.WriteByte((byte)'\n');
22+
if (request.ContentType == "application/x-www-form-urlencoded" && body != null)
23+
{
24+
buffer.Write(body, 0, body.Length);
25+
}
26+
byte[] digest = hmac.ComputeHash(buffer.ToArray());
27+
string digestBase64 = Base64UrlSafe.Encode(digest);
2028

21-
string authHead = "QBox " + Config.ACCESS_KEY + ":" + digestBase64;
22-
request.Headers.Add("Authorization", authHead);
29+
string authHead = "QBox " + Config.ACCESS_KEY + ":" + digestBase64;
30+
request.Headers.Add("Authorization", authHead);
31+
}
2332
}
2433
}
2534
}

0 commit comments

Comments
 (0)