2020-11-17 07:37:52 +13:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
2021-01-09 10:41:37 +13:00
|
|
|
keySessionCtx key = iota
|
2020-11-17 07:37:52 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetSession(r *http.Request, session types.Session) *http.Request {
|
|
|
|
ctx := context.WithValue(r.Context(), keySessionCtx, session)
|
|
|
|
return r.WithContext(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSession(r *http.Request) types.Session {
|
|
|
|
return r.Context().Value(keySessionCtx).(types.Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AdminsOnly(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session := GetSession(r)
|
2021-03-14 12:45:51 +13:00
|
|
|
if !session.Profile().IsAdmin {
|
2021-03-22 09:32:52 +13:00
|
|
|
utils.HttpForbidden(w, "Only admin can do this.")
|
2020-11-17 07:37:52 +13:00
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func HostsOnly(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session := GetSession(r)
|
|
|
|
if !session.IsHost() {
|
2020-11-29 03:00:21 +13:00
|
|
|
utils.HttpForbidden(w, "Only host can do this.")
|
2020-11-17 07:37:52 +13:00
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:26:53 +13:00
|
|
|
func CanWatchOnly(next http.Handler) http.Handler {
|
2020-11-17 07:37:52 +13:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session := GetSession(r)
|
2021-03-22 09:26:53 +13:00
|
|
|
if !session.Profile().CanWatch {
|
2021-03-22 09:32:52 +13:00
|
|
|
utils.HttpForbidden(w, "Only sessions, that can watch.")
|
2020-11-17 07:37:52 +13:00
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-12-30 10:00:28 +13:00
|
|
|
|
|
|
|
func CanHostOnly(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session := GetSession(r)
|
2021-03-14 12:45:51 +13:00
|
|
|
if !session.Profile().CanHost {
|
2021-03-22 09:32:52 +13:00
|
|
|
utils.HttpForbidden(w, "Only sessions, that can host.")
|
2020-12-30 10:00:28 +13:00
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-01-22 08:44:09 +13:00
|
|
|
|
2021-03-22 09:26:53 +13:00
|
|
|
func CanAccessClipboardOnly(next http.Handler) http.Handler {
|
2021-01-22 08:44:09 +13:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session := GetSession(r)
|
2021-03-22 09:26:53 +13:00
|
|
|
if !session.Profile().CanAccessClipboard {
|
2021-03-22 09:32:52 +13:00
|
|
|
utils.HttpForbidden(w, "Only sessions, that can access clipboard.")
|
2021-01-22 08:44:09 +13:00
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|