-
Notifications
You must be signed in to change notification settings - Fork 595
/
run.csx
73 lines (63 loc) · 3.19 KB
/
run.csx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#r "Newtonsoft.Json"
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
string captcha = req.Query["captcha"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
captcha = captcha ?? data?.captcha;
log.LogInformation($"RecaptchaVerify - params: captcha={captcha}");
if ( string.IsNullOrEmpty(captcha) ) {
var respContent = new { version = "1.0.0", status = (int)HttpStatusCode.BadRequest, userMessage = "Invalid Captcha"};
var json = JsonConvert.SerializeObject(respContent);
log.LogInformation(json);
return new HttpResponseMessage(HttpStatusCode.Conflict) {
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
};
}
// store your Captcha secret key in thge app config
var captchaSecret = System.Environment.GetEnvironmentVariable("B2C_Recaptcha_Secret");
HttpClient client = new HttpClient();
var url = $"https://www.google.com/recaptcha/api/siteverify";
var dict= new Dictionary<string, string>();
dict.Add("secret", captchaSecret);
dict.Add("response", captcha);
log.LogInformation(url);
HttpResponseMessage res = client.PostAsync(url, new FormUrlEncodedContent(dict)).Result;
var contents = await res.Content.ReadAsStringAsync();
client.Dispose();
log.LogInformation("HttpStatusCode=" + res.StatusCode.ToString());
// { {
// "success": false, "success": true,
// "error-codes": [ "challenge_ts": "2020-06-16T09:38:08Z",
// "invalid-input-secret" "hostname": "yourtenant.b2clogin.com"
// ] }
// }
log.LogInformation("Content=" + contents);
// return either good message that REST API expects or 409 conflict
if ( res.StatusCode != HttpStatusCode.OK ) {
var respContent = new { version = "1.0.0", status = (int)HttpStatusCode.BadRequest, userMessage = "Captcha API failed"};
var json = JsonConvert.SerializeObject(respContent);
log.LogInformation(json);
return new HttpResponseMessage(HttpStatusCode.Conflict) {
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
};
}
JObject obj = JObject.Parse(contents);
bool success = (bool)obj["success"];
if ( success ) {
return new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(contents, System.Text.Encoding.UTF8, "application/json")
};
} else {
var respContent = new { version = "1.0.0", status = (int)HttpStatusCode.BadRequest, userMessage = "Captcha failed, retry the Captcha."};
var json = JsonConvert.SerializeObject(respContent);
log.LogInformation(json);
return new HttpResponseMessage(HttpStatusCode.Conflict) {
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
};
}
}