func authHandler(c *fb.Context) (int, error) {
	cfgM := c.GetAuthConfig()
	if cfgM.AuthMethod == "none" {
		return 0, nil
	} else if cfgM.AuthMethod == "proxy" || cfgM.AuthMethod == "ip" {
		isIp := cfgM.AuthMethod == "ip"
		var uc *config.UserConfig
		var ok bool
		if isIp {
			uc, ok = c.Config.GetUserByIp(c.REQ.RemoteAddr)
		} else {
			uc, ok = c.Config.GetUserByUsername(c.REQ.Header.Get(c.FileBrowser.Config.Header))
		}
		if !ok {
			return http.StatusForbidden, nil
		}
		c.User = fb.ToUserModel(uc, c.Config)
		return printToken(c)
	}
	var cred cred
	if c.REQ.Body == nil {
		return http.StatusForbidden, nil
	}
	err := json.NewDecoder(c.REQ.Body).Decode(&cred)
	if err != nil {
		return http.StatusForbidden, err
	}
	if len(c.ReCaptcha.Secret) > 0 {
		ok, err := reCaptcha(c.ReCaptcha.Host, c.ReCaptcha.Secret, cred.ReCaptcha)
		if err != nil {
			return http.StatusForbidden, err
		}
		if !ok {
			return http.StatusForbidden, nil
		}
	}
	uc, ok := c.Config.GetUserByUsername(cred.Username)
	if !ok {
		return http.StatusForbidden, nil
	}
	if !uc.IsGuest() {
		if !ok || !fb.CheckPasswordHash(cred.Password, uc.Password) {
			return http.StatusForbidden, nil
		}
	}
	c.User = fb.ToUserModel(uc, c.Config)
	return printToken(c)
}
authHandler processes the authentication for the user.
package web
import (
	"encoding/json"
	"github.com/browsefile/backend/src/cnst"
	"github.com/browsefile/backend/src/config"
	"log"
	"net/http"
	"net/url"
	"strings"
	"sync"
	"time"
	fb "github.com/browsefile/backend/src/lib"
	"github.com/dgrijalva/jwt-go"
	"github.com/dgrijalva/jwt-go/request"
)
var (
	authKeySession = make(map[string]bool)
	authKeyLock    = new(sync.RWMutex)
)
const reCaptchaAPI = "/recaptcha/api/siteverify"
type cred struct {
	Password  string `json:"password"`
	Username  string `json:"username"`
	ReCaptcha string `json:"recaptcha"`
}
func reCaptcha(host, secret, response string) (bool, error) {
	body := url.Values{}
	body.Set("secret", secret)
	body.Add("response", response)
	client := &http.Client{}
	resp, err := client.Post(host+reCaptchaAPI, "application/x-www-form-urlencoded", strings.NewReader(body.Encode()))
	if err != nil {
		return false, err
	}
	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
}
func authDavHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (res bool) {
	cfgM := c.GetAuthConfig()
	if cfgM.AuthMethod == "ip" {
		u, res := c.Config.GetUserByIp(r.RemoteAddr)
		if !res {
			return false
		}
		c.User = fb.ToUserModel(u, c.Config)
		return true
	}
	w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
	username, password, ok := r.BasicAuth()
	if !ok {
		http.Error(w, "Not authorized", http.StatusUnauthorized)
		return
	}
	user, ok := c.Config.GetUserByUsername(username)
	if !ok {
		http.Error(w, "Not authorized", http.StatusUnauthorized)
		return
	}
	auth := r.Header.Get("Authorization")
	authKeyLock.RLock()
	isAuth := authKeySession[auth]
	authKeyLock.RUnlock()
	if !isAuth {
		if !fb.CheckPasswordHash(password, user.Password) {
			log.Println("Wrong Password for user", username)
			http.Error(w, "Not authorized", http.StatusUnauthorized)
			return
		}
		auth := r.Header.Get("Authorization")
		authKeyLock.Lock()
		authKeySession[auth] = true
		authKeyLock.Unlock()
	}
	c.User = fb.ToUserModel(user, c.Config)
	res = true
	return
}