2019-10-07 16:19:01 +00:00
|
|
|
// Package httpd implements REST API and Web interface for SFTPGo.
|
|
|
|
// The OpenAPI 3 schema for the exposed API can be found inside the source tree:
|
2021-11-21 08:32:51 +00:00
|
|
|
// https://github.com/drakkan/sftpgo/blob/main/openapi/openapi.yaml
|
2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-07-01 18:17:40 +00:00
|
|
|
"crypto/sha256"
|
2019-10-07 16:19:01 +00:00
|
|
|
"fmt"
|
2021-05-11 04:54:06 +00:00
|
|
|
"net"
|
2019-10-07 16:19:01 +00:00
|
|
|
"net/http"
|
2021-04-09 20:02:48 +00:00
|
|
|
"path"
|
2019-10-07 16:19:01 +00:00
|
|
|
"path/filepath"
|
2020-12-29 18:02:56 +00:00
|
|
|
"runtime"
|
2021-01-17 21:29:08 +00:00
|
|
|
"strings"
|
2021-01-26 21:35:36 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-10-07 16:19:01 +00:00
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2021-03-05 17:50:45 +00:00
|
|
|
"github.com/go-chi/jwtauth/v5"
|
2021-04-11 06:38:43 +00:00
|
|
|
"github.com/lestrrat-go/jwx/jwa"
|
2020-05-06 17:36:34 +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/ftpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-09-04 10:11:04 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/mfa"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/sftpd"
|
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/webdavd"
|
2019-10-07 16:19:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-29 16:46:15 +00:00
|
|
|
logSender = "httpd"
|
|
|
|
tokenPath = "/api/v2/token"
|
|
|
|
logoutPath = "/api/v2/logout"
|
|
|
|
userTokenPath = "/api/v2/user/token"
|
|
|
|
userLogoutPath = "/api/v2/user/logout"
|
|
|
|
activeConnectionsPath = "/api/v2/connections"
|
|
|
|
quotasBasePath = "/api/v2/quotas"
|
|
|
|
quotaScanPath = "/api/v2/quota-scans"
|
|
|
|
quotaScanVFolderPath = "/api/v2/folder-quota-scans"
|
|
|
|
userPath = "/api/v2/users"
|
|
|
|
versionPath = "/api/v2/version"
|
|
|
|
folderPath = "/api/v2/folders"
|
|
|
|
serverStatusPath = "/api/v2/status"
|
|
|
|
dumpDataPath = "/api/v2/dumpdata"
|
|
|
|
loadDataPath = "/api/v2/loaddata"
|
|
|
|
updateUsedQuotaPath = "/api/v2/quota-update"
|
|
|
|
updateFolderUsedQuotaPath = "/api/v2/folder-quota-update"
|
|
|
|
defenderHosts = "/api/v2/defender/hosts"
|
|
|
|
defenderBanTime = "/api/v2/defender/bantime"
|
|
|
|
defenderUnban = "/api/v2/defender/unban"
|
|
|
|
defenderScore = "/api/v2/defender/score"
|
|
|
|
adminPath = "/api/v2/admins"
|
|
|
|
adminPwdPath = "/api/v2/admin/changepwd"
|
|
|
|
adminPwdCompatPath = "/api/v2/changepwd/admin"
|
|
|
|
adminProfilePath = "/api/v2/admin/profile"
|
|
|
|
userPwdPath = "/api/v2/user/changepwd"
|
|
|
|
userPublicKeysPath = "/api/v2/user/publickeys"
|
|
|
|
userFolderPath = "/api/v2/user/folder"
|
|
|
|
userDirsPath = "/api/v2/user/dirs"
|
|
|
|
userFilePath = "/api/v2/user/file"
|
|
|
|
userFilesPath = "/api/v2/user/files"
|
|
|
|
userStreamZipPath = "/api/v2/user/streamzip"
|
2021-12-08 18:25:22 +00:00
|
|
|
userUploadFilePath = "/api/v2/user/files/upload"
|
|
|
|
userFilesDirsMetadataPath = "/api/v2/user/files/metadata"
|
2021-09-29 16:46:15 +00:00
|
|
|
apiKeysPath = "/api/v2/apikeys"
|
|
|
|
adminTOTPConfigsPath = "/api/v2/admin/totp/configs"
|
|
|
|
adminTOTPGeneratePath = "/api/v2/admin/totp/generate"
|
|
|
|
adminTOTPValidatePath = "/api/v2/admin/totp/validate"
|
|
|
|
adminTOTPSavePath = "/api/v2/admin/totp/save"
|
|
|
|
admin2FARecoveryCodesPath = "/api/v2/admin/2fa/recoverycodes"
|
|
|
|
userTOTPConfigsPath = "/api/v2/user/totp/configs"
|
|
|
|
userTOTPGeneratePath = "/api/v2/user/totp/generate"
|
|
|
|
userTOTPValidatePath = "/api/v2/user/totp/validate"
|
|
|
|
userTOTPSavePath = "/api/v2/user/totp/save"
|
|
|
|
user2FARecoveryCodesPath = "/api/v2/user/2fa/recoverycodes"
|
|
|
|
userProfilePath = "/api/v2/user/profile"
|
2021-11-06 13:13:20 +00:00
|
|
|
userSharesPath = "/api/v2/user/shares"
|
2021-09-29 16:46:15 +00:00
|
|
|
retentionBasePath = "/api/v2/retention/users"
|
|
|
|
retentionChecksPath = "/api/v2/retention/users/checks"
|
2021-12-16 17:18:36 +00:00
|
|
|
metadataBasePath = "/api/v2/metadata/users"
|
|
|
|
metadataChecksPath = "/api/v2/metadata/users/checks"
|
2021-10-23 13:47:21 +00:00
|
|
|
fsEventsPath = "/api/v2/events/fs"
|
|
|
|
providerEventsPath = "/api/v2/events/provider"
|
2021-11-06 13:13:20 +00:00
|
|
|
sharesPath = "/api/v2/shares"
|
2021-09-29 16:46:15 +00:00
|
|
|
healthzPath = "/healthz"
|
|
|
|
webRootPathDefault = "/"
|
|
|
|
webBasePathDefault = "/web"
|
|
|
|
webBasePathAdminDefault = "/web/admin"
|
|
|
|
webBasePathClientDefault = "/web/client"
|
|
|
|
webAdminSetupPathDefault = "/web/admin/setup"
|
|
|
|
webLoginPathDefault = "/web/admin/login"
|
|
|
|
webAdminTwoFactorPathDefault = "/web/admin/twofactor"
|
|
|
|
webAdminTwoFactorRecoveryPathDefault = "/web/admin/twofactor-recovery"
|
|
|
|
webLogoutPathDefault = "/web/admin/logout"
|
|
|
|
webUsersPathDefault = "/web/admin/users"
|
|
|
|
webUserPathDefault = "/web/admin/user"
|
|
|
|
webConnectionsPathDefault = "/web/admin/connections"
|
|
|
|
webFoldersPathDefault = "/web/admin/folders"
|
|
|
|
webFolderPathDefault = "/web/admin/folder"
|
|
|
|
webStatusPathDefault = "/web/admin/status"
|
|
|
|
webAdminsPathDefault = "/web/admin/managers"
|
|
|
|
webAdminPathDefault = "/web/admin/manager"
|
|
|
|
webMaintenancePathDefault = "/web/admin/maintenance"
|
|
|
|
webBackupPathDefault = "/web/admin/backup"
|
|
|
|
webRestorePathDefault = "/web/admin/restore"
|
|
|
|
webScanVFolderPathDefault = "/web/admin/quotas/scanfolder"
|
|
|
|
webQuotaScanPathDefault = "/web/admin/quotas/scanuser"
|
|
|
|
webChangeAdminPwdPathDefault = "/web/admin/changepwd"
|
2021-11-13 12:25:43 +00:00
|
|
|
webAdminForgotPwdPathDefault = "/web/admin/forgot-password"
|
|
|
|
webAdminResetPwdPathDefault = "/web/admin/reset-password"
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminProfilePathDefault = "/web/admin/profile"
|
|
|
|
webAdminMFAPathDefault = "/web/admin/mfa"
|
|
|
|
webAdminTOTPGeneratePathDefault = "/web/admin/totp/generate"
|
|
|
|
webAdminTOTPValidatePathDefault = "/web/admin/totp/validate"
|
|
|
|
webAdminTOTPSavePathDefault = "/web/admin/totp/save"
|
|
|
|
webAdminRecoveryCodesPathDefault = "/web/admin/recoverycodes"
|
|
|
|
webTemplateUserDefault = "/web/admin/template/user"
|
|
|
|
webTemplateFolderDefault = "/web/admin/template/folder"
|
|
|
|
webDefenderPathDefault = "/web/admin/defender"
|
|
|
|
webDefenderHostsPathDefault = "/web/admin/defender/hosts"
|
|
|
|
webClientLoginPathDefault = "/web/client/login"
|
|
|
|
webClientTwoFactorPathDefault = "/web/client/twofactor"
|
|
|
|
webClientTwoFactorRecoveryPathDefault = "/web/client/twofactor-recovery"
|
|
|
|
webClientFilesPathDefault = "/web/client/files"
|
2021-12-08 18:25:22 +00:00
|
|
|
webClientFilePathDefault = "/web/client/file"
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientSharesPathDefault = "/web/client/shares"
|
|
|
|
webClientSharePathDefault = "/web/client/share"
|
2021-10-09 12:17:28 +00:00
|
|
|
webClientEditFilePathDefault = "/web/client/editfile"
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientDirsPathDefault = "/web/client/dirs"
|
|
|
|
webClientDownloadZipPathDefault = "/web/client/downloadzip"
|
|
|
|
webClientProfilePathDefault = "/web/client/profile"
|
|
|
|
webClientMFAPathDefault = "/web/client/mfa"
|
|
|
|
webClientTOTPGeneratePathDefault = "/web/client/totp/generate"
|
|
|
|
webClientTOTPValidatePathDefault = "/web/client/totp/validate"
|
|
|
|
webClientTOTPSavePathDefault = "/web/client/totp/save"
|
|
|
|
webClientRecoveryCodesPathDefault = "/web/client/recoverycodes"
|
|
|
|
webChangeClientPwdPathDefault = "/web/client/changepwd"
|
|
|
|
webClientLogoutPathDefault = "/web/client/logout"
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientPubSharesPathDefault = "/web/client/pubshares"
|
2021-11-13 12:25:43 +00:00
|
|
|
webClientForgotPwdPathDefault = "/web/client/forgot-password"
|
|
|
|
webClientResetPwdPathDefault = "/web/client/reset-password"
|
2021-11-25 18:24:32 +00:00
|
|
|
webClientViewPDFPathDefault = "/web/client/viewpdf"
|
2021-09-29 16:46:15 +00:00
|
|
|
webStaticFilesPathDefault = "/static"
|
2021-11-21 08:32:51 +00:00
|
|
|
webOpenAPIPathDefault = "/openapi"
|
2020-10-20 16:42:37 +00:00
|
|
|
// MaxRestoreSize defines the max size for the loaddata input file
|
2021-10-09 12:17:28 +00:00
|
|
|
MaxRestoreSize = 10485760 // 10 MB
|
|
|
|
maxRequestSize = 1048576 // 1MB
|
|
|
|
maxLoginBodySize = 262144 // 256 KB
|
2021-11-25 18:24:32 +00:00
|
|
|
httpdMaxEditFileSize = 1048576 // 1 MB
|
2021-12-01 16:53:19 +00:00
|
|
|
maxMultipartMem = 10485760 // 10 MB
|
2021-10-09 12:17:28 +00:00
|
|
|
osWindows = "windows"
|
|
|
|
otpHeaderCode = "X-SFTPGO-OTP"
|
2021-12-08 18:25:22 +00:00
|
|
|
mTimeHeader = "X-SFTPGO-MTIME"
|
2019-10-07 16:19:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-09-29 16:46:15 +00:00
|
|
|
backupsPath string
|
|
|
|
certMgr *common.CertManager
|
|
|
|
cleanupTicker *time.Ticker
|
|
|
|
cleanupDone chan bool
|
|
|
|
invalidatedJWTTokens sync.Map
|
|
|
|
csrfTokenAuth *jwtauth.JWTAuth
|
|
|
|
webRootPath string
|
|
|
|
webBasePath string
|
|
|
|
webBaseAdminPath string
|
|
|
|
webBaseClientPath string
|
|
|
|
webAdminSetupPath string
|
|
|
|
webLoginPath string
|
|
|
|
webAdminTwoFactorPath string
|
|
|
|
webAdminTwoFactorRecoveryPath string
|
|
|
|
webLogoutPath string
|
|
|
|
webUsersPath string
|
|
|
|
webUserPath string
|
|
|
|
webConnectionsPath string
|
|
|
|
webFoldersPath string
|
|
|
|
webFolderPath string
|
|
|
|
webStatusPath string
|
|
|
|
webAdminsPath string
|
|
|
|
webAdminPath string
|
|
|
|
webMaintenancePath string
|
|
|
|
webBackupPath string
|
|
|
|
webRestorePath string
|
|
|
|
webScanVFolderPath string
|
|
|
|
webQuotaScanPath string
|
|
|
|
webAdminProfilePath string
|
|
|
|
webAdminMFAPath string
|
|
|
|
webAdminTOTPGeneratePath string
|
|
|
|
webAdminTOTPValidatePath string
|
|
|
|
webAdminTOTPSavePath string
|
|
|
|
webAdminRecoveryCodesPath string
|
|
|
|
webChangeAdminPwdPath string
|
2021-11-13 12:25:43 +00:00
|
|
|
webAdminForgotPwdPath string
|
|
|
|
webAdminResetPwdPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webTemplateUser string
|
|
|
|
webTemplateFolder string
|
|
|
|
webDefenderPath string
|
|
|
|
webDefenderHostsPath string
|
|
|
|
webClientLoginPath string
|
|
|
|
webClientTwoFactorPath string
|
|
|
|
webClientTwoFactorRecoveryPath string
|
|
|
|
webClientFilesPath string
|
2021-12-08 18:25:22 +00:00
|
|
|
webClientFilePath string
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientSharesPath string
|
|
|
|
webClientSharePath string
|
2021-10-09 12:17:28 +00:00
|
|
|
webClientEditFilePath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientDirsPath string
|
|
|
|
webClientDownloadZipPath string
|
|
|
|
webClientProfilePath string
|
|
|
|
webChangeClientPwdPath string
|
|
|
|
webClientMFAPath string
|
|
|
|
webClientTOTPGeneratePath string
|
|
|
|
webClientTOTPValidatePath string
|
|
|
|
webClientTOTPSavePath string
|
|
|
|
webClientRecoveryCodesPath string
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientPubSharesPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientLogoutPath string
|
2021-11-13 12:25:43 +00:00
|
|
|
webClientForgotPwdPath string
|
|
|
|
webClientResetPwdPath string
|
2021-11-25 18:24:32 +00:00
|
|
|
webClientViewPDFPath string
|
2021-09-29 16:46:15 +00:00
|
|
|
webStaticFilesPath string
|
2021-11-21 08:32:51 +00:00
|
|
|
webOpenAPIPath string
|
2021-07-23 08:19:27 +00:00
|
|
|
// max upload size for http clients, 1GB by default
|
2022-02-27 13:27:53 +00:00
|
|
|
maxUploadFileSize = int64(1048576000)
|
|
|
|
installationCode string
|
|
|
|
installationCodeHint string
|
2019-10-07 16:19:01 +00:00
|
|
|
)
|
|
|
|
|
2021-04-09 20:02:48 +00:00
|
|
|
func init() {
|
|
|
|
updateWebAdminURLs("")
|
2021-05-06 19:35:43 +00:00
|
|
|
updateWebClientURLs("")
|
2021-04-09 20:02:48 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 17:33:08 +00:00
|
|
|
// WebClientIntegration defines the configuration for an external Web Client integration
|
|
|
|
type WebClientIntegration struct {
|
|
|
|
// Files with these extensions can be sent to the configured URL
|
|
|
|
FileExtensions []string `json:"file_extensions" mapstructure:"file_extensions"`
|
|
|
|
// URL that will receive the files
|
|
|
|
URL string `json:"url" mapstructure:"url"`
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// Binding defines the configuration for a network listener
|
|
|
|
type Binding struct {
|
|
|
|
// The address to listen on. A blank value means listen on all available network interfaces.
|
|
|
|
Address string `json:"address" mapstructure:"address"`
|
|
|
|
// The port used for serving requests
|
|
|
|
Port int `json:"port" mapstructure:"port"`
|
2021-01-19 17:59:41 +00:00
|
|
|
// Enable the built-in admin interface.
|
|
|
|
// You have to define TemplatesPath and StaticFilesPath for this to work
|
|
|
|
EnableWebAdmin bool `json:"enable_web_admin" mapstructure:"enable_web_admin"`
|
2021-05-06 19:35:43 +00:00
|
|
|
// Enable the built-in client interface.
|
|
|
|
// You have to define TemplatesPath and StaticFilesPath for this to work
|
|
|
|
EnableWebClient bool `json:"enable_web_client" mapstructure:"enable_web_client"`
|
2021-01-19 17:59:41 +00:00
|
|
|
// you also need to provide a certificate for enabling HTTPS
|
|
|
|
EnableHTTPS bool `json:"enable_https" mapstructure:"enable_https"`
|
|
|
|
// set to 1 to require client certificate authentication in addition to basic auth.
|
|
|
|
// You need to define at least a certificate authority for this to work
|
|
|
|
ClientAuthType int `json:"client_auth_type" mapstructure:"client_auth_type"`
|
2021-02-18 19:17:16 +00:00
|
|
|
// TLSCipherSuites is a list of supported cipher suites for TLS version 1.2.
|
|
|
|
// If CipherSuites is nil/empty, a default list of secure cipher suites
|
|
|
|
// is used, with a preference order based on hardware performance.
|
|
|
|
// Note that TLS 1.3 ciphersuites are not configurable.
|
|
|
|
// The supported ciphersuites names are defined here:
|
|
|
|
//
|
|
|
|
// https://github.com/golang/go/blob/master/src/crypto/tls/cipher_suites.go#L52
|
|
|
|
//
|
|
|
|
// any invalid name will be silently ignored.
|
|
|
|
// The order matters, the ciphers listed first will be the preferred ones.
|
|
|
|
TLSCipherSuites []string `json:"tls_cipher_suites" mapstructure:"tls_cipher_suites"`
|
2021-05-11 04:54:06 +00:00
|
|
|
// List of IP addresses and IP ranges allowed to set X-Forwarded-For, X-Real-IP,
|
|
|
|
// X-Forwarded-Proto headers.
|
2021-07-27 16:43:00 +00:00
|
|
|
ProxyAllowed []string `json:"proxy_allowed" mapstructure:"proxy_allowed"`
|
|
|
|
// If both web admin and web client are enabled each login page will show a link
|
|
|
|
// to the other one. This setting allows to hide this link:
|
|
|
|
// - 0 login links are displayed on both admin and client login page. This is the default
|
|
|
|
// - 1 the login link to the web client login page is hidden on admin login page
|
|
|
|
// - 2 the login link to the web admin login page is hidden on client login page
|
|
|
|
// The flags can be combined, for example 3 will disable both login links.
|
2021-11-21 08:32:51 +00:00
|
|
|
HideLoginURL int `json:"hide_login_url" mapstructure:"hide_login_url"`
|
|
|
|
// Enable the built-in OpenAPI renderer
|
2021-12-03 17:33:08 +00:00
|
|
|
RenderOpenAPI bool `json:"render_openapi" mapstructure:"render_openapi"`
|
|
|
|
// Enabling web client integrations you can render or modify the files with the specified
|
|
|
|
// extensions using an external tool.
|
|
|
|
WebClientIntegrations []WebClientIntegration `json:"web_client_integrations" mapstructure:"web_client_integrations"`
|
|
|
|
allowHeadersFrom []func(net.IP) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Binding) checkWebClientIntegrations() {
|
|
|
|
var integrations []WebClientIntegration
|
|
|
|
for _, integration := range b.WebClientIntegrations {
|
|
|
|
if integration.URL != "" && len(integration.FileExtensions) > 0 {
|
|
|
|
integrations = append(integrations, integration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.WebClientIntegrations = integrations
|
2021-05-11 04:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Binding) parseAllowedProxy() error {
|
2021-07-11 13:26:51 +00:00
|
|
|
allowedFuncs, err := util.ParseAllowedIPAndRanges(b.ProxyAllowed)
|
2021-05-11 04:54:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.allowHeadersFrom = allowedFuncs
|
|
|
|
return nil
|
2021-01-19 17:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddress returns the binding address
|
|
|
|
func (b *Binding) GetAddress() string {
|
|
|
|
return fmt.Sprintf("%s:%d", b.Address, b.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid returns true if the binding is valid
|
|
|
|
func (b *Binding) IsValid() bool {
|
|
|
|
if b.Port > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if filepath.IsAbs(b.Address) && runtime.GOOS != osWindows {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 16:43:00 +00:00
|
|
|
func (b *Binding) showAdminLoginURL() bool {
|
|
|
|
if !b.EnableWebAdmin {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if b.HideLoginURL&2 != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Binding) showClientLoginURL() bool {
|
|
|
|
if !b.EnableWebClient {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if b.HideLoginURL&1 != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-02 18:33:24 +00:00
|
|
|
type defenderStatus struct {
|
|
|
|
IsActive bool `json:"is_active"`
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
// ServicesStatus keep the state of the running services
|
|
|
|
type ServicesStatus struct {
|
|
|
|
SSH sftpd.ServiceStatus `json:"ssh"`
|
|
|
|
FTP ftpd.ServiceStatus `json:"ftp"`
|
|
|
|
WebDAV webdavd.ServiceStatus `json:"webdav"`
|
|
|
|
DataProvider dataprovider.ProviderStatus `json:"data_provider"`
|
2021-01-02 18:33:24 +00:00
|
|
|
Defender defenderStatus `json:"defender"`
|
2021-09-04 10:11:04 +00:00
|
|
|
MFA mfa.ServiceStatus `json:"mfa"`
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 13:27:53 +00:00
|
|
|
// SetupConfig defines the configuration parameters for the initial web admin setup
|
|
|
|
type SetupConfig struct {
|
|
|
|
// Installation code to require when creating the first admin account.
|
|
|
|
// As for the other configurations, this value is read at SFTPGo startup and not at runtime
|
|
|
|
// even if set using an environment variable.
|
|
|
|
// This is not a license key or similar, the purpose here is to prevent anyone who can access
|
|
|
|
// to the initial setup screen from creating an admin user
|
|
|
|
InstallationCode string `json:"installation_code" mapstructure:"installation_code"`
|
|
|
|
// Description for the installation code input field
|
|
|
|
InstallationCodeHint string `json:"installation_code_hint" mapstructure:"installation_code_hint"`
|
|
|
|
}
|
|
|
|
|
2021-11-13 22:14:50 +00:00
|
|
|
// CorsConfig defines the CORS configuration
|
|
|
|
type CorsConfig struct {
|
|
|
|
AllowedOrigins []string `json:"allowed_origins" mapstructure:"allowed_origins"`
|
|
|
|
AllowedMethods []string `json:"allowed_methods" mapstructure:"allowed_methods"`
|
|
|
|
AllowedHeaders []string `json:"allowed_headers" mapstructure:"allowed_headers"`
|
|
|
|
ExposedHeaders []string `json:"exposed_headers" mapstructure:"exposed_headers"`
|
|
|
|
AllowCredentials bool `json:"allow_credentials" mapstructure:"allow_credentials"`
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
|
|
|
MaxAge int `json:"max_age" mapstructure:"max_age"`
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
// Conf httpd daemon configuration
|
|
|
|
type Conf struct {
|
2021-01-19 17:59:41 +00:00
|
|
|
// Addresses and ports to bind to
|
|
|
|
Bindings []Binding `json:"bindings" mapstructure:"bindings"`
|
2019-10-07 16:19:01 +00:00
|
|
|
// Path to the HTML web templates. This can be an absolute path or a path relative to the config dir
|
|
|
|
TemplatesPath string `json:"templates_path" mapstructure:"templates_path"`
|
2020-06-18 21:53:38 +00:00
|
|
|
// Path to the static files for the web interface. This can be an absolute path or a path relative to the config dir.
|
|
|
|
// If both TemplatesPath and StaticFilesPath are empty the built-in web interface will be disabled
|
2019-10-07 16:19:01 +00:00
|
|
|
StaticFilesPath string `json:"static_files_path" mapstructure:"static_files_path"`
|
2019-12-27 22:12:44 +00:00
|
|
|
// Path to the backup directory. This can be an absolute path or a path relative to the config dir
|
|
|
|
BackupsPath string `json:"backups_path" mapstructure:"backups_path"`
|
2021-11-21 08:32:51 +00:00
|
|
|
// Path to the directory that contains the OpenAPI schema and the default renderer.
|
|
|
|
// This can be an absolute path or a path relative to the config dir
|
|
|
|
OpenAPIPath string `json:"openapi_path" mapstructure:"openapi_path"`
|
2021-05-06 19:35:43 +00:00
|
|
|
// Defines a base URL for the web admin and client interfaces. If empty web admin and client resources will
|
|
|
|
// be available at the root ("/") URI. If defined it must be an absolute URI or it will be ignored.
|
|
|
|
WebRoot string `json:"web_root" mapstructure:"web_root"`
|
2020-02-03 23:08:00 +00:00
|
|
|
// If files containing a certificate and matching private key for the server are provided the server will expect
|
2020-02-04 22:21:33 +00:00
|
|
|
// HTTPS connections.
|
|
|
|
// Certificate and key files can be reloaded on demand sending a "SIGHUP" signal on Unix based systems and a
|
|
|
|
// "paramchange" request to the running service on Windows.
|
2020-02-03 23:08:00 +00:00
|
|
|
CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
|
|
|
|
CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
|
2021-01-19 17:59:41 +00:00
|
|
|
// CACertificates defines the set of root certificate authorities to be used to verify client certificates.
|
|
|
|
CACertificates []string `json:"ca_certificates" mapstructure:"ca_certificates"`
|
|
|
|
// CARevocationLists defines a set a revocation lists, one for each root CA, to be used to check
|
|
|
|
// if a client certificate has been revoked
|
|
|
|
CARevocationLists []string `json:"ca_revocation_lists" mapstructure:"ca_revocation_lists"`
|
2021-07-01 18:17:40 +00:00
|
|
|
// SigningPassphrase defines the passphrase to use to derive the signing key for JWT and CSRF tokens.
|
|
|
|
// If empty a random signing key will be generated each time SFTPGo starts. If you set a
|
|
|
|
// signing passphrase you should consider rotating it periodically for added security
|
|
|
|
SigningPassphrase string `json:"signing_passphrase" mapstructure:"signing_passphrase"`
|
2021-07-23 08:19:27 +00:00
|
|
|
// MaxUploadFileSize Defines the maximum request body size, in bytes, for Web Client/API HTTP upload requests.
|
|
|
|
// 0 means no limit
|
|
|
|
MaxUploadFileSize int64 `json:"max_upload_file_size" mapstructure:"max_upload_file_size"`
|
2021-11-13 22:14:50 +00:00
|
|
|
// CORS configuration
|
|
|
|
Cors CorsConfig `json:"cors" mapstructure:"cors"`
|
2022-02-27 13:27:53 +00:00
|
|
|
// Initial setup configuration
|
|
|
|
Setup SetupConfig `json:"setup" mapstructure:"setup"`
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
type apiResponse struct {
|
2020-09-08 07:45:21 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Message string `json:"message"`
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
// ShouldBind returns true if there is at least a valid binding
|
|
|
|
func (c *Conf) ShouldBind() bool {
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if binding.IsValid() {
|
|
|
|
return true
|
|
|
|
}
|
2020-12-29 18:02:56 +00:00
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
|
2020-12-29 18:02:56 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func (c *Conf) isWebAdminEnabled() bool {
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if binding.EnableWebAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conf) isWebClientEnabled() bool {
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if binding.EnableWebClient {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
func (c *Conf) checkRequiredDirs(staticFilesPath, templatesPath string) error {
|
|
|
|
if (c.isWebAdminEnabled() || c.isWebClientEnabled()) && (staticFilesPath == "" || templatesPath == "") {
|
|
|
|
return fmt.Errorf("required directory is invalid, static file path: %#v template path: %#v",
|
|
|
|
staticFilesPath, templatesPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:17:40 +00:00
|
|
|
func (c *Conf) getRedacted() Conf {
|
|
|
|
conf := *c
|
2022-02-27 13:27:53 +00:00
|
|
|
redactedSecret := "[redacted]"
|
|
|
|
conf.SigningPassphrase = redactedSecret
|
|
|
|
conf.Setup.InstallationCode = redactedSecret
|
2021-07-01 18:17:40 +00:00
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:29:09 +00:00
|
|
|
// Initialize configures and starts the HTTP server
|
2021-01-19 17:59:41 +00:00
|
|
|
func (c *Conf) Initialize(configDir string) error {
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.Info(logSender, "", "initializing HTTP server with config %+v", c.getRedacted())
|
2020-02-03 23:08:00 +00:00
|
|
|
backupsPath = getConfigPath(c.BackupsPath, configDir)
|
|
|
|
staticFilesPath := getConfigPath(c.StaticFilesPath, configDir)
|
|
|
|
templatesPath := getConfigPath(c.TemplatesPath, configDir)
|
2021-11-21 08:32:51 +00:00
|
|
|
openAPIPath := getConfigPath(c.OpenAPIPath, configDir)
|
2021-01-05 08:50:22 +00:00
|
|
|
if backupsPath == "" {
|
2021-03-21 18:15:47 +00:00
|
|
|
return fmt.Errorf("required directory is invalid, backup path %#v", backupsPath)
|
2020-06-18 21:53:38 +00:00
|
|
|
}
|
2021-05-11 04:54:06 +00:00
|
|
|
if err := c.checkRequiredDirs(staticFilesPath, templatesPath); err != nil {
|
|
|
|
return err
|
2020-03-03 08:09:58 +00:00
|
|
|
}
|
2020-02-03 23:08:00 +00:00
|
|
|
certificateFile := getConfigPath(c.CertificateFile, configDir)
|
|
|
|
certificateKeyFile := getConfigPath(c.CertificateKeyFile, configDir)
|
2021-05-06 19:35:43 +00:00
|
|
|
if c.isWebAdminEnabled() {
|
|
|
|
updateWebAdminURLs(c.WebRoot)
|
|
|
|
loadAdminTemplates(templatesPath)
|
|
|
|
} else {
|
|
|
|
logger.Info(logSender, "", "built-in web admin interface disabled")
|
|
|
|
}
|
|
|
|
if c.isWebClientEnabled() {
|
|
|
|
updateWebClientURLs(c.WebRoot)
|
|
|
|
loadClientTemplates(templatesPath)
|
2020-06-18 21:53:38 +00:00
|
|
|
} else {
|
2021-05-06 19:35:43 +00:00
|
|
|
logger.Info(logSender, "", "built-in web client interface disabled")
|
2020-06-18 21:53:38 +00:00
|
|
|
}
|
2020-12-18 15:04:42 +00:00
|
|
|
if certificateFile != "" && certificateKeyFile != "" {
|
2021-01-19 17:59:41 +00:00
|
|
|
mgr, err := common.NewCertManager(certificateFile, certificateKeyFile, configDir, logSender)
|
2020-02-04 22:21:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
mgr.SetCACertificates(c.CACertificates)
|
|
|
|
if err := mgr.LoadRootCAs(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mgr.SetCARevocationLists(c.CARevocationLists)
|
|
|
|
if err := mgr.LoadCRLs(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
certMgr = mgr
|
2020-02-03 23:08:00 +00:00
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
|
2021-07-01 18:17:40 +00:00
|
|
|
csrfTokenAuth = jwtauth.New(jwa.HS256.String(), getSigningKey(c.SigningPassphrase), nil)
|
2021-02-03 07:55:28 +00:00
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
exitChannel := make(chan error, 1)
|
|
|
|
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if !binding.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-11 04:54:06 +00:00
|
|
|
if err := binding.parseAllowedProxy(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-03 17:33:08 +00:00
|
|
|
binding.checkWebClientIntegrations()
|
2021-01-19 17:59:41 +00:00
|
|
|
|
|
|
|
go func(b Binding) {
|
2021-11-21 08:32:51 +00:00
|
|
|
server := newHttpdServer(b, staticFilesPath, c.SigningPassphrase, c.Cors, openAPIPath)
|
2021-01-19 17:59:41 +00:00
|
|
|
|
|
|
|
exitChannel <- server.listenAndServe()
|
|
|
|
}(binding)
|
|
|
|
}
|
|
|
|
|
2021-07-23 08:19:27 +00:00
|
|
|
maxUploadFileSize = c.MaxUploadFileSize
|
2022-02-27 13:27:53 +00:00
|
|
|
installationCode = c.Setup.InstallationCode
|
|
|
|
installationCodeHint = c.Setup.InstallationCodeHint
|
2021-11-13 12:25:43 +00:00
|
|
|
startCleanupTicker(tokenDuration / 2)
|
2021-01-19 17:59:41 +00:00
|
|
|
return <-exitChannel
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func isWebRequest(r *http.Request) bool {
|
2021-01-17 21:29:08 +00:00
|
|
|
return strings.HasPrefix(r.RequestURI, webBasePath+"/")
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2020-02-03 23:08:00 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func isWebClientRequest(r *http.Request) bool {
|
|
|
|
return strings.HasPrefix(r.RequestURI, webBaseClientPath+"/")
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:03:04 +00:00
|
|
|
// ReloadCertificateMgr reloads the certificate manager
|
|
|
|
func ReloadCertificateMgr() error {
|
2020-02-04 22:21:33 +00:00
|
|
|
if certMgr != nil {
|
2021-01-03 16:03:04 +00:00
|
|
|
return certMgr.Reload()
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
return nil
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:08:00 +00:00
|
|
|
func getConfigPath(name, configDir string) string {
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsFileInputValid(name) {
|
2020-03-03 08:09:58 +00:00
|
|
|
return ""
|
|
|
|
}
|
2020-12-18 15:04:42 +00:00
|
|
|
if name != "" && !filepath.IsAbs(name) {
|
2020-02-03 23:08:00 +00:00
|
|
|
return filepath.Join(configDir, name)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
2020-12-08 10:18:34 +00:00
|
|
|
|
|
|
|
func getServicesStatus() ServicesStatus {
|
|
|
|
status := ServicesStatus{
|
|
|
|
SSH: sftpd.GetStatus(),
|
|
|
|
FTP: ftpd.GetStatus(),
|
|
|
|
WebDAV: webdavd.GetStatus(),
|
|
|
|
DataProvider: dataprovider.GetProviderStatus(),
|
2021-01-02 18:33:24 +00:00
|
|
|
Defender: defenderStatus{
|
|
|
|
IsActive: common.Config.DefenderConfig.Enabled,
|
|
|
|
},
|
2021-09-04 10:11:04 +00:00
|
|
|
MFA: mfa.GetStatus(),
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
func fileServer(r chi.Router, path string, root http.FileSystem) {
|
|
|
|
if path != "/" && path[len(path)-1] != '/' {
|
|
|
|
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
path += "*"
|
|
|
|
|
|
|
|
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rctx := chi.RouteContext(r.Context())
|
|
|
|
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
|
|
|
|
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
|
|
|
|
fs.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func updateWebClientURLs(baseURL string) {
|
|
|
|
if !path.IsAbs(baseURL) {
|
|
|
|
baseURL = "/"
|
|
|
|
}
|
|
|
|
webRootPath = path.Join(baseURL, webRootPathDefault)
|
|
|
|
webBasePath = path.Join(baseURL, webBasePathDefault)
|
|
|
|
webBaseClientPath = path.Join(baseURL, webBasePathClientDefault)
|
|
|
|
webClientLoginPath = path.Join(baseURL, webClientLoginPathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webClientTwoFactorPath = path.Join(baseURL, webClientTwoFactorPathDefault)
|
|
|
|
webClientTwoFactorRecoveryPath = path.Join(baseURL, webClientTwoFactorRecoveryPathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
webClientFilesPath = path.Join(baseURL, webClientFilesPathDefault)
|
2021-12-08 18:25:22 +00:00
|
|
|
webClientFilePath = path.Join(baseURL, webClientFilePathDefault)
|
2021-11-06 13:13:20 +00:00
|
|
|
webClientSharesPath = path.Join(baseURL, webClientSharesPathDefault)
|
|
|
|
webClientPubSharesPath = path.Join(baseURL, webClientPubSharesPathDefault)
|
|
|
|
webClientSharePath = path.Join(baseURL, webClientSharePathDefault)
|
2021-10-09 12:17:28 +00:00
|
|
|
webClientEditFilePath = path.Join(baseURL, webClientEditFilePathDefault)
|
2021-07-26 18:55:49 +00:00
|
|
|
webClientDirsPath = path.Join(baseURL, webClientDirsPathDefault)
|
2021-06-05 14:07:09 +00:00
|
|
|
webClientDownloadZipPath = path.Join(baseURL, webClientDownloadZipPathDefault)
|
2021-09-29 16:46:15 +00:00
|
|
|
webClientProfilePath = path.Join(baseURL, webClientProfilePathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
webChangeClientPwdPath = path.Join(baseURL, webChangeClientPwdPathDefault)
|
|
|
|
webClientLogoutPath = path.Join(baseURL, webClientLogoutPathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webClientMFAPath = path.Join(baseURL, webClientMFAPathDefault)
|
|
|
|
webClientTOTPGeneratePath = path.Join(baseURL, webClientTOTPGeneratePathDefault)
|
|
|
|
webClientTOTPValidatePath = path.Join(baseURL, webClientTOTPValidatePathDefault)
|
|
|
|
webClientTOTPSavePath = path.Join(baseURL, webClientTOTPSavePathDefault)
|
|
|
|
webClientRecoveryCodesPath = path.Join(baseURL, webClientRecoveryCodesPathDefault)
|
2021-11-13 12:25:43 +00:00
|
|
|
webClientForgotPwdPath = path.Join(baseURL, webClientForgotPwdPathDefault)
|
|
|
|
webClientResetPwdPath = path.Join(baseURL, webClientResetPwdPathDefault)
|
2021-11-25 18:24:32 +00:00
|
|
|
webClientViewPDFPath = path.Join(baseURL, webClientViewPDFPathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 20:02:48 +00:00
|
|
|
func updateWebAdminURLs(baseURL string) {
|
|
|
|
if !path.IsAbs(baseURL) {
|
|
|
|
baseURL = "/"
|
|
|
|
}
|
|
|
|
webRootPath = path.Join(baseURL, webRootPathDefault)
|
|
|
|
webBasePath = path.Join(baseURL, webBasePathDefault)
|
2021-05-06 19:35:43 +00:00
|
|
|
webBaseAdminPath = path.Join(baseURL, webBasePathAdminDefault)
|
2021-05-14 17:21:15 +00:00
|
|
|
webAdminSetupPath = path.Join(baseURL, webAdminSetupPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
webLoginPath = path.Join(baseURL, webLoginPathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webAdminTwoFactorPath = path.Join(baseURL, webAdminTwoFactorPathDefault)
|
|
|
|
webAdminTwoFactorRecoveryPath = path.Join(baseURL, webAdminTwoFactorRecoveryPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
webLogoutPath = path.Join(baseURL, webLogoutPathDefault)
|
|
|
|
webUsersPath = path.Join(baseURL, webUsersPathDefault)
|
|
|
|
webUserPath = path.Join(baseURL, webUserPathDefault)
|
|
|
|
webConnectionsPath = path.Join(baseURL, webConnectionsPathDefault)
|
|
|
|
webFoldersPath = path.Join(baseURL, webFoldersPathDefault)
|
|
|
|
webFolderPath = path.Join(baseURL, webFolderPathDefault)
|
|
|
|
webStatusPath = path.Join(baseURL, webStatusPathDefault)
|
|
|
|
webAdminsPath = path.Join(baseURL, webAdminsPathDefault)
|
|
|
|
webAdminPath = path.Join(baseURL, webAdminPathDefault)
|
|
|
|
webMaintenancePath = path.Join(baseURL, webMaintenancePathDefault)
|
|
|
|
webBackupPath = path.Join(baseURL, webBackupPathDefault)
|
|
|
|
webRestorePath = path.Join(baseURL, webRestorePathDefault)
|
|
|
|
webScanVFolderPath = path.Join(baseURL, webScanVFolderPathDefault)
|
|
|
|
webQuotaScanPath = path.Join(baseURL, webQuotaScanPathDefault)
|
|
|
|
webChangeAdminPwdPath = path.Join(baseURL, webChangeAdminPwdPathDefault)
|
2021-11-13 12:25:43 +00:00
|
|
|
webAdminForgotPwdPath = path.Join(baseURL, webAdminForgotPwdPathDefault)
|
|
|
|
webAdminResetPwdPath = path.Join(baseURL, webAdminResetPwdPathDefault)
|
2021-09-29 16:46:15 +00:00
|
|
|
webAdminProfilePath = path.Join(baseURL, webAdminProfilePathDefault)
|
2021-09-04 10:11:04 +00:00
|
|
|
webAdminMFAPath = path.Join(baseURL, webAdminMFAPathDefault)
|
|
|
|
webAdminTOTPGeneratePath = path.Join(baseURL, webAdminTOTPGeneratePathDefault)
|
|
|
|
webAdminTOTPValidatePath = path.Join(baseURL, webAdminTOTPValidatePathDefault)
|
|
|
|
webAdminTOTPSavePath = path.Join(baseURL, webAdminTOTPSavePathDefault)
|
|
|
|
webAdminRecoveryCodesPath = path.Join(baseURL, webAdminRecoveryCodesPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
webTemplateUser = path.Join(baseURL, webTemplateUserDefault)
|
|
|
|
webTemplateFolder = path.Join(baseURL, webTemplateFolderDefault)
|
2021-06-08 11:24:28 +00:00
|
|
|
webDefenderHostsPath = path.Join(baseURL, webDefenderHostsPathDefault)
|
|
|
|
webDefenderPath = path.Join(baseURL, webDefenderPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
webStaticFilesPath = path.Join(baseURL, webStaticFilesPathDefault)
|
2021-11-21 08:32:51 +00:00
|
|
|
webOpenAPIPath = path.Join(baseURL, webOpenAPIPathDefault)
|
2021-04-09 20:02:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// GetHTTPRouter returns an HTTP handler suitable to use for test cases
|
2021-12-03 17:33:08 +00:00
|
|
|
func GetHTTPRouter(b Binding) http.Handler {
|
2021-11-21 08:32:51 +00:00
|
|
|
server := newHttpdServer(b, "../static", "", CorsConfig{}, "../openapi")
|
2021-01-17 21:29:08 +00:00
|
|
|
server.initializeRouter()
|
|
|
|
return server.router
|
|
|
|
}
|
2021-01-26 21:35:36 +00:00
|
|
|
|
|
|
|
// the ticker cannot be started/stopped from multiple goroutines
|
2021-09-04 10:11:04 +00:00
|
|
|
func startCleanupTicker(duration time.Duration) {
|
|
|
|
stopCleanupTicker()
|
|
|
|
cleanupTicker = time.NewTicker(duration)
|
|
|
|
cleanupDone = make(chan bool)
|
2021-01-26 21:35:36 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-09-04 10:11:04 +00:00
|
|
|
case <-cleanupDone:
|
2021-01-26 21:35:36 +00:00
|
|
|
return
|
2021-09-04 10:11:04 +00:00
|
|
|
case <-cleanupTicker.C:
|
2021-01-26 21:35:36 +00:00
|
|
|
cleanupExpiredJWTTokens()
|
2021-11-13 12:25:43 +00:00
|
|
|
cleanupExpiredResetCodes()
|
2021-01-26 21:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func stopCleanupTicker() {
|
|
|
|
if cleanupTicker != nil {
|
|
|
|
cleanupTicker.Stop()
|
|
|
|
cleanupDone <- true
|
|
|
|
cleanupTicker = nil
|
2021-01-26 21:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupExpiredJWTTokens() {
|
|
|
|
invalidatedJWTTokens.Range(func(key, value interface{}) bool {
|
|
|
|
exp, ok := value.(time.Time)
|
|
|
|
if !ok || exp.Before(time.Now().UTC()) {
|
|
|
|
invalidatedJWTTokens.Delete(key)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2021-07-01 18:17:40 +00:00
|
|
|
|
|
|
|
func getSigningKey(signingPassphrase string) []byte {
|
|
|
|
if signingPassphrase != "" {
|
|
|
|
sk := sha256.Sum256([]byte(signingPassphrase))
|
|
|
|
return sk[:]
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.GenerateRandomBytes(32)
|
2021-07-01 18:17:40 +00:00
|
|
|
}
|