alternative ReCaptcha, close #366
parent
f0a703baa7
commit
6e1c6a4a8c
|
@ -49,6 +49,7 @@ func Parse(c *caddy.Controller, plugin string) ([]*filebrowser.FileBrowser, erro
|
||||||
scope := "."
|
scope := "."
|
||||||
database := ""
|
database := ""
|
||||||
noAuth := false
|
noAuth := false
|
||||||
|
alterRecaptcha := false
|
||||||
reCaptchaKey := ""
|
reCaptchaKey := ""
|
||||||
reCaptchaSecret := ""
|
reCaptchaSecret := ""
|
||||||
|
|
||||||
|
@ -158,6 +159,16 @@ func Parse(c *caddy.Controller, plugin string) ([]*filebrowser.FileBrowser, erro
|
||||||
if u.ViewMode != filebrowser.MosaicViewMode && u.ViewMode != filebrowser.ListViewMode {
|
if u.ViewMode != filebrowser.MosaicViewMode && u.ViewMode != filebrowser.ListViewMode {
|
||||||
return nil, c.ArgErr()
|
return nil, c.ArgErr()
|
||||||
}
|
}
|
||||||
|
case "alternative_recaptcha":
|
||||||
|
if !c.NextArg() {
|
||||||
|
alterRecaptcha = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
alterRecaptcha, err = strconv.ParseBool(c.Val())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case "recaptcha_key":
|
case "recaptcha_key":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return nil, c.ArgErr()
|
return nil, c.ArgErr()
|
||||||
|
@ -227,10 +238,16 @@ func Parse(c *caddy.Controller, plugin string) ([]*filebrowser.FileBrowser, erro
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recaptchaHost := "https://www.google.com"
|
||||||
|
if alterRecaptcha {
|
||||||
|
recaptchaHost = "https://recaptcha.net"
|
||||||
|
}
|
||||||
|
|
||||||
m := &filebrowser.FileBrowser{
|
m := &filebrowser.FileBrowser{
|
||||||
NoAuth: noAuth,
|
NoAuth: noAuth,
|
||||||
BaseURL: "",
|
BaseURL: "",
|
||||||
PrefixURL: "",
|
PrefixURL: "",
|
||||||
|
ReCaptchaHost: recaptchaHost,
|
||||||
ReCaptchaKey: reCaptchaKey,
|
ReCaptchaKey: reCaptchaKey,
|
||||||
ReCaptchaSecret: reCaptchaSecret,
|
ReCaptchaSecret: reCaptchaSecret,
|
||||||
DefaultUser: u,
|
DefaultUser: u,
|
||||||
|
|
|
@ -44,6 +44,7 @@ var (
|
||||||
allowNew bool
|
allowNew bool
|
||||||
allowPublish bool
|
allowPublish bool
|
||||||
showVer bool
|
showVer bool
|
||||||
|
alterRecaptcha bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -64,6 +65,7 @@ func init() {
|
||||||
flag.BoolVar(&allowPublish, "allow-publish", true, "Default allow publish option for new users")
|
flag.BoolVar(&allowPublish, "allow-publish", true, "Default allow publish option for new users")
|
||||||
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option for new users")
|
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option for new users")
|
||||||
flag.BoolVar(&noAuth, "no-auth", false, "Disables authentication")
|
flag.BoolVar(&noAuth, "no-auth", false, "Disables authentication")
|
||||||
|
flag.BoolVar(&alterRecaptcha, "alternative-recaptcha", false, "Use recaptcha.net for serving and handling, useful in China")
|
||||||
flag.StringVar(&locale, "locale", "", "Default locale for new users, set it empty to enable auto detect from browser")
|
flag.StringVar(&locale, "locale", "", "Default locale for new users, set it empty to enable auto detect from browser")
|
||||||
flag.StringVar(&staticg, "staticgen", "", "Static Generator you want to enable")
|
flag.StringVar(&staticg, "staticgen", "", "Static Generator you want to enable")
|
||||||
flag.BoolVarP(&showVer, "version", "v", false, "Show version")
|
flag.BoolVarP(&showVer, "version", "v", false, "Show version")
|
||||||
|
@ -86,6 +88,7 @@ func setupViper() {
|
||||||
viper.SetDefault("BaseURL", "")
|
viper.SetDefault("BaseURL", "")
|
||||||
viper.SetDefault("PrefixURL", "")
|
viper.SetDefault("PrefixURL", "")
|
||||||
viper.SetDefault("ViewMode", filebrowser.MosaicViewMode)
|
viper.SetDefault("ViewMode", filebrowser.MosaicViewMode)
|
||||||
|
viper.SetDefault("AlternativeRecaptcha", false)
|
||||||
viper.SetDefault("ReCaptchaKey", "")
|
viper.SetDefault("ReCaptchaKey", "")
|
||||||
viper.SetDefault("ReCaptchaSecret", "")
|
viper.SetDefault("ReCaptchaSecret", "")
|
||||||
|
|
||||||
|
@ -105,6 +108,7 @@ func setupViper() {
|
||||||
viper.BindPFlag("BaseURL", flag.Lookup("baseurl"))
|
viper.BindPFlag("BaseURL", flag.Lookup("baseurl"))
|
||||||
viper.BindPFlag("PrefixURL", flag.Lookup("prefixurl"))
|
viper.BindPFlag("PrefixURL", flag.Lookup("prefixurl"))
|
||||||
viper.BindPFlag("ViewMode", flag.Lookup("view-mode"))
|
viper.BindPFlag("ViewMode", flag.Lookup("view-mode"))
|
||||||
|
viper.BindPFlag("AlternativeRecaptcha", flag.Lookup("alternative-recaptcha"))
|
||||||
viper.BindPFlag("ReCaptchaKey", flag.Lookup("recaptcha-key"))
|
viper.BindPFlag("ReCaptchaKey", flag.Lookup("recaptcha-key"))
|
||||||
viper.BindPFlag("ReCaptchaSecret", flag.Lookup("recaptcha-secret"))
|
viper.BindPFlag("ReCaptchaSecret", flag.Lookup("recaptcha-secret"))
|
||||||
|
|
||||||
|
@ -186,10 +190,16 @@ func handler() http.Handler {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recaptchaHost := "https://www.google.com"
|
||||||
|
if viper.GetBool("AlternativeRecaptcha") {
|
||||||
|
recaptchaHost = "https://recaptcha.net"
|
||||||
|
}
|
||||||
|
|
||||||
fm := &filebrowser.FileBrowser{
|
fm := &filebrowser.FileBrowser{
|
||||||
NoAuth: viper.GetBool("NoAuth"),
|
NoAuth: viper.GetBool("NoAuth"),
|
||||||
BaseURL: viper.GetString("BaseURL"),
|
BaseURL: viper.GetString("BaseURL"),
|
||||||
PrefixURL: viper.GetString("PrefixURL"),
|
PrefixURL: viper.GetString("PrefixURL"),
|
||||||
|
ReCaptchaHost: recaptchaHost,
|
||||||
ReCaptchaKey: viper.GetString("ReCaptchaKey"),
|
ReCaptchaKey: viper.GetString("ReCaptchaKey"),
|
||||||
ReCaptchaSecret: viper.GetString("ReCaptchaSecret"),
|
ReCaptchaSecret: viper.GetString("ReCaptchaSecret"),
|
||||||
DefaultUser: &filebrowser.User{
|
DefaultUser: &filebrowser.User{
|
||||||
|
|
|
@ -71,7 +71,8 @@ type FileBrowser struct {
|
||||||
// there will only exist one user, called "admin".
|
// there will only exist one user, called "admin".
|
||||||
NoAuth bool
|
NoAuth bool
|
||||||
|
|
||||||
// ReCaptcha Site key and secret.
|
// ReCaptcha host, key and secret.
|
||||||
|
ReCaptchaHost string
|
||||||
ReCaptchaKey string
|
ReCaptchaKey string
|
||||||
ReCaptchaSecret string
|
ReCaptchaSecret string
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
fm "github.com/filebrowser/filebrowser"
|
fm "github.com/filebrowser/filebrowser"
|
||||||
)
|
)
|
||||||
|
|
||||||
const reCaptchaAPI = "https://www.google.com/recaptcha/api/siteverify"
|
const reCaptchaAPI = "/recaptcha/api/siteverify"
|
||||||
|
|
||||||
type cred struct {
|
type cred struct {
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
|
@ -21,14 +21,14 @@ type cred struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// reCaptcha checks the reCaptcha code.
|
// reCaptcha checks the reCaptcha code.
|
||||||
func reCaptcha(secret string, response string) (bool, error) {
|
func reCaptcha(host, secret, response string) (bool, error) {
|
||||||
body := url.Values{}
|
body := url.Values{}
|
||||||
body.Set("secret", secret)
|
body.Set("secret", secret)
|
||||||
body.Add("response", response)
|
body.Add("response", response)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
resp, err := client.Post(reCaptchaAPI, "application/x-www-form-urlencoded", strings.NewReader(body.Encode()))
|
resp, err := client.Post(host+reCaptchaAPI, "application/x-www-form-urlencoded", strings.NewReader(body.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ func authHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, er
|
||||||
|
|
||||||
// If ReCaptcha is enabled, check the code.
|
// If ReCaptcha is enabled, check the code.
|
||||||
if len(c.ReCaptchaSecret) > 0 {
|
if len(c.ReCaptchaSecret) > 0 {
|
||||||
ok, err := reCaptcha(c.ReCaptchaSecret, cred.ReCaptcha)
|
ok, err := reCaptcha(c.ReCaptchaHost, c.ReCaptchaSecret, cred.ReCaptcha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusForbidden, err
|
return http.StatusForbidden, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -228,6 +228,7 @@ func renderFile(c *fm.Context, w http.ResponseWriter, file string) (int, error)
|
||||||
"Version": fm.Version,
|
"Version": fm.Version,
|
||||||
"CSS": template.CSS(c.CSS),
|
"CSS": template.CSS(c.CSS),
|
||||||
"ReCaptcha": c.ReCaptchaKey != "" && c.ReCaptchaSecret != "",
|
"ReCaptcha": c.ReCaptchaKey != "" && c.ReCaptchaSecret != "",
|
||||||
|
"ReCaptchaHost": c.ReCaptchaHost,
|
||||||
"ReCaptchaKey": c.ReCaptchaKey,
|
"ReCaptchaKey": c.ReCaptchaKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue