Files
mtusi2026lab2/service/internal/pkg/authprovider/provider.go
T

83 lines
1.5 KiB
Go

package authprovider
import (
"encoding/json"
"strings"
"git.dev.mamont-optics.ru/mamont-optics/backend/application/internal/pkg/types"
)
type Type string
const (
TypeUndefined Type = "undefined"
TypePassword Type = "password"
TypeEmail Type = "email"
TypePhone Type = "phone"
TypeGoogle Type = "google"
TypeTelegram Type = "telegram"
)
var allowedTypes = map[Type]struct{}{
TypePassword: {},
TypeEmail: {},
TypePhone: {},
TypeGoogle: {},
TypeTelegram: {},
}
func ParseType(value string) Type {
t := Type(strings.ToLower(value))
if !t.IsValid() {
return TypeUndefined
}
return t
}
func ParseAndValidateType(value string) (Type, error) {
parsed := ParseType(value)
if _, ok := allowedTypes[parsed]; ok {
return parsed, nil
}
return TypeUndefined, types.ErrUnsupportedOTPStatus
}
func (t Type) String() string {
return string(t)
}
func (t Type) Validate() error {
if _, ok := allowedTypes[t]; ok {
return nil
}
return types.ErrUnsupportedAuthProviderType
}
func (t Type) IsValid() bool {
_, ok := allowedTypes[t]
return ok
}
func (t Type) MarshalJSON() ([]byte, error) {
if err := t.Validate(); err != nil {
return nil, err
}
return json.Marshal(t.String())
}
func (t *Type) UnmarshalJSON(data []byte) error {
if t == nil {
return types.ErrUnmarshalAuthProviderTypeIntoNilPointer
}
var value string
if err := json.Unmarshal(data, &value); err != nil {
return err
}
parsed := ParseType(value)
if err := parsed.Validate(); err != nil {
return err
}
*t = parsed
return nil
}