/
middleware.go
110 lines (94 loc) · 2.86 KB
/
middleware.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
package account
import (
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"github.com/Alkemic/webrss/repository"
)
const (
sessionCookieName = "session"
sessionUsernameKey = "userID"
backParamName = "back"
LoginPageURL = "/login"
LogoutPageURL = "/logout"
)
var ErrMissingUserID = errors.New("missing user id in session data")
type sessionRepository interface {
Get(sessionID string) (map[string]interface{}, error)
Set(sessionID string, data map[string]interface{}) error
Delete(sessionID string) error
}
func buildLoginUrl(loginURL string, req *http.Request) string {
return loginURL + "?" + backParamName + "=" + url.PathEscape(req.RequestURI)
}
type Middleware struct {
log *log.Logger
settingsRepo settingsRepository
sessionRepo sessionRepository
}
func NewAuthenticateMiddleware(log *log.Logger, settingsRepo settingsRepository, sessionRepo sessionRepository) *Middleware {
return &Middleware{
log: log,
settingsRepo: settingsRepo,
sessionRepo: sessionRepo,
}
}
func (m *Middleware) getUsername(sessionID string) (string, error) {
sessionData, err := m.sessionRepo.Get(sessionID)
if err != nil {
return "", fmt.Errorf("cannot get session: %w", err)
}
rawUsername, ok := sessionData[sessionUsernameKey]
if !ok || rawUsername == "" {
return "", ErrMissingUserID
}
username, ok := rawUsername.(string)
if !ok {
return "", ErrMissingUserID
}
return username, nil
}
func (m *Middleware) LoginRequiredMiddleware(f http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == LoginPageURL || req.URL.Path == LogoutPageURL {
f(rw, req)
return
}
sessionID, err := getSessionID(req)
if err != nil {
m.log.Println("cannot get session:", err)
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
username, err := m.getUsername(sessionID)
if err != nil {
if errors.Is(err, repository.ErrNotFound) || errors.Is(err, ErrMissingUserID) {
if strings.Contains(req.Header.Get("Accept"), "application/json") {
http.Error(rw, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
http.Redirect(rw, req, buildLoginUrl(LoginPageURL, req), http.StatusFound)
return
}
m.log.Println("cannot get user id:", err)
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
user, err := m.settingsRepo.GetUser(req.Context())
if err != nil {
m.log.Println("cannot get user:", err)
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if user.Name != username {
m.log.Println("wrong user in session")
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
SetUser(req, user)
f(rw, req)
}
}