2025-02-17 18:16:41 +05:30
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/types/authtypes"
|
2025-02-17 18:16:41 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Auth struct {
|
|
|
|
|
jwt *authtypes.JWT
|
|
|
|
|
headers []string
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 11:40:39 +05:30
|
|
|
func NewAuth(jwt *authtypes.JWT, headers []string) *Auth {
|
|
|
|
|
return &Auth{jwt: jwt, headers: headers}
|
2025-02-17 18:16:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Auth) Wrap(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var values []string
|
|
|
|
|
for _, header := range a.headers {
|
|
|
|
|
values = append(values, r.Header.Get(header))
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-26 15:50:02 +05:30
|
|
|
ctx, err := a.jwt.ContextFromRequest(r.Context(), values...)
|
2025-02-17 18:16:41 +05:30
|
|
|
if err != nil {
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|