2024-02-16 12:46:33 +05:30
|
|
|
package auth
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"go.signoz.io/signoz/ee/query-service/app/api"
|
|
|
|
|
baseauth "go.signoz.io/signoz/pkg/query-service/auth"
|
|
|
|
|
"go.signoz.io/signoz/pkg/query-service/telemetry"
|
2025-03-06 15:39:45 +05:30
|
|
|
"go.signoz.io/signoz/pkg/types"
|
2025-02-17 18:16:41 +05:30
|
|
|
"go.signoz.io/signoz/pkg/types/authtypes"
|
2024-02-16 12:46:33 +05:30
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
)
|
|
|
|
|
|
2025-03-06 15:39:45 +05:30
|
|
|
func GetUserFromRequestContext(ctx context.Context, apiHandler *api.APIHandler) (*types.GettableUser, error) {
|
2025-02-17 18:16:41 +05:30
|
|
|
patToken, ok := authtypes.UUIDFromContext(ctx)
|
|
|
|
|
if ok && patToken != "" {
|
2024-03-27 00:07:29 +05:30
|
|
|
zap.L().Debug("Received a non-zero length PAT token")
|
2024-02-16 12:46:33 +05:30
|
|
|
ctx := context.Background()
|
|
|
|
|
dao := apiHandler.AppDao()
|
|
|
|
|
|
|
|
|
|
pat, err := dao.GetPAT(ctx, patToken)
|
|
|
|
|
if err == nil && pat != nil {
|
2024-03-27 00:07:29 +05:30
|
|
|
zap.L().Debug("Found valid PAT: ", zap.Any("pat", pat))
|
2024-02-16 12:46:33 +05:30
|
|
|
if pat.ExpiresAt < time.Now().Unix() && pat.ExpiresAt != 0 {
|
2024-03-27 00:07:29 +05:30
|
|
|
zap.L().Info("PAT has expired: ", zap.Any("pat", pat))
|
2024-02-16 12:46:33 +05:30
|
|
|
return nil, fmt.Errorf("PAT has expired")
|
|
|
|
|
}
|
|
|
|
|
group, apiErr := dao.GetGroupByName(ctx, pat.Role)
|
|
|
|
|
if apiErr != nil {
|
2024-03-27 00:07:29 +05:30
|
|
|
zap.L().Error("Error while getting group for PAT: ", zap.Any("apiErr", apiErr))
|
2024-02-16 12:46:33 +05:30
|
|
|
return nil, apiErr
|
|
|
|
|
}
|
|
|
|
|
user, err := dao.GetUser(ctx, pat.UserID)
|
|
|
|
|
if err != nil {
|
2024-03-27 00:07:29 +05:30
|
|
|
zap.L().Error("Error while getting user for PAT: ", zap.Error(err))
|
2024-02-16 12:46:33 +05:30
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
telemetry.GetInstance().SetPatTokenUser()
|
|
|
|
|
dao.UpdatePATLastUsed(ctx, patToken, time.Now().Unix())
|
2025-03-06 15:39:45 +05:30
|
|
|
user.User.GroupID = group.ID
|
|
|
|
|
user.User.ID = pat.Id
|
|
|
|
|
return &types.GettableUser{
|
2024-02-16 12:46:33 +05:30
|
|
|
User: user.User,
|
|
|
|
|
Role: pat.Role,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2024-03-27 00:07:29 +05:30
|
|
|
zap.L().Error("Error while getting user for PAT: ", zap.Error(err))
|
2024-02-16 12:46:33 +05:30
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-17 18:16:41 +05:30
|
|
|
return baseauth.GetUserFromReqContext(ctx)
|
2024-02-16 12:46:33 +05:30
|
|
|
}
|