summaryrefslogtreecommitdiff
path: root/internal/handlers/middleware.go
blob: 8b871a2f00460c85546d9e5f5b396bc94636340b (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
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
package handlers

import (
	"context"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"errors"
	"net/http"
)

func (h *Handlers) GetSession(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		cookieName := "session_token"
		sessionCookie, err := r.Cookie(cookieName)
		if err != nil {
			msg := "auth failed; failed to get session token from cookies"
			h.log.Debug(msg, "error", err)
			next.ServeHTTP(w, r)
			return
		}
		cookieValueB, err := base64.URLEncoding.
			DecodeString(sessionCookie.Value)
		if err != nil {
			msg := "auth failed; failed to decode b64 cookie"
			h.log.Debug(msg, "error", err)
			next.ServeHTTP(w, r)
			return
		}
		cookieValue := string(cookieValueB)
		if len(cookieValue) < sha256.Size {
			h.log.Warn("small cookie", "size", len(cookieValue))
			next.ServeHTTP(w, r)
			return
		}
		// Split apart the signature and original cookie value.
		signature := cookieValue[:sha256.Size]
		sessionToken := cookieValue[sha256.Size:]
		//verify signature
		mac := hmac.New(sha256.New, []byte(h.cfg.CookieSecret))
		mac.Write([]byte(cookieName))
		mac.Write([]byte(sessionToken))
		expectedSignature := mac.Sum(nil)
		if !hmac.Equal([]byte(signature), expectedSignature) {
			h.log.Debug("cookie with an invalid sign")
			next.ServeHTTP(w, r)
			return
		}
		userSession, err := h.cacheGetSession(sessionToken)
		if err != nil {
			msg := "auth failed; session does not exists"
			err = errors.New(msg)
			h.log.Debug(msg, "error", err)
			next.ServeHTTP(w, r)
			return
		}
		if userSession.IsExpired() {
			h.mc.RemoveKey(sessionToken)
			msg := "session is expired"
			h.log.Debug(msg, "error", err, "token", sessionToken)
			next.ServeHTTP(w, r)
			return
		}
		ctx := context.WithValue(r.Context(),
			"username", userSession.Username)
		if err := h.cacheSetSession(sessionToken,
			userSession); err != nil {
			msg := "failed to marshal user session"
			h.log.Warn(msg, "error", err)
			next.ServeHTTP(w, r)
			return
		}
		next.ServeHTTP(w, r.WithContext(ctx))
	})
}