auth/internal/domain/grant_type.go

96 lines
2.4 KiB
Go
Raw Normal View History

2021-12-26 08:57:22 +00:00
package domain
import (
"fmt"
2021-12-29 20:08:30 +00:00
"strconv"
2021-12-26 08:57:22 +00:00
"strings"
"source.toby3d.me/toby3d/auth/internal/common"
2021-12-26 08:57:22 +00:00
)
// GrantType represent fixed grant_type parameter.
//
2021-12-26 08:57:22 +00:00
// 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 {
grantType string
2021-12-26 08:57:22 +00:00
}
2022-12-26 14:09:34 +00:00
//nolint:gochecknoglobals // structs cannot be constants
2021-12-26 08:57:22 +00:00
var (
GrantTypeUnd = GrantType{grantType: ""} // "und"
GrantTypeAuthorizationCode = GrantType{grantType: "authorization_code"} // "authorization_code"
GrantTypeRefreshToken = GrantType{grantType: "refresh_token"} // "refresh_token"
2021-12-29 20:08:30 +00:00
// TicketAuth extension.
GrantTypeTicket = GrantType{grantType: "ticket"}
2021-12-26 08:57:22 +00:00
)
var ErrGrantTypeUnknown error = NewError(
ErrorCodeInvalidGrant,
"unknown grant type",
"",
)
2021-12-26 08:57:22 +00:00
2022-12-26 14:09:34 +00:00
//nolint:gochecknoglobals // maps cannot be constants
var uidsGrantTypes = map[string]GrantType{
GrantTypeAuthorizationCode.grantType: GrantTypeAuthorizationCode,
GrantTypeRefreshToken.grantType: GrantTypeRefreshToken,
GrantTypeTicket.grantType: GrantTypeTicket,
}
2022-01-29 17:50:45 +00:00
// ParseGrantType parse grant_type value as GrantType struct enum.
func ParseGrantType(uid string) (GrantType, error) {
if grantType, ok := uidsGrantTypes[strings.ToLower(uid)]; ok {
return grantType, nil
2021-12-26 08:57:22 +00:00
}
return GrantTypeUnd, fmt.Errorf("%w: %s", ErrGrantTypeUnknown, uid)
2021-12-26 08:57:22 +00:00
}
2022-01-29 17:50:45 +00:00
// UnmarshalForm implements custom unmarshler for form values.
2021-12-26 08:57:22 +00:00
func (gt *GrantType) UnmarshalForm(src []byte) error {
responseType, err := ParseGrantType(string(src))
if err != nil {
return fmt.Errorf("GrantType: UnmarshalForm: %w", err)
2021-12-26 08:57:22 +00:00
}
*gt = responseType
return nil
}
2022-01-29 17:50:45 +00:00
// UnmarshalJSON implements custom unmarshler for JSON.
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 fmt.Errorf("GrantType: UnmarshalJSON: %w", err)
2021-12-29 20:08:30 +00:00
}
responseType, err := ParseGrantType(src)
if err != nil {
return fmt.Errorf("GrantType: UnmarshalJSON: %w", err)
2021-12-29 20:08:30 +00:00
}
*gt = responseType
return nil
}
func (gt GrantType) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(gt.grantType)), nil
}
2022-01-29 17:50:45 +00:00
// String returns string representation of grant type.
2021-12-26 08:57:22 +00:00
func (gt GrantType) String() string {
if gt.grantType != "" {
return gt.grantType
}
return common.Und
}
func (gt GrantType) GoString() string {
return "domain.GrantType(" + gt.String() + ")"
2021-12-26 08:57:22 +00:00
}