2023-02-15 23:49:03 +05:30
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
2023-10-17 17:50:54 +00:00
|
|
|
"context"
|
2023-02-15 23:49:03 +05:30
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"go.signoz.io/signoz/pkg/query-service/auth"
|
2023-10-17 17:50:54 +00:00
|
|
|
"go.signoz.io/signoz/pkg/query-service/constants"
|
2023-02-15 23:49:03 +05:30
|
|
|
"go.signoz.io/signoz/pkg/query-service/model"
|
2025-03-06 15:39:45 +05:30
|
|
|
"go.signoz.io/signoz/pkg/types"
|
2023-02-15 23:49:03 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AuthMiddleware struct {
|
2025-03-06 15:39:45 +05:30
|
|
|
GetUserFromRequest func(r context.Context) (*types.GettableUser, error)
|
2023-02-15 23:49:03 +05:30
|
|
|
}
|
|
|
|
|
|
2025-03-06 15:39:45 +05:30
|
|
|
func NewAuthMiddleware(f func(ctx context.Context) (*types.GettableUser, error)) *AuthMiddleware {
|
2023-02-15 23:49:03 +05:30
|
|
|
return &AuthMiddleware{
|
|
|
|
|
GetUserFromRequest: f,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (am *AuthMiddleware) OpenAccess(f func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
f(w, r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (am *AuthMiddleware) ViewAccess(f func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2025-02-17 18:16:41 +05:30
|
|
|
user, err := am.GetUserFromRequest(r.Context())
|
2023-02-15 23:49:03 +05:30
|
|
|
if err != nil {
|
|
|
|
|
RespondError(w, &model.ApiError{
|
|
|
|
|
Typ: model.ErrorUnauthorized,
|
|
|
|
|
Err: err,
|
|
|
|
|
}, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !(auth.IsViewer(user) || auth.IsEditor(user) || auth.IsAdmin(user)) {
|
|
|
|
|
RespondError(w, &model.ApiError{
|
|
|
|
|
Typ: model.ErrorForbidden,
|
2023-10-17 17:50:54 +00:00
|
|
|
Err: errors.New("API is accessible to viewers/editors/admins"),
|
2023-02-15 23:49:03 +05:30
|
|
|
}, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-10-17 17:50:54 +00:00
|
|
|
ctx := context.WithValue(r.Context(), constants.ContextUserKey, user)
|
|
|
|
|
r = r.WithContext(ctx)
|
2023-02-15 23:49:03 +05:30
|
|
|
f(w, r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (am *AuthMiddleware) EditAccess(f func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2025-02-17 18:16:41 +05:30
|
|
|
user, err := am.GetUserFromRequest(r.Context())
|
2023-02-15 23:49:03 +05:30
|
|
|
if err != nil {
|
|
|
|
|
RespondError(w, &model.ApiError{
|
|
|
|
|
Typ: model.ErrorUnauthorized,
|
|
|
|
|
Err: err,
|
|
|
|
|
}, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !(auth.IsEditor(user) || auth.IsAdmin(user)) {
|
|
|
|
|
RespondError(w, &model.ApiError{
|
|
|
|
|
Typ: model.ErrorForbidden,
|
2024-06-11 20:10:38 +05:30
|
|
|
Err: errors.New("API is accessible to editors/admins"),
|
2023-02-15 23:49:03 +05:30
|
|
|
}, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-10-17 17:50:54 +00:00
|
|
|
ctx := context.WithValue(r.Context(), constants.ContextUserKey, user)
|
|
|
|
|
r = r.WithContext(ctx)
|
2023-02-15 23:49:03 +05:30
|
|
|
f(w, r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (am *AuthMiddleware) SelfAccess(f func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2025-02-17 18:16:41 +05:30
|
|
|
user, err := am.GetUserFromRequest(r.Context())
|
2023-02-15 23:49:03 +05:30
|
|
|
if err != nil {
|
|
|
|
|
RespondError(w, &model.ApiError{
|
|
|
|
|
Typ: model.ErrorUnauthorized,
|
|
|
|
|
Err: err,
|
|
|
|
|
}, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
id := mux.Vars(r)["id"]
|
|
|
|
|
if !(auth.IsSelfAccessRequest(user, id) || auth.IsAdmin(user)) {
|
|
|
|
|
RespondError(w, &model.ApiError{
|
|
|
|
|
Typ: model.ErrorForbidden,
|
2024-06-11 20:10:38 +05:30
|
|
|
Err: errors.New("API is accessible for self access or to the admins"),
|
2023-02-15 23:49:03 +05:30
|
|
|
}, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-10-17 17:50:54 +00:00
|
|
|
ctx := context.WithValue(r.Context(), constants.ContextUserKey, user)
|
|
|
|
|
r = r.WithContext(ctx)
|
2023-02-15 23:49:03 +05:30
|
|
|
f(w, r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (am *AuthMiddleware) AdminAccess(f func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2025-02-17 18:16:41 +05:30
|
|
|
user, err := am.GetUserFromRequest(r.Context())
|
2023-02-15 23:49:03 +05:30
|
|
|
if err != nil {
|
|
|
|
|
RespondError(w, &model.ApiError{
|
|
|
|
|
Typ: model.ErrorUnauthorized,
|
|
|
|
|
Err: err,
|
|
|
|
|
}, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !auth.IsAdmin(user) {
|
|
|
|
|
RespondError(w, &model.ApiError{
|
|
|
|
|
Typ: model.ErrorForbidden,
|
|
|
|
|
Err: errors.New("API is accessible to admins only"),
|
|
|
|
|
}, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-10-17 17:50:54 +00:00
|
|
|
ctx := context.WithValue(r.Context(), constants.ContextUserKey, user)
|
|
|
|
|
r = r.WithContext(ctx)
|
2023-02-15 23:49:03 +05:30
|
|
|
f(w, r)
|
|
|
|
|
}
|
|
|
|
|
}
|