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"
|
2022-07-28 18:07:37 +00:00
|
|
|
|
|
|
|
"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 {
|
2023-03-16 15:30:00 +00:00
|
|
|
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 (
|
2023-03-16 15:30:00 +00:00
|
|
|
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
|
2023-03-16 15:30:00 +00:00
|
|
|
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
|
2023-03-16 15:30:00 +00:00
|
|
|
ResponseTypeCode = ResponseType{responseType: "code"} // "code"
|
2021-12-25 18:56:19 +00:00
|
|
|
)
|
|
|
|
|
2022-01-29 20:47:05 +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) {
|
2023-03-16 15:30:00 +00:00
|
|
|
case ResponseTypeCode.responseType:
|
2021-12-25 18:56:19 +00:00
|
|
|
return ResponseTypeCode, nil
|
2023-03-16 15:30:00 +00:00
|
|
|
case ResponseTypeID.responseType:
|
2021-12-25 18:56:19 +00:00
|
|
|
return ResponseTypeID, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 18:07:37 +00:00
|
|
|
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 {
|
2022-02-15 20:07:39 +00:00
|
|
|
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 {
|
2022-02-15 20:07:39 +00:00
|
|
|
return fmt.Errorf("ResponseType: UnmarshalJSON: %w", err)
|
2021-12-29 20:08:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
responseType, err := ParseResponseType(uid)
|
2021-12-29 20:08:30 +00:00
|
|
|
if err != nil {
|
2022-02-15 20:07:39 +00:00
|
|
|
return fmt.Errorf("ResponseType: UnmarshalJSON: %w", err)
|
2021-12-29 20:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*rt = responseType
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 20:07:39 +00:00
|
|
|
func (rt ResponseType) MarshalJSON() ([]byte, error) {
|
2023-03-16 15:30:00 +00:00
|
|
|
return []byte(strconv.Quote(rt.responseType)), nil
|
2022-02-15 20:07:39 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-03-16 15:30:00 +00:00
|
|
|
if rt.responseType != "" {
|
|
|
|
return rt.responseType
|
2022-07-28 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return common.Und
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt ResponseType) GoString() string {
|
|
|
|
return "domain.ResponseType(" + rt.String() + ")"
|
2021-12-25 18:56:19 +00:00
|
|
|
}
|