2021-12-26 08:57:22 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-12-29 20:08:30 +00:00
|
|
|
"strconv"
|
2021-12-26 08:57:22 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NOTE(toby3d): Encapsulate enums in structs for extra compile-time safety:
|
|
|
|
// https://threedots.tech/post/safer-enums-in-go/#struct-based-enums
|
|
|
|
type GrantType struct {
|
|
|
|
slug string
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint: gochecknoglobals // NOTE(toby3d): structs cannot be constants
|
|
|
|
var (
|
|
|
|
GrantTypeUndefined = GrantType{slug: ""}
|
|
|
|
GrantTypeAuthorizationCode = GrantType{slug: "authorization_code"}
|
2021-12-29 20:08:30 +00:00
|
|
|
|
|
|
|
// TicketAuth extension.
|
|
|
|
GrantTypeTicket = GrantType{slug: "ticket"}
|
2021-12-26 08:57:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ErrGrantTypeUnknown = errors.New("unknown grant type")
|
|
|
|
|
|
|
|
func ParseGrantType(slug string) (GrantType, error) {
|
2021-12-29 20:08:30 +00:00
|
|
|
switch strings.ToLower(slug) {
|
|
|
|
case GrantTypeAuthorizationCode.slug:
|
2021-12-26 08:57:22 +00:00
|
|
|
return GrantTypeAuthorizationCode, nil
|
2021-12-29 20:08:30 +00:00
|
|
|
case GrantTypeTicket.slug:
|
|
|
|
return GrantTypeTicket, nil
|
2021-12-26 08:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GrantTypeUndefined, fmt.Errorf("%w: %s", ErrGrantTypeUnknown, slug)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gt *GrantType) UnmarshalForm(src []byte) error {
|
|
|
|
responseType, err := ParseGrantType(string(src))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("grant_type: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*gt = responseType
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-29 20:08:30 +00:00
|
|
|
func (gt *GrantType) UnmarshalJSON(v []byte) error {
|
|
|
|
src, err := strconv.Unquote(string(v))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
responseType, err := ParseGrantType(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("grant_type: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*gt = responseType
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-26 08:57:22 +00:00
|
|
|
func (gt GrantType) String() string {
|
|
|
|
return gt.slug
|
|
|
|
}
|