2019-01-06 06:44:33 +08:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MethodJSONAuth is used to identify json auth.
|
|
|
|
const MethodJSONAuth settings.AuthMethod = "json"
|
|
|
|
|
|
|
|
type jsonCred struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
ReCaptcha string `json:"recaptcha"`
|
|
|
|
}
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
// JSONAuth is a json implementation of an Auther.
|
2019-01-06 06:44:33 +08:00
|
|
|
type JSONAuth struct {
|
2019-01-08 16:57:24 +08:00
|
|
|
ReCaptcha *ReCaptcha `json:"recaptcha" yaml:"recaptcha"`
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Auth authenticates the user via a json in content body.
|
2024-04-02 00:24:06 +08:00
|
|
|
func (a JSONAuth) Auth(r *http.Request, usr users.Store, _ *settings.Settings, srv *settings.Server) (*users.User, error) {
|
2019-01-06 06:44:33 +08:00
|
|
|
var cred jsonCred
|
|
|
|
|
|
|
|
if r.Body == nil {
|
|
|
|
return nil, os.ErrPermission
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&cred)
|
|
|
|
if err != nil {
|
|
|
|
return nil, os.ErrPermission
|
|
|
|
}
|
|
|
|
|
|
|
|
// If ReCaptcha is enabled, check the code.
|
2024-04-02 00:24:06 +08:00
|
|
|
if a.ReCaptcha != nil && a.ReCaptcha.Secret != "" {
|
2021-03-04 07:10:08 +08:00
|
|
|
ok, err := a.ReCaptcha.Ok(cred.ReCaptcha) //nolint:govet
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, os.ErrPermission
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 21:47:06 +08:00
|
|
|
u, err := usr.Get(srv.Root, cred.Username)
|
2019-01-06 06:44:33 +08:00
|
|
|
if err != nil || !users.CheckPwd(cred.Password, u.Password) {
|
|
|
|
return nil, os.ErrPermission
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2019-02-15 20:58:45 +08:00
|
|
|
// LoginPage tells that json auth doesn't require a login page.
|
|
|
|
func (a JSONAuth) LoginPage() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
const reCaptchaAPI = "/recaptcha/api/siteverify"
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
// ReCaptcha identifies a recaptcha connection.
|
2019-01-06 06:44:33 +08:00
|
|
|
type ReCaptcha struct {
|
|
|
|
Host string `json:"host"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Secret string `json:"secret"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok checks if a reCaptcha responde is correct.
|
|
|
|
func (r *ReCaptcha) Ok(response string) (bool, error) {
|
|
|
|
body := url.Values{}
|
2019-10-10 05:22:08 +08:00
|
|
|
body.Set("secret", r.Secret)
|
2019-01-06 06:44:33 +08:00
|
|
|
body.Add("response", response)
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
resp, err := client.Post(
|
|
|
|
r.Host+reCaptchaAPI,
|
|
|
|
"application/x-www-form-urlencoded",
|
|
|
|
strings.NewReader(body.Encode()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-06-01 07:12:36 +08:00
|
|
|
defer resp.Body.Close()
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.Success, nil
|
|
|
|
}
|