aboutsummaryrefslogtreecommitdiff
path: root/pkg/ext/middleware.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-09-12 18:37:30 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-09-12 18:40:00 +0200
commitae10e121875982d6956d6bff453544cc59a75616 (patch)
tree9b6508c9b2a105ce3027bb24342916050e2f50cc /pkg/ext/middleware.go
parentd33ba9ee675eedf47ce4a7977d116bf81dda5b2e (diff)
downloadlens-ae10e121875982d6956d6bff453544cc59a75616.tar.gz
lens-ae10e121875982d6956d6bff453544cc59a75616.tar.bz2
lens-ae10e121875982d6956d6bff453544cc59a75616.zip
feat: Add admin control
Now only admins can access settings.
Diffstat (limited to 'pkg/ext/middleware.go')
-rw-r--r--pkg/ext/middleware.go47
1 files changed, 34 insertions, 13 deletions
diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go
index 061cf7c..6a94c4f 100644
--- a/pkg/ext/middleware.go
+++ b/pkg/ext/middleware.go
@@ -20,9 +20,17 @@ func HTML(next http.HandlerFunc) http.HandlerFunc {
}
}
-type LogMiddleware struct {
- entry *logrus.Entry
-}
+type (
+ User string
+
+ LogMiddleware struct {
+ entry *logrus.Entry
+ }
+)
+
+const (
+ UserKey User = "user"
+)
func NewLogMiddleare(log *logrus.Entry) *LogMiddleware {
return &LogMiddleware{
@@ -43,14 +51,20 @@ func (l *LogMiddleware) HTTP(next http.HandlerFunc) http.HandlerFunc {
}
type AuthMiddleware struct {
- key []byte
- entry *logrus.Entry
+ key []byte
+ entry *logrus.Entry
+ userRepository repository.UserRepository
}
-func NewAuthMiddleware(key []byte, log *logrus.Entry) *AuthMiddleware {
+func NewAuthMiddleware(
+ key []byte,
+ log *logrus.Entry,
+ userRepository repository.UserRepository,
+) *AuthMiddleware {
return &AuthMiddleware{
- key: key,
- entry: log.WithField("context", "auth"),
+ key: key,
+ entry: log.WithField("context", "auth"),
+ userRepository: userRepository,
}
}
@@ -82,7 +96,14 @@ func (a *AuthMiddleware) LoggedIn(next http.HandlerFunc) http.HandlerFunc {
http.Redirect(w, r, redirectLogin, http.StatusTemporaryRedirect)
return
}
- r = r.WithContext(context.WithValue(r.Context(), service.TokenKey, token))
+
+ user, err := a.userRepository.Get(r.Context(), token.UserID)
+ if err != nil {
+ a.entry.Error(err)
+ return
+ }
+
+ r = r.WithContext(context.WithValue(r.Context(), UserKey, user))
a.entry.
WithField("userID", token.UserID).
WithField("username", token.Username).
@@ -91,9 +112,9 @@ func (a *AuthMiddleware) LoggedIn(next http.HandlerFunc) http.HandlerFunc {
}
}
-func GetTokenFromCtx(r *http.Request) *service.Token {
- tokenValue := r.Context().Value(service.TokenKey)
- if token, ok := tokenValue.(*service.Token); ok {
+func GetUserFromCtx(r *http.Request) *repository.User {
+ tokenValue := r.Context().Value(UserKey)
+ if token, ok := tokenValue.(*repository.User); ok {
return token
}
return nil
@@ -113,7 +134,7 @@ func (i *InitialSetupMiddleware) Check(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// if user has been set to context it is logged in already
- token := GetTokenFromCtx(r)
+ token := GetUserFromCtx(r)
if token != nil {
next(w, r)
return