mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-06-21 05:58:23 +08:00

* chore: integrate pipelines API * fix: limit support integrated in pipelines * fix: interface to string * fix: json parser and allow deleting all pipelines * fix: output modified if operators are disabled * fix: validation updated for operators * fix: expression check added * fix: regex expression check added * fix: remove operator validation updated * fix: tests updated for pipeline builder * fix: fix error messages in http handler * fix: dont return payload if there is an error * fix: extracting userId from context moved to auth package * fix: api errors moved to http handler * fix: get version logic updated * fix: deployment result message updated * fix: pipeline builder edgecase fixed and tests updated * fix: get failing postablePipeline tests to pass --------- Co-authored-by: Vishal Sharma <makeavish786@gmail.com> Co-authored-by: Raj <rkssisodiya@gmail.com>
114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
jwtmiddleware "github.com/auth0/go-jwt-middleware"
|
|
"github.com/golang-jwt/jwt"
|
|
"github.com/pkg/errors"
|
|
"go.signoz.io/signoz/pkg/query-service/model"
|
|
"go.uber.org/zap"
|
|
"google.golang.org/grpc/metadata"
|
|
)
|
|
|
|
var (
|
|
JwtSecret string
|
|
JwtExpiry = 30 * time.Minute
|
|
JwtRefresh = 30 * 24 * time.Hour
|
|
)
|
|
|
|
func ParseJWT(jwtStr string) (jwt.MapClaims, error) {
|
|
token, err := jwt.Parse(jwtStr, func(token *jwt.Token) (interface{}, error) {
|
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
return nil, errors.Errorf("unknown signing algo: %v", token.Header["alg"])
|
|
}
|
|
return []byte(JwtSecret), nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to parse jwt token")
|
|
}
|
|
|
|
claims, ok := token.Claims.(jwt.MapClaims)
|
|
if !ok || !token.Valid {
|
|
return nil, errors.Errorf("Not a valid jwt claim")
|
|
}
|
|
return claims, nil
|
|
}
|
|
|
|
func validateUser(tok string) (*model.UserPayload, error) {
|
|
claims, err := ParseJWT(tok)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
now := time.Now().Unix()
|
|
if !claims.VerifyExpiresAt(now, true) {
|
|
return nil, model.ErrorTokenExpired
|
|
}
|
|
return &model.UserPayload{
|
|
User: model.User{
|
|
Id: claims["id"].(string),
|
|
GroupId: claims["gid"].(string),
|
|
Email: claims["email"].(string),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// AttachJwtToContext attached the jwt token from the request header to the context.
|
|
func AttachJwtToContext(ctx context.Context, r *http.Request) context.Context {
|
|
token, err := ExtractJwtFromRequest(r)
|
|
if err != nil {
|
|
zap.S().Debugf("Error while getting token from header, %v", err)
|
|
return ctx
|
|
}
|
|
|
|
if len(token) > 0 {
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
if !ok {
|
|
md = metadata.New(nil)
|
|
}
|
|
|
|
md.Append("accessJwt", token)
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
func ExtractJwtFromContext(ctx context.Context) (string, error) {
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
if !ok {
|
|
return "", errors.New("No JWT metadata token found")
|
|
}
|
|
accessJwt := md.Get("accessJwt")
|
|
if len(accessJwt) == 0 {
|
|
return "", errors.New("No JWT token found")
|
|
}
|
|
|
|
return accessJwt[0], nil
|
|
}
|
|
|
|
func ExtractJwtFromRequest(r *http.Request) (string, error) {
|
|
return jwtmiddleware.FromAuthHeader(r)
|
|
}
|
|
|
|
func ExtractUserIdFromContext(ctx context.Context) (string, error) {
|
|
userId := ""
|
|
jwt, err := ExtractJwtFromContext(ctx)
|
|
if err != nil {
|
|
return "", model.InternalError(fmt.Errorf("failed to extract jwt from context %v", err))
|
|
}
|
|
|
|
claims, err := ParseJWT(jwt)
|
|
if err != nil {
|
|
return "", model.InternalError(fmt.Errorf("failed get claims from jwt %v", err))
|
|
}
|
|
|
|
if v, ok := claims["id"]; ok {
|
|
userId = v.(string)
|
|
}
|
|
return userId, nil
|
|
}
|