2021-12-25 18:55:59 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-29 20:08:30 +00:00
|
|
|
"strconv"
|
2021-12-25 18:55:59 +00:00
|
|
|
"strings"
|
2022-07-28 18:07:37 +00:00
|
|
|
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/common"
|
2021-12-25 18:55:59 +00:00
|
|
|
)
|
|
|
|
|
2022-12-26 16:23:57 +00:00
|
|
|
// Scope represent single token scope supported by IndieAuth.
|
|
|
|
//
|
|
|
|
// NOTE(toby3d): Encapsulate enums in structs for extra compile-time safety:
|
|
|
|
// https://threedots.tech/post/safer-enums-in-go/#struct-based-enums
|
|
|
|
type Scope struct {
|
2023-03-16 15:30:00 +00:00
|
|
|
scope string
|
2022-12-26 16:23:57 +00:00
|
|
|
}
|
2021-12-25 18:55:59 +00:00
|
|
|
|
2022-01-29 19:30:57 +00:00
|
|
|
var ErrScopeUnknown error = NewError(ErrorCodeInvalidRequest, "unknown scope", "https://indieweb.org/scope")
|
2021-12-25 18:55:59 +00:00
|
|
|
|
2022-12-26 14:09:34 +00:00
|
|
|
//nolint:gochecknoglobals // structs cannot be constants
|
2021-12-25 18:55:59 +00:00
|
|
|
var (
|
2023-03-16 15:30:00 +00:00
|
|
|
ScopeUnd = Scope{scope: ""} // "und"
|
2021-12-25 18:55:59 +00:00
|
|
|
|
|
|
|
// https://indieweb.org/scope#Micropub_Scopes
|
2023-03-16 15:30:00 +00:00
|
|
|
ScopeCreate = Scope{scope: "create"} // "create"
|
|
|
|
ScopeDelete = Scope{scope: "delete"} // "delete"
|
|
|
|
ScopeDraft = Scope{scope: "draft"} // "draft"
|
|
|
|
ScopeMedia = Scope{scope: "media"} // "media"
|
|
|
|
ScopeUndelete = Scope{scope: "undelete"} // "undelete"
|
|
|
|
ScopeUpdate = Scope{scope: "update"} // "update"
|
2021-12-25 18:55:59 +00:00
|
|
|
|
|
|
|
// https://indieweb.org/scope#Microsub_Scopes
|
2023-03-16 15:30:00 +00:00
|
|
|
ScopeBlock = Scope{scope: "block"} // "block"
|
|
|
|
ScopeChannels = Scope{scope: "channels"} // "channels"
|
|
|
|
ScopeFollow = Scope{scope: "follow"} // "follow"
|
|
|
|
ScopeMute = Scope{scope: "mute"} // "mute"
|
|
|
|
ScopeRead = Scope{scope: "read"} // "read"
|
2021-12-25 18:55:59 +00:00
|
|
|
|
|
|
|
// This scope requests access to the user's default profile information
|
2022-02-02 17:52:52 +00:00
|
|
|
// which include the following properties: name, photo, url.
|
2021-12-25 18:55:59 +00:00
|
|
|
//
|
|
|
|
// NOTE(toby3d): https://indieauth.net/source/#profile-information
|
2023-03-16 15:30:00 +00:00
|
|
|
ScopeProfile = Scope{scope: "profile"} // "profile"
|
2021-12-25 18:55:59 +00:00
|
|
|
|
|
|
|
// This scope requests access to the user's email address in the
|
|
|
|
// following property: email.
|
|
|
|
//
|
|
|
|
// Note that because the profile scope is required when requesting
|
|
|
|
// profile information, the email scope cannot be requested on its own
|
|
|
|
// and must be requested along with the profile scope if desired.
|
|
|
|
//
|
|
|
|
// NOTE(toby3d): https://indieauth.net/source/#profile-information
|
2023-03-16 15:30:00 +00:00
|
|
|
ScopeEmail = Scope{scope: "email"} // "email"
|
2021-12-25 18:55:59 +00:00
|
|
|
)
|
|
|
|
|
2022-12-26 14:09:34 +00:00
|
|
|
//nolint:gochecknoglobals // maps cannot be constants
|
2022-01-29 17:50:45 +00:00
|
|
|
var uidsScopes = map[string]Scope{
|
2023-03-16 15:30:00 +00:00
|
|
|
ScopeBlock.scope: ScopeBlock,
|
|
|
|
ScopeChannels.scope: ScopeChannels,
|
|
|
|
ScopeCreate.scope: ScopeCreate,
|
|
|
|
ScopeDelete.scope: ScopeDelete,
|
|
|
|
ScopeDraft.scope: ScopeDraft,
|
|
|
|
ScopeEmail.scope: ScopeEmail,
|
|
|
|
ScopeFollow.scope: ScopeFollow,
|
|
|
|
ScopeMedia.scope: ScopeMedia,
|
|
|
|
ScopeMute.scope: ScopeMute,
|
|
|
|
ScopeProfile.scope: ScopeProfile,
|
|
|
|
ScopeRead.scope: ScopeRead,
|
|
|
|
ScopeUndelete.scope: ScopeUndelete,
|
|
|
|
ScopeUpdate.scope: ScopeUpdate,
|
2021-12-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseScope parses scope slug into Scope domain.
|
2022-01-29 17:50:45 +00:00
|
|
|
func ParseScope(uid string) (Scope, error) {
|
|
|
|
if scope, ok := uidsScopes[strings.ToLower(uid)]; ok {
|
2021-12-25 18:55:59 +00:00
|
|
|
return scope, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 18:07:37 +00:00
|
|
|
return ScopeUnd, fmt.Errorf("%w: %s", ErrScopeUnknown, uid)
|
2021-12-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
func (s *Scope) UnmarshalJSON(v []byte) error {
|
|
|
|
src, err := strconv.Unquote(string(v))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Scope: UnmarshalJSON: cannot unquote string: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := ParseScope(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Scopes: UnmarshalJSON: cannot parse scope: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = out
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 20:07:39 +00:00
|
|
|
func (s Scope) MarshalJSON() ([]byte, error) {
|
2023-03-16 15:30:00 +00:00
|
|
|
return []byte(strconv.Quote(s.scope)), nil
|
2022-02-15 20:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of scope.
|
|
|
|
func (s Scope) String() string {
|
2023-03-16 15:30:00 +00:00
|
|
|
if s.scope != "" {
|
|
|
|
return s.scope
|
2022-07-28 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return common.Und
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Scope) GoString() string {
|
|
|
|
return "domain.Scope(" + s.String() + ")"
|
2022-02-15 20:07:39 +00:00
|
|
|
}
|