Browse Source

invisible captcha added

master
Christian Müller 8 years ago
parent
commit
09667f893e
  1. 22
      assets/templates/form.html
  2. 4
      server.go

22
assets/templates/form.html

@ -6,12 +6,11 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" /> <meta content="width=device-width, initial-scale=1.0" name="viewport" />
<link href="/style.css" rel="stylesheet" type="text/css" /> <link href="/style.css" rel="stylesheet" type="text/css" />
<base target="_blank" />
<script src='https://www.google.com/recaptcha/api.js'></script> <script src='https://www.google.com/recaptcha/api.js'></script>
<script src='/note.js'></script> <script src='/note.js'></script>
</head> </head>
<body> <body>
<form action="/note" autocomplete="off" method="POST" target="_self"> <form autocomplete="off" onsubmit="return false">
<textarea autofocus id="text">{{.Text}}</textarea> <textarea autofocus id="text">{{.Text}}</textarea>
<fieldset> <fieldset>
<input id="id" value="{{.ID}}" type="hidden" /> <input id="id" value="{{.ID}}" type="hidden" />
@ -20,8 +19,7 @@
<input id="tos" type="checkbox" onClick="toggleButton()" /> <input id="tos" type="checkbox" onClick="toggleButton()" />
Accept <a href="/TOS.md">Terms of Service</a> Accept <a href="/TOS.md">Terms of Service</a>
</label> </label>
<div id="captcha" class="g-recaptcha" data-sitekey="6LemnDEUAAAAAC6A4VNRefz0BSLiC343W4sXQd6I"></div> <button class="button ui-elem" disabled id="publish-button" type="button" onclick="grecaptcha.execute()">
<button class="button ui-elem" disabled id="publish-button" type="button" onclick="submitForm()">
{{if .ID}}Update{{else}}Publish{{end}} Note {{if .ID}}Update{{else}}Publish{{end}} Note
</button> </button>
<span id="feedback"></span> <span id="feedback"></span>
@ -34,11 +32,8 @@
</footer> </footer>
<script> <script>
function $(id) { return document.getElementById(id) } function $(id) { return document.getElementById(id) }
function toggleButton() { function toggleButton() { $('publish-button').disabled = !$('tos').checked }
$('publish-button').disabled = !$('tos').checked; function submitForm(token) {
$('captcha').style.display = $('tos').checked ? 'block' : 'none';
}
function submitForm() {
var id = $("id").value; var id = $("id").value;
var text = $("text").value; var text = $("text").value;
var deletion = id != "" && text == ""; var deletion = id != "" && text == "";
@ -49,17 +44,24 @@
"id": id, "id": id,
"text": text, "text": text,
"tos": $("tos").value, "tos": $("tos").value,
"password": $("password").value "password": $("password").value,
"token": token
}, function (status, responseRaw) { }, function (status, responseRaw) {
var response = JSON.parse(responseRaw); var response = JSON.parse(responseRaw);
if (status < 400 && response.Success) { if (status < 400 && response.Success) {
window.location.replace(deletion ? "/" : "/" + response.Payload) window.location.replace(deletion ? "/" : "/" + response.Payload)
} else { } else {
grecaptcha.reset();
$('feedback').innerHTML = status + ": " + response.Payload; $('feedback').innerHTML = status + ": " + response.Payload;
} }
}) })
} }
</script> </script>
<div class="g-recaptcha"
data-sitekey="6LfamjEUAAAAAANI45H3fpWG_xaSAcpYhENN4EnO"
data-callback="submitForm"
data-size="invisible">
</div>
</body> </body>
</html> </html>
{{end}} {{end}}

4
server.go

@ -123,9 +123,9 @@ func main() {
e.POST("/", func(c echo.Context) error { e.POST("/", func(c echo.Context) error {
c.Logger().Debug("POST /") c.Logger().Debug("POST /")
if !skipCaptcha && !checkRecaptcha(c, c.FormValue("g-recaptcha-response")) { if !skipCaptcha && !checkRecaptcha(c, c.FormValue("token")) {
code := http.StatusForbidden code := http.StatusForbidden
return c.JSON(code, postResp{false, statuses[code]}) return c.JSON(code, postResp{false, statuses[code] + ": robot check failed"})
} }
if c.FormValue("tos") != "on" { if c.FormValue("tos") != "on" {
code := http.StatusPreconditionFailed code := http.StatusPreconditionFailed

Loading…
Cancel
Save