-
-
Notifications
You must be signed in to change notification settings - Fork 692
/
web.go
160 lines (141 loc) · 4.02 KB
/
web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (C) 2019 Nicola Murino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package httpd
import (
"errors"
"net/http"
"strings"
"github.com/go-chi/render"
"github.com/unrolled/secure"
"github.com/drakkan/sftpgo/v2/internal/util"
"github.com/drakkan/sftpgo/v2/internal/version"
)
const (
pageMFATitle = "Two-factor authentication"
pageTwoFactorTitle = "Two-Factor authentication"
pageTwoFactorRecoveryTitle = "Two-Factor recovery"
webDateTimeFormat = "2006-01-02 15:04:05" // YYYY-MM-DD HH:MM:SS
redactedSecret = "[**redacted**]"
csrfFormToken = "_form_token"
csrfHeaderToken = "X-CSRF-TOKEN"
templateCommonDir = "common"
templateTwoFactor = "twofactor.html"
templateTwoFactorRecovery = "twofactor-recovery.html"
templateForgotPassword = "forgot-password.html"
templateResetPassword = "reset-password.html"
templateChangePwd = "changepassword.html"
templateMessage = "message.html"
templateCommonBase = "base.html"
templateCommonBaseLogin = "baselogin.html"
templateCommonLogin = "login.html"
)
var (
errInvalidTokenClaims = errors.New("invalid token claims")
)
type commonBasePage struct {
CSPNonce string
StaticURL string
Version string
}
type loginPage struct {
commonBasePage
CurrentURL string
Error *util.I18nError
CSRFToken string
AltLoginURL string
AltLoginName string
ForgotPwdURL string
OpenIDLoginURL string
Title string
Branding UIBranding
FormDisabled bool
CheckRedirect bool
}
type twoFactorPage struct {
commonBasePage
CurrentURL string
Error *util.I18nError
CSRFToken string
RecoveryURL string
Title string
Branding UIBranding
}
type forgotPwdPage struct {
commonBasePage
CurrentURL string
Error *util.I18nError
CSRFToken string
LoginURL string
Title string
Branding UIBranding
}
type resetPwdPage struct {
commonBasePage
CurrentURL string
Error *util.I18nError
CSRFToken string
LoginURL string
Title string
Branding UIBranding
}
func getSliceFromDelimitedValues(values, delimiter string) []string {
result := []string{}
for _, v := range strings.Split(values, delimiter) {
cleaned := strings.TrimSpace(v)
if cleaned != "" {
result = append(result, cleaned)
}
}
return result
}
func hasPrefixAndSuffix(key, prefix, suffix string) bool {
return strings.HasPrefix(key, prefix) && strings.HasSuffix(key, suffix)
}
func getCommonBasePage(r *http.Request) commonBasePage {
return commonBasePage{
CSPNonce: secure.CSPNonce(r.Context()),
StaticURL: webStaticFilesPath,
Version: version.GetServerVersion(" ", true),
}
}
func i18nListDirMsg(status int) string {
if status == http.StatusForbidden {
return util.I18nErrorDirList403
}
return util.I18nErrorDirListGeneric
}
func i18nFsMsg(status int) string {
if status == http.StatusForbidden {
return util.I18nError403Message
}
return util.I18nErrorFsGeneric
}
func getI18NErrorString(err error, fallback string) string {
var errI18n *util.I18nError
if errors.As(err, &errI18n) {
return errI18n.Message
}
return fallback
}
func getI18nError(err error) *util.I18nError {
var errI18n *util.I18nError
if err != nil {
errI18n = util.NewI18nError(err, util.I18nError500Message)
}
return errI18n
}
func handlePingRequest(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
render.PlainText(w, r, "PONG")
}