2020-11-16 19:37:52 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2022-03-20 11:43:00 +01:00
|
|
|
"gitlab.com/demodesk/neko/server/pkg/types"
|
|
|
|
"gitlab.com/demodesk/neko/server/pkg/utils"
|
2020-11-16 19:37:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
2021-09-02 21:37:24 +02:00
|
|
|
const keySessionCtx key = iota
|
2020-11-16 19:37:52 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func SetSession(r *http.Request, session types.Session) context.Context {
|
|
|
|
return context.WithValue(r.Context(), keySessionCtx, session)
|
2020-11-16 19:37:52 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func GetSession(r *http.Request) (types.Session, bool) {
|
|
|
|
session, ok := r.Context().Value(keySessionCtx).(types.Session)
|
|
|
|
return session, ok
|
2020-11-16 19:37:52 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func AdminsOnly(w http.ResponseWriter, r *http.Request) (context.Context, error) {
|
|
|
|
session, ok := GetSession(r)
|
|
|
|
if !ok || !session.Profile().IsAdmin {
|
|
|
|
return nil, utils.HttpForbidden("session is not admin")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2020-11-16 19:37:52 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func HostsOnly(w http.ResponseWriter, r *http.Request) (context.Context, error) {
|
|
|
|
session, ok := GetSession(r)
|
|
|
|
if !ok || !session.IsHost() {
|
|
|
|
return nil, utils.HttpForbidden("session is not host")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2020-11-16 19:37:52 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func CanWatchOnly(w http.ResponseWriter, r *http.Request) (context.Context, error) {
|
|
|
|
session, ok := GetSession(r)
|
|
|
|
if !ok || !session.Profile().CanWatch {
|
|
|
|
return nil, utils.HttpForbidden("session cannot watch")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2020-11-16 19:37:52 +01:00
|
|
|
}
|
2020-12-29 22:00:28 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func CanHostOnly(w http.ResponseWriter, r *http.Request) (context.Context, error) {
|
|
|
|
session, ok := GetSession(r)
|
|
|
|
if !ok || !session.Profile().CanHost {
|
|
|
|
return nil, utils.HttpForbidden("session cannot host")
|
|
|
|
}
|
|
|
|
|
2022-03-26 23:20:38 +01:00
|
|
|
if session.PrivateModeEnabled() {
|
|
|
|
return nil, utils.HttpUnprocessableEntity("private mode is enabled")
|
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return nil, nil
|
2020-12-29 22:00:28 +01:00
|
|
|
}
|
2021-01-21 20:44:09 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func CanAccessClipboardOnly(w http.ResponseWriter, r *http.Request) (context.Context, error) {
|
|
|
|
session, ok := GetSession(r)
|
|
|
|
if !ok || !session.Profile().CanAccessClipboard {
|
|
|
|
return nil, utils.HttpForbidden("session cannot access clipboard")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2021-01-21 20:44:09 +01:00
|
|
|
}
|