2021-01-17 21:29:08 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-05-11 04:54:06 +00:00
|
|
|
"context"
|
2021-01-17 21:29:08 +00:00
|
|
|
"crypto/tls"
|
2021-01-19 17:59:41 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
2021-01-17 21:29:08 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-05-11 04:54:06 +00:00
|
|
|
"net"
|
2021-01-17 21:29:08 +00:00
|
|
|
"net/http"
|
2021-11-21 08:32:51 +00:00
|
|
|
"strings"
|
2021-01-17 21:29:08 +00:00
|
|
|
"time"
|
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2021-03-05 17:50:45 +00:00
|
|
|
"github.com/go-chi/jwtauth/v5"
|
2021-01-17 21:29:08 +00:00
|
|
|
"github.com/go-chi/render"
|
2021-04-11 06:38:43 +00:00
|
|
|
"github.com/lestrrat-go/jwx/jwa"
|
2021-11-13 22:14:50 +00:00
|
|
|
"github.com/rs/cors"
|
2021-05-06 19:35:43 +00:00
|
|
|
"github.com/rs/xid"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-09-04 10:11:04 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/mfa"
|
2021-11-13 12:25:43 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/smtp"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/version"
|
2021-01-17 21:29:08 +00:00
|
|
|
)
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
var (
|
|
|
|
compressor = middleware.NewCompressor(5)
|
|
|
|
xForwardedProto = http.CanonicalHeaderKey("X-Forwarded-Proto")
|
|
|
|
)
|
2021-02-04 21:32:55 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
type httpdServer struct {
|
2021-07-01 18:17:40 +00:00
|
|
|
binding Binding
|
|
|
|
staticFilesPath string
|
2021-11-21 08:32:51 +00:00
|
|
|
openAPIPath string
|
2021-07-01 18:17:40 +00:00
|
|
|
enableWebAdmin bool
|
|
|
|
enableWebClient bool
|
2021-11-21 08:32:51 +00:00
|
|
|
renderOpenAPI bool
|
2021-07-01 18:17:40 +00:00
|
|
|
router *chi.Mux
|
|
|
|
tokenAuth *jwtauth.JWTAuth
|
|
|
|
signingPassphrase string
|
2021-11-13 22:14:50 +00:00
|
|
|
cors CorsConfig
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 08:32:51 +00:00
|
|
|
func newHttpdServer(b Binding, staticFilesPath, signingPassphrase string, cors CorsConfig,
|
|
|
|
openAPIPath string,
|
|
|
|
) *httpdServer {
|
|
|
|
if openAPIPath == "" {
|
|
|
|
b.RenderOpenAPI = false
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
return &httpdServer{
|
2021-07-01 18:17:40 +00:00
|
|
|
binding: b,
|
|
|
|
staticFilesPath: staticFilesPath,
|
2021-11-21 08:32:51 +00:00
|
|
|
openAPIPath: openAPIPath,
|
2021-07-01 18:17:40 +00:00
|
|
|
enableWebAdmin: b.EnableWebAdmin,
|
|
|
|
enableWebClient: b.EnableWebClient,
|
2021-11-21 08:32:51 +00:00
|
|
|
renderOpenAPI: b.RenderOpenAPI,
|
2021-07-01 18:17:40 +00:00
|
|
|
signingPassphrase: signingPassphrase,
|
2021-11-13 22:14:50 +00:00
|
|
|
cors: cors,
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) listenAndServe() error {
|
|
|
|
s.initializeRouter()
|
|
|
|
httpServer := &http.Server{
|
2021-05-06 19:35:43 +00:00
|
|
|
Handler: s.router,
|
|
|
|
ReadHeaderTimeout: 30 * time.Second,
|
2021-05-07 18:41:20 +00:00
|
|
|
ReadTimeout: 60 * time.Second,
|
|
|
|
WriteTimeout: 60 * time.Second,
|
|
|
|
IdleTimeout: 60 * time.Second,
|
2021-05-06 19:35:43 +00:00
|
|
|
MaxHeaderBytes: 1 << 16, // 64KB
|
2022-01-05 10:37:45 +00:00
|
|
|
ErrorLog: log.New(&logger.StdLoggerWrapper{Sender: logSender}, "", 0),
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
if certMgr != nil && s.binding.EnableHTTPS {
|
2021-01-17 21:29:08 +00:00
|
|
|
config := &tls.Config{
|
2021-02-18 19:17:16 +00:00
|
|
|
GetCertificate: certMgr.GetCertificateFunc(),
|
2022-02-13 14:56:07 +00:00
|
|
|
MinVersion: util.GetTLSVersion(s.binding.MinTLSVersion),
|
2021-12-08 18:25:22 +00:00
|
|
|
NextProtos: []string{"http/1.1", "h2"},
|
2021-07-11 13:26:51 +00:00
|
|
|
CipherSuites: util.GetTLSCiphersFromNames(s.binding.TLSCipherSuites),
|
2021-02-18 19:17:16 +00:00
|
|
|
PreferServerCipherSuites: true,
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-02-18 19:17:16 +00:00
|
|
|
logger.Debug(logSender, "", "configured TLS cipher suites for binding %#v: %v", s.binding.GetAddress(),
|
|
|
|
config.CipherSuites)
|
2021-01-17 21:29:08 +00:00
|
|
|
httpServer.TLSConfig = config
|
2021-01-19 17:59:41 +00:00
|
|
|
if s.binding.ClientAuthType == 1 {
|
|
|
|
httpServer.TLSConfig.ClientCAs = certMgr.GetRootCAs()
|
|
|
|
httpServer.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
httpServer.TLSConfig.VerifyConnection = s.verifyTLSConnection
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.HTTPListenAndServe(httpServer, s.binding.Address, s.binding.Port, true, logSender)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.HTTPListenAndServe(httpServer, s.binding.Address, s.binding.Port, false, logSender)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
func (s *httpdServer) verifyTLSConnection(state tls.ConnectionState) error {
|
|
|
|
if certMgr != nil {
|
|
|
|
var clientCrt *x509.Certificate
|
|
|
|
var clientCrtName string
|
|
|
|
if len(state.PeerCertificates) > 0 {
|
|
|
|
clientCrt = state.PeerCertificates[0]
|
|
|
|
clientCrtName = clientCrt.Subject.String()
|
|
|
|
}
|
|
|
|
if len(state.VerifiedChains) == 0 {
|
|
|
|
logger.Warn(logSender, "", "TLS connection cannot be verified: unable to get verification chain")
|
|
|
|
return errors.New("TLS connection cannot be verified: unable to get verification chain")
|
|
|
|
}
|
|
|
|
for _, verifiedChain := range state.VerifiedChains {
|
|
|
|
var caCrt *x509.Certificate
|
|
|
|
if len(verifiedChain) > 0 {
|
|
|
|
caCrt = verifiedChain[len(verifiedChain)-1]
|
|
|
|
}
|
|
|
|
if certMgr.IsRevoked(clientCrt, caCrt) {
|
|
|
|
logger.Debug(logSender, "", "tls handshake error, client certificate %#v has been revoked", clientCrtName)
|
|
|
|
return common.ErrCrtRevoked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (s *httpdServer) refreshCookie(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
s.checkCookieExpiration(w, r)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:43:00 +00:00
|
|
|
func (s *httpdServer) renderClientLoginPage(w http.ResponseWriter, error string) {
|
|
|
|
data := loginPage{
|
|
|
|
CurrentURL: webClientLoginPath,
|
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
|
|
|
CSRFToken: createCSRFToken(),
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
}
|
|
|
|
if s.binding.showAdminLoginURL() {
|
2022-02-13 13:30:20 +00:00
|
|
|
data.AltLoginURL = webAdminLoginPath
|
2021-07-27 16:43:00 +00:00
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
if smtp.IsEnabled() {
|
|
|
|
data.ForgotPwdURL = webClientForgotPwdPath
|
|
|
|
}
|
2022-02-13 13:30:20 +00:00
|
|
|
if s.binding.OIDC.isEnabled() {
|
|
|
|
data.OpenIDLoginURL = webClientOIDCLoginPath
|
|
|
|
}
|
2021-07-27 16:43:00 +00:00
|
|
|
renderClientTemplate(w, templateClientLogin, data)
|
|
|
|
}
|
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
func (s *httpdServer) handleWebClientLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
c := jwtTokenClaims{}
|
|
|
|
c.removeCookie(w, r, webBaseClientPath)
|
|
|
|
s.logoutOIDCUser(w, r)
|
|
|
|
|
|
|
|
http.Redirect(w, r, webClientLoginPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebClientChangePwdPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
renderClientChangePasswordPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderClientForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = doChangeUserPassword(r, r.Form.Get("current_password"), r.Form.Get("new_password1"),
|
|
|
|
r.Form.Get("new_password2"))
|
|
|
|
if err != nil {
|
|
|
|
renderClientChangePasswordPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.handleWebClientLogout(w, r)
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:43:00 +00:00
|
|
|
func (s *httpdServer) handleClientWebLogin(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-09-25 10:20:31 +00:00
|
|
|
if !dataprovider.HasAdmin() {
|
|
|
|
http.Redirect(w, r, webAdminSetupPath, http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2022-02-13 13:30:20 +00:00
|
|
|
s.renderClientLoginPage(w, getFlashMessage(w, r))
|
2021-07-27 16:43:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func (s *httpdServer) handleWebClientLoginPost(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-05-08 17:45:21 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderClientLoginPage(w, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
2021-05-06 19:35:43 +00:00
|
|
|
username := r.Form.Get("username")
|
|
|
|
password := r.Form.Get("password")
|
|
|
|
if username == "" || password == "" {
|
2021-07-11 13:26:51 +00:00
|
|
|
updateLoginMetrics(&dataprovider.User{BaseUser: sdk.BaseUser{Username: username}}, ipAddr, common.ErrNoCredentials)
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderClientLoginPage(w, "Invalid credentials")
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
updateLoginMetrics(&dataprovider.User{BaseUser: sdk.BaseUser{Username: username}}, ipAddr, err)
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderClientLoginPage(w, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := common.Config.ExecutePostConnectHook(ipAddr, common.ProtocolHTTP); err != nil {
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderClientLoginPage(w, fmt.Sprintf("access denied by post connect hook: %v", err))
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := dataprovider.CheckUserAndPass(username, password, ipAddr, common.ProtocolHTTP)
|
|
|
|
if err != nil {
|
|
|
|
updateLoginMetrics(&user, ipAddr, err)
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderClientLoginPage(w, dataprovider.ErrInvalidCredentials.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolHTTP, xid.New().String())
|
2021-06-05 14:07:09 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID); err != nil {
|
2021-05-06 19:35:43 +00:00
|
|
|
updateLoginMetrics(&user, ipAddr, err)
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderClientLoginPage(w, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer user.CloseFs() //nolint:errcheck
|
|
|
|
err = user.CheckFsRoot(connectionID)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, connectionID, "unable to check fs root: %v", err)
|
2021-06-05 14:07:09 +00:00
|
|
|
updateLoginMetrics(&user, ipAddr, common.ErrInternalFailure)
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderClientLoginPage(w, err.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
s.loginUser(w, r, &user, connectionID, ipAddr, false, s.renderClientLoginPage)
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2021-11-13 12:25:43 +00:00
|
|
|
func (s *httpdServer) handleWebClientPasswordResetPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
renderClientResetPwdPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderClientForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, user, err := handleResetPassword(r, r.Form.Get("code"), r.Form.Get("password"), false)
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*util.ValidationError); ok {
|
|
|
|
renderClientResetPwdPage(w, e.GetErrorString())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
renderClientResetPwdPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolHTTP, xid.New().String())
|
|
|
|
if err := checkHTTPClientUser(user, r, connectionID); err != nil {
|
|
|
|
renderClientResetPwdPage(w, fmt.Sprintf("Password reset successfully but unable to login: %v", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer user.CloseFs() //nolint:errcheck
|
|
|
|
err = user.CheckFsRoot(connectionID)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, connectionID, "unable to check fs root: %v", err)
|
|
|
|
renderClientResetPwdPage(w, fmt.Sprintf("Password reset successfully but unable to login: %v", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
|
|
|
s.loginUser(w, r, user, connectionID, ipAddr, false, renderClientResetPwdPage)
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func (s *httpdServer) handleWebClientTwoFactorRecoveryPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil {
|
|
|
|
renderNotFoundPage(w, r, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
renderClientTwoFactorRecoveryPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := claims.Username
|
|
|
|
recoveryCode := r.Form.Get("recovery_code")
|
|
|
|
if username == "" || recoveryCode == "" {
|
|
|
|
renderClientTwoFactorRecoveryPage(w, "Invalid credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderClientTwoFactorRecoveryPage(w, err.Error())
|
|
|
|
return
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
user, err := dataprovider.UserExists(username)
|
|
|
|
if err != nil {
|
|
|
|
renderClientTwoFactorRecoveryPage(w, "Invalid credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !user.Filters.TOTPConfig.Enabled || !util.IsStringInSlice(common.ProtocolHTTP, user.Filters.TOTPConfig.Protocols) {
|
|
|
|
renderClientTwoFactorPage(w, "Two factory authentication is not enabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for idx, code := range user.Filters.RecoveryCodes {
|
|
|
|
if err := code.Secret.Decrypt(); err != nil {
|
|
|
|
renderClientInternalServerErrorPage(w, r, fmt.Errorf("unable to decrypt recovery code: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if code.Secret.GetPayload() == recoveryCode {
|
|
|
|
if code.Used {
|
|
|
|
renderClientTwoFactorRecoveryPage(w, "This recovery code was already used")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.Filters.RecoveryCodes[idx].Used = true
|
2021-10-10 11:08:05 +00:00
|
|
|
err = dataprovider.UpdateUser(&user, dataprovider.ActionExecutorSelf, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-09-04 10:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "unable to set the recovery code %#v as used: %v", recoveryCode, err)
|
|
|
|
renderClientInternalServerErrorPage(w, r, errors.New("unable to set the recovery code as used"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolHTTP, xid.New().String())
|
|
|
|
s.loginUser(w, r, &user, connectionID, util.GetIPFromRemoteAddress(r.RemoteAddr), true,
|
|
|
|
renderClientTwoFactorRecoveryPage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
renderClientTwoFactorRecoveryPage(w, "Invalid recovery code")
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func (s *httpdServer) handleWebClientTwoFactorPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
claims, err := getTokenClaims(r)
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil {
|
2021-09-04 10:11:04 +00:00
|
|
|
renderNotFoundPage(w, r, nil)
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
renderClientTwoFactorPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := claims.Username
|
|
|
|
passcode := r.Form.Get("passcode")
|
|
|
|
if username == "" || passcode == "" {
|
|
|
|
renderClientTwoFactorPage(w, "Invalid credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderClientTwoFactorPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := dataprovider.UserExists(username)
|
|
|
|
if err != nil {
|
|
|
|
renderClientTwoFactorPage(w, "Invalid credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !user.Filters.TOTPConfig.Enabled || !util.IsStringInSlice(common.ProtocolHTTP, user.Filters.TOTPConfig.Protocols) {
|
|
|
|
renderClientTwoFactorPage(w, "Two factory authentication is not enabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = user.Filters.TOTPConfig.Secret.Decrypt()
|
|
|
|
if err != nil {
|
|
|
|
renderClientInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
match, err := mfa.ValidateTOTPPasscode(user.Filters.TOTPConfig.ConfigName, passcode,
|
|
|
|
user.Filters.TOTPConfig.Secret.GetPayload())
|
|
|
|
if !match || err != nil {
|
|
|
|
renderClientTwoFactorPage(w, "Invalid authentication code")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolHTTP, xid.New().String())
|
|
|
|
s.loginUser(w, r, &user, connectionID, util.GetIPFromRemoteAddress(r.RemoteAddr), true, renderClientTwoFactorPage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAdminTwoFactorRecoveryPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil {
|
|
|
|
renderNotFoundPage(w, r, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
renderTwoFactorRecoveryPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := claims.Username
|
|
|
|
recoveryCode := r.Form.Get("recovery_code")
|
|
|
|
if username == "" || recoveryCode == "" {
|
|
|
|
renderTwoFactorRecoveryPage(w, "Invalid credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderTwoFactorRecoveryPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(username)
|
|
|
|
if err != nil {
|
|
|
|
renderTwoFactorRecoveryPage(w, "Invalid credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !admin.Filters.TOTPConfig.Enabled {
|
|
|
|
renderTwoFactorRecoveryPage(w, "Two factory authentication is not enabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for idx, code := range admin.Filters.RecoveryCodes {
|
|
|
|
if err := code.Secret.Decrypt(); err != nil {
|
|
|
|
renderInternalServerErrorPage(w, r, fmt.Errorf("unable to decrypt recovery code: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if code.Secret.GetPayload() == recoveryCode {
|
|
|
|
if code.Used {
|
|
|
|
renderTwoFactorRecoveryPage(w, "This recovery code was already used")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin.Filters.RecoveryCodes[idx].Used = true
|
2021-10-10 11:08:05 +00:00
|
|
|
err = dataprovider.UpdateAdmin(&admin, dataprovider.ActionExecutorSelf, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-09-04 10:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "unable to set the recovery code %#v as used: %v", recoveryCode, err)
|
|
|
|
renderInternalServerErrorPage(w, r, errors.New("unable to set the recovery code as used"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.loginAdmin(w, r, &admin, true, renderTwoFactorRecoveryPage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
renderTwoFactorRecoveryPage(w, "Invalid recovery code")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAdminTwoFactorPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil {
|
|
|
|
renderNotFoundPage(w, r, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
renderTwoFactorPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := claims.Username
|
|
|
|
passcode := r.Form.Get("passcode")
|
|
|
|
if username == "" || passcode == "" {
|
|
|
|
renderTwoFactorPage(w, "Invalid credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderTwoFactorPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(username)
|
|
|
|
if err != nil {
|
|
|
|
renderTwoFactorPage(w, "Invalid credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !admin.Filters.TOTPConfig.Enabled {
|
|
|
|
renderTwoFactorPage(w, "Two factory authentication is not enabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = admin.Filters.TOTPConfig.Secret.Decrypt()
|
|
|
|
if err != nil {
|
|
|
|
renderInternalServerErrorPage(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
match, err := mfa.ValidateTOTPPasscode(admin.Filters.TOTPConfig.ConfigName, passcode,
|
|
|
|
admin.Filters.TOTPConfig.Secret.GetPayload())
|
|
|
|
if !match || err != nil {
|
|
|
|
renderTwoFactorPage(w, "Invalid authentication code")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.loginAdmin(w, r, &admin, true, renderTwoFactorPage)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAdminLoginPost(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderAdminLoginPage(w, err.Error())
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
username := r.Form.Get("username")
|
|
|
|
password := r.Form.Get("password")
|
|
|
|
if username == "" || password == "" {
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderAdminLoginPage(w, "Invalid credentials")
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderAdminLoginPage(w, err.Error())
|
2021-02-03 07:55:28 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
admin, err := dataprovider.CheckAdminAndPass(username, password, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
2021-07-27 16:43:00 +00:00
|
|
|
s.renderAdminLoginPage(w, err.Error())
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
s.loginAdmin(w, r, &admin, false, s.renderAdminLoginPage)
|
2021-05-16 19:36:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 16:43:00 +00:00
|
|
|
func (s *httpdServer) renderAdminLoginPage(w http.ResponseWriter, error string) {
|
|
|
|
data := loginPage{
|
2022-02-13 13:30:20 +00:00
|
|
|
CurrentURL: webAdminLoginPath,
|
2021-07-27 16:43:00 +00:00
|
|
|
Version: version.Get().Version,
|
|
|
|
Error: error,
|
|
|
|
CSRFToken: createCSRFToken(),
|
|
|
|
StaticURL: webStaticFilesPath,
|
|
|
|
}
|
|
|
|
if s.binding.showClientLoginURL() {
|
|
|
|
data.AltLoginURL = webClientLoginPath
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
if smtp.IsEnabled() {
|
|
|
|
data.ForgotPwdURL = webAdminForgotPwdPath
|
|
|
|
}
|
2022-02-13 13:30:20 +00:00
|
|
|
if s.binding.OIDC.hasRoles() {
|
|
|
|
data.OpenIDLoginURL = webAdminOIDCLoginPath
|
|
|
|
}
|
2021-07-27 16:43:00 +00:00
|
|
|
renderAdminTemplate(w, templateLogin, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAdminLogin(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-07-27 16:43:00 +00:00
|
|
|
if !dataprovider.HasAdmin() {
|
|
|
|
http.Redirect(w, r, webAdminSetupPath, http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2022-02-13 13:30:20 +00:00
|
|
|
s.renderAdminLoginPage(w, getFlashMessage(w, r))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAdminLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
c := jwtTokenClaims{}
|
|
|
|
c.removeCookie(w, r, webBaseAdminPath)
|
|
|
|
s.logoutOIDCUser(w, r)
|
|
|
|
|
|
|
|
http.Redirect(w, r, webAdminLoginPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) handleWebAdminChangePwdPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
renderChangePasswordPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = doChangeAdminPassword(r, r.Form.Get("current_password"), r.Form.Get("new_password1"),
|
|
|
|
r.Form.Get("new_password2"))
|
|
|
|
if err != nil {
|
|
|
|
renderChangePasswordPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.handleWebAdminLogout(w, r)
|
2021-07-27 16:43:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:25:43 +00:00
|
|
|
func (s *httpdServer) handleWebAdminPasswordResetPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
renderResetPwdPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin, _, err := handleResetPassword(r, r.Form.Get("code"), r.Form.Get("password"), true)
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*util.ValidationError); ok {
|
|
|
|
renderResetPwdPage(w, e.GetErrorString())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
renderResetPwdPage(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.loginAdmin(w, r, admin, false, renderResetPwdPage)
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:36:57 +00:00
|
|
|
func (s *httpdServer) handleWebAdminSetupPost(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-05-16 19:36:57 +00:00
|
|
|
if dataprovider.HasAdmin() {
|
|
|
|
renderBadRequestPage(w, r, errors.New("an admin user already exists"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
renderAdminSetupPage(w, r, "", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCSRFToken(r.Form.Get(csrfFormToken)); err != nil {
|
|
|
|
renderForbiddenPage(w, r, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := r.Form.Get("username")
|
|
|
|
password := r.Form.Get("password")
|
|
|
|
confirmPassword := r.Form.Get("confirm_password")
|
|
|
|
if username == "" {
|
|
|
|
renderAdminSetupPage(w, r, username, "Please set a username")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if password == "" {
|
|
|
|
renderAdminSetupPage(w, r, username, "Please set a password")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if password != confirmPassword {
|
|
|
|
renderAdminSetupPage(w, r, username, "Passwords mismatch")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
Status: 1,
|
|
|
|
Permissions: []string{dataprovider.PermAdminAny},
|
|
|
|
}
|
2021-10-10 11:08:05 +00:00
|
|
|
err = dataprovider.AddAdmin(&admin, username, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-05-16 19:36:57 +00:00
|
|
|
if err != nil {
|
|
|
|
renderAdminSetupPage(w, r, username, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
s.loginAdmin(w, r, &admin, false, nil)
|
2021-05-16 19:36:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func (s *httpdServer) loginUser(
|
|
|
|
w http.ResponseWriter, r *http.Request, user *dataprovider.User, connectionID, ipAddr string,
|
|
|
|
isSecondFactorAuth bool, errorFunc func(w http.ResponseWriter, error string),
|
|
|
|
) {
|
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: user.Username,
|
|
|
|
Permissions: user.Filters.WebClient,
|
|
|
|
Signature: user.GetSignature(),
|
|
|
|
}
|
|
|
|
|
|
|
|
audience := tokenAudienceWebClient
|
|
|
|
if user.Filters.TOTPConfig.Enabled && util.IsStringInSlice(common.ProtocolHTTP, user.Filters.TOTPConfig.Protocols) &&
|
|
|
|
user.CanManageMFA() && !isSecondFactorAuth {
|
|
|
|
audience = tokenAudienceWebClientPartial
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.createAndSetCookie(w, r, s.tokenAuth, audience)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, connectionID, "unable to set user login cookie %v", err)
|
|
|
|
updateLoginMetrics(user, ipAddr, common.ErrInternalFailure)
|
|
|
|
errorFunc(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if isSecondFactorAuth {
|
|
|
|
invalidateToken(r)
|
|
|
|
}
|
|
|
|
if audience == tokenAudienceWebClientPartial {
|
|
|
|
http.Redirect(w, r, webClientTwoFactorPath, http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateLoginMetrics(user, ipAddr, err)
|
|
|
|
dataprovider.UpdateLastLogin(user)
|
|
|
|
http.Redirect(w, r, webClientFilesPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) loginAdmin(
|
|
|
|
w http.ResponseWriter, r *http.Request, admin *dataprovider.Admin,
|
|
|
|
isSecondFactorAuth bool, errorFunc func(w http.ResponseWriter, error string),
|
|
|
|
) {
|
2021-01-17 21:29:08 +00:00
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: admin.Username,
|
|
|
|
Permissions: admin.Permissions,
|
|
|
|
Signature: admin.GetSignature(),
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
audience := tokenAudienceWebAdmin
|
|
|
|
if admin.Filters.TOTPConfig.Enabled && admin.CanManageMFA() && !isSecondFactorAuth {
|
|
|
|
audience = tokenAudienceWebAdminPartial
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.createAndSetCookie(w, r, s.tokenAuth, audience)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
2021-05-16 07:13:00 +00:00
|
|
|
logger.Warn(logSender, "", "unable to set admin login cookie %v", err)
|
2021-09-04 10:11:04 +00:00
|
|
|
if errorFunc == nil {
|
|
|
|
renderAdminSetupPage(w, r, admin.Username, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errorFunc(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if isSecondFactorAuth {
|
|
|
|
invalidateToken(r)
|
|
|
|
}
|
|
|
|
if audience == tokenAudienceWebAdminPartial {
|
|
|
|
http.Redirect(w, r, webAdminTwoFactorPath, http.StatusFound)
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-19 13:51:43 +00:00
|
|
|
dataprovider.UpdateAdminLastLogin(admin)
|
2021-09-04 10:11:04 +00:00
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusFound)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 21:35:36 +00:00
|
|
|
func (s *httpdServer) logout(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-01-26 21:35:36 +00:00
|
|
|
invalidateToken(r)
|
|
|
|
sendAPIResponse(w, r, nil, "Your token has been invalidated", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
func (s *httpdServer) getUserToken(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2021-07-11 13:26:51 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
2021-06-05 14:07:09 +00:00
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
2021-07-11 13:26:51 +00:00
|
|
|
updateLoginMetrics(&dataprovider.User{BaseUser: sdk.BaseUser{Username: username}}, ipAddr, common.ErrNoCredentials)
|
2021-06-05 14:07:09 +00:00
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
sendAPIResponse(w, r, nil, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if username == "" || password == "" {
|
2021-07-11 13:26:51 +00:00
|
|
|
updateLoginMetrics(&dataprovider.User{BaseUser: sdk.BaseUser{Username: username}}, ipAddr, common.ErrNoCredentials)
|
2021-06-05 14:07:09 +00:00
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
sendAPIResponse(w, r, nil, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := common.Config.ExecutePostConnectHook(ipAddr, common.ProtocolHTTP); err != nil {
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := dataprovider.CheckUserAndPass(username, password, ipAddr, common.ProtocolHTTP)
|
|
|
|
if err != nil {
|
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
updateLoginMetrics(&user, ipAddr, err)
|
|
|
|
sendAPIResponse(w, r, dataprovider.ErrInvalidCredentials, http.StatusText(http.StatusUnauthorized),
|
|
|
|
http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolHTTP, xid.New().String())
|
|
|
|
if err := checkHTTPClientUser(&user, r, connectionID); err != nil {
|
|
|
|
updateLoginMetrics(&user, ipAddr, err)
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
if user.Filters.TOTPConfig.Enabled && util.IsStringInSlice(common.ProtocolHTTP, user.Filters.TOTPConfig.Protocols) {
|
|
|
|
passcode := r.Header.Get(otpHeaderCode)
|
|
|
|
if passcode == "" {
|
|
|
|
logger.Debug(logSender, "", "TOTP enabled for user %#v and not passcode provided, authentication refused", user.Username)
|
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
updateLoginMetrics(&user, ipAddr, dataprovider.ErrInvalidCredentials)
|
|
|
|
sendAPIResponse(w, r, dataprovider.ErrInvalidCredentials, http.StatusText(http.StatusUnauthorized),
|
|
|
|
http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = user.Filters.TOTPConfig.Secret.Decrypt()
|
|
|
|
if err != nil {
|
|
|
|
updateLoginMetrics(&user, ipAddr, common.ErrInternalFailure)
|
|
|
|
sendAPIResponse(w, r, fmt.Errorf("unable to decrypt TOTP secret: %w", err), http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
match, err := mfa.ValidateTOTPPasscode(user.Filters.TOTPConfig.ConfigName, passcode,
|
|
|
|
user.Filters.TOTPConfig.Secret.GetPayload())
|
|
|
|
if !match || err != nil {
|
|
|
|
logger.Debug(logSender, "invalid passcode for user %#v, match? %v, err: %v", user.Username, match, err)
|
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
updateLoginMetrics(&user, ipAddr, dataprovider.ErrInvalidCredentials)
|
|
|
|
sendAPIResponse(w, r, dataprovider.ErrInvalidCredentials, http.StatusText(http.StatusUnauthorized),
|
|
|
|
http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
defer user.CloseFs() //nolint:errcheck
|
|
|
|
err = user.CheckFsRoot(connectionID)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, connectionID, "unable to check fs root: %v", err)
|
|
|
|
updateLoginMetrics(&user, ipAddr, common.ErrInternalFailure)
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.generateAndSendUserToken(w, r, ipAddr, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) generateAndSendUserToken(w http.ResponseWriter, r *http.Request, ipAddr string, user dataprovider.User) {
|
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: user.Username,
|
|
|
|
Permissions: user.Filters.WebClient,
|
|
|
|
Signature: user.GetSignature(),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.createTokenResponse(s.tokenAuth, tokenAudienceAPIUser)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
updateLoginMetrics(&user, ipAddr, common.ErrInternalFailure)
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateLoginMetrics(&user, ipAddr, err)
|
2021-08-19 13:51:43 +00:00
|
|
|
dataprovider.UpdateLastLogin(&user)
|
2021-06-05 14:07:09 +00:00
|
|
|
|
|
|
|
render.JSON(w, r, resp)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (s *httpdServer) getToken(w http.ResponseWriter, r *http.Request) {
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
sendAPIResponse(w, r, nil, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
admin, err := dataprovider.CheckAdminAndPass(username, password, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
if admin.Filters.TOTPConfig.Enabled {
|
|
|
|
passcode := r.Header.Get(otpHeaderCode)
|
|
|
|
if passcode == "" {
|
|
|
|
logger.Debug(logSender, "", "TOTP enabled for admin %#v and not passcode provided, authentication refused", admin.Username)
|
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
sendAPIResponse(w, r, dataprovider.ErrInvalidCredentials, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = admin.Filters.TOTPConfig.Secret.Decrypt()
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, fmt.Errorf("unable to decrypt TOTP secret: %w", err),
|
|
|
|
http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
match, err := mfa.ValidateTOTPPasscode(admin.Filters.TOTPConfig.ConfigName, passcode,
|
|
|
|
admin.Filters.TOTPConfig.Secret.GetPayload())
|
|
|
|
if !match || err != nil {
|
|
|
|
logger.Debug(logSender, "invalid passcode for admin %#v, match? %v, err: %v", admin.Username, match, err)
|
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, basicRealm)
|
|
|
|
sendAPIResponse(w, r, dataprovider.ErrInvalidCredentials, http.StatusText(http.StatusUnauthorized),
|
|
|
|
http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
s.generateAndSendToken(w, r, admin)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
func (s *httpdServer) generateAndSendToken(w http.ResponseWriter, r *http.Request, admin dataprovider.Admin) {
|
2021-01-17 21:29:08 +00:00
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: admin.Username,
|
|
|
|
Permissions: admin.Permissions,
|
|
|
|
Signature: admin.GetSignature(),
|
|
|
|
}
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
resp, err := c.createTokenResponse(s.tokenAuth, tokenAudienceAPI)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:51:43 +00:00
|
|
|
dataprovider.UpdateAdminLastLogin(&admin)
|
2021-01-17 21:29:08 +00:00
|
|
|
render.JSON(w, r, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) checkCookieExpiration(w http.ResponseWriter, r *http.Request) {
|
2022-02-13 13:30:20 +00:00
|
|
|
if _, ok := r.Context().Value(oidcTokenKey).(string); ok {
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
token, claims, err := jwtauth.FromContext(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tokenClaims := jwtTokenClaims{}
|
|
|
|
tokenClaims.Decode(claims)
|
|
|
|
if tokenClaims.Username == "" || tokenClaims.Signature == "" {
|
|
|
|
return
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
if time.Until(token.Expiration()) > tokenRefreshThreshold {
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice(tokenAudienceWebClient, token.Audience()) {
|
2021-05-06 19:35:43 +00:00
|
|
|
s.refreshClientToken(w, r, tokenClaims)
|
|
|
|
} else {
|
|
|
|
s.refreshAdminToken(w, r, tokenClaims)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) refreshClientToken(w http.ResponseWriter, r *http.Request, tokenClaims jwtTokenClaims) {
|
|
|
|
user, err := dataprovider.UserExists(tokenClaims.Username)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if user.GetSignature() != tokenClaims.Signature {
|
|
|
|
logger.Debug(logSender, "", "signature mismatch for user %#v, unable to refresh cookie", user.Username)
|
|
|
|
return
|
|
|
|
}
|
2021-06-05 14:07:09 +00:00
|
|
|
if err := checkHTTPClientUser(&user, r, xid.New().String()); err != nil {
|
2021-05-06 19:35:43 +00:00
|
|
|
logger.Debug(logSender, "", "unable to refresh cookie for user %#v: %v", user.Username, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-20 09:48:39 +00:00
|
|
|
tokenClaims.Permissions = user.Filters.WebClient
|
2021-05-06 19:35:43 +00:00
|
|
|
logger.Debug(logSender, "", "cookie refreshed for user %#v", user.Username)
|
|
|
|
tokenClaims.createAndSetCookie(w, r, s.tokenAuth, tokenAudienceWebClient) //nolint:errcheck
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) refreshAdminToken(w http.ResponseWriter, r *http.Request, tokenClaims jwtTokenClaims) {
|
2021-01-17 21:29:08 +00:00
|
|
|
admin, err := dataprovider.AdminExists(tokenClaims.Username)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if admin.Status != 1 {
|
|
|
|
logger.Debug(logSender, "", "admin %#v is disabled, unable to refresh cookie", admin.Username)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if admin.GetSignature() != tokenClaims.Signature {
|
|
|
|
logger.Debug(logSender, "", "signature mismatch for admin %#v, unable to refresh cookie", admin.Username)
|
|
|
|
return
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if !admin.CanLoginFromIP(util.GetIPFromRemoteAddress(r.RemoteAddr)) {
|
2021-01-17 21:29:08 +00:00
|
|
|
logger.Debug(logSender, "", "admin %#v cannot login from %v, unable to refresh cookie", admin.Username, r.RemoteAddr)
|
|
|
|
return
|
|
|
|
}
|
2021-11-20 09:48:39 +00:00
|
|
|
tokenClaims.Permissions = admin.Permissions
|
2021-01-17 21:29:08 +00:00
|
|
|
logger.Debug(logSender, "", "cookie refreshed for admin %#v", admin.Username)
|
2021-05-06 19:35:43 +00:00
|
|
|
tokenClaims.createAndSetCookie(w, r, s.tokenAuth, tokenAudienceWebAdmin) //nolint:errcheck
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) updateContextFromCookie(r *http.Request) *http.Request {
|
|
|
|
token, _, err := jwtauth.FromContext(r.Context())
|
|
|
|
if token == nil || err != nil {
|
2022-02-13 13:30:20 +00:00
|
|
|
_, err = r.Cookie(jwtCookieKey)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
token, err = jwtauth.VerifyRequest(s.tokenAuth, r, jwtauth.TokenFromCookie)
|
|
|
|
ctx := jwtauth.NewContext(r.Context(), token, err)
|
|
|
|
return r.WithContext(ctx)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
func (s *httpdServer) checkConnection(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-07-11 13:26:51 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
|
2021-05-11 04:54:06 +00:00
|
|
|
ip := net.ParseIP(ipAddr)
|
|
|
|
if ip != nil {
|
|
|
|
for _, allow := range s.binding.allowHeadersFrom {
|
|
|
|
if allow(ip) {
|
2021-07-11 13:26:51 +00:00
|
|
|
parsedIP := util.GetRealIP(r)
|
2021-05-11 04:54:06 +00:00
|
|
|
if parsedIP != "" {
|
|
|
|
ipAddr = parsedIP
|
|
|
|
r.RemoteAddr = ipAddr
|
|
|
|
}
|
|
|
|
if forwardedProto := r.Header.Get(xForwardedProto); forwardedProto != "" {
|
|
|
|
ctx := context.WithValue(r.Context(), forwardedProtoKey, forwardedProto)
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
common.Connections.AddClientConnection(ipAddr)
|
|
|
|
defer common.Connections.RemoveClientConnection(ipAddr)
|
|
|
|
|
|
|
|
if !common.Connections.IsNewConnectionAllowed(ipAddr) {
|
|
|
|
logger.Log(logger.LevelDebug, common.ProtocolHTTP, "", "connection refused, configured limit reached")
|
|
|
|
s.sendForbiddenResponse(w, r, "configured connections limit reached")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if common.IsBanned(ipAddr) {
|
|
|
|
s.sendForbiddenResponse(w, r, "your IP address is banned")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if delay, err := common.LimitRate(common.ProtocolHTTP, ipAddr); err != nil {
|
|
|
|
delay += 499999999 * time.Nanosecond
|
|
|
|
w.Header().Set("Retry-After", fmt.Sprintf("%.0f", delay.Seconds()))
|
|
|
|
w.Header().Set("X-Retry-In", delay.String())
|
|
|
|
s.sendTooManyRequestResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) sendTooManyRequestResponse(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
if (s.enableWebAdmin || s.enableWebClient) && isWebRequest(r) {
|
|
|
|
r = s.updateContextFromCookie(r)
|
|
|
|
if s.enableWebClient && (isWebClientRequest(r) || !s.enableWebAdmin) {
|
|
|
|
renderClientMessagePage(w, r, http.StatusText(http.StatusTooManyRequests), "Rate limit exceeded",
|
|
|
|
http.StatusTooManyRequests, err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
renderMessagePage(w, r, http.StatusText(http.StatusTooManyRequests), "Rate limit exceeded", http.StatusTooManyRequests,
|
|
|
|
err, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) sendForbiddenResponse(w http.ResponseWriter, r *http.Request, message string) {
|
|
|
|
if (s.enableWebAdmin || s.enableWebClient) && isWebRequest(r) {
|
|
|
|
r = s.updateContextFromCookie(r)
|
|
|
|
if s.enableWebClient && (isWebClientRequest(r) || !s.enableWebAdmin) {
|
|
|
|
renderClientForbiddenPage(w, r, message)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
renderForbiddenPage(w, r, message)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, errors.New(message), message, http.StatusForbidden)
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:21:15 +00:00
|
|
|
func (s *httpdServer) redirectToWebPath(w http.ResponseWriter, r *http.Request, webPath string) {
|
|
|
|
if dataprovider.HasAdmin() {
|
2022-01-26 19:46:33 +00:00
|
|
|
http.Redirect(w, r, webPath, http.StatusFound)
|
2021-05-14 17:21:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.enableWebAdmin {
|
|
|
|
http.Redirect(w, r, webAdminSetupPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:32:51 +00:00
|
|
|
func (s *httpdServer) isStaticFileURL(r *http.Request) bool {
|
|
|
|
var urlPath string
|
|
|
|
rctx := chi.RouteContext(r.Context())
|
|
|
|
if rctx != nil && rctx.RoutePath != "" {
|
|
|
|
urlPath = rctx.RoutePath
|
|
|
|
} else {
|
|
|
|
urlPath = r.URL.Path
|
|
|
|
}
|
|
|
|
return !strings.HasPrefix(urlPath, webOpenAPIPath) && !strings.HasPrefix(urlPath, webStaticFilesPath)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (s *httpdServer) initializeRouter() {
|
2021-07-01 18:17:40 +00:00
|
|
|
s.tokenAuth = jwtauth.New(jwa.HS256.String(), getSigningKey(s.signingPassphrase), nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
s.router = chi.NewRouter()
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Use(middleware.RequestID)
|
2021-09-19 06:14:59 +00:00
|
|
|
s.router.Use(s.checkConnection)
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Use(logger.NewStructuredLogger(logger.GetLogger()))
|
2022-01-04 08:48:16 +00:00
|
|
|
s.router.Use(middleware.Recoverer)
|
2021-11-13 22:14:50 +00:00
|
|
|
if s.cors.Enabled {
|
|
|
|
c := cors.New(cors.Options{
|
|
|
|
AllowedOrigins: s.cors.AllowedOrigins,
|
|
|
|
AllowedMethods: s.cors.AllowedMethods,
|
|
|
|
AllowedHeaders: s.cors.AllowedHeaders,
|
|
|
|
ExposedHeaders: s.cors.ExposedHeaders,
|
|
|
|
MaxAge: s.cors.MaxAge,
|
|
|
|
AllowCredentials: s.cors.AllowCredentials,
|
|
|
|
})
|
|
|
|
s.router.Use(c.Handler)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
s.router.Use(middleware.GetHead)
|
2021-11-21 08:32:51 +00:00
|
|
|
// StripSlashes causes infinite redirects at the root path if used with http.FileServer
|
|
|
|
s.router.Use(middleware.Maybe(middleware.StripSlashes, s.isStaticFileURL))
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.NotFound(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-11 04:54:06 +00:00
|
|
|
if (s.enableWebAdmin || s.enableWebClient) && isWebRequest(r) {
|
|
|
|
r = s.updateContextFromCookie(r)
|
|
|
|
if s.enableWebClient && (isWebClientRequest(r) || !s.enableWebAdmin) {
|
|
|
|
renderClientNotFoundPage(w, r, nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-11 04:54:06 +00:00
|
|
|
renderNotFoundPage(w, r, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, nil, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
}))
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Get(healthzPath, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.PlainText(w, r, "ok")
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
// share API exposed to external users
|
|
|
|
s.router.Get(sharesPath+"/{id}", downloadFromShare)
|
2021-12-08 18:25:22 +00:00
|
|
|
s.router.Post(sharesPath+"/{id}", uploadFilesToShare)
|
|
|
|
s.router.Post(sharesPath+"/{id}/{name}", uploadFileToShare)
|
2022-02-08 11:43:08 +00:00
|
|
|
s.router.With(compressor.Handler).Get(sharesPath+"/{id}/dirs", readBrowsableShareContents)
|
2022-02-06 15:46:43 +00:00
|
|
|
s.router.Get(sharesPath+"/{id}/files", downloadBrowsableSharedFile)
|
2021-11-06 13:13:20 +00:00
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Get(tokenPath, s.getToken)
|
2021-11-13 12:25:43 +00:00
|
|
|
s.router.Post(adminPath+"/{username}/forgot-password", forgotAdminPassword)
|
|
|
|
s.router.Post(adminPath+"/{username}/reset-password", resetAdminPassword)
|
|
|
|
s.router.Post(userPath+"/{username}/forgot-password", forgotUserPassword)
|
|
|
|
s.router.Post(userPath+"/{username}/reset-password", resetUserPassword)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Group(func(router chi.Router) {
|
2021-08-17 16:08:32 +00:00
|
|
|
router.Use(checkAPIKeyAuth(s.tokenAuth, dataprovider.APIKeyScopeAdmin))
|
2021-05-11 04:54:06 +00:00
|
|
|
router.Use(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromHeader))
|
|
|
|
router.Use(jwtAuthenticatorAPI)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
router.Get(versionPath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-11 04:54:06 +00:00
|
|
|
render.JSON(w, r, version.Get())
|
2021-01-17 21:29:08 +00:00
|
|
|
})
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication).Get(logoutPath, s.logout)
|
2021-09-29 16:46:15 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication).Get(adminProfilePath, getAdminProfile)
|
|
|
|
router.With(forbidAPIKeyAuthentication).Put(adminProfilePath, updateAdminProfile)
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication).Put(adminPwdPath, changeAdminPassword)
|
2021-06-05 14:07:09 +00:00
|
|
|
// compatibility layer to remove in v2.2
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication).Put(adminPwdCompatPath, changeAdminPassword)
|
2021-09-04 10:11:04 +00:00
|
|
|
// admin TOTP APIs
|
|
|
|
router.With(forbidAPIKeyAuthentication).Get(adminTOTPConfigsPath, getTOTPConfigs)
|
|
|
|
router.With(forbidAPIKeyAuthentication).Post(adminTOTPGeneratePath, generateTOTPSecret)
|
|
|
|
router.With(forbidAPIKeyAuthentication).Post(adminTOTPValidatePath, validateTOTPPasscode)
|
|
|
|
router.With(forbidAPIKeyAuthentication).Post(adminTOTPSavePath, saveTOTPConfig)
|
|
|
|
router.With(forbidAPIKeyAuthentication).Get(admin2FARecoveryCodesPath, getRecoveryCodes)
|
|
|
|
router.With(forbidAPIKeyAuthentication).Post(admin2FARecoveryCodesPath, generateRecoveryCodes)
|
2021-05-11 04:54:06 +00:00
|
|
|
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewServerStatus)).
|
|
|
|
Get(serverStatusPath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-11 04:54:06 +00:00
|
|
|
render.JSON(w, r, getServicesStatus())
|
2021-01-17 21:29:08 +00:00
|
|
|
})
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewConnections)).
|
|
|
|
Get(activeConnectionsPath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-11 04:54:06 +00:00
|
|
|
render.JSON(w, r, common.Connections.GetStats())
|
|
|
|
})
|
|
|
|
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminCloseConnections)).
|
|
|
|
Delete(activeConnectionsPath+"/{connectionID}", handleCloseConnection)
|
2021-06-07 19:52:43 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans)).Get(quotaScanPath, getUsersQuotaScans)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans)).Get(quotasBasePath+"/users/scans", getUsersQuotaScans)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans)).Post(quotaScanPath, startUserQuotaScanCompat)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans)).Post(quotasBasePath+"/users/{username}/scan", startUserQuotaScan)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans)).Get(quotaScanVFolderPath, getFoldersQuotaScans)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans)).Get(quotasBasePath+"/folders/scans", getFoldersQuotaScans)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans)).Post(quotaScanVFolderPath, startFolderQuotaScanCompat)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans)).Post(quotasBasePath+"/folders/{name}/scan", startFolderQuotaScan)
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewUsers)).Get(userPath, getUsers)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminAddUsers)).Post(userPath, addUser)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewUsers)).Get(userPath+"/{username}", getUserByUsername)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Put(userPath+"/{username}", updateUser)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminDeleteUsers)).Delete(userPath+"/{username}", deleteUser)
|
2021-09-04 10:11:04 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Put(userPath+"/{username}/2fa/disable", disableUser2FA)
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewUsers)).Get(folderPath, getFolders)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewUsers)).Get(folderPath+"/{name}", getFolderByName)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminAddUsers)).Post(folderPath, addFolder)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Put(folderPath+"/{name}", updateFolder)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminDeleteUsers)).Delete(folderPath+"/{name}", deleteFolder)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem)).Get(dumpDataPath, dumpData)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem)).Get(loadDataPath, loadData)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem)).Post(loadDataPath, loadDataFromRequest)
|
2021-06-07 19:52:43 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Put(updateUsedQuotaPath, updateUserQuotaUsageCompat)
|
2022-01-30 10:42:36 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Put(quotasBasePath+"/users/{username}/usage",
|
|
|
|
updateUserQuotaUsage)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Put(quotasBasePath+"/users/{username}/transfer-usage",
|
|
|
|
updateUserTransferQuotaUsage)
|
2021-06-07 19:52:43 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Put(updateFolderUsedQuotaPath, updateFolderQuotaUsageCompat)
|
2022-01-30 10:42:36 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Put(quotasBasePath+"/folders/{name}/usage",
|
|
|
|
updateFolderQuotaUsage)
|
2021-06-07 19:52:43 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewDefender)).Get(defenderHosts, getDefenderHosts)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewDefender)).Get(defenderHosts+"/{id}", getDefenderHostByID)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageDefender)).Delete(defenderHosts+"/{id}", deleteDefenderHostByID)
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewDefender)).Get(defenderBanTime, getBanTime)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewDefender)).Get(defenderScore, getScore)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageDefender)).Post(defenderUnban, unban)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins)).Get(adminPath, getAdmins)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins)).Post(adminPath, addAdmin)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins)).Get(adminPath+"/{username}", getAdminByUsername)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins)).Put(adminPath+"/{username}", updateAdmin)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins)).Delete(adminPath+"/{username}", deleteAdmin)
|
2021-09-04 10:11:04 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins)).Put(adminPath+"/{username}/2fa/disable", disableAdmin2FA)
|
2021-09-25 10:20:31 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminRetentionChecks)).Get(retentionChecksPath, getRetentionChecks)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminRetentionChecks)).Post(retentionBasePath+"/{username}/check",
|
|
|
|
startRetentionCheck)
|
2021-12-16 17:18:36 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminMetadataChecks)).Get(metadataChecksPath, getMetadataChecks)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminMetadataChecks)).Post(metadataBasePath+"/{username}/check",
|
|
|
|
startMetadataCheck)
|
2021-10-23 13:47:21 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewEvents), compressor.Handler).
|
|
|
|
Get(fsEventsPath, searchFsEvents)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewEvents), compressor.Handler).
|
|
|
|
Get(providerEventsPath, searchProviderEvents)
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication, checkPerm(dataprovider.PermAdminManageAPIKeys)).
|
|
|
|
Get(apiKeysPath, getAPIKeys)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkPerm(dataprovider.PermAdminManageAPIKeys)).
|
|
|
|
Post(apiKeysPath, addAPIKey)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkPerm(dataprovider.PermAdminManageAPIKeys)).
|
|
|
|
Get(apiKeysPath+"/{id}", getAPIKeyByID)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkPerm(dataprovider.PermAdminManageAPIKeys)).
|
|
|
|
Put(apiKeysPath+"/{id}", updateAPIKey)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkPerm(dataprovider.PermAdminManageAPIKeys)).
|
|
|
|
Delete(apiKeysPath+"/{id}", deleteAPIKey)
|
2021-05-11 04:54:06 +00:00
|
|
|
})
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
s.router.Get(userTokenPath, s.getUserToken)
|
|
|
|
|
|
|
|
s.router.Group(func(router chi.Router) {
|
2021-08-17 16:08:32 +00:00
|
|
|
router.Use(checkAPIKeyAuth(s.tokenAuth, dataprovider.APIKeyScopeUser))
|
2021-06-05 14:07:09 +00:00
|
|
|
router.Use(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromHeader))
|
|
|
|
router.Use(jwtAuthenticatorAPIUser)
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication).Get(userLogoutPath, s.logout)
|
2021-09-05 16:49:13 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientPasswordChangeDisabled)).
|
|
|
|
Put(userPwdPath, changeUserPassword)
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientPubKeyChangeDisabled)).
|
|
|
|
Get(userPublicKeysPath, getUserPublicKeys)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientPubKeyChangeDisabled)).
|
|
|
|
Put(userPublicKeysPath, setUserPublicKeys)
|
2021-09-29 16:46:15 +00:00
|
|
|
router.With(forbidAPIKeyAuthentication).Get(userProfilePath, getUserProfile)
|
|
|
|
router.With(forbidAPIKeyAuthentication).Put(userProfilePath, updateUserProfile)
|
2021-09-04 10:11:04 +00:00
|
|
|
// user TOTP APIs
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientMFADisabled)).
|
|
|
|
Get(userTOTPConfigsPath, getTOTPConfigs)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientMFADisabled)).
|
|
|
|
Post(userTOTPGeneratePath, generateTOTPSecret)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientMFADisabled)).
|
|
|
|
Post(userTOTPValidatePath, validateTOTPPasscode)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientMFADisabled)).
|
|
|
|
Post(userTOTPSavePath, saveTOTPConfig)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientMFADisabled)).
|
|
|
|
Get(user2FARecoveryCodesPath, getRecoveryCodes)
|
|
|
|
router.With(forbidAPIKeyAuthentication, checkHTTPUserPerm(sdk.WebClientMFADisabled)).
|
|
|
|
Post(user2FARecoveryCodesPath, generateRecoveryCodes)
|
|
|
|
|
2021-07-26 18:55:49 +00:00
|
|
|
// compatibility layer to remove in v2.3
|
|
|
|
router.With(compressor.Handler).Get(userFolderPath, readUserFolder)
|
2021-07-23 08:19:27 +00:00
|
|
|
router.Get(userFilePath, getUserFile)
|
2021-07-26 18:55:49 +00:00
|
|
|
|
|
|
|
router.With(compressor.Handler).Get(userDirsPath, readUserFolder)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled)).Post(userDirsPath, createUserDir)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled)).Patch(userDirsPath, renameUserDir)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled)).Delete(userDirsPath, deleteUserDir)
|
|
|
|
router.Get(userFilesPath, getUserFile)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled)).Post(userFilesPath, uploadUserFiles)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled)).Patch(userFilesPath, renameUserFile)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled)).Delete(userFilesPath, deleteUserFile)
|
2021-06-05 14:07:09 +00:00
|
|
|
router.Post(userStreamZipPath, getUserFilesAsZipStream)
|
2021-11-06 13:13:20 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled)).Get(userSharesPath, getShares)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled)).Post(userSharesPath, addShare)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled)).Get(userSharesPath+"/{id}", getShareByID)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled)).Put(userSharesPath+"/{id}", updateShare)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled)).Delete(userSharesPath+"/{id}", deleteShare)
|
2021-12-08 18:25:22 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled)).Post(userUploadFilePath, uploadUserFile)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled)).Patch(userFilesDirsMetadataPath, setFileDirMetadata)
|
2021-06-05 14:07:09 +00:00
|
|
|
})
|
|
|
|
|
2021-11-21 08:32:51 +00:00
|
|
|
if s.renderOpenAPI {
|
|
|
|
s.router.Group(func(router chi.Router) {
|
|
|
|
router.Use(compressor.Handler)
|
|
|
|
fileServer(router, webOpenAPIPath, http.Dir(s.openAPIPath))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
if s.enableWebAdmin || s.enableWebClient {
|
|
|
|
s.router.Group(func(router chi.Router) {
|
|
|
|
router.Use(compressor.Handler)
|
|
|
|
fileServer(router, webStaticFilesPath, http.Dir(s.staticFilesPath))
|
|
|
|
})
|
2022-02-13 13:30:20 +00:00
|
|
|
if s.binding.OIDC.isEnabled() {
|
|
|
|
s.router.Get(webOIDCRedirectPath, s.handleOIDCRedirect)
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
if s.enableWebClient {
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Get(webRootPath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-14 17:21:15 +00:00
|
|
|
s.redirectToWebPath(w, r, webClientLoginPath)
|
2021-05-06 19:35:43 +00:00
|
|
|
})
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Get(webBasePath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-05-14 17:21:15 +00:00
|
|
|
s.redirectToWebPath(w, r, webClientLoginPath)
|
2021-01-17 21:29:08 +00:00
|
|
|
})
|
2021-05-11 04:54:06 +00:00
|
|
|
} else {
|
|
|
|
s.router.Get(webRootPath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-02-13 13:30:20 +00:00
|
|
|
s.redirectToWebPath(w, r, webAdminLoginPath)
|
2021-05-06 19:35:43 +00:00
|
|
|
})
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Get(webBasePath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-02-13 13:30:20 +00:00
|
|
|
s.redirectToWebPath(w, r, webAdminLoginPath)
|
2021-01-17 21:29:08 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-11 04:54:06 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
s.setupWebClientRoutes()
|
|
|
|
s.setupWebAdminRoutes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpdServer) setupWebClientRoutes() {
|
2021-05-11 04:54:06 +00:00
|
|
|
if s.enableWebClient {
|
|
|
|
s.router.Get(webBaseClientPath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-01-26 19:46:33 +00:00
|
|
|
http.Redirect(w, r, webClientLoginPath, http.StatusFound)
|
2021-05-11 04:54:06 +00:00
|
|
|
})
|
2021-07-27 16:43:00 +00:00
|
|
|
s.router.Get(webClientLoginPath, s.handleClientWebLogin)
|
2022-02-13 13:30:20 +00:00
|
|
|
if s.binding.OIDC.isEnabled() {
|
|
|
|
s.router.Get(webClientOIDCLoginPath, s.handleWebClientOIDCLogin)
|
|
|
|
}
|
2021-05-11 04:54:06 +00:00
|
|
|
s.router.Post(webClientLoginPath, s.handleWebClientLoginPost)
|
2021-11-13 12:25:43 +00:00
|
|
|
s.router.Get(webClientForgotPwdPath, handleWebClientForgotPwd)
|
|
|
|
s.router.Post(webClientForgotPwdPath, handleWebClientForgotPwdPost)
|
|
|
|
s.router.Get(webClientResetPwdPath, handleWebClientPasswordReset)
|
|
|
|
s.router.Post(webClientResetPwdPath, s.handleWebClientPasswordResetPost)
|
2021-09-04 10:11:04 +00:00
|
|
|
s.router.With(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromCookie),
|
|
|
|
jwtAuthenticatorPartial(tokenAudienceWebClientPartial)).
|
|
|
|
Get(webClientTwoFactorPath, handleWebClientTwoFactor)
|
|
|
|
s.router.With(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromCookie),
|
|
|
|
jwtAuthenticatorPartial(tokenAudienceWebClientPartial)).
|
|
|
|
Post(webClientTwoFactorPath, s.handleWebClientTwoFactorPost)
|
|
|
|
s.router.With(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromCookie),
|
|
|
|
jwtAuthenticatorPartial(tokenAudienceWebClientPartial)).
|
|
|
|
Get(webClientTwoFactorRecoveryPath, handleWebClientTwoFactorRecovery)
|
|
|
|
s.router.With(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromCookie),
|
|
|
|
jwtAuthenticatorPartial(tokenAudienceWebClientPartial)).
|
|
|
|
Post(webClientTwoFactorRecoveryPath, s.handleWebClientTwoFactorRecoveryPost)
|
2021-11-06 13:13:20 +00:00
|
|
|
// share API exposed to external users
|
|
|
|
s.router.Get(webClientPubSharesPath+"/{id}", downloadFromShare)
|
2022-02-15 18:19:25 +00:00
|
|
|
s.router.Get(webClientPubSharesPath+"/{id}/browse", handleShareGetFiles)
|
|
|
|
s.router.Get(webClientPubSharesPath+"/{id}/upload", handleClientUploadToShare)
|
|
|
|
s.router.With(compressor.Handler).Get(webClientPubSharesPath+"/{id}/dirs", handleShareGetDirContents)
|
2021-12-08 18:25:22 +00:00
|
|
|
s.router.Post(webClientPubSharesPath+"/{id}", uploadFilesToShare)
|
|
|
|
s.router.Post(webClientPubSharesPath+"/{id}/{name}", uploadFileToShare)
|
2021-05-11 04:54:06 +00:00
|
|
|
|
|
|
|
s.router.Group(func(router chi.Router) {
|
2022-02-13 13:30:20 +00:00
|
|
|
if s.binding.OIDC.isEnabled() {
|
|
|
|
router.Use(s.oidcTokenAuthenticator(tokenAudienceWebClient))
|
|
|
|
}
|
|
|
|
router.Use(jwtauth.Verify(s.tokenAuth, tokenFromContext, jwtauth.TokenFromCookie))
|
2021-05-11 04:54:06 +00:00
|
|
|
router.Use(jwtAuthenticatorWebClient)
|
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
router.Get(webClientLogoutPath, s.handleWebClientLogout)
|
2021-12-03 17:33:08 +00:00
|
|
|
router.With(s.refreshCookie).Get(webClientFilesPath, s.handleClientGetFiles)
|
2021-11-25 18:24:32 +00:00
|
|
|
router.With(s.refreshCookie).Get(webClientViewPDFPath, handleClientViewPDF)
|
2021-12-08 18:25:22 +00:00
|
|
|
router.With(s.refreshCookie, verifyCSRFHeader).Get(webClientFilePath, getUserFile)
|
2021-07-26 18:55:49 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled), verifyCSRFHeader).
|
2021-12-08 18:25:22 +00:00
|
|
|
Post(webClientFilePath, uploadUserFile)
|
2021-11-25 18:24:32 +00:00
|
|
|
router.With(s.refreshCookie).Get(webClientEditFilePath, handleClientEditFile)
|
2021-07-26 18:55:49 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled), verifyCSRFHeader).
|
|
|
|
Patch(webClientFilesPath, renameUserFile)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled), verifyCSRFHeader).
|
|
|
|
Delete(webClientFilesPath, deleteUserFile)
|
2021-12-03 17:33:08 +00:00
|
|
|
router.With(compressor.Handler, s.refreshCookie).Get(webClientDirsPath, s.handleClientGetDirContents)
|
2021-07-26 18:55:49 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled), verifyCSRFHeader).
|
|
|
|
Post(webClientDirsPath, createUserDir)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled), verifyCSRFHeader).
|
|
|
|
Patch(webClientDirsPath, renameUserDir)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientWriteDisabled), verifyCSRFHeader).
|
|
|
|
Delete(webClientDirsPath, deleteUserDir)
|
2021-06-05 14:07:09 +00:00
|
|
|
router.With(s.refreshCookie).Get(webClientDownloadZipPath, handleWebClientDownloadZip)
|
2022-02-13 13:30:20 +00:00
|
|
|
router.With(s.refreshCookie, requireBuiltinLogin).Get(webClientProfilePath, handleClientGetProfile)
|
|
|
|
router.With(requireBuiltinLogin).Post(webClientProfilePath, handleWebClientProfilePost)
|
2021-09-29 16:46:15 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientPasswordChangeDisabled)).
|
|
|
|
Get(webChangeClientPwdPath, handleWebClientChangePwd)
|
2021-09-05 16:49:13 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientPasswordChangeDisabled)).
|
2022-02-13 13:30:20 +00:00
|
|
|
Post(webChangeClientPwdPath, s.handleWebClientChangePwdPost)
|
2021-09-04 10:11:04 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientMFADisabled), s.refreshCookie).
|
|
|
|
Get(webClientMFAPath, handleWebClientMFA)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientMFADisabled), verifyCSRFHeader).
|
|
|
|
Post(webClientTOTPGeneratePath, generateTOTPSecret)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientMFADisabled), verifyCSRFHeader).
|
|
|
|
Post(webClientTOTPValidatePath, validateTOTPPasscode)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientMFADisabled), verifyCSRFHeader).
|
|
|
|
Post(webClientTOTPSavePath, saveTOTPConfig)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientMFADisabled), verifyCSRFHeader, s.refreshCookie).
|
|
|
|
Get(webClientRecoveryCodesPath, getRecoveryCodes)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientMFADisabled), verifyCSRFHeader).
|
|
|
|
Post(webClientRecoveryCodesPath, generateRecoveryCodes)
|
2021-11-06 13:13:20 +00:00
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled), s.refreshCookie).
|
|
|
|
Get(webClientSharesPath, handleClientGetShares)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled), s.refreshCookie).
|
|
|
|
Get(webClientSharePath, handleClientAddShareGet)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled)).Post(webClientSharePath,
|
|
|
|
handleClientAddSharePost)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled), s.refreshCookie).
|
|
|
|
Get(webClientSharePath+"/{id}", handleClientUpdateShareGet)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled)).
|
|
|
|
Post(webClientSharePath+"/{id}", handleClientUpdateSharePost)
|
|
|
|
router.With(checkHTTPUserPerm(sdk.WebClientSharesDisabled), verifyCSRFHeader).
|
|
|
|
Delete(webClientSharePath+"/{id}", deleteShare)
|
2021-05-11 04:54:06 +00:00
|
|
|
})
|
|
|
|
}
|
2022-02-13 13:30:20 +00:00
|
|
|
}
|
2021-05-11 04:54:06 +00:00
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
func (s *httpdServer) setupWebAdminRoutes() {
|
2021-05-11 04:54:06 +00:00
|
|
|
if s.enableWebAdmin {
|
|
|
|
s.router.Get(webBaseAdminPath, func(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxLoginBodySize)
|
2022-02-13 13:30:20 +00:00
|
|
|
s.redirectToWebPath(w, r, webAdminLoginPath)
|
2021-05-11 04:54:06 +00:00
|
|
|
})
|
2022-02-13 13:30:20 +00:00
|
|
|
s.router.Get(webAdminLoginPath, s.handleWebAdminLogin)
|
|
|
|
if s.binding.OIDC.hasRoles() {
|
|
|
|
s.router.Get(webAdminOIDCLoginPath, s.handleWebAdminOIDCLogin)
|
|
|
|
}
|
|
|
|
s.router.Post(webAdminLoginPath, s.handleWebAdminLoginPost)
|
2021-05-14 17:21:15 +00:00
|
|
|
s.router.Get(webAdminSetupPath, handleWebAdminSetupGet)
|
2021-05-16 19:36:57 +00:00
|
|
|
s.router.Post(webAdminSetupPath, s.handleWebAdminSetupPost)
|
2021-11-13 12:25:43 +00:00
|
|
|
s.router.Get(webAdminForgotPwdPath, handleWebAdminForgotPwd)
|
|
|
|
s.router.Post(webAdminForgotPwdPath, handleWebAdminForgotPwdPost)
|
|
|
|
s.router.Get(webAdminResetPwdPath, handleWebAdminPasswordReset)
|
|
|
|
s.router.Post(webAdminResetPwdPath, s.handleWebAdminPasswordResetPost)
|
2021-09-04 10:11:04 +00:00
|
|
|
s.router.With(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromCookie),
|
|
|
|
jwtAuthenticatorPartial(tokenAudienceWebAdminPartial)).
|
|
|
|
Get(webAdminTwoFactorPath, handleWebAdminTwoFactor)
|
|
|
|
s.router.With(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromCookie),
|
|
|
|
jwtAuthenticatorPartial(tokenAudienceWebAdminPartial)).
|
|
|
|
Post(webAdminTwoFactorPath, s.handleWebAdminTwoFactorPost)
|
|
|
|
s.router.With(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromCookie),
|
|
|
|
jwtAuthenticatorPartial(tokenAudienceWebAdminPartial)).
|
|
|
|
Get(webAdminTwoFactorRecoveryPath, handleWebAdminTwoFactorRecovery)
|
|
|
|
s.router.With(jwtauth.Verify(s.tokenAuth, jwtauth.TokenFromCookie),
|
|
|
|
jwtAuthenticatorPartial(tokenAudienceWebAdminPartial)).
|
|
|
|
Post(webAdminTwoFactorRecoveryPath, s.handleWebAdminTwoFactorRecoveryPost)
|
2021-05-11 04:54:06 +00:00
|
|
|
|
|
|
|
s.router.Group(func(router chi.Router) {
|
2022-02-13 13:30:20 +00:00
|
|
|
if s.binding.OIDC.isEnabled() {
|
|
|
|
router.Use(s.oidcTokenAuthenticator(tokenAudienceWebAdmin))
|
|
|
|
}
|
|
|
|
router.Use(jwtauth.Verify(s.tokenAuth, tokenFromContext, jwtauth.TokenFromCookie))
|
2021-05-11 04:54:06 +00:00
|
|
|
router.Use(jwtAuthenticatorWebAdmin)
|
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
router.Get(webLogoutPath, s.handleWebAdminLogout)
|
|
|
|
router.With(s.refreshCookie, requireBuiltinLogin).Get(webAdminProfilePath, handleWebAdminProfile)
|
|
|
|
router.With(requireBuiltinLogin).Post(webAdminProfilePath, handleWebAdminProfilePost)
|
|
|
|
router.With(s.refreshCookie, requireBuiltinLogin).Get(webChangeAdminPwdPath, handleWebAdminChangePwd)
|
|
|
|
router.With(requireBuiltinLogin).Post(webChangeAdminPwdPath, s.handleWebAdminChangePwdPost)
|
|
|
|
|
|
|
|
router.With(s.refreshCookie, requireBuiltinLogin).Get(webAdminMFAPath, handleWebAdminMFA)
|
|
|
|
router.With(verifyCSRFHeader, requireBuiltinLogin).Post(webAdminTOTPGeneratePath, generateTOTPSecret)
|
|
|
|
router.With(verifyCSRFHeader, requireBuiltinLogin).Post(webAdminTOTPValidatePath, validateTOTPPasscode)
|
|
|
|
router.With(verifyCSRFHeader, requireBuiltinLogin).Post(webAdminTOTPSavePath, saveTOTPConfig)
|
|
|
|
router.With(verifyCSRFHeader, requireBuiltinLogin, s.refreshCookie).Get(webAdminRecoveryCodesPath, getRecoveryCodes)
|
|
|
|
router.With(verifyCSRFHeader, requireBuiltinLogin).Post(webAdminRecoveryCodesPath, generateRecoveryCodes)
|
2021-09-04 10:11:04 +00:00
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewUsers), s.refreshCookie).
|
|
|
|
Get(webUsersPath, handleGetWebUsers)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminAddUsers), s.refreshCookie).
|
|
|
|
Get(webUserPath, handleWebAddUserGet)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers), s.refreshCookie).
|
|
|
|
Get(webUserPath+"/{username}", handleWebUpdateUserGet)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminAddUsers)).Post(webUserPath, handleWebAddUserPost)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Post(webUserPath+"/{username}", handleWebUpdateUserPost)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewConnections), s.refreshCookie).
|
|
|
|
Get(webConnectionsPath, handleWebGetConnections)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewUsers), s.refreshCookie).
|
|
|
|
Get(webFoldersPath, handleWebGetFolders)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminAddUsers), s.refreshCookie).
|
|
|
|
Get(webFolderPath, handleWebAddFolderGet)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminAddUsers)).Post(webFolderPath, handleWebAddFolderPost)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewServerStatus), s.refreshCookie).
|
|
|
|
Get(webStatusPath, handleWebGetStatus)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins), s.refreshCookie).
|
|
|
|
Get(webAdminsPath, handleGetWebAdmins)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins), s.refreshCookie).
|
|
|
|
Get(webAdminPath, handleWebAddAdminGet)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins), s.refreshCookie).
|
|
|
|
Get(webAdminPath+"/{username}", handleWebUpdateAdminGet)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins)).Post(webAdminPath, handleWebAddAdminPost)
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins)).Post(webAdminPath+"/{username}",
|
|
|
|
handleWebUpdateAdminPost)
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageAdmins), verifyCSRFHeader).
|
|
|
|
Delete(webAdminPath+"/{username}", deleteAdmin)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminCloseConnections), verifyCSRFHeader).
|
|
|
|
Delete(webConnectionsPath+"/{connectionID}", handleCloseConnection)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers), s.refreshCookie).
|
|
|
|
Get(webFolderPath+"/{name}", handleWebUpdateFolderGet)
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminChangeUsers)).Post(webFolderPath+"/{name}",
|
|
|
|
handleWebUpdateFolderPost)
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminDeleteUsers), verifyCSRFHeader).
|
|
|
|
Delete(webFolderPath+"/{name}", deleteFolder)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans), verifyCSRFHeader).
|
2021-06-07 19:52:43 +00:00
|
|
|
Post(webScanVFolderPath+"/{name}", startFolderQuotaScan)
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminDeleteUsers), verifyCSRFHeader).
|
|
|
|
Delete(webUserPath+"/{username}", deleteUser)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminQuotaScans), verifyCSRFHeader).
|
2021-06-07 19:52:43 +00:00
|
|
|
Post(webQuotaScanPath+"/{username}", startUserQuotaScan)
|
2021-05-11 04:54:06 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem)).Get(webMaintenancePath, handleWebMaintenance)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem)).Get(webBackupPath, dumpData)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem)).Post(webRestorePath, handleWebRestore)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem), s.refreshCookie).
|
|
|
|
Get(webTemplateUser, handleWebTemplateUserGet)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem)).Post(webTemplateUser, handleWebTemplateUserPost)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem), s.refreshCookie).
|
|
|
|
Get(webTemplateFolder, handleWebTemplateFolderGet)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageSystem)).Post(webTemplateFolder, handleWebTemplateFolderPost)
|
2021-06-08 11:24:28 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewDefender)).Get(webDefenderPath, handleWebDefenderPage)
|
|
|
|
router.With(checkPerm(dataprovider.PermAdminViewDefender)).Get(webDefenderHostsPath, getDefenderHosts)
|
2021-08-17 16:08:32 +00:00
|
|
|
router.With(checkPerm(dataprovider.PermAdminManageDefender)).Delete(webDefenderHostsPath+"/{id}",
|
|
|
|
deleteDefenderHostByID)
|
2021-05-11 04:54:06 +00:00
|
|
|
})
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|