-
Notifications
You must be signed in to change notification settings - Fork 457
/
web.go
331 lines (292 loc) · 7.25 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"bytes"
"encoding/gob"
"fmt"
"html/template"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"golang.org/x/net/publicsuffix"
humanize "github.com/dustin/go-humanize"
httprouter "github.com/julienschmidt/httprouter"
)
var (
SessionCookieName = "__subspace_session"
SessionCookieNameSSO = "__subspace_sso_session"
)
type Session struct {
Admin bool
UserID string
NotBefore time.Time
NotAfter time.Time
}
type Web struct {
// Internal
w http.ResponseWriter
r *http.Request
ps httprouter.Params
template string
// Default
Backlink string
Version string
Request *http.Request
Section string
Time time.Time
Info Info
Admin bool
SAML *samlsp.Middleware
User User
Users []User
Profile Profile
Profiles []Profile
TargetUser User
TargetProfiles []Profile
}
func init() {
gob.Register(Session{})
}
func Error(w http.ResponseWriter, err error) {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, errorPageHTML+"\n")
}
func (w *Web) HTML() {
t := template.New(w.template).Funcs(template.FuncMap{
"hasprefix": strings.HasPrefix,
"hassuffix": strings.HasSuffix,
"add": func(a, b int) int {
return a + b
},
"bytes": func(n int64) string {
return fmt.Sprintf("%.2f GB", float64(n)/1024/1024/1024)
},
"date": func(t time.Time) string {
return t.Format(time.UnixDate)
},
"time": humanize.Time,
"ssoprovider": func() string {
if samlSP == nil {
return ""
}
redirect, err := url.Parse(samlSP.ServiceProvider.GetSSOBindingLocation(saml.HTTPRedirectBinding))
if err != nil {
logger.Warnf("SSO redirect invalid URL: %s", err)
return "unknown"
}
domain, err := publicsuffix.EffectiveTLDPlusOne(redirect.Host)
if err != nil {
logger.Warnf("SSO redirect invalid URL domain: %s", err)
return "unknown"
}
suffix, icann := publicsuffix.PublicSuffix(domain)
if icann {
suffix = "." + suffix
}
return strings.Title(strings.TrimSuffix(domain, suffix))
},
})
for _, filename := range AssetNames() {
if !strings.HasPrefix(filename, "templates/") {
continue
}
name := strings.TrimPrefix(filename, "templates/")
b, err := Asset(filename)
if err != nil {
Error(w.w, err)
return
}
var tmpl *template.Template
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
if _, err := tmpl.Parse(string(b)); err != nil {
Error(w.w, err)
return
}
}
w.w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := t.Execute(w.w, w); err != nil {
Error(w.w, err)
return
}
}
func (w *Web) Redirect(format string, a ...interface{}) {
location := fmt.Sprintf(format, a...)
http.Redirect(w.w, w.r, location, http.StatusFound)
}
func WebHandler(h func(*Web), section string) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
web := &Web{
w: w,
r: r,
ps: ps,
template: section + ".html",
Backlink: backlink,
Time: time.Now(),
Version: version,
Request: r,
Section: section,
Info: config.FindInfo(),
SAML: samlSP,
}
if section == "signin" || section == "forgot" || section == "configure" {
h(web)
return
}
if !config.FindInfo().Configured {
web.Redirect("/configure")
return
}
// Has an existing session.
if session, _ := ValidateSession(r); session != nil {
if session.UserID != "" {
user, err := config.FindUser(session.UserID)
if err != nil {
signoutHandler(web)
return
}
web.User = user
web.Admin = user.Admin
} else {
web.Admin = session.Admin
}
h(web)
return
}
// Needs a new session.
if samlSP != nil {
if token := samlSP.GetAuthorizationToken(r); token != nil {
r = r.WithContext(samlsp.WithToken(r.Context(), token))
email := token.StandardClaims.Subject
if email == "" {
Error(w, fmt.Errorf("SAML token missing email"))
return
}
logger.Infof("SAML: finding user with email %q", email)
user, err := config.FindUserByEmail(email)
if err != nil && err != ErrUserNotFound {
Error(w, err)
return
}
if user.ID == "" {
logger.Infof("SAML: creating user with email %q", email)
user, err = config.AddUser(email)
if err != nil {
Error(w, err)
return
}
}
web.User = user
web.Admin = user.Admin
if err := web.SigninSession(false, user.ID); err != nil {
Error(web.w, err)
return
}
h(web)
return
}
}
logger.Warnf("auth: sign in required")
web.Redirect("/signin")
}
}
func Log(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
start := time.Now()
h(w, r, ps)
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
ua := r.Header.Get("User-Agent")
xff := r.Header.Get("X-Forwarded-For")
xrealip := r.Header.Get("X-Real-IP")
rang := r.Header.Get("Range")
logger.Infof("%s %q %q %q %q %q %q %s %q %d ms", start, ip, xff, xrealip, ua, rang, r.Referer(), r.Method, r.RequestURI, int64(time.Since(start)/time.Millisecond))
}
}
func staticHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveAsset(w, r, ps.ByName("path"))
}
func serveAsset(w http.ResponseWriter, r *http.Request, filename string) {
path := "static" + filename
b, err := Asset(path)
if err != nil {
http.NotFound(w, r)
return
}
fi, err := AssetInfo(path)
if err != nil {
Error(w, err)
return
}
http.ServeContent(w, r, path, fi.ModTime(), bytes.NewReader(b))
}
func ValidateSession(r *http.Request) (*Session, error) {
cookie, err := r.Cookie(SessionCookieName)
if err != nil {
return nil, fmt.Errorf("auth: missing cookie")
}
session := &Session{}
if err := securetoken.Decode(SessionCookieName, cookie.Value, session); err != nil {
return nil, err
}
if time.Now().Before(session.NotBefore) {
return nil, fmt.Errorf("invalid session (before valid)")
}
if time.Now().After(session.NotAfter) {
return nil, fmt.Errorf("invalid session (expired session.NotAfter is %s and now is %s)", session.NotAfter, time.Now())
}
return session, nil
}
func (w *Web) SignoutSession() {
if samlSP != nil {
http.SetCookie(w.w, &http.Cookie{
Name: SessionCookieNameSSO,
Value: "",
Path: "/",
HttpOnly: true,
Domain: httpHost,
Secure: !httpInsecure,
MaxAge: -1,
Expires: time.Unix(1, 0),
})
}
http.SetCookie(w.w, &http.Cookie{
Name: SessionCookieName,
Value: "",
Path: "/",
HttpOnly: true,
Domain: httpHost,
Secure: !httpInsecure,
MaxAge: -1,
Expires: time.Unix(1, 0),
})
}
func (w *Web) SigninSession(admin bool, userID string) error {
expires := time.Now().Add(12 * time.Hour)
encoded, err := securetoken.Encode(SessionCookieName, Session{
Admin: admin,
UserID: userID,
NotBefore: time.Now(),
NotAfter: expires,
})
if err != nil {
return fmt.Errorf("auth: encoding error: %s", err)
}
http.SetCookie(w.w, &http.Cookie{
Name: SessionCookieName,
Value: encoded,
Path: "/",
HttpOnly: true,
Domain: httpHost,
Secure: !httpInsecure,
Expires: expires,
})
return nil
}