-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
347 lines (315 loc) · 10.7 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// Copyright (c) 2023 whawty contributors (see AUTHORS file)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of whawty.nginx-sso nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
package main
import (
"errors"
"net"
"net/http"
"path"
"strconv"
"strings"
"time"
"github.com/flosch/pongo2/v6"
"github.com/gin-gonic/gin"
"github.com/mileusna/useragent"
"github.com/oklog/ulid/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/whawty/nginx-sso/auth"
"github.com/whawty/nginx-sso/cookie"
"github.com/whawty/nginx-sso/ui"
"gitlab.com/go-box/pongo2gin/v6"
)
var (
webRequests = prometheus.NewCounterVec(prometheus.CounterOpts{Subsystem: "web", Name: "requests_total"}, []string{"status", "endpoint"})
webRequestDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Subsystem: "web", Name: "request_duration_seconds",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}}, []string{"endpoint"})
)
func promMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
endpoint := path.Base(c.Request.URL.Path)
now := time.Now()
c.Next()
webRequestDuration.WithLabelValues(endpoint).Observe(time.Since(now).Seconds())
webRequests.WithLabelValues(strconv.Itoa(c.Writer.Status()), endpoint).Inc()
}
}
type WebError struct {
Error string `json:"error"`
}
func logTemplateErrors(c *gin.Context) {
for _, err := range c.Errors {
wl.Printf("HTML template error: %s", err)
}
}
func getAgentInfo(c *gin.Context) cookie.AgentInfo {
ua := useragent.Parse(c.GetHeader("User-Agent"))
deviceType := "unknown"
if ua.Mobile {
deviceType = cookie.DeviceTypeMobile
} else if ua.Tablet {
deviceType = cookie.DeviceTypeTablet
} else if ua.Desktop {
deviceType = cookie.DeviceTypeDesktop
} else if ua.Bot {
deviceType = cookie.DeviceTypeBot
}
return cookie.AgentInfo{Name: ua.Name, OS: ua.OS, DeviceType: deviceType}
}
type HandlerContext struct {
conf *WebConfig
cookies *cookie.Store
auth auth.Backend
}
func (h *HandlerContext) verifyCookie(c *gin.Context) (*cookie.Session, error) {
cookie, err := c.Cookie(h.cookies.Options().Name)
if err != nil {
return nil, err
}
if cookie == "" {
return nil, errors.New("no cookie found")
}
session, err := h.cookies.Verify(cookie)
if err != nil {
return nil, err
}
return &session, nil
}
func (h *HandlerContext) getBasePath(c *gin.Context) string {
if h.conf.Login.BasePath != "" {
return strings.TrimRight(h.conf.Login.BasePath, "/")
}
hdr := c.GetHeader("X-BasePath")
if hdr != "" {
return strings.TrimRight(hdr, "/")
}
return ""
}
func (h *HandlerContext) renderLoggedIn(c *gin.Context, code int, session *cookie.Session, alerts []ui.Alert) {
login := h.conf.Login
login.BasePath = h.getBasePath(c)
tmplCtx := pongo2.Context{"login": login, "session": session}
if sessions, err := h.cookies.ListUser(session.Username); err == nil {
tmplCtx["sessions"] = sessions
} else {
alerts = append(alerts, ui.Alert{Level: ui.AlertDanger, Heading: "failed to load user sessions", Message: err.Error()})
}
tmplCtx["alerts"] = alerts
c.HTML(http.StatusOK, "logged-in.htmpl", tmplCtx)
logTemplateErrors(c)
}
func (h *HandlerContext) handleAuth(c *gin.Context) {
session, err := h.verifyCookie(c)
if err != nil {
c.Data(http.StatusUnauthorized, "text/plain", []byte(err.Error()))
return
}
c.Header("X-Username", session.Username)
c.Status(http.StatusOK)
}
func (h *HandlerContext) handleLoginGet(c *gin.Context) {
if session, err := h.verifyCookie(c); err == nil {
h.renderLoggedIn(c, http.StatusOK, session, nil)
return
}
login := h.conf.Login
login.BasePath = h.getBasePath(c)
tmplCtx := pongo2.Context{"login": login}
tmplCtx["redirect"], _ = c.GetQuery("redir")
c.HTML(http.StatusOK, "login.htmpl", tmplCtx)
logTemplateErrors(c)
return
}
func (h *HandlerContext) handleLoginPost(c *gin.Context) {
login := h.conf.Login
login.BasePath = h.getBasePath(c)
username := c.PostForm("username")
password := c.PostForm("password")
redirect := c.PostForm("redirect")
tmplCtx := pongo2.Context{"login": login, "redirect": redirect}
if username == "" || password == "" {
tmplCtx["alert"] = ui.Alert{Level: ui.AlertDanger, Heading: "missing parameter", Message: "username and password are mandatory"}
c.HTML(http.StatusBadRequest, "login.htmpl", tmplCtx)
logTemplateErrors(c)
return
}
err := h.auth.Authenticate(username, password)
if err != nil {
tmplCtx["alert"] = ui.Alert{Level: ui.AlertDanger, Heading: "login failed", Message: err.Error()}
c.HTML(http.StatusBadRequest, "login.htmpl", tmplCtx)
logTemplateErrors(c)
return
}
value, opts, err := h.cookies.New(username, getAgentInfo(c))
if err != nil {
tmplCtx["alert"] = ui.Alert{Level: ui.AlertDanger, Heading: "failed to generate cookie", Message: err.Error()}
c.HTML(http.StatusBadRequest, "login.htmpl", tmplCtx)
logTemplateErrors(c)
return
}
c.SetCookie(opts.Name, value, opts.MaxAge, "/", opts.Domain, opts.Secure, true)
if redirect == "" {
redirect = path.Join(h.getBasePath(c), "login")
}
c.Redirect(http.StatusSeeOther, redirect)
}
func (h *HandlerContext) handleLogout(c *gin.Context) {
session, err := h.verifyCookie(c)
if err == nil {
if idParam, _ := c.GetQuery("id"); idParam != "" {
id, err := ulid.ParseStrict(idParam)
if err != nil {
alert := ui.Alert{Level: ui.AlertDanger, Heading: "invalid Session", Message: err.Error()}
h.renderLoggedIn(c, http.StatusBadRequest, session, []ui.Alert{alert})
return
}
if err = h.cookies.RevokeID(session.Username, id); err != nil {
alert := ui.Alert{Level: ui.AlertDanger, Heading: "failed to revoke session", Message: err.Error()}
h.renderLoggedIn(c, http.StatusInternalServerError, session, []ui.Alert{alert})
return
}
h.renderLoggedIn(c, http.StatusOK, session, nil)
return
}
if err = h.cookies.Revoke(*session); err != nil {
alert := ui.Alert{Level: ui.AlertDanger, Heading: "failed to revoke session", Message: err.Error()}
h.renderLoggedIn(c, http.StatusInternalServerError, session, []ui.Alert{alert})
return
}
}
opts := h.cookies.Options()
c.SetCookie(opts.Name, "invalid", -1, "/", opts.Domain, opts.Secure, true)
redirect, _ := c.GetQuery("redir")
if redirect == "" {
redirect = path.Join(h.getBasePath(c), "login")
}
c.Redirect(http.StatusSeeOther, redirect)
}
func (h *HandlerContext) handleSessions(c *gin.Context) {
session, err := h.verifyCookie(c)
if err != nil {
c.JSON(http.StatusUnauthorized, WebError{err.Error()})
return
}
sessions, err := h.cookies.ListUser(session.Username)
if err != nil {
c.JSON(http.StatusInternalServerError, WebError{err.Error()})
return
}
c.JSON(http.StatusOK, sessions)
}
func (h *HandlerContext) handleRevocations(c *gin.Context) {
auth_header := c.GetHeader("Authorization")
if auth_header == "" {
c.JSON(http.StatusUnauthorized, WebError{"no authorization header found"})
return
}
auth_parts := strings.SplitN(auth_header, " ", 2)
if len(auth_parts) != 2 || auth_parts[0] != "Bearer" {
c.JSON(http.StatusUnauthorized, WebError{"authorization header is invalid"})
return
}
authenticated := false
for _, token := range h.conf.Revocations.Tokens {
if token == auth_parts[1] {
authenticated = true
break
}
}
if !authenticated {
c.JSON(http.StatusUnauthorized, WebError{"unauthorized token"})
return
}
revocations, err := h.cookies.ListRevoked()
if err != nil {
c.JSON(http.StatusInternalServerError, WebError{err.Error()})
return
}
c.JSON(http.StatusOK, revocations)
}
func runWeb(config *WebConfig, prom *MetricsHandler, cookies *cookie.Store, auth auth.Backend) (err error) {
if config.Listen == "" {
config.Listen = ":http"
}
if config.Login.Title == "" {
config.Login.Title = "whawty.nginx-sso Login"
}
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery())
r.HandleMethodNotAllowed = true
var htmlTmplLoader pongo2.TemplateLoader
if config.Login.TemplatesPath != "" {
htmlTmplLoader, err = pongo2.NewLocalFileSystemLoader(config.Login.TemplatesPath)
if err != nil {
return
}
} else {
htmlTmplLoader = pongo2.NewFSLoader(ui.Assets)
}
r.HTMLRender = pongo2gin.New(pongo2gin.RenderOptions{
TemplateSet: pongo2.NewSet("html", htmlTmplLoader),
ContentType: "text/html; charset=utf-8"})
h := &HandlerContext{conf: config, cookies: cookies, auth: auth}
r.GET("/", func(c *gin.Context) { c.Redirect(http.StatusSeeOther, path.Join(h.getBasePath(c), "login")) })
r.StaticFS("/ui/", http.FS(ui.StaticAssets))
prom.install(r)
g := r.Group("/")
if reg := prom.reg(); reg != nil {
g.Use(promMiddleware())
if err = reg.Register(webRequests); err != nil {
return
}
if err = reg.Register(webRequestDuration); err != nil {
return
}
}
g.GET("/auth", h.handleAuth)
g.GET("/login", h.handleLoginGet)
g.POST("/login", h.handleLoginPost)
g.GET("/logout", h.handleLogout)
g.GET("/sessions", h.handleSessions)
g.GET("/revocations", h.handleRevocations)
listener, err := net.Listen("tcp", config.Listen)
if err != nil {
return err
}
server := &http.Server{Handler: r, WriteTimeout: 60 * time.Second, ReadTimeout: 60 * time.Second}
if config != nil && config.TLS != nil {
server.TLSConfig, err = config.TLS.ToGoTLSConfig()
if err != nil {
return
}
wl.Printf("web-api: listening on '%s' using TLS", listener.Addr())
return server.ServeTLS(listener, "", "")
}
wl.Printf("web-api: listening on '%s'", listener.Addr())
return server.Serve(listener)
}