2022-05-03 15:26:32 +05:30
package auth
import (
"crypto/rand"
"encoding/hex"
"github.com/pkg/errors"
2022-10-06 20:13:30 +05:30
"go.signoz.io/signoz/pkg/query-service/constants"
"go.signoz.io/signoz/pkg/query-service/model"
2022-05-03 15:26:32 +05:30
)
var (
ErrorEmptyRequest = errors . New ( "Empty request" )
ErrorInvalidEmail = errors . New ( "Invalid email" )
ErrorInvalidRole = errors . New ( "Invalid role" )
ErrorInvalidInviteToken = errors . New ( "Invalid invite token" )
2023-02-24 14:57:07 +05:30
ErrorAskAdmin = errors . New ( "An invitation is needed to create an account. Please ask your admin (the person who has first installed SIgNoz) to send an invite." )
2022-05-03 15:26:32 +05:30
)
func randomHex ( sz int ) ( string , error ) {
bytes := make ( [ ] byte , sz )
if _ , err := rand . Read ( bytes ) ; err != nil {
return "" , err
}
return hex . EncodeToString ( bytes ) , nil
}
func isValidRole ( role string ) bool {
switch role {
case constants . AdminGroup , constants . EditorGroup , constants . ViewerGroup :
return true
default :
return false
}
return false
}
func validateInviteRequest ( req * model . InviteRequest ) error {
if req == nil {
return ErrorEmptyRequest
}
if ! isValidEmail ( req . Email ) {
return ErrorInvalidEmail
}
if ! isValidRole ( req . Role ) {
return ErrorInvalidRole
}
return nil
}
// TODO(Ahsan): Implement check on email semantic.
func isValidEmail ( email string ) bool {
return true
}