aboutsummaryrefslogtreecommitdiff
path: root/pkg/ext/middleware.go
blob: c83b9989cd8e0719062d77df3c6cdae69e5c63c1 (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
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
package ext

import (
	"encoding/base64"
	"time"

	"github.com/sirupsen/logrus"
	"github.com/valyala/fasthttp"

	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
)

func HTML(next fasthttp.RequestHandler) fasthttp.RequestHandler {
	return func(ctx *fasthttp.RequestCtx) {
		if len(ctx.Request.Header.ContentType()) > 0 {
			ctx.Response.Header.SetContentType("text/html")
		}
		next(ctx)
	}
}

type LogMiddleware struct {
	entry *logrus.Entry
}

func NewLogMiddleare(log *logrus.Entry) *LogMiddleware {
	return &LogMiddleware{
		entry: log,
	}
}

func (l *LogMiddleware) HTTP(next fasthttp.RequestHandler) fasthttp.RequestHandler {
	return func(ctx *fasthttp.RequestCtx) {
		start := time.Now()
		next(ctx)
		elapsed := time.Since(start)
		l.entry.
			WithField("time", elapsed).
			WithField("code", ctx.Response.StatusCode()).
			WithField("path", string(ctx.Path())).
			WithField("bytes", len(ctx.Response.Body())).
			Info(string(ctx.Request.Header.Method()))
	}
}

type AuthMiddleware struct {
	key   []byte
	entry *logrus.Entry
}

func NewAuthMiddleware(key []byte, log *logrus.Entry) *AuthMiddleware {
	return &AuthMiddleware{
		key:   key,
		entry: log.WithField("context", "auth"),
	}
}

func (a *AuthMiddleware) LoggedIn(next fasthttp.RequestHandler) fasthttp.RequestHandler {
	return func(ctx *fasthttp.RequestCtx) {
		path := string(ctx.Path())
		if path == "/login" || path == "/initial" {
			next(ctx)
			return
		}

		redirectLogin := "/login?redirect=" + path
		authBase64 := ctx.Request.Header.Cookie("auth")
		if authBase64 == nil {
			a.entry.Info("No auth provided")
			ctx.Redirect(redirectLogin, 307)
			return
		}

		auth, err := base64.StdEncoding.DecodeString(string(authBase64))
		if err != nil {
			a.entry.Error(err)
			return
		}

		token, err := ReadToken(auth, a.key)
		if err != nil {
			a.entry.Error(err)
			ctx.Redirect(redirectLogin, 307)
			return
		}
		ctx.SetUserValue("token", token)
		a.entry.
			WithField("userID", token.UserID).
			WithField("username", token.Username).
			Info("user recognized")
		next(ctx)
	}
}

type InitialSetupMiddleware struct {
	userRepository repository.UserRepository
}

func NewInitialSetupMiddleware(userRepository repository.UserRepository) *InitialSetupMiddleware {
	return &InitialSetupMiddleware{
		userRepository: userRepository,
	}
}

func (i *InitialSetupMiddleware) Check(next fasthttp.RequestHandler) fasthttp.RequestHandler {
	return func(ctx *fasthttp.RequestCtx) {
		// if user has been set to context it is logged in already
		_, ok := ctx.UserValue("token").(*Token)
		if ok {
			next(ctx)
			return
		}

		path := string(ctx.Path())
		if path == "/initial" {
			next(ctx)
			return
		}

		exists, err := i.userRepository.Any(ctx)
		if err != nil {
			InternalServerError(ctx, err)
			return
		}

		if exists {
			next(ctx)
			return
		}
		ctx.Redirect("/initial", 307)
	}
}