Skip to content

Commit 04def03

Browse files
committed
feat: add VerifyRequest to check if request signed by qn
1 parent 5b42d8e commit 04def03

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/Qiniu/Util/Signature.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,50 @@ public string SignRequestV2(string method, string url, StringDictionary headers,
200200
{
201201
return SignRequestV2(method, url, headers, Encoding.UTF8.GetString(body));
202202
}
203+
204+
public bool VerifyRequest(
205+
string method,
206+
string url,
207+
StringDictionary headers,
208+
string body = null
209+
)
210+
{
211+
byte[] bodyBytes = null;
212+
if (!string.IsNullOrEmpty(body)) {
213+
bodyBytes = Encoding.UTF8.GetBytes(body);
214+
}
215+
return VerifyRequest(
216+
method,
217+
url,
218+
headers,
219+
bodyBytes
220+
);
221+
}
222+
223+
public bool VerifyRequest(
224+
string method,
225+
string url,
226+
StringDictionary headers,
227+
byte[] body = null
228+
)
229+
{
230+
if (!headers.ContainsKey("Authorization"))
231+
{
232+
return false;
233+
}
234+
235+
string authString = headers["Authorization"];
236+
if (authString.StartsWith("QBox "))
237+
{
238+
return authString == "QBox " + SignRequest(url, body);
239+
}
240+
241+
if (authString.StartsWith("Qiniu "))
242+
{
243+
return authString == "Qiniu " + SignRequestV2(method, url, headers, body);
244+
}
245+
246+
return false;
247+
}
203248
}
204249
}

src/QiniuTests/Util/Signature.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,13 @@ public string SignatureV2ByBytesTest(string method, string url, StringDictionary
2323
{
2424
return string.Format("Qiniu {0}", sign.SignRequestV2(method, url, headers, Encoding.UTF8.GetBytes(body)));
2525
}
26+
27+
[TestCaseSource(typeof(VerifyRequestDataClass), nameof(VerifyRequestDataClass.TestCases))]
28+
public bool VerifyRequestTest(string method, string url, StringDictionary headers, string body)
29+
{
30+
Mac mac = new Mac("abcdefghklmnopq", "1234567890");
31+
Signature mockSign = new Signature(mac);
32+
return mockSign.VerifyRequest(method, url, headers, body);
33+
}
2634
}
2735
}

src/QiniuTests/Util/TestCases.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,36 @@ public static IEnumerable TestCases
211211
}
212212
}
213213
}
214-
}
214+
215+
public class VerifyRequestDataClass
216+
{
217+
public static IEnumerable TestCases
218+
{
219+
get
220+
{
221+
yield return new TestCaseData(
222+
"",
223+
"https://test.qiniu.com/callback",
224+
new StringDictionary
225+
{
226+
{"Authorization", "QBox abcdefghklmnopq:T7F-SjxX7X2zI4Fc1vANiNt1AUE="},
227+
{"Content-Type", "application/x-www-form-urlencoded"}
228+
},
229+
"name=sunflower.jpg&hash=Fn6qeQi4VDLQ347NiRm-RlQx_4O2&location=Shanghai&price=1500.00&uid=123"
230+
).Returns(true);
231+
232+
yield return new TestCaseData(
233+
"GET",
234+
"https://test.qiniu.com/callback",
235+
new StringDictionary
236+
{
237+
{"Authorization", "Qiniu abcdefghklmnopq:ZqS7EZuAKrhZaEIxqNGxDJi41IQ="},
238+
{"X-Qiniu-Bbb", "BBB"},
239+
{"Content-Type", "application/x-www-form-urlencoded"}
240+
},
241+
"name=sunflower.jpg&hash=Fn6qeQi4VDLQ347NiRm-RlQx_4O2&location=Shanghai&price=1500.00&uid=123"
242+
).Returns(true);
243+
}
244+
}
245+
}
246+
}

0 commit comments

Comments
 (0)