auth/internal/domain/response_type.go

96 lines
2.5 KiB
Go
Raw Normal View History

2021-12-25 18:56:19 +00:00
package domain
import (
"fmt"
2021-12-29 20:08:30 +00:00
"strconv"
2021-12-25 18:56:19 +00:00
"strings"
"source.toby3d.me/toby3d/auth/internal/common"
2021-12-25 18:56:19 +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 ResponseType struct {
responseType string
2021-12-25 18:56:19 +00:00
}
2022-12-26 14:09:34 +00:00
//nolint:gochecknoglobals // structs cannot be constants
2021-12-25 18:56:19 +00:00
var (
ResponseTypeUnd = ResponseType{responseType: ""} // "und"
2021-12-25 18:56:19 +00:00
// Deprecated(toby3d): Only accept response_type=code requests, and for
// backwards-compatible support, treat response_type=id requests as
// response_type=code requests:
// https://aaronparecki.com/2020/12/03/1/indieauth-2020#response-type
ResponseTypeID = ResponseType{responseType: "id"} // "id"
2021-12-25 18:56:19 +00:00
// Indicates to the authorization server that an authorization code
// should be returned as the response:
// https://indieauth.net/source/#authorization-request-li-1
ResponseTypeCode = ResponseType{responseType: "code"} // "code"
2021-12-25 18:56:19 +00:00
)
var ErrResponseTypeUnknown error = NewError(
ErrorCodeUnsupportedResponseType,
"unknown grant type",
"https://indieauth.net/source/#authorization-request",
)
2021-12-25 18:56:19 +00:00
2022-01-29 17:50:45 +00:00
// ParseResponseType parse string as response type struct enum.
func ParseResponseType(uid string) (ResponseType, error) {
switch strings.ToLower(uid) {
case ResponseTypeCode.responseType:
2021-12-25 18:56:19 +00:00
return ResponseTypeCode, nil
case ResponseTypeID.responseType:
2021-12-25 18:56:19 +00:00
return ResponseTypeID, nil
}
return ResponseTypeUnd, fmt.Errorf("%w: %s", ErrResponseTypeUnknown, uid)
2021-12-25 18:56:19 +00:00
}
2022-01-29 17:50:45 +00:00
// UnmarshalForm implements custom unmarshler for form values.
2021-12-25 18:56:19 +00:00
func (rt *ResponseType) UnmarshalForm(src []byte) error {
responseType, err := ParseResponseType(string(src))
if err != nil {
return fmt.Errorf("ResponseType: UnmarshalForm: %w", err)
2021-12-25 18:56:19 +00:00
}
*rt = 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 (rt *ResponseType) UnmarshalJSON(v []byte) error {
2022-01-29 17:50:45 +00:00
uid, err := strconv.Unquote(string(v))
2021-12-29 20:08:30 +00:00
if err != nil {
return fmt.Errorf("ResponseType: UnmarshalJSON: %w", err)
2021-12-29 20:08:30 +00:00
}
responseType, err := ParseResponseType(uid)
2021-12-29 20:08:30 +00:00
if err != nil {
return fmt.Errorf("ResponseType: UnmarshalJSON: %w", err)
2021-12-29 20:08:30 +00:00
}
*rt = responseType
return nil
}
func (rt ResponseType) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(rt.responseType)), nil
}
2022-01-29 17:50:45 +00:00
// String returns string representation of response type.
2021-12-25 18:56:19 +00:00
func (rt ResponseType) String() string {
if rt.responseType != "" {
return rt.responseType
}
return common.Und
}
func (rt ResponseType) GoString() string {
return "domain.ResponseType(" + rt.String() + ")"
2021-12-25 18:56:19 +00:00
}