From 3df71d4177e14004a0ca83081ed3c76082d16528 Mon Sep 17 00:00:00 2001 From: Daniel Mai Date: Tue, 22 Jan 2019 09:44:56 -0800 Subject: [PATCH] Make reCAPTCHA configuration optional. --- main.go | 59 +++++++++++++++++++++++++------------------- static/client.js | 4 ++- templates/index.tmpl | 4 +++ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/main.go b/main.go index cbcc058..8949959 100644 --- a/main.go +++ b/main.go @@ -48,14 +48,18 @@ var c Specification // Specification is the config struct type Specification struct { Port string `envconfig:"PORT" required:"true"` - CaptchaSitekey string `required:"true"` - CaptchaSecret string `required:"true"` + CaptchaSitekey string `required:"false"` + CaptchaSecret string `required:"false"` SlackToken string `required:"true"` CocUrl string `required:"false" default:"http://coc.golangbridge.org/"` EnforceHTTPS bool Debug bool // toggles nlopes/slack client's debug flag } +func recaptchaEnabled() bool { + return len(c.CaptchaSitekey) > 0 && len(c.CaptchaSecret) > 0 +} + func init() { var showUsage = flag.Bool("h", false, "Show usage") flag.Parse() @@ -81,14 +85,17 @@ func init() { m.Set("missing_last_name", &missingLastName) m.Set("missing_email", &missingEmail) m.Set("missing_coc", &missingCoC) - m.Set("failed_captcha", &failedCaptcha) - m.Set("invalid_captcha", &invalidCaptcha) - m.Set("successful_captcha", &successfulCaptcha) m.Set("successful_invites", &successfulInvites) m.Set("active_user_count", &activeUserCount) m.Set("user_count", &userCount) - captcha = recaptcha.New(c.CaptchaSecret) + if recaptchaEnabled() { + m.Set("failed_captcha", &failedCaptcha) + m.Set("invalid_captcha", &invalidCaptcha) + m.Set("successful_captcha", &successfulCaptcha) + captcha = recaptcha.New(c.CaptchaSecret) + } + api = slack.New(c.SlackToken) if c.Debug { @@ -200,27 +207,29 @@ func handleInvite(w http.ResponseWriter, r *http.Request) { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } - captchaResponse := r.FormValue("g-recaptcha-response") - remoteIP, _, err := net.SplitHostPort(r.RemoteAddr) - if err != nil { - failedCaptcha.Add(1) - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) - return - } + if recaptchaEnabled() { + captchaResponse := r.FormValue("g-recaptcha-response") + remoteIP, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + failedCaptcha.Add(1) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } - valid, err := captcha.Verify(captchaResponse, remoteIP) - if err != nil { - failedCaptcha.Add(1) - http.Error(w, "Error validating recaptcha.. Did you click it?", http.StatusPreconditionFailed) - return - } - if !valid { - invalidCaptcha.Add(1) - http.Error(w, "Invalid recaptcha", http.StatusInternalServerError) - return + valid, err := captcha.Verify(captchaResponse, remoteIP) + if err != nil { + failedCaptcha.Add(1) + http.Error(w, "Error validating recaptcha.. Did you click it?", http.StatusPreconditionFailed) + return + } + if !valid { + invalidCaptcha.Add(1) + http.Error(w, "Invalid recaptcha", http.StatusInternalServerError) + return + } + successfulCaptcha.Add(1) } - successfulCaptcha.Add(1) fname := r.FormValue("fname") lname := r.FormValue("lname") email := r.FormValue("email") @@ -245,7 +254,7 @@ func handleInvite(w http.ResponseWriter, r *http.Request) { http.Error(w, "You need to accept the code of conduct", http.StatusPreconditionFailed) return } - err = api.InviteToTeam(ourTeam.Domain(), fname, lname, email) + err := api.InviteToTeam(ourTeam.Domain(), fname, lname, email) if err != nil { log.Println("InviteToTeam error:", err) inviteErrors.Add(1) diff --git a/static/client.js b/static/client.js index 37aad4d..b93ed81 100644 --- a/static/client.js +++ b/static/client.js @@ -14,12 +14,14 @@ var button = body.querySelector('button'); button.className = ''; // capture submit +recaptcha_elem = document.getElementById("g-recaptcha-response"); +recaptcha_res = recaptcha_elem === null ? null : recaptcha_elem.value; body.addEventListener('submit', function(ev){ ev.preventDefault(); button.disabled = true; button.className = ''; button.innerHTML = 'Please Wait'; - invite(coc && coc.checked ? 1 : 0, email.value, first_name.value, last_name.value, document.getElementById("g-recaptcha-response").value, function(err){ + invite(coc && coc.checked ? 1 : 0, email.value, first_name.value, last_name.value, recaptcha_res, function(err){ if (err) { button.removeAttribute('disabled'); button.className = 'error'; diff --git a/templates/index.tmpl b/templates/index.tmpl index a624da6..e2fccbd 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -3,7 +3,9 @@ Join {{.Team.Name}} on Slack! + {{ if .SiteKey }} + {{ end }}
@@ -24,7 +26,9 @@

+ {{ if .SiteKey }}
+ {{ end }}

or