aboutsummaryrefslogtreecommitdiffstats
path: root/web/command/middleware/authenticate.go
blob: 08e3552eaf58ccc136ae5fcf26b19ad862456de8 (plain)
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
package middleware

import (
	"net/http"

	"github.com/LMBishop/scrapbook/pkg/auth"
)

func MustAuthenticate(authenticator *auth.Authenticator, next http.HandlerFunc) http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		jwt, err := r.Cookie("session")
		if err != nil {
			http.Redirect(w, r, "/authenticate", 302)
			return
		}

		err = authenticator.VerifyJwt(jwt.Value)
		if err != nil {
			http.Redirect(w, r, "/authenticate", 302)
			return
		}

		next.ServeHTTP(w, r)
	})
}