Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace recaptcha with self-hosted captcha #281

Merged
merged 3 commits into from
Feb 8, 2019

Conversation

chappjc
Copy link
Member

@chappjc chappjc commented Nov 21, 2018

This replaces Google's reCAPTCHA with a self hosted captcha.

NOTE: This is needs review and testing. It works for me though.

Create controllers/captcha.go, where the captcha GET and POST handlers
are implemented for retrieving images and verifying responses.
Create CaptchaHandler type with image size spec, and add it as a field
of MainController.
Create ApplyCaptcha captcha middleware.
Add GET route for /captchas for serving the captcha images.
Add POST route for /verifyhuman for verifying captcha responses and
redirecting to the previous page with the session updated.
Modify http handlers to display new captcha: PasswordReset, SignUp, and
Settings.
Modify http handlers to check for captcha verification:
PasswordResetPost, SignUpPost, and SettingsPost.
Unrelated: remove unused empty responseHeaderMap.
Comment on how insane core/(*Application).Route is.

@chappjc
Copy link
Member Author

chappjc commented Nov 23, 2018

Force pushed 996f7bf. Tested and working. The pages don't look very pretty, but's that's about the best I can do.

@chappjc
Copy link
Member Author

chappjc commented Nov 23, 2018

Currently it looks a bit like this (showing Password Reset page before solving captcha):

image

Sign up:

image

The idea is the forms protected by captcha don't show up until the captcha is solved. It's not just the forms are not there, of course, but that the POST handler that deals with the usual forms does not process the other non-captcha data. This is the purpose of the ApplyCaptcha middleware.

@chappjc
Copy link
Member Author

chappjc commented Nov 23, 2018

Setting the captcha length (now 6), characters (now just 10 numerals), captcha expiry (now 10 minutes), and other captcha parameters may be worth setting in the future. Although because of limitations in the captcha API, we may just want to fork the captcha repo.

Copy link
Member Author

@chappjc chappjc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotated for other reviewers.

session.Values["CaptchaDone"] = true
status = http.StatusFound
} else {
session.Values["CaptchaDone"] = false
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably do a session.AddFlash("Captcha verification failed. Try again.") here too

"github.com/zenazn/goji/web"
)

type CaptchaHandler struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to doc all these exported things. And copyright the file.

@@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/dchest/captcha"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import should be with the other github imports, not with the std lib imports.

@@ -145,6 +143,11 @@ func NewMainController(params *chaincfg.Params, adminIPs []string,
return nil, err
}

ch := &CaptchaHandler{
ImgHeight: 127,
ImgWidth: 257,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled out of my ass and could be set in the config file, but that's not really needed now.

if controller.smtpHost == "" {
c.Env["SMTPDisabled"] = true
}
c.Env["CaptchaID"] = captcha.New()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reload button next to the captcha. To get a new one, all you have to do is reload the page with the protected forms.

@@ -227,6 +227,10 @@ func runMain() int {
app.Get("/signup", application.Route(controller, "SignUp"))
app.Post("/signup", application.Route(controller, "SignUpPost"))

// Captcha
app.Get("/captchas/*", controller.CaptchaServe)
app.Post("/verifyhuman", controller.CaptchaVerify)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The application.Route function is a shit show. Not using it.

if err := saveSession(c, w, r); err != nil {
log.Errorf("Can't save session: %v", err)
}

if respHeader, exists := c.Env["ResponseHeaderMap"]; exists {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a relic from a much older feature that was partially ripped out.

@chappjc chappjc force-pushed the captcha-self-hosted branch from 05d7cb4 to 9f33e7f Compare November 23, 2018 22:07
@honeycrypto
Copy link

Why to overcomplicate things? Recaptcha works great.

@chappjc
Copy link
Member Author

chappjc commented Nov 24, 2018

@honeycrypto Please see #279

@dajohi dajohi changed the title replace recaptcha with self-hosted captcha [WIP] replace recaptcha with self-hosted captcha Jan 11, 2019
@chappjc chappjc changed the title [WIP] replace recaptcha with self-hosted captcha replace recaptcha with self-hosted captcha Jan 30, 2019
@teknico
Copy link
Contributor

teknico commented Jan 31, 2019

This branch works for me too.

Under a local deployment I was able to sign up with the captcha image: I got the confirmation email and activated the account. Looking at the database I confirmed that the account got id no. 1 (configured as admin), and logging in I was indeed able to see the low-fee ticket page.

(The local deployment consists of one dcrd instance, one dcrwallet for fees, two voting dcrwallet, two stakepoold, one dcrstakepool and one mysqld. To make all the daemons run in the same namespace without virtual machines nor containers, I changed the various listening ports in the daemons' configuration.)

@chappjc
Copy link
Member Author

chappjc commented Feb 1, 2019

Thanks for the feedback @teknico.

@dajohi
Copy link
Member

dajohi commented Feb 1, 2019

@chappjc please rebase on top of current master

@chappjc chappjc force-pushed the captcha-self-hosted branch from 9f33e7f to ed129c7 Compare February 4, 2019 14:48
@chappjc
Copy link
Member Author

chappjc commented Feb 4, 2019

@dajohi rebased

Create controllers/captcha.go, where the captcha GET and POST handlers
are implemented for retrieving images and verifying responses.
Create CaptchaHandler type with image size spec, and add it as a field
of MainController.
Create ApplyCaptcha captcha middleware.
Add GET route for /captchas for serving the captcha images.
Add POST route for /verifyhuman for verifying captcha responses and
redirecting to the previous page with the session udpated.
Modify http handlers to display new captcha: PasswordReset, SignIn, and
Settings.
Modify http handlers to check for captcha verification:
PasswordResetPost, SignInPost, and SettingsPost.
Unrelated: remove unused empty responseHeaderMap.
Comment on how insane core/(*Application).Route is.
Remove the old config file stuff.
@chappjc chappjc force-pushed the captcha-self-hosted branch from ed129c7 to 04cc822 Compare February 8, 2019 15:44
@chappjc
Copy link
Member Author

chappjc commented Feb 8, 2019

@dajohi rebased again

Add a flash message in the CaptchaVerify middleware.
Copy link
Member

@dajohi dajohi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@dajohi dajohi merged commit 9ec3776 into decred:master Feb 8, 2019
@chappjc chappjc deleted the captcha-self-hosted branch February 8, 2019 18:48
girino added a commit to girino/dcrstakepool that referenced this pull request Sep 7, 2019
* commit '9ec3776b1d9ed8f2a06d196ab072b8474e9bb6a9':
  replace recaptcha with self-hosted captcha (decred#281)

# Conflicts:
#	controllers/main.go
#	views/settings.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants